1 # ============LICENSE_START=======================================================
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
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 = { "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"}] }
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'}}
71 assert expected == tasks._parse_streams(**test_input)
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"}] }
83 monkeypatch.setattr(k8splugin.utils, "random_string", not_so_random)
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'}}
94 assert expected == tasks._parse_streams(**test_input)
97 def test_setup_for_discovery(monkeypatch, mockconfig):
99 from k8splugin import tasks
101 test_input = { "name": "some-name",
102 "application_config": { "one": "a", "two": "b" } }
104 def fake_push_config(conn, name, application_config):
107 monkeypatch.setattr(k8splugin.discovery, "push_service_component_config",
110 assert test_input == tasks._setup_for_discovery(**test_input)
112 def fake_push_config_connection_error(conn, name, application_config):
113 raise k8splugin.discovery.DiscoveryConnectionError("Boom")
115 monkeypatch.setattr(k8splugin.discovery, "push_service_component_config",
116 fake_push_config_connection_error)
118 with pytest.raises(RecoverableError):
119 tasks._setup_for_discovery(**test_input)
121 def test_verify_container(monkeypatch, mockconfig):
123 from k8splugin import tasks
124 from k8splugin.exceptions import DockerPluginDeploymentError
126 def fake_is_available_success(loc, ch, scn):
129 monkeypatch.setattr(k8sclient, "is_available",
130 fake_is_available_success)
132 assert tasks._verify_k8s_deployment("some-location","some-name", 3)
134 def fake_is_available_never_good(loc, ch, scn):
137 monkeypatch.setattr(k8sclient, "is_available",
138 fake_is_available_never_good)
140 assert not tasks._verify_k8s_deployment("some-location", "some-name", 2)
142 def test_enhance_docker_params(mockconfig):
143 from k8splugin import tasks
144 # Good - Test empty docker config
146 test_kwargs = { "docker_config": {}, "service_id": None }
147 actual = tasks._enhance_docker_params(**test_kwargs)
149 assert actual == {'envs': {}, 'docker_config': {}, 'ports': [], 'volumes': [], "service_id": None }
151 # Good - Test just docker config ports and volumes
153 test_kwargs = { "docker_config": { "ports": ["1:1", "2:2"],
154 "volumes": [{"container": "somewhere", "host": "somewhere else"}] },
156 actual = tasks._enhance_docker_params(**test_kwargs)
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}
163 # Good - Test just docker config ports and volumes with overrrides
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":
170 actual = tasks._enhance_docker_params(**test_kwargs)
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}
180 test_kwargs = { "docker_config": {}, "service_id": "zed",
181 "deployment_id": "abc" }
182 actual = tasks._enhance_docker_params(**test_kwargs)
184 assert actual["envs"] == {}
187 def test_notify_container(mockconfig):
188 from k8splugin import tasks
190 test_input = { "docker_config": { "policy": { "trigger_type": "unknown" } } }
191 assert [] == tasks._notify_container(**test_input)