Repository: devopsspiral/KubeLibrary (language: python)

Issue:
Write unit tests for service part
Improve test coverage, by writing unit test for getting configmap based on a mock k8s object that can be generated like the one in https://github.com/devopsspiral/KubeLibrary/blob/d102ac0cc5caf573222053356d97f137862f04e0/test/test_KubeLibrary.py#L146 and store it in test/resources/. 

* Create test for generating object .json
* Write test that checks target keyword
* After coverage is up modify the CI threshold in .coveragerc to new value:
```
(...)
[report]
fail_under = 67
```


Discussion / hints:

Fix patch:
diff --git a/.coveragerc b/.coveragerc
index b0ae6d97..5929234d 100644
--- a/.coveragerc
+++ b/.coveragerc
@@ -3,4 +3,4 @@ command_line = -m unittest discover
 source =
     src/
 [report]
-fail_under = 68
+fail_under = 70


Test patch:
diff --git a/test/resources/service.json b/test/resources/service.json
new file mode 100644
index 00000000..f6c8d398
--- /dev/null
+++ b/test/resources/service.json
@@ -0,0 +1,60 @@
+[	
+	{
+		"apiVersion": "v1",
+		"kind": "Service",
+		"metadata": {
+			"creationTimestamp": "2021-03-17T12:27:33Z",
+			"managedFields": [
+				{
+					"apiVersion": "v1",
+					"fieldsType": "FieldsV1",
+					"fieldsV1": {
+						"f:spec": {
+							"f:ports": {
+								".": {},
+								"k:{\"port\":80,\"protocol\":\"TCP\"}": {
+									".": {},
+									"f:port": {},
+									"f:protocol": {},
+									"f:targetPort": {}
+								}
+							},
+							"f:selector": {
+								".": {},
+								"f:app": {}
+							},
+							"f:sessionAffinity": {},
+							"f:type": {}
+						}
+					},
+					"manager": "kubectl",
+					"operation": "Update",
+					"time": "2021-03-17T12:27:33Z"
+				}
+			],
+			"name": "test-service",
+			"namespace": "default",
+			"resourceVersion": "970",
+			"selfLink": "/api/v1/namespaces/default/services/my-service",
+			"uid": "bd0c1526-e540-4e80-aae6-088731013f1a"
+		},
+		"spec": {
+			"clusterIP": "10.108.127.133",
+			"ports": [
+				{
+					"port": 80,
+					"protocol": "TCP",
+					"targetPort": 9376
+				}
+			],
+			"selector": {
+				"app": "MyApp"
+			},
+			"sessionAffinity": "None",
+			"type": "ClusterIP"
+		},
+		"status": {
+			"loadBalancer": {}
+		}
+	}
+]
diff --git a/test/test_KubeLibrary.py b/test/test_KubeLibrary.py
index 4a668e44..1d4f473a 100644
--- a/test/test_KubeLibrary.py
+++ b/test/test_KubeLibrary.py
@@ -1,4 +1,3 @@
-
 import json
 import mock
 import re
@@ -47,6 +46,14 @@ def mock_list_cluster_roles(watch=False):
         return list_of_cluster_roles
 
 
+def mock_list_namespaced_services(namespace, watch=False, label_selector=""):
+    if namespace == 'default':
+        with open('test/resources/service.json') as json_file:
+            services_content = json.load(json_file)
+            list_services = AttributeDict({'items': services_content})
+            return list_services
+
+
 def mock_list_namespaced_pod(namespace, watch=False, label_selector=""):
     if namespace == 'default':
         with open('test/resources/pods.json') as json_file:
@@ -311,3 +318,10 @@ def test_get_role_bindings_in_namespace(self, mock_lnp):
         kl = KubeLibrary(kube_config='test/resources/k3d')
         role_bindings = kl.get_role_bindings_in_namespace('default')
         self.assertEqual(['read-pods'], [item for item in role_bindings])
+
+    @mock.patch('kubernetes.client.CoreV1Api.list_namespaced_service')
+    def test_get_services_in_namespace(self, mock_lnp):
+        mock_lnp.side_effect = mock_list_namespaced_services
+        kl = KubeLibrary(kube_config='test/resources/k3d')
+        services = kl.get_services_in_namespace('default')
+        self.assertEqual(['test-service'], [item for item in services])