blob: 384641fb4d069b5df365b02aff4ebfddc2b5c3c9 [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
35 from sync_slice import SyncSlice
36 self.step_class = SyncSlice
37
38 self.service = OpenStackService()
39 self.site = Site(name="test-site")
40 self.trust_domain = TrustDomain(owner=self.service, name="test-trust")
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 trust_domain_id = 5678
51
52 xos_slice = Slice(name="test-slice", trust_domain=self.trust_domain, site=self.site)
53
54 step = self.step_class()
55 fakeconn.identity.find_project.return_value = None
56 fakeconn.identity.find_domain.return_value = MagicMock(id=trust_domain_id)
57
58 os_slice = MagicMock()
59 os_slice.id = "1234"
60 fakeconn.identity.create_project.return_value = os_slice
61
62 step.sync_record(xos_slice)
63
64 fakeconn.identity.create_project.assert_called_with(name=xos_slice.name, domain_id=trust_domain_id)
65 self.assertEqual(xos_slice.backend_handle, "1234")
66
67 def test_sync_record_create_exists(self):
68 fakeconn = MagicMock()
69 with patch.object(self.step_class, "connect_openstack_admin") as fake_connect_openstack_admin:
70 fake_connect_openstack_admin.return_value = fakeconn
71
72 xos_slice = Slice(name="test-slice", trust_domain=self.trust_domain, site=self.site)
73
74 os_slice = MagicMock()
75 os_slice.id = "1234"
76
77 step = self.step_class()
78 fakeconn.identity.find_project.return_value = os_slice
79 fakeconn.identity.create_user.return_value = None
80
81 step.sync_record(xos_slice)
82
83 fakeconn.identity.create_slice.assert_not_called()
84 self.assertEqual(xos_slice.backend_handle, "1234")
85
86 def test_delete_record(self):
87 fakeconn = MagicMock()
88 with patch.object(self.step_class, "connect_openstack_admin") as fake_connect_openstack_admin:
89 fake_connect_openstack_admin.return_value = fakeconn
90
91 xos_slice = Slice(name="test-slice", trust_domain=self.trust_domain, site=self.site)
92
93 step = self.step_class()
94 os_slice = MagicMock()
95 os_slice.id = "1234"
96 fakeconn.identity.find_project.return_value = os_slice
97 fakeconn.identity.delete_project.return_value = None
98
99 step.delete_record(xos_slice)
100 fakeconn.identity.delete_project.assert_called_with("1234")
101
102if __name__ == '__main__':
103 unittest.main()