Refactor tests for k8s_plugin
[dcaegen2/platform/plugins.git] / k8s / tests / test_k8sclient_deploy.py
index 55daf3b..c7b0646 100644 (file)
 # Test k8sclient deployment functions
 # Verify that for a given configuration and set of inputs, k8sclient generates the proper
 # Kubernetes entities
-
 import pytest
-from common import do_deploy
+from common import do_deploy, verify_ports, verify_image, verify_rediness_probe, verify_volumes, \
+    verify_logs, verify_env_variables, verify_deployment_desc, verify_label
 from common import verify_external_cert
 from common import verify_cert_post_processor
 
-
-def test_deploy_full_tls(mockk8sapi):
-    """ Deploy component with a full TLS configuration, to act as a server """
-
-    dep, deployment_description = do_deploy(tls_info={"use_tls": True, "cert_directory": "/path/to/container/cert/directory"})
-
-    app_container = dep.spec.template.spec.containers[0]
-    assert app_container.volume_mounts[2].mount_path == "/path/to/container/cert/directory"
-
-
-def test_deploy_tls_off(mockk8sapi):
-    """ TLS client only, but with cert directory configured """
-
-    dep, deployment_description = do_deploy(tls_info={"use_tls": False, "cert_directory": "/path/to/container/cert/directory"})
-
+K8S_CONFIGURATION = {
+    "image_pull_secrets": ["secret0", "secret1"],
+    "filebeat": {
+        "log_path": "/var/log/onap",
+        "data_path": "/usr/share/filebeat/data",
+        "config_path": "/usr/share/filebeat/filebeat.yml",
+        "config_subpath": "filebeat.yml",
+        "image": "filebeat-repo/filebeat:latest",
+        "config_map": "dcae-filebeat-configmap"
+    },
+    "tls": {
+        "cert_path": "/opt/certs",
+        "image": "tlsrepo/tls-init-container:1.2.3",
+        "component_cert_dir": "/opt/dcae/cacert"
+    },
+    "external_cert": {
+        "image_tag": "repo/oom-certservice-client:2.1.0",
+        "request_url": "https://request:1010/url",
+        "timeout": "30000",
+        "country": "US",
+        "organization": "Linux-Foundation",
+        "state": "California",
+        "organizational_unit": "ONAP",
+        "location": "San-Francisco",
+        "keystore_password": "secret1",
+        "truststore_password": "secret2"
+    },
+    "cert_post_processor": {
+        "image_tag": "repo/oom-cert-post-processor:2.1.0"
+    },
+    "cbs": {
+        "base_url": "https://config-binding-service:10443/service_component_all/test-component"
+    }
+}
+
+BASIC_KWARGS = {
+    "volumes": [
+        {
+            "host": {
+                "path": "/path/on/host"
+            },
+            "container": {
+                "bind": "/path/on/container",
+                "mode": "rw"
+            }
+        }
+    ],
+    "ports": [
+        "80:0",
+        "443:0"
+    ],
+    "env": {
+        "NAME0": "value0",
+        "NAME1": "value1"
+    },
+    "log_info": {
+        "log_directory": "/path/to/container/log/directory"
+    },
+    "readiness": {
+        "type": "http",
+        "endpoint": "/ready"
+    },
+    "resources": {
+        "limits": {
+            "cpu": 0.5,
+            "memory": "2Gi"
+        },
+        "requests": {
+            "cpu": 0.5,
+            "memory": "2Gi"
+        }
+    }
+}
+
+KWARGS_WITH_FULL_TLS = {"tls_info": {"use_tls": True, "cert_directory": "/path/to/container/cert/directory"}}
+KWARGS_TLS_OFF = {"tls_info": {"use_tls": False, "cert_directory": "/path/to/container/cert/directory"}}
+KWARGS_WITH_EXTERNAL_CERT = {"external_cert": {"external_cert_directory": "/path/to/container/cert/directory/",
+                                               "use_external_tls": True,
+                                               "cert_type": "P12",
+                                               "ca_name": "myname",
+                                               "external_certificate_parameters": {
+                                                   "common_name": "mycommonname",
+                                                   "sans": "mysans"}
+                                               }}
+
+KWARGS_WITH_CONFIG_MAP = {"config_volume": {"name": "myConfigMap"},
+                          "container": {"bind": "/path/to/configMap", "mode": "ro"}}
+
+
+test_data = [(KWARGS_WITH_EXTERNAL_CERT, "/opt/dcae/cacert"),
+             (BASIC_KWARGS, "/opt/dcae/cacert"),
+             (KWARGS_TLS_OFF, "/path/to/container/cert/directory"),
+             (KWARGS_WITH_FULL_TLS, "/path/to/container/cert/directory")]
+
+
+@pytest.mark.parametrize("blueprint_dict, path", test_data)
+def test_deploy(mockk8sapi, blueprint_dict, path):
+    # given
+    kwargs = dict(BASIC_KWARGS)
+    kwargs.update(blueprint_dict)
+
+    # when
+    dep, deployment_description = do_deploy(K8S_CONFIGURATION, kwargs)
     app_container = dep.spec.template.spec.containers[0]
-    assert app_container.volume_mounts[2].mount_path == "/path/to/container/cert/directory"
+    log_container = dep.spec.template.spec.containers[1]
 
-
-def test_deploy_no_tls_info(mockk8sapi):
-    """ TLS client only, but with cert directory configured """
-
-    dep, deployment_description = do_deploy()
-
-    app_container = dep.spec.template.spec.containers[0]
-    assert app_container.volume_mounts[2].mount_path == "/opt/dcae/cacert"
+    # then
+    verify_label(dep)
+    assert app_container.volume_mounts[2].mount_path == path
+    verify_ports(app_container)
+    verify_image(app_container)
+    verify_rediness_probe(app_container)
+    verify_volumes(app_container)
+    verify_logs(log_container)
+    verify_env_variables(app_container)
+    verify_deployment_desc(deployment_description)
 
 
 def test_deploy_external_cert(mockk8sapi):
     """ Deploy component with external TLS configuration """
+    # given
+    kwargs = dict(BASIC_KWARGS)
+    kwargs.update(KWARGS_WITH_EXTERNAL_CERT)
 
-    dep, deployment_description = do_deploy(
-        ext_tls_info={"external_cert_directory": "/path/to/container/cert/directory/",
-                      "use_external_tls": True,
-                      "cert_type": "P12",
-                      "ca_name": "myname",
-                      "external_certificate_parameters": {
-                          "common_name": "mycommonname",
-                          "sans": "mysans"}
-                      })
+    # when
+    dep, deployment_description = do_deploy(K8S_CONFIGURATION, kwargs)
 
-    app_container = dep.spec.template.spec.containers[0]
-    assert app_container.volume_mounts[2].mount_path == "/opt/dcae/cacert"
-
-    # Make sure all of the external init container parameters are correct
+    # then
     verify_external_cert(dep)
     verify_cert_post_processor(dep)
 
+
 def test_deploy_config_map(mockk8sapi):
     """ Deploy component with configMap in volumes """
-    config_map = {"config_volume": {"name": "myConfigMap"}, "container": {"bind": "/path/to/configMap", "mode": "ro"}}
-    dep, deployment_description = do_deploy(config_map)
+    # given
+    kwargs = dict(BASIC_KWARGS)
+    kwargs['volumes'].append(KWARGS_WITH_CONFIG_MAP)
 
+    # when
+    dep, deployment_description = do_deploy(K8S_CONFIGURATION, kwargs)
     app_container = dep.spec.template.spec.containers[0]
-    assert app_container.volume_mounts[1].mount_path == "/path/to/configMap"
\ No newline at end of file
+
+    # then
+    assert app_container.volume_mounts[1].mount_path == "/path/to/configMap"