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 | |
| 35 | from sync_trustdomain import SyncTrustDomain |
| 36 | self.step_class = SyncTrustDomain |
| 37 | |
| 38 | self.service = OpenStackService() |
| 39 | |
| 40 | def tearDown(self): |
| 41 | sys.path = self.unittest_setup["sys_path_save"] |
| 42 | |
| 43 | def test_sync_record_create_noexist(self): |
| 44 | fakeconn = MagicMock() |
| 45 | with patch.object(self.step_class, "connect_openstack_admin") as fake_connect_openstack_admin: |
| 46 | fake_connect_openstack_admin.return_value = fakeconn |
| 47 | |
| 48 | xos_trust_domain = TrustDomain(name="test-trust", owner=self.service) |
| 49 | |
| 50 | step = self.step_class() |
| 51 | fakeconn.identity.find_domain.return_value = None |
| 52 | |
| 53 | os_domain = MagicMock() |
| 54 | os_domain.id = "1234" |
| 55 | fakeconn.identity.create_domain.return_value = os_domain |
| 56 | |
| 57 | step.sync_record(xos_trust_domain) |
| 58 | |
| 59 | fakeconn.identity.create_domain.assert_called_with(name=xos_trust_domain.name) |
| 60 | self.assertEqual(xos_trust_domain.backend_handle, "1234") |
| 61 | |
| 62 | def test_sync_record_create_exists(self): |
| 63 | fakeconn = MagicMock() |
| 64 | with patch.object(self.step_class, "connect_openstack_admin") as fake_connect_openstack_admin: |
| 65 | fake_connect_openstack_admin.return_value = fakeconn |
| 66 | |
| 67 | xos_trust_domain = TrustDomain(name="test-trust", owner=self.service) |
| 68 | |
| 69 | step = self.step_class() |
| 70 | os_domain = MagicMock() |
| 71 | os_domain.id = "1234" |
| 72 | fakeconn.identity.find_domain.return_value = os_domain |
| 73 | |
| 74 | fakeconn.identity.create_domain.return_value = None |
| 75 | |
| 76 | step.sync_record(xos_trust_domain) |
| 77 | |
| 78 | fakeconn.identity.create_domain.assert_not_called() |
| 79 | self.assertEqual(xos_trust_domain.backend_handle, "1234") |
| 80 | |
| 81 | def test_delete_record(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_trust_domain = TrustDomain(name="test-trust", owner=self.service) |
| 87 | |
| 88 | step = self.step_class() |
| 89 | os_domain = MagicMock() |
| 90 | os_domain.id = "1234" |
| 91 | os_domain.enabled = True |
| 92 | fakeconn.identity.find_domain.return_value = os_domain |
| 93 | os_domain2 = MagicMock() |
| 94 | os_domain2.id = "1234" |
| 95 | os_domain2.enabled = False |
| 96 | fakeconn.identity.update_domain.return_value = os_domain2 |
| 97 | fakeconn.identity.delete_domain.return_value = None |
| 98 | |
| 99 | step.delete_record(xos_trust_domain) |
| 100 | fakeconn.identity.update_domain.assert_called_with("1234", enabled=False) |
| 101 | fakeconn.identity.delete_domain.assert_called_with("1234") |
| 102 | |
| 103 | if __name__ == '__main__': |
| 104 | unittest.main() |