Skip to content

Commit

Permalink
Update return values, clean up comments
Browse files Browse the repository at this point in the history
  • Loading branch information
micw523 committed Feb 13, 2019
1 parent b817c17 commit d6cf9b9
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 146 deletions.
219 changes: 88 additions & 131 deletions kubernetes/e2e_test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,111 +24,93 @@ class TestUtils(unittest.TestCase):
def setUpClass(cls):
cls.config = base.get_e2e_configuration()

def test_app_yaml(self):
def test_apps_v1beta1_deployment(self):
"""
Test for creating and deleting a deployment on default namespace.
Using apps API
Using yaml file apps-deployment.yaml
Should be able to create an apps/v1beta1 deployment.
"""
k8s_client = client.api_client.ApiClient(configuration=self.config)
k8s_api = utils.create_from_yaml(
utils.create_from_yaml(
k8s_client, "kubernetes/e2e_test/test_yaml/apps-deployment.yaml")
self.assertEqual("apps/v1beta1",
k8s_api.get_api_resources().group_version)
k8s_api = client.AppsV1beta1Api(k8s_client)
dep = k8s_api.read_namespaced_deployment(name="nginx-app",
namespace="default")
self.assertIsNotNone(dep)
k8s_api.delete_namespaced_deployment(
name="nginx-app", namespace="default",
body={})

def test_extension_yaml(self):
def test_extension_v1beta1_deployment(self):
"""
Test for creating and deleting a deployment on default namespace.
Using extensions API
Using yaml file extensions-deployment.yaml
Should be able to create an extensions/v1beta1 deployment.
"""
k8s_client = client.api_client.ApiClient(configuration=self.config)
k8s_api = utils.create_from_yaml(
utils.create_from_yaml(
k8s_client, "kubernetes/e2e_test/test_yaml/extensions-deployment.yaml")
self.assertEqual("extensions/v1beta1",
k8s_api.get_api_resources().group_version)
k8s_api = client.ExtensionsV1beta1Api(k8s_client)
dep = k8s_api.read_namespaced_deployment(name="nginx-deployment",
namespace="default")
self.assertIsNotNone(dep)
k8s_api.delete_namespaced_deployment(
name="nginx-deployment", namespace="default",
body={})

def test_core_pod_yaml(self):
def test_core_v1_pod(self):
"""
Test for creating and deleting a pod on default namespace.
Using core API
Using yaml file core-pod.yaml
Should be able to create a pod.
"""
k8s_client = client.api_client.ApiClient(configuration=self.config)
k8s_api = utils.create_from_yaml(
utils.create_from_yaml(
k8s_client, "kubernetes/e2e_test/test_yaml/core-pod.yaml")
self.assertEqual("v1",
k8s_api.get_api_resources().group_version)
k8s_api = client.CoreV1Api(k8s_client)
pod = k8s_api.read_namespaced_pod(name="myapp-pod",
namespace="default")
self.assertIsNotNone(pod)
k8s_api.delete_namespaced_pod(
name="myapp-pod", namespace="default",
body={})

def test_core_service_yaml(self):
def test_core_v1_service(self):
"""
Test for creating and deleting a service on default namespace.
Using core API
Using yaml file core-service.yaml
Should be able to create a service.
"""
k8s_client = client.api_client.ApiClient(configuration=self.config)
k8s_api = utils.create_from_yaml(
utils.create_from_yaml(
k8s_client, "kubernetes/e2e_test/test_yaml/core-service.yaml")
self.assertEqual("v1",
k8s_api.get_api_resources().group_version)
k8s_api = client.CoreV1Api(k8s_client)
svc = k8s_api.read_namespaced_service(name="my-service",
namespace="default")
self.assertIsNotNone(svc)
k8s_api.delete_namespaced_service(
name="my-service", namespace="default",
body={})

def test_core_namespace_yaml(self):
def test_core_v1_namespace(self):
"""
Test for creating and deleting namespace
Using core API
Using yaml file core-namespace.yaml
Should be able to create a namespace.
"""
k8s_client = client.api_client.ApiClient(configuration=self.config)
k8s_api = utils.create_from_yaml(
utils.create_from_yaml(
k8s_client, "kubernetes/e2e_test/test_yaml/core-namespace.yaml")
self.assertEqual("v1",
k8s_api.get_api_resources().group_version)
k8s_api = client.CoreV1Api(k8s_client)
nmsp = k8s_api.read_namespace(name="development")
self.assertIsNotNone(nmsp)
k8s_api.delete_namespace(name="development", body={})

def test_deployment_in_namespace(self):
def test_extensions_v1beta1_deployment_non_default_namespace(self):
"""
Test: Create a namespace. Create and delete a deployment on
the namespace just created. Delete the namespace created at the
beginning of the test.
Using core, extensions API
Using yaml file core-namespace-dep.yaml
Using yaml file extensions-deployment-dep.yaml
Should be able to create a namespace,
and then create an extensions/v1beta1 deployment
in the just-created namespace.
"""
k8s_client = client.ApiClient(configuration=self.config)
core_api = utils.create_from_yaml(
utils.create_from_yaml(
k8s_client, "kubernetes/e2e_test/test_yaml/core-namespace-dep.yaml")
self.assertEqual("v1",
core_api.get_api_resources().group_version)
core_api = client.CoreV1Api(k8s_client)
nmsp = core_api.read_namespace(name="dep")
self.assertIsNotNone(nmsp)
dep_api = utils.create_from_yaml(
utils.create_from_yaml(
k8s_client, "kubernetes/e2e_test/test_yaml/extensions-deployment-dep.yaml")
dep_api = client.ExtensionsV1beta1Api(k8s_client)
dep = dep_api.read_namespaced_deployment(name="nginx-deployment",
namespace="dep")
self.assertIsNotNone(dep)
Expand All @@ -137,46 +119,39 @@ def test_deployment_in_namespace(self):
body={})
core_api.delete_namespace(name="dep", body={})

def test_api_service_with_conflict(self):
def test_apiregistration_v1beta1_apiservice_with_conflict(self):
"""
Test: Create an APIService. Recreate the APIService to test conflict
handling. Delete the APIService.
Check the program raises CreationFailedError.
Using apiregistration API
Using yaml file api-service.yaml
Should be able to create an apiregistration.k8s.io/v1beta1 API service.
Should verify that creating the same API service should
fail due to conflict.
"""
k8s_client = client.api_client.ApiClient(configuration=self.config)
k8s_api = utils.create_from_yaml(
utils.create_from_yaml(
k8s_client, "kubernetes/e2e_test/test_yaml/api-service.yaml")
self.assertEqual("apiregistration.k8s.io/v1beta1",
k8s_api.get_api_resources().group_version)
k8s_api = client.ApiregistrationV1beta1Api()
svc = k8s_api.read_api_service(
name="v1alpha1.wardle.k8s.io")
self.assertIsNotNone(svc)
with self.assertRaises(utils.FailToCreateError):
# TODO: verify error message
utils.create_from_yaml(
k8s_client, "kubernetes/e2e_test/test_yaml/api-service.yaml")
k8s_api.delete_api_service(
name="v1alpha1.wardle.k8s.io", body={})

def test_list(self):
def test_service_and_deployment_list(self):
"""
Test for creating and deleting a service and a deployment using
the List kind.
Using core, extensions API
Using yaml file list.yaml
Should be able to create a service and an extensions/v1beta1 deployment
from a kind: List yaml file
"""
k8s_client = client.api_client.ApiClient(configuration=self.config)
k8s_api = utils.create_from_yaml(
utils.create_from_yaml(
k8s_client, "kubernetes/e2e_test/test_yaml/list.yaml")
svc_api = k8s_api[0]
self.assertEqual("v1", svc_api.get_api_resources().group_version)
svc_api = client.CoreV1Api(k8s_client)
svc = svc_api.read_namespaced_service(name="list-service-test",
namespace="default")
self.assertIsNotNone(svc)
ext_api = k8s_api[1]
self.assertEqual("extensions/v1beta1",
ext_api.get_api_resources().group_version)
ext_api = client.ExtensionsV1beta1Api(k8s_client)
dep = ext_api.read_namespaced_deployment(name="list-deployment-test",
namespace="default")
self.assertIsNotNone(dep)
Expand All @@ -185,99 +160,81 @@ def test_list(self):
svc_api.delete_namespaced_service(name="list-service-test",
namespace="default", body={})

def test_multi_resource(self):
# TODO: a xxxList kind with no failure

def test_implicit_service_list_with_conflict(self):
"""
Should be able to create two services from a kind: ServiceList
json file that implicitly indicates the kind of individual objects
"""
k8s_client = client.api_client.ApiClient(configuration=self.config)
with self.assertRaises(utils.FailToCreateError):
utils.create_from_yaml(
k8s_client, "kubernetes/e2e_test/test_yaml/multi-resource-svclist.json")
svc_api = client.CoreV1Api(k8s_client)
svc_0 = svc_api.read_namespaced_service(name="mock-3",
namespace="default")
self.assertIsNotNone(svc_0)
svc_1 = svc_api.read_namespaced_service(name="mock-4",
namespace="default")
self.assertIsNotNone(svc_1)
svc_api.delete_namespaced_service(name="mock-3",
namespace="default", body={})
svc_api.delete_namespaced_service(name="mock-4",
namespace="default", body={})

def test_service_replication_controller_multi_resource(self):
"""
Test for handling a multi-resource yaml file. The yaml file contains
a service and a replication controller. These two objects are
created and deleted.
Using core API
Using yaml file multi-resource-yaml.yaml
Should be able to create a service and a replication controller
from a multi-resource yaml file
"""
k8s_client = client.api_client.ApiClient(configuration=self.config)
k8s_api = utils.create_from_yaml(
utils.create_from_yaml(
k8s_client, "kubernetes/e2e_test/test_yaml/multi-resource-yaml.yaml")
svc_api = k8s_api[0]
self.assertEqual("v1", svc_api.get_api_resources().group_version)
svc = svc_api.read_namespaced_service(name="mock",
core_api = client.CoreV1Api(k8s_client)
svc = core_api.read_namespaced_service(name="mock",
namespace="default")
self.assertIsNotNone(svc)
ctr_api = k8s_api[1]
self.assertEqual("v1", ctr_api.get_api_resources().group_version)
ctr = ctr_api.read_namespaced_replication_controller(
ctr = core_api.read_namespaced_replication_controller(
name="mock", namespace="default")
self.assertIsNotNone(ctr)
ctr_api.delete_namespaced_replication_controller(
core_api.delete_namespaced_replication_controller(
name="mock", namespace="default", body={})
svc_api.delete_namespaced_service(name="mock",
core_api.delete_namespaced_service(name="mock",
namespace="default", body={})

def test_multi_resource_with_conflict(self):
"""
Test for handling conflict.
Create a service from the first yaml file. Attempt to create the
same service and a replication controller using a second yaml file.
The second yaml file is a multi-part file.
The service and the replication controller should be created,
while error is reported when attempting to create the service again.
Since output_list is disabled, a single api object will be returned.
Both object are deleted at the end of the test.
Using core API
Using yaml file yaml-conflict-first.yaml
Using yaml file yaml-conflict-multi.yaml
Should be able to create a service from the first yaml file.
Should fail to create the same service from the second yaml file
and create a replication controller.
Should raise an exception for failure to create the same service twice.
"""
k8s_client = client.api_client.ApiClient(configuration=self.config)
svc_api = utils.create_from_yaml(
utils.create_from_yaml(
k8s_client, "kubernetes/e2e_test/test_yaml/yaml-conflict-first.yaml")
self.assertEqual("v1", svc_api.get_api_resources().group_version)
svc = svc_api.read_namespaced_service(name="mock-2",
core_api = client.CoreV1Api(k8s_client)
svc = core_api.read_namespaced_service(name="mock-2",
namespace="default")
self.assertIsNotNone(svc)
with self.assertRaises(utils.FailToCreateError):
utils.create_from_yaml(
k8s_client, "kubernetes/e2e_test/test_yaml/yaml-conflict-multi.yaml")
ctr_api = client.CoreV1Api(k8s_client)
ctr = ctr_api.read_namespaced_replication_controller(
# TODO: check error message.
ctr = core_api.read_namespaced_replication_controller(
name="mock-2", namespace="default")
self.assertIsNotNone(ctr)
ctr_api.delete_namespaced_replication_controller(
core_api.delete_namespaced_replication_controller(
name="mock-2", namespace="default", body={})
svc_api.delete_namespaced_service(name="mock-2",
core_api.delete_namespaced_service(name="mock-2",
namespace="default", body={})

def test_svc_list_with_conflict_no_kind(self):
"""
Test for handling yaml-compatible json files and a special kind
of list. The kind field of the yaml file is ServiceList while the
individual components does not contain the kind field.
Two replicates exist in the json file. An exception will be raised.
The kind should be inferred as Service and all these services
are created and deleted.
Using core API
Using json file multi-resource-svclist.json
"""
k8s_client = client.api_client.ApiClient(configuration=self.config)
with self.assertRaises(utils.FailToCreateError):
utils.create_from_yaml(
k8s_client, "kubernetes/e2e_test/test_yaml/multi-resource-svclist.json")
svc_api = client.CoreV1Api(k8s_client)
svc_0 = svc_api.read_namespaced_service(name="mock-3",
namespace="default")
self.assertIsNotNone(svc_0)
svc_1 = svc_api.read_namespaced_service(name="mock-4",
namespace="default")
self.assertIsNotNone(svc_1)
svc_api.delete_namespaced_service(name="mock-3",
namespace="default", body={})
svc_api.delete_namespaced_service(name="mock-4",
namespace="default", body={})

def test_double_fail(self):
def test_multiple_failures(self):
"""
Test for a file that contains triple occurences of the same object.
Test if the correct exception is raised and correct message lines are
being shown.
Using extensions API
Using yaml file triple-nginx.yaml
Should create an extensions/v1beta1 deployment and fail to create
the same deployment twice.
Should raise an exception that contains two error messages.
"""
k8s_client = client.api_client.ApiClient(configuration=self.config)
with self.assertRaises(utils.FailToCreateError) as cm:
Expand Down
Loading

0 comments on commit d6cf9b9

Please sign in to comment.