Add support to request certificates from CMPv2 server in DCAE cloudify blueprints
[dcaegen2/platform/plugins.git] / k8s / tests / common.py
1 # ============LICENSE_START=======================================================
2 # org.onap.dcae
3 # ================================================================================
4 # Copyright (c) 2019-2020 AT&T Intellectual Property. All rights reserved.
5 # Copyright (c) 2020 Pantheon.tech. All rights reserved.
6 # Copyright (c) 2020 Nokia. All rights reserved.
7 # ================================================================================
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
11 #
12 #      http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 # ============LICENSE_END=========================================================
20
21 # Common functions for unit testing
22 def _set_k8s_configuration():
23     ''' Set up the basic k8s configuration '''
24     return  {
25         "image_pull_secrets" : ["secret0", "secret1"],
26         "filebeat" : {
27             "log_path": "/var/log/onap",
28             "data_path": "/usr/share/filebeat/data",
29             "config_path": "/usr/share/filebeat/filebeat.yml",
30             "config_subpath": "filebeat.yml",
31             "image" : "filebeat-repo/filebeat:latest",
32             "config_map" : "dcae-filebeat-configmap"
33         },
34         "tls" : {
35             "cert_path": "/opt/certs",
36             "image": "tlsrepo/tls-init-container:1.2.3",
37             "component_cert_dir": "/opt/dcae/cacert"
38         },
39         "external_cert": {
40             "image_tag": "repo/aaf-certservice-client:1.2.3",
41             "request_url" : "https://request:1010/url",
42             "timeout" : "30000",
43             "country" : "US",
44             "organization" : "Linux-Foundation",
45             "state" : "California",
46             "organizational_unit" : "ONAP",
47             "location" : "San-Francisco",
48             "keystore_password" : "secret1",
49             "truststore_password" : "secret2"
50         },
51         "cbs": {
52             "base_url": "https://config-binding-service:10443/service_component_all/test-component"
53         }
54     }
55
56 def _set_resources():
57     ''' Set resources '''
58     return {
59         "limits": {
60             "cpu" : 0.5,
61             "memory" : "2Gi"
62         },
63         "requests": {
64             "cpu" : 0.5,
65             "memory" : "2Gi"
66         }
67     }
68
69 def _set_common_kwargs():
70     ''' Set kwargs common to all test cases '''
71     return {
72         "volumes": [
73             {"host":{"path": "/path/on/host"}, "container":{"bind":"/path/on/container","mode":"rw"}}
74         ],
75         "ports": ["80:0", "443:0"],
76         "env": {"NAME0": "value0", "NAME1": "value1"},
77         "log_info": {"log_directory": "/path/to/container/log/directory"},
78         "readiness": {"type": "http", "endpoint" : "/ready"}
79     }
80
81 def _get_item_by_name(list, name):
82     ''' Search a list of k8s API objects with the specified name '''
83     for item in list:
84         if item.name == name:
85             return item
86     return None
87
88 def check_env_var(env_list, name, value):
89     e = _get_item_by_name(env_list, name)
90     assert e and e.value == value
91
92 def verify_common(dep, deployment_description):
93     ''' Check results common to all test cases '''
94     assert deployment_description["deployment"] == "dep-testcomponent"
95     assert deployment_description["namespace"] == "k8stest"
96     assert deployment_description["services"][0] == "testcomponent"
97
98     # For unit test purposes, we want to make sure that the deployment object
99     # we're passing to the k8s API is correct
100     app_container = dep.spec.template.spec.containers[0]
101     assert app_container.image == "example.com/testcomponent:1.4.3"
102     assert app_container.image_pull_policy == "IfNotPresent"
103     assert len(app_container.ports) == 2
104     assert app_container.ports[0].container_port == 80
105     assert app_container.ports[1].container_port == 443
106     assert app_container.readiness_probe.http_get.path == "/ready"
107     assert app_container.readiness_probe.http_get.scheme == "HTTP"
108     assert len(app_container.volume_mounts) == 3
109     assert app_container.volume_mounts[0].mount_path == "/path/on/container"
110     assert app_container.volume_mounts[1].mount_path == "/path/to/container/log/directory"
111
112     # Check environment variables
113     env = app_container.env
114     check_env_var(env, "NAME0", "value0")
115     check_env_var(env, "NAME1", "value1")
116
117     # Should have a log container with volume mounts
118     log_container = dep.spec.template.spec.containers[1]
119     assert log_container.image == "filebeat-repo/filebeat:latest"
120     assert log_container.volume_mounts[0].mount_path == "/var/log/onap/testcomponent"
121     assert log_container.volume_mounts[0].name == "component-log"
122     assert log_container.volume_mounts[1].mount_path == "/usr/share/filebeat/data"
123     assert log_container.volume_mounts[1].name == "filebeat-data"
124     assert log_container.volume_mounts[2].mount_path == "/usr/share/filebeat/filebeat.yml"
125     assert log_container.volume_mounts[2].name == "filebeat-conf"
126
127     # Needs to be correctly labeled so that the Service can find it
128     assert dep.spec.template.metadata.labels["app"] == "testcomponent"
129
130 def verify_external_cert(dep):
131     cert_container = dep.spec.template.spec.init_containers[1]
132     print(cert_container)
133     assert cert_container.image == "repo/aaf-certservice-client:1.2.3"
134     assert cert_container.name == "cert-service-client"
135     assert len(cert_container.volume_mounts) == 2
136     assert cert_container.volume_mounts[0].name == "tls-info"
137     assert cert_container.volume_mounts[0].mount_path == "/path/to/container/cert/directory/"
138     assert cert_container.volume_mounts[1].name == "tls-volume"
139     assert cert_container.volume_mounts[1].mount_path == "/etc/onap/aaf/certservice/certs/"
140
141     expected_envs = {
142             "REQUEST_URL": "https://request:1010/url",
143             "REQUEST_TIMEOUT": "30000",
144             "OUTPUT_PATH": "/path/to/container/cert/directory/external",
145             "OUTPUT_TYPE": "P12",
146             "CA_NAME": "myname",
147             "COMMON_NAME": "mycommonname",
148             "ORGANIZATION": "Linux-Foundation",
149             "ORGANIZATION_UNIT": "ONAP",
150             "LOCATION": "San-Francisco",
151             "STATE": "California",
152             "COUNTRY": "US",
153             "SANS": "mysans",
154             "KEYSTORE_PATH": "/etc/onap/aaf/certservice/certs/certServiceClient-keystore.jks",
155             "KEYSTORE_PASSWORD": "secret1",
156             "TRUSTSTORE_PATH": "/etc/onap/aaf/certservice/certs/truststore.jks",
157             "TRUSTSTORE_PASSWORD": "secret2"}
158
159     envs = {k.name: k.value for k in cert_container.env}
160     for k in expected_envs:
161         assert (k in envs and expected_envs[k] == envs[k])
162
163 def do_deploy(tls_info=None):
164     ''' Common deployment operations '''
165     import k8sclient.k8sclient
166
167     k8s_test_config = _set_k8s_configuration()
168
169     kwargs = _set_common_kwargs()
170     kwargs['resources'] = _set_resources()
171
172     if tls_info:
173         kwargs["tls_info"] = tls_info
174
175     dep, deployment_description = k8sclient.k8sclient.deploy("k8stest", "testcomponent", "example.com/testcomponent:1.4.3", 1, False, k8s_test_config, **kwargs)
176
177     # Make sure all of the basic k8s parameters are correct
178     verify_common(dep, deployment_description)
179
180     return dep, deployment_description
181
182
183 def do_deploy_ext(ext_tls_info):
184     ''' Common deployment operations '''
185     import k8sclient.k8sclient
186
187     k8s_test_config = _set_k8s_configuration()
188
189     kwargs = _set_common_kwargs()
190     kwargs['resources'] = _set_resources()
191     kwargs["external_cert"] = ext_tls_info
192
193     dep, deployment_description = k8sclient.k8sclient.deploy("k8stest", "testcomponent", "example.com/testcomponent:1.4.3", 1, False, k8s_test_config, **kwargs)
194
195     # Make sure all of the basic k8s parameters are correct
196     verify_common(dep, deployment_description)
197
198     return dep, deployment_description