Repository: devopsspiral/KubeLibrary (language: python)

Issue:
Write unit tests for pvc 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 29fbfc1b..7bbea009 100644
--- a/.coveragerc
+++ b/.coveragerc
@@ -3,4 +3,4 @@ command_line = -m unittest discover
 source =
     src/
 [report]
-fail_under = 71
\ No newline at end of file
+fail_under = 72


Test patch:
diff --git a/test/resources/pvc.json b/test/resources/pvc.json
new file mode 100644
index 00000000..cb8d9f8f
--- /dev/null
+++ b/test/resources/pvc.json
@@ -0,0 +1,79 @@
+[
+{
+    "apiVersion": "v1",
+    "kind": "PersistentVolumeClaim",
+    "metadata": {
+        "creationTimestamp": "2021-03-19T03:42:46Z",
+        "finalizers": [
+            "kubernetes.io/pvc-protection"
+        ],
+        "managedFields": [
+            {
+                "apiVersion": "v1",
+                "fieldsType": "FieldsV1",
+                "fieldsV1": {
+                    "f:spec": {
+                        "f:accessModes": {},
+                        "f:resources": {
+                            "f:requests": {
+                                ".": {},
+                                "f:storage": {}
+                            }
+                        },
+                        "f:selector": {
+                            ".": {},
+                            "f:matchExpressions": {},
+                            "f:matchLabels": {
+                                ".": {},
+                                "f:release": {}
+                            }
+                        },
+                        "f:storageClassName": {},
+                        "f:volumeMode": {}
+                    },
+                    "f:status": {
+                        "f:phase": {}
+                    }
+                },
+                "manager": "kubectl",
+                "operation": "Update",
+                "time": "2021-03-19T03:42:46Z"
+            }
+        ],
+        "name": "myclaim",
+        "namespace": "default",
+        "resourceVersion": "4686",
+        "selfLink": "/api/v1/namespaces/default/persistentvolumeclaims/myclaim",
+        "uid": "42708d93-02f5-4488-9bae-497d1a150de8"
+    },
+    "spec": {
+        "accessModes": [
+            "ReadWriteOnce"
+        ],
+        "resources": {
+            "requests": {
+                "storage": "8Gi"
+            }
+        },
+        "selector": {
+            "matchExpressions": [
+                {
+                    "key": "environment",
+                    "operator": "In",
+                    "values": [
+                        "dev"
+                    ]
+                }
+            ],
+            "matchLabels": {
+                "release": "stable"
+            }
+        },
+        "storageClassName": "slow",
+        "volumeMode": "Filesystem"
+    },
+    "status": {
+        "phase": "Pending"
+    }
+}
+]
diff --git a/test/test_KubeLibrary.py b/test/test_KubeLibrary.py
index 0992017b..af3d23b6 100644
--- a/test/test_KubeLibrary.py
+++ b/test/test_KubeLibrary.py
@@ -46,6 +46,14 @@ def mock_list_namespaced_config_map(namespace, watch=False, label_selector=""):
         return configmap
 
 
+def mock_list_pvc(namespace, watch=False, label_selector=""):
+    if namespace == 'default':
+        with open('test/resources/pvc.json') as json_file:
+            pvc_content = json.load(json_file)
+            list_pvc = AttributeDict({'items': pvc_content})
+            return list_pvc
+
+
 def mock_list_cluster_roles(watch=False):
     with open('test/resources/cluster_role.json') as json_file:
         cluster_roles_content = json.load(json_file)
@@ -332,3 +340,10 @@ def test_get_configmaps_in_namespace(self, mock_lnp):
         kl = KubeLibrary(kube_config='test/resources/k3d')
         configmaps = kl.get_configmaps_in_namespace('.*', 'default')
         self.assertEqual(['game-demo'], kl.filter_configmap_names(configmaps))
+
+    @mock.patch('kubernetes.client.CoreV1Api.list_namespaced_persistent_volume_claim')
+    def test_get_pvc_in_namespace(self, mock_lnp):
+        mock_lnp.side_effect = mock_list_pvc
+        kl = KubeLibrary(kube_config='test/resources/k3d')
+        pvcs = kl.get_pvc_in_namespace('default')
+        self.assertEqual(['myclaim'], [item for item in pvcs])