blob: 82d957e2669ec87513f5b43b22a89fe162c9cb87 [file] [log] [blame]
Scott Baker1a2ee302018-08-27 16:16:19 -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, Mock
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 TestSyncKubernetesResourceInstance(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(),
38 [("kubernetes-service", "kubernetes.proto")] )
39
40 self.MockObjectList = self.unittest_setup["MockObjectList"]
41
42 sys.path.append(os.path.join(os.path.abspath(os.path.dirname(os.path.realpath(__file__))), "../steps"))
43
44 from sync_kubernetesresourceinstance import SyncKubernetesResourceInstance
45 self.step_class = SyncKubernetesResourceInstance
46
47 self.service = KubernetesService()
48
49 def tearDown(self):
50 sys.path = self.unittest_setup["sys_path_save"]
51
52 @patch('subprocess.Popen')
53 def test_run_kubectl(self, mock_popen):
54 proc = Mock()
55 proc.communicate.return_value = ('output', 'error')
56 proc.returncode = 0
57
58 mock_popen.return_value = proc
59
60 step = self.step_class()
61 step.run_kubectl("create", "foo")
62
63 mock_popen.assert_called()
64
65 @patch('subprocess.Popen')
66 def test_run_kubectl_fail(self, mock_popen):
67 proc = Mock()
68 proc.communicate.return_value = ('output', 'error')
69 proc.returncode = 1
70
71 mock_popen.return_value = proc
72
73 step = self.step_class()
74 with self.assertRaises(Exception) as e:
75 step.run_kubectl("create", "foo")
76
77 self.assertEqual(e.exception.message, "Process failed with returncode 1")
78
79 def test_sync_record_create(self):
80 with patch.object(self.step_class, "run_kubectl") as run_kubectl:
81 xos_ri = KubernetesResourceInstance(name="test-instance", owner=self.service, resource_definition="foo")
82
83 run_kubectl.return_value = None
84
85 step = self.step_class()
86 step.sync_record(xos_ri)
87
88 run_kubectl.assert_called_with("apply", "foo")
89
90 self.assertEqual(xos_ri.kubectl_state, "created")
91
92 def test_sync_record_update(self):
93 with patch.object(self.step_class, "run_kubectl") as run_kubectl:
94 xos_ri = KubernetesResourceInstance(name="test-instance", owner=self.service, resource_definition="foo", kubectl_state="created")
95
96 run_kubectl.return_value = None
97
98 step = self.step_class()
99 step.sync_record(xos_ri)
100
101 run_kubectl.assert_called_with("apply", "foo")
102
103 self.assertEqual(xos_ri.kubectl_state, "updated")
104
105 def test_sync_record_delete(self):
106 with patch.object(self.step_class, "run_kubectl") as run_kubectl:
107 xos_ri = KubernetesResourceInstance(name="test-instance", owner=self.service, resource_definition="foo", kubectl_state="created")
108
109 run_kubectl.return_value = None
110
111 step = self.step_class()
112 step.delete_record(xos_ri)
113
114 run_kubectl.assert_called_with("delete", "foo")
115
116 self.assertEqual(xos_ri.kubectl_state, "deleted")
117
118 def test_sync_record_delete_never_created(self):
119 """ If the object was never saved, then we shouldn't try to delete it """
120 with patch.object(self.step_class, "run_kubectl") as run_kubectl:
121 xos_ri = KubernetesResourceInstance(name="test-instance", owner=self.service, resource_definition="foo")
122
123 run_kubectl.return_value = None
124
125 step = self.step_class()
126 step.delete_record(xos_ri)
127
128 run_kubectl.assert_not_called()
129
130
131if __name__ == '__main__':
132 unittest.main()