Skip to content

Commit 34e1024

Browse files
committed
Revive PR kubernetes-client#1392: Implement delete_from_yaml (original by DiptoChakrabarty) and update to latest
1 parent bd32360 commit 34e1024

File tree

3 files changed

+590
-0
lines changed

3 files changed

+590
-0
lines changed

kubernetes/e2e_test/test_utils.py

+257
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from os import path
1818

1919
import yaml
20+
import time
2021

2122
from kubernetes import client, utils
2223
from kubernetes.client.rest import ApiException
@@ -36,6 +37,15 @@ def setUpClass(cls):
3637
body = client.V1Namespace(
3738
metadata=client.V1ObjectMeta(
3839
name=cls.test_namespace))
40+
41+
# Delete the namespace if it already exists
42+
try:
43+
core_v1.delete_namespace(name=cls.test_namespace)
44+
time.sleep(10) # Wait for the namespace to be deleted
45+
except ApiException as e:
46+
if e.status != 404:
47+
raise
48+
3949
core_v1.create_namespace(body=body)
4050

4151
@classmethod
@@ -609,6 +619,253 @@ def test_create_from_list_in_multi_resource_yaml_namespaced(self):
609619
name="mock", namespace=self.test_namespace, body={})
610620

611621

622+
def test_delete_namespace_from_yaml(self):
623+
"""
624+
Should be able to delete a namespace
625+
Create namespace from file first and ensure it is created
626+
"""
627+
k8s_client = client.api_client.ApiClient(configuration=self.config)
628+
utils.create_from_yaml(
629+
k8s_client, self.path_prefix + "core-namespace.yaml")
630+
core_api = client.CoreV1Api(k8s_client)
631+
nmsp = core_api.read_namespace(name="development")
632+
self.assertIsNotNone(nmsp)
633+
"""
634+
Delete namespace from yaml
635+
"""
636+
utils.delete_from_yaml(k8s_client, self.path_prefix + "core-namespace.yaml")
637+
time.sleep(10)
638+
namespace_status=False
639+
try:
640+
nmsp=core_api.read_namespace(name="development")
641+
namespace_status=True
642+
except Exception as e:
643+
self.assertFalse(namespace_status)
644+
self.assertFalse(namespace_status)
645+
646+
def test_delete_apps_deployment_from_yaml(self):
647+
"""
648+
Should delete a deployment
649+
First create deployment from file and ensure it is created
650+
"""
651+
k8s_client = client.api_client.ApiClient(configuration=self.config)
652+
utils.create_from_yaml(
653+
k8s_client, self.path_prefix + "apps-deployment.yaml")
654+
app_api = client.AppsV1Api(k8s_client)
655+
dep = app_api.read_namespaced_deployment(name="nginx-app",
656+
namespace="default")
657+
self.assertIsNotNone(dep)
658+
"""
659+
Deployment should be created
660+
Now delete deployment using delete_from_yaml method
661+
"""
662+
utils.delete_from_yaml(k8s_client, self.path_prefix + "apps-deployment.yaml")
663+
deployment_status=False
664+
time.sleep(10)
665+
try:
666+
response=app_api.read_namespaced_deployment(name="nginx-app",namespace="default")
667+
deployment_status=True
668+
except Exception as e:
669+
self.assertFalse(deployment_status)
670+
671+
self.assertFalse(deployment_status)
672+
673+
def test_delete_service_from_yaml(self):
674+
"""
675+
Should be able to delete a service
676+
Create service from yaml first and ensure it is created
677+
"""
678+
k8s_client = client.api_client.ApiClient(configuration=self.config)
679+
utils.create_from_yaml(
680+
k8s_client, self.path_prefix + "core-service.yaml")
681+
core_api = client.CoreV1Api(k8s_client)
682+
svc = core_api.read_namespaced_service(name="my-service",
683+
namespace="default")
684+
self.assertIsNotNone(svc)
685+
"""
686+
Delete service from yaml
687+
"""
688+
utils.delete_from_yaml(
689+
k8s_client, self.path_prefix + "core-service.yaml")
690+
service_status=False
691+
time.sleep(10)
692+
try:
693+
response = core_api.read_namespaced_service(name="my-service",namespace="default")
694+
service_status=True
695+
except Exception as e:
696+
self.assertFalse(service_status)
697+
self.assertFalse(service_status)
698+
699+
def test_delete_pod_from_yaml(self):
700+
"""
701+
Should be able to delete pod
702+
Create pod from file first and ensure it is created
703+
"""
704+
k8s_client = client.api_client.ApiClient(configuration=self.config)
705+
utils.create_from_yaml(
706+
k8s_client, self.path_prefix + "core-pod.yaml")
707+
core_api = client.CoreV1Api(k8s_client)
708+
pod = core_api.read_namespaced_pod(name="myapp-pod",
709+
namespace="default")
710+
self.assertIsNotNone(pod)
711+
"""
712+
Delete pod using delete_from_yaml
713+
"""
714+
utils.delete_from_yaml(
715+
k8s_client, self.path_prefix + "core-pod.yaml")
716+
time.sleep(10)
717+
pod_status=False
718+
try:
719+
response = core_api.read_namespaced_pod(name="myapp-pod",
720+
namespace="default")
721+
pod_status=True
722+
except Exception as e:
723+
self.assertFalse(pod_status)
724+
self.assertFalse(pod_status)
725+
726+
727+
def test_delete_rbac_role_from_yaml(self):
728+
"""
729+
Should be able to delete rbac role
730+
Create rbac role from file first and ensure it is created
731+
"""
732+
k8s_client = client.api_client.ApiClient(configuration=self.config)
733+
utils.create_from_yaml(
734+
k8s_client, self.path_prefix + "rbac-role.yaml")
735+
rbac_api = client.RbacAuthorizationV1Api(k8s_client)
736+
rbac_role = rbac_api.read_namespaced_role(
737+
name="pod-reader", namespace="default")
738+
self.assertIsNotNone(rbac_role)
739+
"""
740+
Delete rbac role from yaml
741+
"""
742+
utils.delete_from_yaml(
743+
k8s_client, self.path_prefix + "rbac-role.yaml")
744+
rbac_role_status=False
745+
time.sleep(10)
746+
try:
747+
response = rbac_api.read_namespaced_role(
748+
name="pod-reader", namespace="default")
749+
rbac_role_status=True
750+
except Exception as e:
751+
self.assertFalse(rbac_role_status)
752+
self.assertFalse(rbac_role_status)
753+
754+
def test_delete_rbac_role_from_yaml_with_verbose_enabled(self):
755+
"""
756+
Should delete a rbac role with verbose enabled
757+
Create rbac role with verbose enabled and ensure it is created
758+
"""
759+
k8s_client = client.api_client.ApiClient(configuration=self.config)
760+
utils.create_from_yaml(
761+
k8s_client, self.path_prefix + "rbac-role.yaml", verbose=True)
762+
rbac_api = client.RbacAuthorizationV1Api(k8s_client)
763+
rbac_role = rbac_api.read_namespaced_role(
764+
name="pod-reader", namespace="default")
765+
self.assertIsNotNone(rbac_role)
766+
"""
767+
Delete the rbac role from yaml
768+
"""
769+
utils.delete_from_yaml(
770+
k8s_client, self.path_prefix + "rbac-role.yaml", verbose=True)
771+
772+
rbac_role_status=False
773+
time.sleep(10)
774+
try:
775+
response=rbac_api.read_namespaced_role(
776+
name="pod-reader", namespace="default")
777+
rbac_role_status=True
778+
except Exception as e:
779+
self.assertFalse(rbac_role_status)
780+
self.assertFalse(rbac_role_status)
781+
782+
783+
# Deletion Tests for multi resource objects in yaml files
784+
785+
def test_delete_from_multi_resource_yaml(self):
786+
"""
787+
Should be able to delete service and replication controller
788+
from the multi resource yaml files
789+
Create the resources first and ensure they exist
790+
"""
791+
k8s_client = client.api_client.ApiClient(configuration=self.config)
792+
utils.create_from_yaml(
793+
k8s_client, self.path_prefix + "multi-resource.yaml")
794+
core_api = client.CoreV1Api(k8s_client)
795+
svc = core_api.read_namespaced_service(name="mock",
796+
namespace="default")
797+
self.assertIsNotNone(svc)
798+
ctr = core_api.read_namespaced_replication_controller(
799+
name="mock", namespace="default")
800+
self.assertIsNotNone(ctr)
801+
"""
802+
Delete service and replication controller using yaml file
803+
"""
804+
utils.delete_from_yaml(
805+
k8s_client, self.path_prefix + "multi-resource.yaml")
806+
svc_status=False
807+
replication_status=False
808+
time.sleep(10)
809+
try:
810+
resp_svc= core_api.read_namespaced_service(name="mock",
811+
namespace="default")
812+
svc_status=True
813+
resp_repl= core_api.read_namespaced_replication_controller(
814+
name="mock", namespace="default")
815+
repl_status = True
816+
except Exception as e:
817+
self.assertFalse(svc_status)
818+
self.assertFalse(replication_status)
819+
self.assertFalse(svc_status)
820+
self.assertFalse(replication_status)
821+
822+
def test_delete_from_list_in_multi_resource_yaml(self):
823+
"""
824+
Should delete the items in PodList and the deployment in the yaml file
825+
Create the items first and ensure they exist
826+
"""
827+
k8s_client = client.api_client.ApiClient(configuration=self.config)
828+
utils.create_from_yaml(
829+
k8s_client, self.path_prefix + "multi-resource-with-list.yaml")
830+
core_api = client.CoreV1Api(k8s_client)
831+
app_api = client.AppsV1Api(k8s_client)
832+
pod_0 = core_api.read_namespaced_pod(
833+
name="mock-pod-0", namespace="default")
834+
self.assertIsNotNone(pod_0)
835+
pod_1 = core_api.read_namespaced_pod(
836+
name="mock-pod-1", namespace="default")
837+
self.assertIsNotNone(pod_1)
838+
dep = app_api.read_namespaced_deployment(
839+
name="mock", namespace="default")
840+
self.assertIsNotNone(dep)
841+
"""
842+
Delete the PodList and Deployment using the yaml file
843+
"""
844+
utils.delete_from_yaml(
845+
k8s_client, self.path_prefix + "multi-resource-with-list.yaml")
846+
time.sleep(10)
847+
pod0_status=False
848+
pod1_status=False
849+
deploy_status=False
850+
try:
851+
core_api.read_namespaced_pod(
852+
name="mock-pod-0", namespace="default")
853+
core_api.read_namespaced_pod(
854+
name="mock-pod-1", namespace="default")
855+
app_api.read_namespaced_deployment(
856+
name="mock", namespace="default")
857+
pod0_status=True
858+
pod1_status=True
859+
deploy_status=True
860+
except Exception as e:
861+
self.assertFalse(pod0_status)
862+
self.assertFalse(pod1_status)
863+
self.assertFalse(deploy_status)
864+
self.assertFalse(pod0_status)
865+
self.assertFalse(pod1_status)
866+
self.assertFalse(deploy_status)
867+
868+
612869
class TestUtilsUnitTests(unittest.TestCase):
613870

614871
def test_parse_quantity(self):

kubernetes/utils/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@
1616

1717
from .create_from_yaml import (FailToCreateError, create_from_dict,
1818
create_from_yaml, create_from_directory)
19+
from .delete_from_yaml import (FailToDeleteError, delete_from_dict,
20+
delete_from_yaml, delete_from_directory)
1921
from .quantity import parse_quantity
2022
from. duration import parse_duration

0 commit comments

Comments
 (0)