blob: d4fb9b597936930970ff7a282b375174d86b0bab [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
16import json
17import 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
35 from sync_openstackserviceinstance import SyncOpenStackServiceInstance
36 self.step_class = SyncOpenStackServiceInstance
37
38 self.service = OpenStackService()
39 self.site = Site(name="test-site")
40 self.trust_domain = TrustDomain(owner=self.service, name="test-trust")
41 self.flavor = Flavor(name="test-flavor", backend_handle=1114)
42 self.node = Node(name="test-node", backend_handle=1113)
43 self.slice = Slice(name="test-slice", trust_domain=self.trust_domain, backend_handle=1112)
44 self.image = Image(name="test-image", backend_handle=1111)
45
46 def tearDown(self):
47 sys.path = self.unittest_setup["sys_path_save"]
48
49 def test_sync_record_create_noexist(self):
50 fakeconn = MagicMock()
51 with patch.object(self.step_class, "connect_openstack_admin") as fake_connect_openstack_admin:
52 fake_connect_openstack_admin.return_value = fakeconn
53
54 xos_instance = OpenStackServiceInstance(name="test-instance", slice=self.slice, image=self.image,
55 node=self.node, flavor=self.flavor)
56
57 step = self.step_class()
58 fakeconn.compute.servers.return_value = []
59 fakeconn.identity.find_project.return_value = MagicMock(id=self.slice.backend_handle)
60 fakeconn.identity.find_domain.return_value = MagicMock(id=self.trust_domain.backend_handle)
61 fakeconn.compute.find_image.return_value = MagicMock(id=self.image.backend_handle)
62 fakeconn.compute.find_flavor.return_value = MagicMock(id=self.flavor.backend_handle)
63
64 os_instance = MagicMock()
65 os_instance.id = "1234"
66 fakeconn.compute.create_server.return_value = os_instance
67
68 step.sync_record(xos_instance)
69
70 fakeconn.compute.create_server.assert_called_with(admin_password=ANY,
71 availability_zone="nova:test-node",
72 config_drive=True,
73 flavor_id=self.flavor.backend_handle,
74 image_id=self.image.backend_handle,
75 name=xos_instance.name,
76 networks=[],
77 project_domain_id=self.slice.backend_handle,
78 user_data=ANY)
79 self.assertEqual(xos_instance.backend_handle, "1234")
80
Scott Baker50a56112018-06-15 15:48:54 -070081 def test_sync_record_create_noexist_noflavor(self):
82 """ Create a ServiceInstance with no flavor. It should automatically default to m1.small """
83 fakeconn = MagicMock()
84 with patch.object(self.step_class, "connect_openstack_admin") as fake_connect_openstack_admin:
85 fake_connect_openstack_admin.return_value = fakeconn
86
87 xos_instance = OpenStackServiceInstance(name="test-instance", slice=self.slice, image=self.image,
88 node=self.node)
89
90 step = self.step_class()
91 fakeconn.compute.servers.return_value = []
92 fakeconn.identity.find_project.return_value = MagicMock(id=self.slice.backend_handle)
93 fakeconn.identity.find_domain.return_value = MagicMock(id=self.trust_domain.backend_handle)
94 fakeconn.compute.find_image.return_value = MagicMock(id=self.image.backend_handle)
95 fakeconn.compute.find_flavor.return_value = MagicMock(id=self.flavor.backend_handle)
96
97 os_instance = MagicMock()
98 os_instance.id = "1234"
99 fakeconn.compute.create_server.return_value = os_instance
100
101 step.sync_record(xos_instance)
102
103 fakeconn.compute.create_server.assert_called_with(admin_password=ANY,
104 availability_zone="nova:test-node",
105 config_drive=True,
106 flavor_id=self.flavor.backend_handle,
107 image_id=self.image.backend_handle,
108 name=xos_instance.name,
109 networks=[],
110 project_domain_id=self.slice.backend_handle,
111 user_data=ANY)
112 self.assertEqual(xos_instance.backend_handle, "1234")
113
114 def test_sync_record_create_noexist_nohost(self):
115 """ create a ServiceInstance with no node. It should be assigned to the "nova" availability zone
116 """
117
118 fakeconn = MagicMock()
119 with patch.object(self.step_class, "connect_openstack_admin") as fake_connect_openstack_admin:
120 fake_connect_openstack_admin.return_value = fakeconn
121
122 xos_instance = OpenStackServiceInstance(name="test-instance", slice=self.slice, image=self.image,
123 flavor=self.flavor)
124
125 step = self.step_class()
126 fakeconn.compute.servers.return_value = []
127 fakeconn.identity.find_project.return_value = MagicMock(id=self.slice.backend_handle)
128 fakeconn.identity.find_domain.return_value = MagicMock(id=self.trust_domain.backend_handle)
129 fakeconn.compute.find_image.return_value = MagicMock(id=self.image.backend_handle)
130 fakeconn.compute.find_flavor.return_value = MagicMock(id=self.flavor.backend_handle)
131
132 os_instance = MagicMock()
133 os_instance.id = "1234"
134 fakeconn.compute.create_server.return_value = os_instance
135
136 step.sync_record(xos_instance)
137
138 fakeconn.compute.create_server.assert_called_with(admin_password=ANY,
139 availability_zone="nova",
140 config_drive=True,
141 flavor_id=self.flavor.backend_handle,
142 image_id=self.image.backend_handle,
143 name=xos_instance.name,
144 networks=[],
145 project_domain_id=self.slice.backend_handle,
146 user_data=ANY)
147 self.assertEqual(xos_instance.backend_handle, "1234")
148
Scott Baker62c7eaf2018-05-22 15:59:26 -0700149 def test_sync_record_create_exists(self):
150 fakeconn = MagicMock()
151 with patch.object(self.step_class, "connect_openstack_admin") as fake_connect_openstack_admin:
152 fake_connect_openstack_admin.return_value = fakeconn
153
154 xos_instance = OpenStackServiceInstance(name="test-instance", slice=self.slice, image=self.image,
155 node=self.node, flavor=self.flavor)
156
157 os_instance = MagicMock()
158 os_instance.id = "1234"
159
160 step = self.step_class()
161 fakeconn.identity.find_project.return_value = os_instance
162 fakeconn.compute.create_server.return_value = None
163 fakeconn.compute.servers.return_value = [os_instance]
164
165 step.sync_record(xos_instance)
166
167 fakeconn.compute.create_server.assert_not_called()
168 self.assertEqual(xos_instance.backend_handle, "1234")
169
170 def test_delete_record(self):
171 fakeconn = MagicMock()
172 with patch.object(self.step_class, "connect_openstack_admin") as fake_connect_openstack_admin:
173 fake_connect_openstack_admin.return_value = fakeconn
174
175 xos_instance = OpenStackServiceInstance(name="test-instance", slice=self.slice, image=self.image,
176 node=self.node, flavor=self.flavor)
177
178 step = self.step_class()
179 os_instance = MagicMock()
180 os_instance.id = "1234"
181 fakeconn.compute.servers.return_value = [os_instance]
182 fakeconn.compute.delete_server.return_value = None
183
184 step.delete_record(xos_instance)
185 fakeconn.compute.delete_server.assert_called_with("1234")
186
187if __name__ == '__main__':
188 unittest.main()