1 # ============LICENSE_START=======================================================
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
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=========================================================
22 from cloudify.exceptions import NonRecoverableError, RecoverableError
25 def test_generate_component_name(mockconfig):
26 from k8splugin import tasks
27 kwargs = { "service_component_type": "doodle",
28 "service_component_name_override": None }
30 assert "doodle" in tasks._generate_component_name(**kwargs)["name"]
32 kwargs["service_component_name_override"] = "yankee"
34 assert "yankee" == tasks._generate_component_name(**kwargs)["name"]
37 def test_parse_streams(monkeypatch, mockconfig):
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": {} }
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'}
52 assert expected == tasks._parse_streams(**test_input)
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"}] }
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'}}
70 assert expected == tasks._parse_streams(**test_input)
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"}] }
82 monkeypatch.setattr(k8splugin.utils, "random_string", not_so_random)
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'}}
93 assert expected == tasks._parse_streams(**test_input)
96 def test_setup_for_discovery(monkeypatch, mockconfig):
98 from k8splugin import tasks
100 test_input = { "name": "some-name",
101 "application_config": { "one": "a", "two": "b" } }
103 def fake_push_config(conn, name, application_config):
106 monkeypatch.setattr(k8splugin.discovery, "push_service_component_config",
109 assert test_input == tasks._setup_for_discovery(**test_input)
111 def fake_push_config_connection_error(conn, name, application_config):
112 raise k8splugin.discovery.DiscoveryConnectionError("Boom")
114 monkeypatch.setattr(k8splugin.discovery, "push_service_component_config",
115 fake_push_config_connection_error)
117 with pytest.raises(RecoverableError):
118 tasks._setup_for_discovery(**test_input)
120 def test_verify_container(monkeypatch, mockconfig):
122 from k8splugin import tasks
123 from k8splugin.exceptions import DockerPluginDeploymentError
125 def fake_is_available_success(loc, ch, scn):
128 monkeypatch.setattr(k8sclient, "is_available",
129 fake_is_available_success)
131 assert tasks._verify_k8s_deployment("some-location","some-name", 3)
133 def fake_is_available_never_good(loc, ch, scn):
136 monkeypatch.setattr(k8sclient, "is_available",
137 fake_is_available_never_good)
139 assert not tasks._verify_k8s_deployment("some-location", "some-name", 2)
141 def test_enhance_docker_params(mockconfig):
142 from k8splugin import tasks
143 # Good - Test empty docker config
145 test_kwargs = { "docker_config": {}, "service_id": None }
146 actual = tasks._enhance_docker_params(**test_kwargs)
148 assert actual == {'envs': {}, 'docker_config': {}, 'ports': [], 'volumes': [], "service_id": None }
150 # Good - Test just docker config ports and volumes
152 test_kwargs = { "docker_config": { "ports": [{u'concat': [u'8080', u':', 0], u'ipv6': True}, "1:1"],
153 "volumes": [{"container": "somewhere", "host": "somewhere else"}] },
155 actual = tasks._enhance_docker_params(**test_kwargs)
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}
162 # Good - Test just docker config ports and volumes with overrrides
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":
169 actual = tasks._enhance_docker_params(**test_kwargs)
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}
179 test_kwargs = { "docker_config": {}, "service_id": "zed",
180 "deployment_id": "abc" }
181 actual = tasks._enhance_docker_params(**test_kwargs)
183 assert actual["envs"] == {}
186 def test_notify_container(mockconfig):
187 from k8splugin import tasks
189 test_input = { "docker_config": { "policy": { "trigger_type": "unknown" } } }
190 assert [] == tasks._notify_container(**test_input)