blob: 101283f7289dab8547545ad0e3f2d046123f66ec [file] [log] [blame]
Scott Baker62c7eaf2018-05-22 15:59:26 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
Scott Baker004e1f12018-06-26 16:47:35 -070016import base64
Scott Baker62c7eaf2018-05-22 15:59:26 -070017import os
18import sys
19import unittest
20from mock import patch, PropertyMock, ANY, MagicMock
21from unit_test_common import setup_sync_unit_test
22
23def fake_connect_openstack_admin(self, service, required_version=None):
24 return MagicMock()
25
26class TestSyncOpenStackServiceInstance(unittest.TestCase):
27
28 def setUp(self):
29 self.unittest_setup = setup_sync_unit_test(os.path.abspath(os.path.dirname(os.path.realpath(__file__))),
30 globals(),
31 [("openstack", "openstack.xproto")] )
32
33 sys.path.append(os.path.join(os.path.abspath(os.path.dirname(os.path.realpath(__file__))), "../steps"))
34
Scott Bakerc808c672019-02-04 11:38:20 -080035 self.model_accessor = self.unittest_setup["model_accessor"]
36
Scott Baker62c7eaf2018-05-22 15:59:26 -070037 from sync_openstackserviceinstance import SyncOpenStackServiceInstance
38 self.step_class = SyncOpenStackServiceInstance
39
40 self.service = OpenStackService()
41 self.site = Site(name="test-site")
42 self.trust_domain = TrustDomain(owner=self.service, name="test-trust")
43 self.flavor = Flavor(name="test-flavor", backend_handle=1114)
44 self.node = Node(name="test-node", backend_handle=1113)
45 self.slice = Slice(name="test-slice", trust_domain=self.trust_domain, backend_handle=1112)
46 self.image = Image(name="test-image", backend_handle=1111)
47
48 def tearDown(self):
49 sys.path = self.unittest_setup["sys_path_save"]
50
51 def test_sync_record_create_noexist(self):
52 fakeconn = MagicMock()
53 with patch.object(self.step_class, "connect_openstack_admin") as fake_connect_openstack_admin:
54 fake_connect_openstack_admin.return_value = fakeconn
55
56 xos_instance = OpenStackServiceInstance(name="test-instance", slice=self.slice, image=self.image,
57 node=self.node, flavor=self.flavor)
58
Scott Bakerc808c672019-02-04 11:38:20 -080059 step = self.step_class(model_accessor=self.model_accessor)
Scott Baker62c7eaf2018-05-22 15:59:26 -070060 fakeconn.compute.servers.return_value = []
61 fakeconn.identity.find_project.return_value = MagicMock(id=self.slice.backend_handle)
62 fakeconn.identity.find_domain.return_value = MagicMock(id=self.trust_domain.backend_handle)
63 fakeconn.compute.find_image.return_value = MagicMock(id=self.image.backend_handle)
64 fakeconn.compute.find_flavor.return_value = MagicMock(id=self.flavor.backend_handle)
65
66 os_instance = MagicMock()
67 os_instance.id = "1234"
68 fakeconn.compute.create_server.return_value = os_instance
69
70 step.sync_record(xos_instance)
71
72 fakeconn.compute.create_server.assert_called_with(admin_password=ANY,
73 availability_zone="nova:test-node",
74 config_drive=True,
75 flavor_id=self.flavor.backend_handle,
76 image_id=self.image.backend_handle,
77 name=xos_instance.name,
78 networks=[],
79 project_domain_id=self.slice.backend_handle,
80 user_data=ANY)
81 self.assertEqual(xos_instance.backend_handle, "1234")
82
Scott Baker50a56112018-06-15 15:48:54 -070083 def test_sync_record_create_noexist_noflavor(self):
84 """ Create a ServiceInstance with no flavor. It should automatically default to m1.small """
85 fakeconn = MagicMock()
86 with patch.object(self.step_class, "connect_openstack_admin") as fake_connect_openstack_admin:
87 fake_connect_openstack_admin.return_value = fakeconn
88
89 xos_instance = OpenStackServiceInstance(name="test-instance", slice=self.slice, image=self.image,
90 node=self.node)
91
Scott Bakerc808c672019-02-04 11:38:20 -080092 step = self.step_class(model_accessor=self.model_accessor)
Scott Baker50a56112018-06-15 15:48:54 -070093 fakeconn.compute.servers.return_value = []
94 fakeconn.identity.find_project.return_value = MagicMock(id=self.slice.backend_handle)
95 fakeconn.identity.find_domain.return_value = MagicMock(id=self.trust_domain.backend_handle)
96 fakeconn.compute.find_image.return_value = MagicMock(id=self.image.backend_handle)
97 fakeconn.compute.find_flavor.return_value = MagicMock(id=self.flavor.backend_handle)
98
99 os_instance = MagicMock()
100 os_instance.id = "1234"
101 fakeconn.compute.create_server.return_value = os_instance
102
103 step.sync_record(xos_instance)
104
105 fakeconn.compute.create_server.assert_called_with(admin_password=ANY,
106 availability_zone="nova:test-node",
107 config_drive=True,
108 flavor_id=self.flavor.backend_handle,
109 image_id=self.image.backend_handle,
110 name=xos_instance.name,
111 networks=[],
112 project_domain_id=self.slice.backend_handle,
113 user_data=ANY)
114 self.assertEqual(xos_instance.backend_handle, "1234")
115
116 def test_sync_record_create_noexist_nohost(self):
117 """ create a ServiceInstance with no node. It should be assigned to the "nova" availability zone
118 """
119
120 fakeconn = MagicMock()
121 with patch.object(self.step_class, "connect_openstack_admin") as fake_connect_openstack_admin:
122 fake_connect_openstack_admin.return_value = fakeconn
123
124 xos_instance = OpenStackServiceInstance(name="test-instance", slice=self.slice, image=self.image,
125 flavor=self.flavor)
126
Scott Bakerc808c672019-02-04 11:38:20 -0800127 step = self.step_class(model_accessor=self.model_accessor)
Scott Baker50a56112018-06-15 15:48:54 -0700128 fakeconn.compute.servers.return_value = []
129 fakeconn.identity.find_project.return_value = MagicMock(id=self.slice.backend_handle)
130 fakeconn.identity.find_domain.return_value = MagicMock(id=self.trust_domain.backend_handle)
131 fakeconn.compute.find_image.return_value = MagicMock(id=self.image.backend_handle)
132 fakeconn.compute.find_flavor.return_value = MagicMock(id=self.flavor.backend_handle)
133
134 os_instance = MagicMock()
135 os_instance.id = "1234"
136 fakeconn.compute.create_server.return_value = os_instance
137
138 step.sync_record(xos_instance)
139
140 fakeconn.compute.create_server.assert_called_with(admin_password=ANY,
141 availability_zone="nova",
142 config_drive=True,
143 flavor_id=self.flavor.backend_handle,
144 image_id=self.image.backend_handle,
145 name=xos_instance.name,
146 networks=[],
147 project_domain_id=self.slice.backend_handle,
148 user_data=ANY)
149 self.assertEqual(xos_instance.backend_handle, "1234")
150
Scott Baker004e1f12018-06-26 16:47:35 -0700151 def test_sync_record_create_noexist_with_keys(self):
152 fakeconn = MagicMock()
153 with patch.object(self.step_class, "connect_openstack_admin") as fake_connect_openstack_admin:
154 fake_connect_openstack_admin.return_value = fakeconn
155
156 xos_instance = OpenStackServiceInstance(name="test-instance", slice=self.slice, image=self.image,
157 node=self.node, flavor=self.flavor)
158
159 admin_user = User(email="test_user@test.com", public_key="key1")
160 self.slice.creator = admin_user
161
162 owning_service = Service(name="test_service", public_key="key2")
163 self.slice.service = owning_service
164
Scott Bakerc808c672019-02-04 11:38:20 -0800165 step = self.step_class(model_accessor=self.model_accessor)
Scott Baker004e1f12018-06-26 16:47:35 -0700166 fakeconn.compute.servers.return_value = []
167 fakeconn.identity.find_project.return_value = MagicMock(id=self.slice.backend_handle)
168 fakeconn.identity.find_domain.return_value = MagicMock(id=self.trust_domain.backend_handle)
169 fakeconn.compute.find_image.return_value = MagicMock(id=self.image.backend_handle)
170 fakeconn.compute.find_flavor.return_value = MagicMock(id=self.flavor.backend_handle)
171
172 os_instance = MagicMock()
173 os_instance.id = "1234"
174 fakeconn.compute.create_server.return_value = os_instance
175
176 step.sync_record(xos_instance)
177
178 expected_userdata = base64.b64encode('#cloud-config\n\nssh_authorized_keys:\n - key1\n - key2\n')
179
180 fakeconn.compute.create_server.assert_called_with(admin_password=ANY,
181 availability_zone="nova:test-node",
182 config_drive=True,
183 flavor_id=self.flavor.backend_handle,
184 image_id=self.image.backend_handle,
185 name=xos_instance.name,
186 networks=[],
187 project_domain_id=self.slice.backend_handle,
188 user_data=expected_userdata)
189 self.assertEqual(xos_instance.backend_handle, "1234")
190
Scott Baker62c7eaf2018-05-22 15:59:26 -0700191 def test_sync_record_create_exists(self):
192 fakeconn = MagicMock()
193 with patch.object(self.step_class, "connect_openstack_admin") as fake_connect_openstack_admin:
194 fake_connect_openstack_admin.return_value = fakeconn
195
196 xos_instance = OpenStackServiceInstance(name="test-instance", slice=self.slice, image=self.image,
197 node=self.node, flavor=self.flavor)
198
199 os_instance = MagicMock()
200 os_instance.id = "1234"
201
Scott Bakerc808c672019-02-04 11:38:20 -0800202 step = self.step_class(model_accessor=self.model_accessor)
Scott Baker62c7eaf2018-05-22 15:59:26 -0700203 fakeconn.identity.find_project.return_value = os_instance
204 fakeconn.compute.create_server.return_value = None
205 fakeconn.compute.servers.return_value = [os_instance]
206
207 step.sync_record(xos_instance)
208
209 fakeconn.compute.create_server.assert_not_called()
210 self.assertEqual(xos_instance.backend_handle, "1234")
211
212 def test_delete_record(self):
213 fakeconn = MagicMock()
214 with patch.object(self.step_class, "connect_openstack_admin") as fake_connect_openstack_admin:
215 fake_connect_openstack_admin.return_value = fakeconn
216
217 xos_instance = OpenStackServiceInstance(name="test-instance", slice=self.slice, image=self.image,
218 node=self.node, flavor=self.flavor)
219
Scott Bakerc808c672019-02-04 11:38:20 -0800220 step = self.step_class(model_accessor=self.model_accessor)
Scott Baker62c7eaf2018-05-22 15:59:26 -0700221 os_instance = MagicMock()
222 os_instance.id = "1234"
223 fakeconn.compute.servers.return_value = [os_instance]
224 fakeconn.compute.delete_server.return_value = None
225
226 step.delete_record(xos_instance)
227 fakeconn.compute.delete_server.assert_called_with("1234")
228
229if __name__ == '__main__':
230 unittest.main()