Scott Baker | 62c7eaf | 2018-05-22 15:59:26 -0700 | [diff] [blame] | 1 | |
| 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 | |
| 16 | import json |
| 17 | import os |
| 18 | import sys |
| 19 | import unittest |
| 20 | from mock import patch, PropertyMock, ANY, MagicMock |
| 21 | from unit_test_common import setup_sync_unit_test |
| 22 | |
| 23 | def fake_connect_openstack_admin(self, service, required_version=None): |
| 24 | return MagicMock() |
| 25 | |
| 26 | class 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 Baker | c808c67 | 2019-02-04 11:38:20 -0800 | [diff] [blame] | 35 | self.model_accessor = self.unittest_setup["model_accessor"] |
| 36 | |
Scott Baker | 62c7eaf | 2018-05-22 15:59:26 -0700 | [diff] [blame] | 37 | 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 Baker | c808c67 | 2019-02-04 11:38:20 -0800 | [diff] [blame] | 56 | step = self.step_class(model_accessor=self.model_accessor) |
Scott Baker | 62c7eaf | 2018-05-22 15:59:26 -0700 | [diff] [blame] | 57 | 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 Baker | c808c67 | 2019-02-04 11:38:20 -0800 | [diff] [blame] | 79 | step = self.step_class(model_accessor=self.model_accessor) |
Scott Baker | 62c7eaf | 2018-05-22 15:59:26 -0700 | [diff] [blame] | 80 | 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 Baker | c808c67 | 2019-02-04 11:38:20 -0800 | [diff] [blame] | 95 | step = self.step_class(model_accessor=self.model_accessor) |
Scott Baker | 62c7eaf | 2018-05-22 15:59:26 -0700 | [diff] [blame] | 96 | 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 | |
| 104 | if __name__ == '__main__': |
| 105 | unittest.main() |