Remove ContainerizedPlatformContainer from types
[dcaegen2/platform/plugins.git] / k8s / tests / test_tasks.py
1 # ============LICENSE_START=======================================================
2 # org.onap.dcae
3 # ================================================================================
4 # Copyright (c) 2017-2019 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 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
20
21 import copy
22 import pytest
23 from cloudify.exceptions import NonRecoverableError, RecoverableError
24
25
26 def test_generate_component_name(mockconfig):
27     from k8splugin import tasks
28     kwargs = { "service_component_type": "doodle",
29             "service_component_name_override": None }
30
31     assert "doodle" in tasks._generate_component_name(**kwargs)["name"]
32
33     kwargs["service_component_name_override"] = "yankee"
34
35     assert "yankee" == tasks._generate_component_name(**kwargs)["name"]
36
37
38 def test_parse_streams(monkeypatch, mockconfig):
39     import k8splugin
40     from k8splugin import tasks
41     # Good case for streams_publishes
42     test_input = { "streams_publishes": [{"name": "topic00", "type": "message_router"},
43             {"name": "feed00", "type": "data_router"}],
44             "streams_subscribes": {} }
45
46     expected = {'feed00': {'type': 'data_router', 'name': 'feed00'},
47             'streams_publishes': [{'type': 'message_router', 'name': 'topic00'},
48                 {'type': 'data_router', 'name': 'feed00'}],
49             'streams_subscribes': {},
50             'topic00': {'type': 'message_router', 'name': 'topic00'}
51             }
52
53     assert expected == tasks._parse_streams(**test_input)
54
55     # Good case for streams_subscribes (password provided)
56     test_input = { "ports": ["1919:0", "1920:0"],"name": "testcomponent",
57             "streams_publishes": {},
58             "streams_subscribes": [{"name": "topic01", "type": "message_router"},
59                 {"name": "feed01", "type": "data_router", "username": "hero",
60                     "password": "123456", "route":"test/v0"}] }
61
62     expected = {'ports': ['1919:0', '1920:0'], 'name': 'testcomponent',
63                 'feed01': {'type': 'data_router', 'name': 'feed01',
64                     'username': 'hero', 'password': '123456', 'route': 'test/v0', 'delivery_url':'http://testcomponent:1919/test/v0'},
65                 'streams_publishes': {},
66                 'streams_subscribes': [{'type': 'message_router', 'name': 'topic01'},
67                 {'type': 'data_router', 'name': 'feed01', 'username': 'hero',
68                     'password': '123456', 'route':'test/v0'}],
69                 'topic01': {'type': 'message_router', 'name': 'topic01'}}
70
71     assert expected == tasks._parse_streams(**test_input)
72
73     # Good case for streams_subscribes (password generated)
74     test_input = { "ports": ["1919:0", "1920:0"],"name": "testcomponent",
75         "streams_publishes": {},
76         "streams_subscribes": [{"name": "topic01", "type": "message_router"},
77                 {"name": "feed01", "type": "data_router", "username": None,
78                     "password": None, "route": "test/v0"}] }
79
80     def not_so_random(n):
81         return "nosurprise"
82
83     monkeypatch.setattr(k8splugin.utils, "random_string", not_so_random)
84
85     expected = { 'ports': ['1919:0', '1920:0'], 'name': 'testcomponent',
86              'feed01': {'type': 'data_router', 'name': 'feed01',
87                     'username': 'nosurprise', 'password': 'nosurprise', 'route':'test/v0', 'delivery_url':'http://testcomponent:1919/test/v0'},
88             'streams_publishes': {},
89             'streams_subscribes': [{'type': 'message_router', 'name': 'topic01'},
90                 {'type': 'data_router', 'name': 'feed01', 'username': None,
91                     'password': None, 'route': 'test/v0'}],
92             'topic01': {'type': 'message_router', 'name': 'topic01'}}
93
94     assert expected == tasks._parse_streams(**test_input)
95
96
97 def test_setup_for_discovery(monkeypatch, mockconfig):
98     import k8splugin
99     from k8splugin import tasks
100
101     test_input = { "name": "some-name",
102             "application_config": { "one": "a", "two": "b" } }
103
104     def fake_push_config(conn, name, application_config):
105         return
106
107     monkeypatch.setattr(k8splugin.discovery, "push_service_component_config",
108             fake_push_config)
109
110     assert test_input == tasks._setup_for_discovery(**test_input)
111
112     def fake_push_config_connection_error(conn, name, application_config):
113         raise k8splugin.discovery.DiscoveryConnectionError("Boom")
114
115     monkeypatch.setattr(k8splugin.discovery, "push_service_component_config",
116             fake_push_config_connection_error)
117
118     with pytest.raises(RecoverableError):
119         tasks._setup_for_discovery(**test_input)
120
121 def test_verify_container(monkeypatch, mockconfig):
122     import k8sclient
123     from k8splugin import tasks
124     from k8splugin.exceptions import DockerPluginDeploymentError
125
126     def fake_is_available_success(loc, ch, scn):
127         return True
128
129     monkeypatch.setattr(k8sclient, "is_available",
130             fake_is_available_success)
131
132     assert tasks._verify_k8s_deployment("some-location","some-name", 3)
133
134     def fake_is_available_never_good(loc, ch, scn):
135         return False
136
137     monkeypatch.setattr(k8sclient, "is_available",
138             fake_is_available_never_good)
139
140     assert not tasks._verify_k8s_deployment("some-location", "some-name", 2)
141
142 def test_enhance_docker_params(mockconfig):
143     from k8splugin import tasks
144     # Good - Test empty docker config
145
146     test_kwargs = { "docker_config": {}, "service_id": None }
147     actual = tasks._enhance_docker_params(**test_kwargs)
148
149     assert actual == {'envs': {}, 'docker_config': {}, 'ports': [], 'volumes': [], "service_id": None }
150
151     # Good - Test just docker config ports and volumes
152
153     test_kwargs = { "docker_config": { "ports": ["1:1", "2:2"],
154         "volumes": [{"container": "somewhere", "host": "somewhere else"}] },
155         "service_id": None }
156     actual = tasks._enhance_docker_params(**test_kwargs)
157
158     assert actual == {'envs': {}, 'docker_config': {'ports': ['1:1', '2:2'],
159         'volumes': [{'host': 'somewhere else', 'container': 'somewhere'}]},
160         'ports': ['1:1', '2:2'], 'volumes': [{'host': 'somewhere else',
161             'container': 'somewhere'}], "service_id": None}
162
163     # Good - Test just docker config ports and volumes with overrrides
164
165     test_kwargs = { "docker_config": { "ports": ["1:1", "2:2"],
166         "volumes": [{"container": "somewhere", "host": "somewhere else"}] },
167         "ports": ["3:3", "4:4"], "volumes": [{"container": "nowhere", "host":
168         "nowhere else"}],
169         "service_id": None }
170     actual = tasks._enhance_docker_params(**test_kwargs)
171
172     assert actual == {'envs': {}, 'docker_config': {'ports': ['1:1', '2:2'],
173         'volumes': [{'host': 'somewhere else', 'container': 'somewhere'}]},
174         'ports': ['1:1', '2:2', '3:3', '4:4'], 'volumes': [{'host': 'somewhere else',
175             'container': 'somewhere'}, {'host': 'nowhere else', 'container':
176             'nowhere'}], "service_id": None}
177
178     # Good
179
180     test_kwargs = { "docker_config": {}, "service_id": "zed",
181             "deployment_id": "abc" }
182     actual = tasks._enhance_docker_params(**test_kwargs)
183
184     assert actual["envs"] == {}
185
186
187 def test_notify_container(mockconfig):
188     from k8splugin import tasks
189
190     test_input = { "docker_config": { "policy": { "trigger_type": "unknown" } } }
191     assert [] == tasks._notify_container(**test_input)