Add certificate custom resource creation when CertManager CMPv2 integration is enabled
[dcaegen2/platform/plugins.git] / k8s / tests / test_tasks.py
1 # ============LICENSE_START=======================================================
2 # org.onap.dcae
3 # ================================================================================
4 # Copyright (c) 2017-2020 AT&T Intellectual Property. All rights reserved.
5 # ================================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #      http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 # ============LICENSE_END=========================================================
18 #
19
20 import copy
21 import pytest
22 from cloudify.exceptions import NonRecoverableError, RecoverableError
23
24
25 def test_generate_component_name(mockconfig):
26     from k8splugin import tasks
27     kwargs = { "service_component_type": "doodle",
28             "service_component_name_override": None }
29
30     assert "doodle" in tasks._generate_component_name(**kwargs)["name"]
31
32     kwargs["service_component_name_override"] = "yankee"
33
34     assert "yankee" == tasks._generate_component_name(**kwargs)["name"]
35
36
37 def test_parse_streams(monkeypatch, mockconfig):
38     import k8splugin
39     from k8splugin import tasks
40     # Good case for streams_publishes
41     test_input = { "streams_publishes": [{"name": "topic00", "type": "message_router"},
42             {"name": "feed00", "type": "data_router"}],
43             "streams_subscribes": {} }
44
45     expected = {'feed00': {'type': 'data_router', 'name': 'feed00'},
46             'streams_publishes': [{'type': 'message_router', 'name': 'topic00'},
47                 {'type': 'data_router', 'name': 'feed00'}],
48             'streams_subscribes': {},
49             'topic00': {'type': 'message_router', 'name': 'topic00'}
50             }
51
52     assert expected == tasks._parse_streams(**test_input)
53
54     # Good case for streams_subscribes (password provided)
55     test_input = { "ports": ["1919:0", "1920:0"],"name": "testcomponent",
56             "streams_publishes": {},
57             "streams_subscribes": [{"name": "topic01", "type": "message_router"},
58                 {"name": "feed01", "type": "data_router", "username": "hero",
59                     "password": "123456", "route":"test/v0"}] }
60
61     expected = {'ports': ['1919:0', '1920:0'], 'name': 'testcomponent',
62                 'feed01': {'type': 'data_router', 'name': 'feed01',
63                     'username': 'hero', 'password': '123456', 'route': 'test/v0', 'delivery_url':'http://testcomponent:1919/test/v0'},
64                 'streams_publishes': {},
65                 'streams_subscribes': [{'type': 'message_router', 'name': 'topic01'},
66                 {'type': 'data_router', 'name': 'feed01', 'username': 'hero',
67                     'password': '123456', 'route':'test/v0'}],
68                 'topic01': {'type': 'message_router', 'name': 'topic01'}}
69
70     assert expected == tasks._parse_streams(**test_input)
71
72     # Good case for streams_subscribes (password generated)
73     test_input = { "ports": ["1919:0", "1920:0"],"name": "testcomponent",
74         "streams_publishes": {},
75         "streams_subscribes": [{"name": "topic01", "type": "message_router"},
76                 {"name": "feed01", "type": "data_router", "username": None,
77                     "password": None, "route": "test/v0"}] }
78
79     def not_so_random(n):
80         return "nosurprise"
81
82     monkeypatch.setattr(k8splugin.utils, "random_string", not_so_random)
83
84     expected = { 'ports': ['1919:0', '1920:0'], 'name': 'testcomponent',
85              'feed01': {'type': 'data_router', 'name': 'feed01',
86                     'username': 'nosurprise', 'password': 'nosurprise', 'route':'test/v0', 'delivery_url':'http://testcomponent:1919/test/v0'},
87             'streams_publishes': {},
88             'streams_subscribes': [{'type': 'message_router', 'name': 'topic01'},
89                 {'type': 'data_router', 'name': 'feed01', 'username': None,
90                     'password': None, 'route': 'test/v0'}],
91             'topic01': {'type': 'message_router', 'name': 'topic01'}}
92
93     assert expected == tasks._parse_streams(**test_input)
94
95
96 def test_setup_for_discovery(monkeypatch, mockconfig):
97     import k8splugin
98     from k8splugin import tasks
99
100     test_input = { "name": "some-name",
101             "application_config": { "one": "a", "two": "b" } }
102
103     def fake_push_config(conn, name, application_config):
104         return
105
106     monkeypatch.setattr(k8splugin.discovery, "push_service_component_config",
107             fake_push_config)
108
109     assert test_input == tasks._setup_for_discovery(**test_input)
110
111     def fake_push_config_connection_error(conn, name, application_config):
112         raise k8splugin.discovery.DiscoveryConnectionError("Boom")
113
114     monkeypatch.setattr(k8splugin.discovery, "push_service_component_config",
115             fake_push_config_connection_error)
116
117     with pytest.raises(RecoverableError):
118         tasks._setup_for_discovery(**test_input)
119
120 def test_verify_container(monkeypatch, mockconfig):
121     import k8sclient
122     from k8splugin import tasks
123     from k8splugin.exceptions import DockerPluginDeploymentError
124
125     def fake_is_available_success(loc, ch, scn):
126         return True
127
128     monkeypatch.setattr(k8sclient, "is_available",
129             fake_is_available_success)
130
131     assert tasks._verify_k8s_deployment("some-location","some-name", 3)
132
133     def fake_is_available_never_good(loc, ch, scn):
134         return False
135
136     monkeypatch.setattr(k8sclient, "is_available",
137             fake_is_available_never_good)
138
139     assert not tasks._verify_k8s_deployment("some-location", "some-name", 2)
140
141 def test_enhance_docker_params(mockconfig):
142     from k8splugin import tasks
143     # Good - Test empty docker config
144
145     test_kwargs = { "docker_config": {}, "service_id": None }
146     actual = tasks._enhance_docker_params(**test_kwargs)
147
148     assert actual == {'envs': {}, 'docker_config': {}, 'ports': [], 'volumes': [], "service_id": None }
149
150     # Good - Test just docker config ports and volumes
151
152     test_kwargs = { "docker_config": { "ports": [{u'concat': [u'8080', u':', 0], u'ipv6': True}, "1:1"],
153         "volumes": [{"container": "somewhere", "host": "somewhere else"}] },
154         "service_id": None }
155     actual = tasks._enhance_docker_params(**test_kwargs)
156
157     assert actual == {'envs': {}, 'docker_config': {'ports': [{u'concat': [u'8080', u':', 0], u'ipv6': True}, "1:1"],
158         'volumes': [{'host': 'somewhere else', 'container': 'somewhere'}]},
159         'ports': [{u'concat': [u'8080', u':', 0], u'ipv6': True}, "1:1"], 'volumes': [{'host': 'somewhere else',
160         'container': 'somewhere'}], "service_id": None}
161
162     # Good - Test just docker config ports and volumes with overrrides
163
164     test_kwargs = { "docker_config": { "ports": ["1:1", "2:2"],
165         "volumes": [{"container": "somewhere", "host": "somewhere else"}] },
166         "ports": ["3:3", "4:4"], "volumes": [{"container": "nowhere", "host":
167         "nowhere else"}],
168         "service_id": None }
169     actual = tasks._enhance_docker_params(**test_kwargs)
170
171     assert actual == {'envs': {}, 'docker_config': {'ports': ['1:1', '2:2'],
172         'volumes': [{'host': 'somewhere else', 'container': 'somewhere'}]},
173         'ports': ['1:1', '2:2', '3:3', '4:4'], 'volumes': [{'host': 'somewhere else',
174             'container': 'somewhere'}, {'host': 'nowhere else', 'container':
175             'nowhere'}], "service_id": None}
176
177     # Good
178
179     test_kwargs = { "docker_config": {}, "service_id": "zed",
180             "deployment_id": "abc" }
181     actual = tasks._enhance_docker_params(**test_kwargs)
182
183     assert actual["envs"] == {}
184
185
186 def test_notify_container(mockconfig):
187     from k8splugin import tasks
188
189     test_input = { "docker_config": { "policy": { "trigger_type": "unknown" } } }
190     assert [] == tasks._notify_container(**test_input)