| aticig | 56b86c2 | 2022-06-29 10:43:05 +0300 | [diff] [blame] | 1 | # Copyright 2022 Canonical Ltd. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | from unittest import TestCase |
| 16 | import os |
| 17 | import tempfile |
| 18 | |
| 19 | from osm_lcm.lcm_hc import get_health_check_file |
| 20 | from osm_lcm.lcm_utils import LcmException |
| 21 | from test_lcm import create_lcm_config |
| 22 | |
| 23 | |
| 24 | class TestLcmHealthCheck(TestCase): |
| 25 | def setUp(self): |
| 26 | self.config_temp = os.getcwd() + "/osm_lcm/tests/test_lcm_config_file.yaml" |
| 27 | |
| 28 | def test_get_health_check_path(self): |
| 29 | with self.subTest(i=1, t="Empty Config Input"): |
| 30 | hc_path = get_health_check_file() |
| 31 | expected_hc_path = "/app/storage/time_last_ping" |
| 32 | self.assertEqual(hc_path, expected_hc_path) |
| 33 | |
| 34 | with self.subTest(i=2, t="Config Input as Dictionary"): |
| 35 | config_dict = {"storage": {"path": "/tmp/sample_hc"}} |
| 36 | hc_path = get_health_check_file(config_dict) |
| 37 | expected_hc_path = "/tmp/sample_hc/time_last_ping" |
| 38 | self.assertEqual(hc_path, expected_hc_path) |
| 39 | |
| 40 | with self.subTest(i=3, t="Config Input as Dictionary with wrong format"): |
| 41 | config_dict = {"folder": {"path": "/tmp/sample_hc"}} |
| 42 | # it will return default health check path |
| 43 | hc_path = get_health_check_file(config_dict) |
| 44 | expected_hc_path = "/app/storage/time_last_ping" |
| 45 | self.assertEqual(hc_path, expected_hc_path) |
| 46 | |
| 47 | def test_get_health_check_path_config_file_not_found(self): |
| 48 | # open raises the FileNotFoundError |
| 49 | with self.assertRaises(LcmException): |
| 50 | get_health_check_file("/tmp2/config_yaml") |
| 51 | |
| 52 | def test_get_health_check_path_config_file(self): |
| 53 | config_file = tempfile.mkstemp()[1] |
| 54 | create_lcm_config(self.config_temp, config_file) |
| 55 | hc_path = get_health_check_file(config_file) |
| 56 | expected_hc_path = "/tmp/storage/time_last_ping" |
| 57 | self.assertEqual(hc_path, expected_hc_path) |
| 58 | |
| 59 | def test_get_health_check_path_config_file_empty(self): |
| 60 | new_config = tempfile.mkstemp()[1] |
| 61 | # Empty file will cause AttributeError |
| 62 | # and it will raise LCMException |
| 63 | with self.assertRaises(LcmException): |
| 64 | get_health_check_file(new_config) |
| 65 | |
| 66 | def test_get_health_check_path_config_file_not_include_storage_path(self): |
| 67 | config_file = tempfile.mkstemp()[1] |
| 68 | create_lcm_config(self.config_temp, config_file, 36) |
| 69 | # It will return default health check path |
| 70 | hc_path = get_health_check_file(config_file) |
| 71 | expected_hc_path = "/app/storage/time_last_ping" |
| 72 | self.assertEqual(hc_path, expected_hc_path) |