Matteo Scandolo | 5f2840f | 2019-05-16 16:08:23 -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 | class ApiException(Exception): |
| 24 | def __init__(self, status, *args, **kwargs): |
| 25 | super(ApiException, self).__init__(*args, **kwargs) |
| 26 | self.status = status |
| 27 | |
| 28 | def fake_init_kubernetes_client(self): |
| 29 | self.api_instance = MagicMock() |
| 30 | self.ApiException = ApiException |
| 31 | |
| 32 | class TestSyncKubernetesServiceInstance(unittest.TestCase): |
| 33 | |
| 34 | def setUp(self): |
| 35 | self.unittest_setup = setup_sync_unit_test(os.path.abspath(os.path.dirname(os.path.realpath(__file__))), |
| 36 | globals(), |
| 37 | [("kubernetes-service", "kubernetes.xproto")] ) |
| 38 | |
| 39 | self.MockObjectList = self.unittest_setup["MockObjectList"] |
| 40 | self.model_accessor = self.unittest_setup["model_accessor"] |
| 41 | |
| 42 | sys.path.append(os.path.join(os.path.abspath(os.path.dirname(os.path.realpath(__file__))), "../steps")) |
| 43 | |
| 44 | from sync_kubernetes_service import SyncK8Service |
| 45 | self.step_class = SyncK8Service |
| 46 | |
| 47 | self.service = KubernetesService(name="TestK8") |
| 48 | |
| 49 | def tearDown(self): |
| 50 | sys.path = self.unittest_setup["sys_path_save"] |
| 51 | |
| 52 | def test_valid_version(self): |
| 53 | |
| 54 | version = MagicMock() |
Matteo Scandolo | f6a60e5 | 2019-05-24 15:11:52 -0700 | [diff] [blame] | 55 | version.major = '1' |
| 56 | version.minor = '13' |
| 57 | version.git_version = "v1.13.0" |
| 58 | |
| 59 | with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client): |
| 60 | step = self.step_class(model_accessor=self.model_accessor) |
| 61 | |
| 62 | step.api_instance.get_code.return_value = version |
| 63 | |
| 64 | step.sync_record(self.service) |
| 65 | |
| 66 | def test_valid_version_int(self): |
| 67 | |
| 68 | version = MagicMock() |
Matteo Scandolo | 5f2840f | 2019-05-16 16:08:23 -0700 | [diff] [blame] | 69 | version.major = 1 |
| 70 | version.minor = 13 |
| 71 | version.git_version = "v1.13.0" |
| 72 | |
| 73 | with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client): |
| 74 | step = self.step_class(model_accessor=self.model_accessor) |
| 75 | |
| 76 | step.api_instance.get_code.return_value = version |
| 77 | |
| 78 | step.sync_record(self.service) |
| 79 | |
| 80 | def test_invalid_version(self): |
| 81 | |
| 82 | version = MagicMock() |
| 83 | version.major = 1 |
Woojoong Kim | 6ab5501 | 2019-09-04 15:15:42 -0700 | [diff] [blame^] | 84 | version.minor = 16 |
| 85 | version.git_version = "v1.16.0" |
Matteo Scandolo | 5f2840f | 2019-05-16 16:08:23 -0700 | [diff] [blame] | 86 | |
| 87 | with patch.object(self.step_class, "init_kubernetes_client", new=fake_init_kubernetes_client): |
| 88 | step = self.step_class(model_accessor=self.model_accessor) |
| 89 | |
| 90 | step.api_instance.get_code.return_value = version |
| 91 | |
| 92 | with self.assertRaises(Exception): |
| 93 | step.sync_record(self.service) |
| 94 | |
| 95 | |
| 96 | if __name__ == '__main__': |
| 97 | unittest.main() |