blob: c39bd0b6cac79c472984d902bea5407bed6c12bb [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
81 def test_sync_record_create_exists(self):
82 fakeconn = MagicMock()
83 with patch.object(self.step_class, "connect_openstack_admin") as fake_connect_openstack_admin:
84 fake_connect_openstack_admin.return_value = fakeconn
85
86 xos_instance = OpenStackServiceInstance(name="test-instance", slice=self.slice, image=self.image,
87 node=self.node, flavor=self.flavor)
88
89 os_instance = MagicMock()
90 os_instance.id = "1234"
91
92 step = self.step_class()
93 fakeconn.identity.find_project.return_value = os_instance
94 fakeconn.compute.create_server.return_value = None
95 fakeconn.compute.servers.return_value = [os_instance]
96
97 step.sync_record(xos_instance)
98
99 fakeconn.compute.create_server.assert_not_called()
100 self.assertEqual(xos_instance.backend_handle, "1234")
101
102 def test_delete_record(self):
103 fakeconn = MagicMock()
104 with patch.object(self.step_class, "connect_openstack_admin") as fake_connect_openstack_admin:
105 fake_connect_openstack_admin.return_value = fakeconn
106
107 xos_instance = OpenStackServiceInstance(name="test-instance", slice=self.slice, image=self.image,
108 node=self.node, flavor=self.flavor)
109
110 step = self.step_class()
111 os_instance = MagicMock()
112 os_instance.id = "1234"
113 fakeconn.compute.servers.return_value = [os_instance]
114 fakeconn.compute.delete_server.return_value = None
115
116 step.delete_record(xos_instance)
117 fakeconn.compute.delete_server.assert_called_with("1234")
118
119if __name__ == '__main__':
120 unittest.main()