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 TestSyncTrustDomain(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_trustdomain import SyncTrustDomain |
| 38 | self.step_class = SyncTrustDomain |
| 39 | |
| 40 | self.service = OpenStackService() |
| 41 | |
| 42 | def tearDown(self): |
| 43 | sys.path = self.unittest_setup["sys_path_save"] |
| 44 | |
| 45 | def test_sync_record_create_noexist(self): |
| 46 | fakeconn = MagicMock() |
| 47 | with patch.object(self.step_class, "connect_openstack_admin") as fake_connect_openstack_admin: |
| 48 | fake_connect_openstack_admin.return_value = fakeconn |
| 49 | |
| 50 | xos_trust_domain = TrustDomain(name="test-trust", owner=self.service) |
| 51 | |
Scott Baker | c808c67 | 2019-02-04 11:38:20 -0800 | [diff] [blame] | 52 | step = self.step_class(model_accessor=self.model_accessor) |
Scott Baker | 62c7eaf | 2018-05-22 15:59:26 -0700 | [diff] [blame] | 53 | fakeconn.identity.find_domain.return_value = None |
| 54 | |
| 55 | os_domain = MagicMock() |
| 56 | os_domain.id = "1234" |
| 57 | fakeconn.identity.create_domain.return_value = os_domain |
| 58 | |
| 59 | step.sync_record(xos_trust_domain) |
| 60 | |
| 61 | fakeconn.identity.create_domain.assert_called_with(name=xos_trust_domain.name) |
| 62 | self.assertEqual(xos_trust_domain.backend_handle, "1234") |
| 63 | |
| 64 | def test_sync_record_create_exists(self): |
| 65 | fakeconn = MagicMock() |
| 66 | with patch.object(self.step_class, "connect_openstack_admin") as fake_connect_openstack_admin: |
| 67 | fake_connect_openstack_admin.return_value = fakeconn |
| 68 | |
| 69 | xos_trust_domain = TrustDomain(name="test-trust", owner=self.service) |
| 70 | |
Scott Baker | c808c67 | 2019-02-04 11:38:20 -0800 | [diff] [blame] | 71 | step = self.step_class(model_accessor=self.model_accessor) |
Scott Baker | 62c7eaf | 2018-05-22 15:59:26 -0700 | [diff] [blame] | 72 | os_domain = MagicMock() |
| 73 | os_domain.id = "1234" |
| 74 | fakeconn.identity.find_domain.return_value = os_domain |
| 75 | |
| 76 | fakeconn.identity.create_domain.return_value = None |
| 77 | |
| 78 | step.sync_record(xos_trust_domain) |
| 79 | |
| 80 | fakeconn.identity.create_domain.assert_not_called() |
| 81 | self.assertEqual(xos_trust_domain.backend_handle, "1234") |
| 82 | |
| 83 | def test_delete_record(self): |
| 84 | fakeconn = MagicMock() |
| 85 | with patch.object(self.step_class, "connect_openstack_admin") as fake_connect_openstack_admin: |
| 86 | fake_connect_openstack_admin.return_value = fakeconn |
| 87 | |
| 88 | xos_trust_domain = TrustDomain(name="test-trust", owner=self.service) |
| 89 | |
Scott Baker | c808c67 | 2019-02-04 11:38:20 -0800 | [diff] [blame] | 90 | step = self.step_class(model_accessor=self.model_accessor) |
Scott Baker | 62c7eaf | 2018-05-22 15:59:26 -0700 | [diff] [blame] | 91 | os_domain = MagicMock() |
| 92 | os_domain.id = "1234" |
| 93 | os_domain.enabled = True |
| 94 | fakeconn.identity.find_domain.return_value = os_domain |
| 95 | os_domain2 = MagicMock() |
| 96 | os_domain2.id = "1234" |
| 97 | os_domain2.enabled = False |
| 98 | fakeconn.identity.update_domain.return_value = os_domain2 |
| 99 | fakeconn.identity.delete_domain.return_value = None |
| 100 | |
| 101 | step.delete_record(xos_trust_domain) |
| 102 | fakeconn.identity.update_domain.assert_called_with("1234", enabled=False) |
| 103 | fakeconn.identity.delete_domain.assert_called_with("1234") |
| 104 | |
| 105 | if __name__ == '__main__': |
| 106 | unittest.main() |