Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unittest for get service , daemonset. cronjob and ingress details #78

Closed
5 changes: 4 additions & 1 deletion test/resources/cronjob.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"kind": "CronJob",
"metadata": {
"creationTimestamp": "2021-03-25T04:02:24Z",
"labels": {
"TestLabel": "mytestlabel"
},
"managedFields": [
{
"apiVersion": "batch/v1beta1",
Expand Down Expand Up @@ -95,4 +98,4 @@
},
"status": {}
}
]
]
6 changes: 3 additions & 3 deletions test/resources/daemonset.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"creationTimestamp": "2021-03-25T04:00:10Z",
"generation": 1,
"labels": {
"k8s-app": "fluentd-logging"
},
"TestLabel": "mytestlabel"
},
"managedFields": [
{
"apiVersion": "apps/v1",
Expand Down Expand Up @@ -233,4 +233,4 @@
"updatedNumberScheduled": 2
}
}
]
]
5 changes: 4 additions & 1 deletion test/resources/ingress.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"kind": "Ingress",
"metadata": {
"creationTimestamp": "2021-03-25T04:04:21Z",
"labels": {
"TestLabel": "mytestlabel"
},
"generation": 1,
"managedFields": [
{
Expand Down Expand Up @@ -47,4 +50,4 @@
"loadBalancer": {}
}
}
]
]
9 changes: 5 additions & 4 deletions test/resources/service.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"kind": "Service",
"metadata": {
"creationTimestamp": "2021-03-17T12:27:33Z",
"labels": {
"Test": "mytest"
},
"managedFields": [
{
"apiVersion": "v1",
Expand Down Expand Up @@ -32,7 +35,7 @@
"time": "2021-03-17T12:27:33Z"
}
],
"name": "test-service",
"name": "my-service2",
"namespace": "default",
"resourceVersion": "970",
"selfLink": "/api/v1/namespaces/default/services/my-service",
Expand All @@ -55,6 +58,4 @@
},
"status": {
"loadBalancer": {}
}
}
]

60 changes: 60 additions & 0 deletions test/test_KubeLibrary.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,38 @@ def mock_list_namespaced_role_bindings(namespace, watch=False):
return list_of_role_bind


def mock_read_daemonset_details_in_namespace(name, namespace):
if namespace == 'default':
with open('test/resources/daemonset.json') as json_file:
daemonset_details_content = json.load(json_file)
read_daemonset_details = AttributeDict({'items': daemonset_details_content})
return read_daemonset_details


def mock_read_service_details_in_namespace(name, namespace):
if namespace == 'default':
with open('test/resources/service.json') as json_file:
service_details_content = json.load(json_file)
read_service_details = AttributeDict({'items': service_details_content})
return read_service_details


def mock_read_ingress_details_in_namespace(name, namespace):
if namespace == 'default':
with open('test/resources/ingress.json') as json_file:
ingress_details_content = json.load(json_file)
read_ingress_details = AttributeDict({'items': ingress_details_content})
return read_ingress_details


def mock_read_cron_job_details_in_namespace(name, namespace):
if namespace == 'default':
with open('test/resources/cronjob.json') as json_file:
cron_job_details_content = json.load(json_file)
read_cron_job_details = AttributeDict({'items': cron_job_details_content})
return read_cron_job_details


bearer_token = 'eyJhbGciOiJSUzI1NiIsImtpZCI6IjdXVWJMOUdTaDB1TjcyNmF0Sjk4RWlzQ05RaWdSUFoyN004TmlGT1pSX28ifQ.' \
'eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1' \
'lc3BhY2UiOiJkZWZhdWx0Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZWNyZXQubmFtZSI6Im15c2EtdG' \
Expand Down Expand Up @@ -404,6 +436,34 @@ def test_get_role_bindings_in_namespace(self, mock_lnp):
role_bindings = kl.get_role_bindings_in_namespace('default')
self.assertEqual(['read-pods'], [item for item in role_bindings])

@mock.patch('kubernetes.client.AppsV1Api.read_namespaced_daemon_set')
def test_get_daemonset_details_in_namespace(self, mock_lnp):
mock_lnp.side_effect = mock_read_daemonset_details_in_namespace
kl = KubeLibrary(kube_config='test/resources/k3d')
daemonset_details = kl.get_daemonset_details_in_namespace('fluentd-elasticsearch', 'default')
self.assertEqual('mytestlabel', daemonset_details.items[0].metadata.labels.TestLabel)

@mock.patch('kubernetes.client.CoreV1Api.read_namespaced_service')
def test_get_service_details_in_namespace(self, mock_lnp):
mock_lnp.side_effect = mock_read_service_details_in_namespace
kl = KubeLibrary(kube_config='test/resources/k3d')
service_details = kl.get_service_details_in_namespace('test-service', 'default')
self.assertEqual('mytest', service_details.items[0].metadata.labels.Test)

@mock.patch('kubernetes.client.ExtensionsV1beta1Api.read_namespaced_ingress')
def test_get_ingress_details_in_namespace(self, mock_lnp):
mock_lnp.side_effect = mock_read_ingress_details_in_namespace
kl = KubeLibrary(kube_config='test/resources/k3d')
ingress_details = kl.get_ingress_details_in_namespace('max-ingress', 'default')
self.assertEqual('mytestlabel', ingress_details.items[0].metadata.labels.TestLabel)

@mock.patch('kubernetes.client.BatchV1beta1Api.read_namespaced_cron_job')
def test_get_cron_job_details_in_namespace(self, mock_lnp):
mock_lnp.side_effect = mock_read_cron_job_details_in_namespace
kl = KubeLibrary(kube_config='test/resources/k3d')
cron_job_details = kl.get_cron_job_details_in_namespace('hello', 'default')
self.assertEqual('mytestlabel', cron_job_details.items[0].metadata.labels.TestLabel)

@mock.patch('kubernetes.client.AppsV1Api.list_namespaced_deployment')
def test_get_deployments_in_namespace(self, mock_lnp):
mock_lnp.side_effect = mock_list_namespaced_deployments
Expand Down