Refactor tests for k8s_plugin
[dcaegen2/platform/plugins.git] / k8s / tests / test_k8sclient_deploy.py
1 # ============LICENSE_START=======================================================
2 # org.onap.dcae
3 # ================================================================================
4 # Copyright (c) 2018-2020 AT&T Intellectual Property. All rights reserved.
5 # Copyright (c) 2020-2021 Nokia. All rights reserved.
6 # ================================================================================
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #      http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 # ============LICENSE_END=========================================================
19
20 # Test k8sclient deployment functions
21 # Verify that for a given configuration and set of inputs, k8sclient generates the proper
22 # Kubernetes entities
23 import pytest
24 from common import do_deploy, verify_ports, verify_image, verify_rediness_probe, verify_volumes, \
25     verify_logs, verify_env_variables, verify_deployment_desc, verify_label
26 from common import verify_external_cert
27 from common import verify_cert_post_processor
28
29 K8S_CONFIGURATION = {
30     "image_pull_secrets": ["secret0", "secret1"],
31     "filebeat": {
32         "log_path": "/var/log/onap",
33         "data_path": "/usr/share/filebeat/data",
34         "config_path": "/usr/share/filebeat/filebeat.yml",
35         "config_subpath": "filebeat.yml",
36         "image": "filebeat-repo/filebeat:latest",
37         "config_map": "dcae-filebeat-configmap"
38     },
39     "tls": {
40         "cert_path": "/opt/certs",
41         "image": "tlsrepo/tls-init-container:1.2.3",
42         "component_cert_dir": "/opt/dcae/cacert"
43     },
44     "external_cert": {
45         "image_tag": "repo/oom-certservice-client:2.1.0",
46         "request_url": "https://request:1010/url",
47         "timeout": "30000",
48         "country": "US",
49         "organization": "Linux-Foundation",
50         "state": "California",
51         "organizational_unit": "ONAP",
52         "location": "San-Francisco",
53         "keystore_password": "secret1",
54         "truststore_password": "secret2"
55     },
56     "cert_post_processor": {
57         "image_tag": "repo/oom-cert-post-processor:2.1.0"
58     },
59     "cbs": {
60         "base_url": "https://config-binding-service:10443/service_component_all/test-component"
61     }
62 }
63
64 BASIC_KWARGS = {
65     "volumes": [
66         {
67             "host": {
68                 "path": "/path/on/host"
69             },
70             "container": {
71                 "bind": "/path/on/container",
72                 "mode": "rw"
73             }
74         }
75     ],
76     "ports": [
77         "80:0",
78         "443:0"
79     ],
80     "env": {
81         "NAME0": "value0",
82         "NAME1": "value1"
83     },
84     "log_info": {
85         "log_directory": "/path/to/container/log/directory"
86     },
87     "readiness": {
88         "type": "http",
89         "endpoint": "/ready"
90     },
91     "resources": {
92         "limits": {
93             "cpu": 0.5,
94             "memory": "2Gi"
95         },
96         "requests": {
97             "cpu": 0.5,
98             "memory": "2Gi"
99         }
100     }
101 }
102
103 KWARGS_WITH_FULL_TLS = {"tls_info": {"use_tls": True, "cert_directory": "/path/to/container/cert/directory"}}
104 KWARGS_TLS_OFF = {"tls_info": {"use_tls": False, "cert_directory": "/path/to/container/cert/directory"}}
105 KWARGS_WITH_EXTERNAL_CERT = {"external_cert": {"external_cert_directory": "/path/to/container/cert/directory/",
106                                                "use_external_tls": True,
107                                                "cert_type": "P12",
108                                                "ca_name": "myname",
109                                                "external_certificate_parameters": {
110                                                    "common_name": "mycommonname",
111                                                    "sans": "mysans"}
112                                                }}
113
114 KWARGS_WITH_CONFIG_MAP = {"config_volume": {"name": "myConfigMap"},
115                           "container": {"bind": "/path/to/configMap", "mode": "ro"}}
116
117
118 test_data = [(KWARGS_WITH_EXTERNAL_CERT, "/opt/dcae/cacert"),
119              (BASIC_KWARGS, "/opt/dcae/cacert"),
120              (KWARGS_TLS_OFF, "/path/to/container/cert/directory"),
121              (KWARGS_WITH_FULL_TLS, "/path/to/container/cert/directory")]
122
123
124 @pytest.mark.parametrize("blueprint_dict, path", test_data)
125 def test_deploy(mockk8sapi, blueprint_dict, path):
126     # given
127     kwargs = dict(BASIC_KWARGS)
128     kwargs.update(blueprint_dict)
129
130     # when
131     dep, deployment_description = do_deploy(K8S_CONFIGURATION, kwargs)
132     app_container = dep.spec.template.spec.containers[0]
133     log_container = dep.spec.template.spec.containers[1]
134
135     # then
136     verify_label(dep)
137     assert app_container.volume_mounts[2].mount_path == path
138     verify_ports(app_container)
139     verify_image(app_container)
140     verify_rediness_probe(app_container)
141     verify_volumes(app_container)
142     verify_logs(log_container)
143     verify_env_variables(app_container)
144     verify_deployment_desc(deployment_description)
145
146
147 def test_deploy_external_cert(mockk8sapi):
148     """ Deploy component with external TLS configuration """
149     # given
150     kwargs = dict(BASIC_KWARGS)
151     kwargs.update(KWARGS_WITH_EXTERNAL_CERT)
152
153     # when
154     dep, deployment_description = do_deploy(K8S_CONFIGURATION, kwargs)
155
156     # then
157     verify_external_cert(dep)
158     verify_cert_post_processor(dep)
159
160
161 def test_deploy_config_map(mockk8sapi):
162     """ Deploy component with configMap in volumes """
163     # given
164     kwargs = dict(BASIC_KWARGS)
165     kwargs['volumes'].append(KWARGS_WITH_CONFIG_MAP)
166
167     # when
168     dep, deployment_description = do_deploy(K8S_CONFIGURATION, kwargs)
169     app_container = dep.spec.template.spec.containers[0]
170
171     # then
172     assert app_container.volume_mounts[1].mount_path == "/path/to/configMap"