1 # ============LICENSE_START=======================================================
3 # ================================================================================
4 # Copyright (c) 2017-2018 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
10 # http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
19 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 from cloudify.exceptions import NonRecoverableError, RecoverableError
26 def test_generate_component_name(mockconfig):
27 from k8splugin import tasks
28 kwargs = { "service_component_type": "doodle",
29 "service_component_name_override": None }
31 assert "doodle" in tasks._generate_component_name(**kwargs)["name"]
33 kwargs["service_component_name_override"] = "yankee"
35 assert "yankee" == tasks._generate_component_name(**kwargs)["name"]
38 def test_parse_streams(monkeypatch, mockconfig):
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": {} }
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'}
53 assert expected == tasks._parse_streams(**test_input)
55 # Good case for streams_subscribes (password provided)
56 test_input = { "streams_publishes": {},
57 "streams_subscribes": [{"name": "topic01", "type": "message_router"},
58 {"name": "feed01", "type": "data_router", "username": "hero",
59 "password": "123456"}] }
61 expected = {'feed01': {'type': 'data_router', 'name': 'feed01',
62 'username': 'hero', 'password': '123456'},
63 'streams_publishes': {},
64 'streams_subscribes': [{'type': 'message_router', 'name': 'topic01'},
65 {'type': 'data_router', 'name': 'feed01', 'username': 'hero',
66 'password': '123456'}],
67 'topic01': {'type': 'message_router', 'name': 'topic01'}}
69 assert expected == tasks._parse_streams(**test_input)
71 # Good case for streams_subscribes (password generated)
72 test_input = { "streams_publishes": {},
73 "streams_subscribes": [{"name": "topic01", "type": "message_router"},
74 {"name": "feed01", "type": "data_router", "username": None,
80 monkeypatch.setattr(k8splugin.utils, "random_string", not_so_random)
82 expected = {'feed01': {'type': 'data_router', 'name': 'feed01',
83 'username': 'nosurprise', 'password': 'nosurprise'},
84 'streams_publishes': {},
85 'streams_subscribes': [{'type': 'message_router', 'name': 'topic01'},
86 {'type': 'data_router', 'name': 'feed01', 'username': None,
88 'topic01': {'type': 'message_router', 'name': 'topic01'}}
90 assert expected == tasks._parse_streams(**test_input)
93 def test_setup_for_discovery(monkeypatch, mockconfig):
95 from k8splugin import tasks
97 test_input = { "name": "some-name",
98 "application_config": { "one": "a", "two": "b" } }
100 def fake_push_config(conn, name, application_config):
103 monkeypatch.setattr(k8splugin.discovery, "push_service_component_config",
106 assert test_input == tasks._setup_for_discovery(**test_input)
108 def fake_push_config_connection_error(conn, name, application_config):
109 raise k8splugin.discovery.DiscoveryConnectionError("Boom")
111 monkeypatch.setattr(k8splugin.discovery, "push_service_component_config",
112 fake_push_config_connection_error)
114 with pytest.raises(RecoverableError):
115 tasks._setup_for_discovery(**test_input)
118 def test_setup_for_discovery_streams(monkeypatch, mockconfig):
120 from k8splugin import tasks
121 test_input = {'feed01': {'type': 'data_router', 'name': 'feed01',
122 'username': 'hero', 'password': '123456', 'location': 'Bedminster'},
123 'streams_publishes': {},
124 'streams_subscribes': [{'type': 'message_router', 'name': 'topic01'},
125 {'type': 'data_router', 'name': 'feed01', 'username': 'hero',
126 'password': '123456', 'location': 'Bedminster'}],
127 'topic01': {'type': 'message_router', 'name': 'topic01'}}
128 test_input["name"] = "some-foo-service-component"
131 def fake_add_to_entry(conn, key, add_name, add_value):
133 This fake method will check all the pieces that are used to make store
136 if key != test_input["name"] + ":dmaap":
138 if add_name != "feed01":
140 if add_value != {"location": "Bedminster", "delivery_url": None,
141 "username": "hero", "password": "123456", "subscriber_id": None}:
146 monkeypatch.setattr(k8splugin.discovery, "add_to_entry",
149 assert tasks._setup_for_discovery_streams(**test_input) == test_input
151 # Good case - no data router subscribers
152 test_input = {"streams_publishes": [{"name": "topic00", "type": "message_router"}],
153 'streams_subscribes': [{'type': 'message_router', 'name': 'topic01'}]}
154 test_input["name"] = "some-foo-service-component"
156 assert tasks._setup_for_discovery_streams(**test_input) == test_input
158 # Bad case - something happened from the Consul call
159 test_input = {'feed01': {'type': 'data_router', 'name': 'feed01',
160 'username': 'hero', 'password': '123456', 'location': 'Bedminster'},
161 'streams_publishes': {},
162 'streams_subscribes': [{'type': 'message_router', 'name': 'topic01'},
163 {'type': 'data_router', 'name': 'feed01', 'username': 'hero',
164 'password': '123456', 'location': 'Bedminster'}],
165 'topic01': {'type': 'message_router', 'name': 'topic01'}}
166 test_input["name"] = "some-foo-service-component"
168 def barf(conn, key, add_name, add_value):
169 raise RuntimeError("Barf")
171 monkeypatch.setattr(k8splugin.discovery, "add_to_entry",
174 with pytest.raises(NonRecoverableError):
175 tasks._setup_for_discovery_streams(**test_input)
178 def test_lookup_service(monkeypatch, mockconfig):
180 from k8splugin import tasks
181 def fake_lookup(conn, scn):
182 return [{"ServiceAddress": "192.168.1.1", "ServicePort": "80"}]
184 monkeypatch.setattr(k8splugin.discovery, "lookup_service",
187 assert "192.168.1.1" == tasks._lookup_service("some-component")
188 assert "192.168.1.1:80" == tasks._lookup_service("some-component",
192 def test_verify_container(monkeypatch, mockconfig):
193 from k8sclient import k8sclient
194 from k8splugin import tasks
195 from k8splugin.exceptions import DockerPluginDeploymentError
197 def fake_is_available_success(ch, scn):
200 monkeypatch.setattr(k8sclient, "is_available",
201 fake_is_available_success)
203 assert tasks._verify_container("some-name", 3)
205 def fake_is_available_never_good(ch, scn):
208 monkeypatch.setattr(k8sclient, "is_available",
209 fake_is_available_never_good)
211 with pytest.raises(DockerPluginDeploymentError):
212 tasks._verify_container("some-name", 2)
215 def test_update_delivery_url(monkeypatch, mockconfig):
217 from k8splugin import tasks
218 test_input = {'feed01': {'type': 'data_router', 'name': 'feed01',
219 'username': 'hero', 'password': '123456', 'location': 'Bedminster',
220 'route': 'some-path'},
221 'streams_publishes': {},
222 'streams_subscribes': [{'type': 'message_router', 'name': 'topic01'},
223 {'type': 'data_router', 'name': 'feed01', 'username': 'hero',
224 'password': '123456', 'location': 'Bedminster',
225 'route': 'some-path'}],
226 'topic01': {'type': 'message_router', 'name': 'topic01'}}
227 test_input["service_component_name"] = "some-foo-service-component"
229 def fake_lookup_service(name, with_port=False):
231 return "10.100.1.100:8080"
235 monkeypatch.setattr(k8splugin.tasks, "_lookup_service",
238 expected = copy.deepcopy(test_input)
239 expected["feed01"]["delivery_url"] = "http://10.100.1.100:8080/some-path"
241 assert tasks._update_delivery_url(**test_input) == expected
244 def test_enhance_docker_params(mockconfig):
245 from k8splugin import tasks
246 # Good - Test empty docker config
248 test_kwargs = { "docker_config": {}, "service_id": None }
249 actual = tasks._enhance_docker_params(**test_kwargs)
251 assert actual == {'envs': {"SERVICE_TAGS": ""}, 'docker_config': {}, "service_id": None }
253 # Good - Test just docker config ports and volumes
255 test_kwargs = { "docker_config": { "ports": ["1:1", "2:2"],
256 "volumes": [{"container": "somewhere", "host": "somewhere else"}] },
258 actual = tasks._enhance_docker_params(**test_kwargs)
260 assert actual == {'envs': {"SERVICE_TAGS": ""}, 'docker_config': {'ports': ['1:1', '2:2'],
261 'volumes': [{'host': 'somewhere else', 'container': 'somewhere'}]},
262 'ports': ['1:1', '2:2'], 'volumes': [{'host': 'somewhere else',
263 'container': 'somewhere'}], "service_id": None}
265 # Good - Test just docker config ports and volumes with overrrides
267 test_kwargs = { "docker_config": { "ports": ["1:1", "2:2"],
268 "volumes": [{"container": "somewhere", "host": "somewhere else"}] },
269 "ports": ["3:3", "4:4"], "volumes": [{"container": "nowhere", "host":
272 actual = tasks._enhance_docker_params(**test_kwargs)
274 assert actual == {'envs': {"SERVICE_TAGS": ""}, 'docker_config': {'ports': ['1:1', '2:2'],
275 'volumes': [{'host': 'somewhere else', 'container': 'somewhere'}]},
276 'ports': ['1:1', '2:2', '3:3', '4:4'], 'volumes': [{'host': 'somewhere else',
277 'container': 'somewhere'}, {'host': 'nowhere else', 'container':
278 'nowhere'}], "service_id": None}
282 test_kwargs = { "docker_config": {}, "service_id": "zed",
283 "deployment_id": "abc" }
284 actual = tasks._enhance_docker_params(**test_kwargs)
286 assert actual["envs"] == {"SERVICE_TAGS": "abc,zed"}
289 def test_notify_container(mockconfig):
290 from k8splugin import tasks
292 test_input = { "docker_config": { "trigger_type": "unknown" } }
293 assert test_input == tasks._notify_container(**test_input)