blob: 2f1046d5b3471c1d366cc520c69cd9d5c2c17153 [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 TestSyncSlice(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_slice import SyncSlice
38 self.step_class = SyncSlice
39
40 self.service = OpenStackService()
41 self.site = Site(name="test-site")
42 self.trust_domain = TrustDomain(owner=self.service, name="test-trust")
43
44 def tearDown(self):
45 sys.path = self.unittest_setup["sys_path_save"]
46
47 def test_sync_record_create_noexist(self):
48 fakeconn = MagicMock()
49 with patch.object(self.step_class, "connect_openstack_admin") as fake_connect_openstack_admin:
50 fake_connect_openstack_admin.return_value = fakeconn
51
52 trust_domain_id = 5678
53
54 xos_slice = Slice(name="test-slice", trust_domain=self.trust_domain, site=self.site)
55
Scott Bakerc808c672019-02-04 11:38:20 -080056 step = self.step_class(model_accessor=self.model_accessor)
Scott Baker62c7eaf2018-05-22 15:59:26 -070057 fakeconn.identity.find_project.return_value = None
58 fakeconn.identity.find_domain.return_value = MagicMock(id=trust_domain_id)
59
60 os_slice = MagicMock()
61 os_slice.id = "1234"
62 fakeconn.identity.create_project.return_value = os_slice
63
64 step.sync_record(xos_slice)
65
66 fakeconn.identity.create_project.assert_called_with(name=xos_slice.name, domain_id=trust_domain_id)
67 self.assertEqual(xos_slice.backend_handle, "1234")
68
69 def test_sync_record_create_exists(self):
70 fakeconn = MagicMock()
71 with patch.object(self.step_class, "connect_openstack_admin") as fake_connect_openstack_admin:
72 fake_connect_openstack_admin.return_value = fakeconn
73
74 xos_slice = Slice(name="test-slice", trust_domain=self.trust_domain, site=self.site)
75
76 os_slice = MagicMock()
77 os_slice.id = "1234"
78
Scott Bakerc808c672019-02-04 11:38:20 -080079 step = self.step_class(model_accessor=self.model_accessor)
Scott Baker62c7eaf2018-05-22 15:59:26 -070080 fakeconn.identity.find_project.return_value = os_slice
81 fakeconn.identity.create_user.return_value = None
82
83 step.sync_record(xos_slice)
84
85 fakeconn.identity.create_slice.assert_not_called()
86 self.assertEqual(xos_slice.backend_handle, "1234")
87
88 def test_delete_record(self):
89 fakeconn = MagicMock()
90 with patch.object(self.step_class, "connect_openstack_admin") as fake_connect_openstack_admin:
91 fake_connect_openstack_admin.return_value = fakeconn
92
93 xos_slice = Slice(name="test-slice", trust_domain=self.trust_domain, site=self.site)
94
Scott Bakerc808c672019-02-04 11:38:20 -080095 step = self.step_class(model_accessor=self.model_accessor)
Scott Baker62c7eaf2018-05-22 15:59:26 -070096 os_slice = MagicMock()
97 os_slice.id = "1234"
98 fakeconn.identity.find_project.return_value = os_slice
99 fakeconn.identity.delete_project.return_value = None
100
101 step.delete_record(xos_slice)
102 fakeconn.identity.delete_project.assert_called_with("1234")
103
104if __name__ == '__main__':
105 unittest.main()