Coverage for n2vc/tests/unit/test_config.py: 100%

30 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-05-07 06:04 +0000

1# Copyright 2020 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 

15from unittest import TestCase 

16from unittest.mock import patch 

17 

18 

19from n2vc.config import EnvironConfig, ModelConfig, MODEL_CONFIG_KEYS 

20 

21 

22def generate_os_environ_dict(config, prefix): 

23 return {f"{prefix}{k.upper()}": v for k, v in config.items()} 

24 

25 

26class TestEnvironConfig(TestCase): 

27 def setUp(self): 

28 self.config = {"host": "1.2.3.4", "port": "17070", "k8s_cloud": "k8s"} 

29 

30 @patch("os.environ.items") 

31 def test_environ_config_lcm(self, mock_environ_items): 

32 envs = generate_os_environ_dict(self.config, "OSMLCM_VCA_") 

33 envs["not_valid_env"] = "something" 

34 mock_environ_items.return_value = envs.items() 

35 config = EnvironConfig() 

36 self.assertEqual(config, self.config) 

37 

38 @patch("os.environ.items") 

39 def test_environ_config_mon(self, mock_environ_items): 

40 envs = generate_os_environ_dict(self.config, "OSMMON_VCA_") 

41 envs["not_valid_env"] = "something" 

42 mock_environ_items.return_value = envs.items() 

43 config = EnvironConfig() 

44 self.assertEqual(config, self.config) 

45 

46 

47class TestModelConfig(TestCase): 

48 def setUp(self): 

49 self.config = { 

50 f'model_config_{model_key.replace("-", "_")}': "somevalue" 

51 for model_key in MODEL_CONFIG_KEYS 

52 } 

53 self.config["model_config_invalid"] = "something" 

54 self.model_config = {model_key: "somevalue" for model_key in MODEL_CONFIG_KEYS} 

55 

56 def test_model_config(self): 

57 model_config = ModelConfig(self.config) 

58 self.assertEqual(model_config, self.model_config)