blob: bdc98f92313c11fdad2b70c21907fc59920d82fe [file] [log] [blame]
Scott Baker13e953c2018-05-17 09:19:15 -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
23class ApiException(Exception):
24 def __init__(self, status, *args, **kwargs):
25 super(ApiException, self).__init__(*args, **kwargs)
26 self.status = status
27
28def fake_init_kubernetes_client(self):
29 self.kubernetes_client = MagicMock()
30 self.v1core = MagicMock()
31 self.ApiException = ApiException
32
33class TestSyncConfigmap(unittest.TestCase):
34
35 def setUp(self):
36 self.unittest_setup = setup_sync_unit_test(os.path.abspath(os.path.dirname(os.path.realpath(__file__))),
37 globals(),
Scott Bakera30fae72019-02-01 16:14:43 -080038 [("kubernetes-service", "kubernetes.xproto")] )
39
40 self.model_accessor = self.unittest_setup["model_accessor"]
Scott Baker13e953c2018-05-17 09:19:15 -070041
42 sys.path.append(os.path.join(os.path.abspath(os.path.dirname(os.path.realpath(__file__))), "../steps"))
43
44 from sync_configmap import SyncKubernetesConfigMap
45 self.step_class = SyncKubernetesConfigMap
46
47 self.service = KubernetesService()
48 self.trust_domain = TrustDomain(name="test-trust", owner=self.service)
49
50 def tearDown(self):
51 sys.path = self.unittest_setup["sys_path_save"]
52
53 def test_get_config_map_exists(self):
54 with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client):
55 configmap = KubernetesConfigMap(trust_domain=self.trust_domain, name="test-configmap")
56
Scott Bakera30fae72019-02-01 16:14:43 -080057 step = self.step_class(model_accessor = self.model_accessor)
Scott Baker13e953c2018-05-17 09:19:15 -070058 map = MagicMock()
59 step.v1core.read_namespaced_config_map.return_value = map
60
61 result = step.get_config_map(configmap)
62
63 self.assertEqual(result, map)
64 step.v1core.read_namespaced_config_map.assert_called_with("test-configmap", "test-trust")
65
66 def test_get_config_map_noexist(self):
67 with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client):
68 configmap = KubernetesConfigMap(trust_domain=self.trust_domain, name="test-configmap")
69
Scott Bakera30fae72019-02-01 16:14:43 -080070 step = self.step_class(model_accessor = self.model_accessor)
Scott Baker13e953c2018-05-17 09:19:15 -070071 map = MagicMock()
72 step.v1core.read_namespaced_config_map.side_effect = step.ApiException(status=404)
73
74 result = step.get_config_map(configmap)
75
76 self.assertEqual(result, None)
77
78 def test_sync_record_create(self):
79 with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client):
80 data = {"foo": "bar"}
81 configmap = KubernetesConfigMap(trust_domain=self.trust_domain, name="test-configmap", data=json.dumps(data))
82
Scott Bakera30fae72019-02-01 16:14:43 -080083 step = self.step_class(model_accessor = self.model_accessor)
Scott Baker13e953c2018-05-17 09:19:15 -070084 step.v1core.read_namespaced_config_map.side_effect = step.ApiException(status=404)
85
86 map = MagicMock()
87 map.metadata.self_link="1234"
88 step.v1core.create_namespaced_config_map.return_value = map
89
90 step.sync_record(configmap)
91
92 step.v1core.create_namespaced_config_map.assert_called()
93 self.assertEqual(configmap.backend_handle, "1234")
94
95 def test_sync_record_update(self):
96 with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client):
97 data = {"foo": "bar"}
98 configmap = KubernetesConfigMap(trust_domain=self.trust_domain, name="test-configmap", data=json.dumps(data))
99
100 orig_map = MagicMock()
101 orig_map.data = {"foo": "not_bar"}
102 orig_map.metadata.self_link = "1234"
103
Scott Bakera30fae72019-02-01 16:14:43 -0800104 step = self.step_class(model_accessor = self.model_accessor)
Scott Baker13e953c2018-05-17 09:19:15 -0700105 step.v1core.read_namespaced_config_map.return_value = orig_map
106
107 new_map = MagicMock()
108 new_map.data = {"foo": "bar"}
109 new_map.metadata.self_link = "1234"
110
111 step.v1core.patch_namespaced_config_map.return_value = new_map
112
113 step.sync_record(configmap)
114
115 self.assertEqual(step.v1core.patch_namespaced_config_map.call_count, 1)
116 call_args = step.v1core.patch_namespaced_config_map.call_args[0]
117 self.assertEqual(call_args[0], "test-configmap")
118 self.assertEqual(call_args[1], "test-trust")
119 self.assertEqual(call_args[2].data, {"foo": "bar"})
120
121 self.assertEqual(configmap.backend_handle, "1234")
122
Scott Baker393d0152018-05-21 09:17:49 -0700123 def test_delete_record(self):
124 with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client):
125 data = {"foo": "bar"}
126 configmap = KubernetesConfigMap(trust_domain=self.trust_domain, name="test-configmap", data=json.dumps(data))
127
128 orig_map = MagicMock()
129 orig_map.data = {"foo": "not_bar"}
130 orig_map.metadata.self_link = "1234"
131
Scott Bakera30fae72019-02-01 16:14:43 -0800132 step = self.step_class(model_accessor = self.model_accessor)
Scott Baker393d0152018-05-21 09:17:49 -0700133 step.v1core.read_namespaced_config_map.return_value = orig_map
134
135 step.delete_record(configmap)
136
137 step.v1core.delete_namespaced_config_map.assert_called_with("test-configmap", self.trust_domain.name, ANY)
138
139
140
Scott Baker13e953c2018-05-17 09:19:15 -0700141
142if __name__ == '__main__':
143 unittest.main()