Get properly env vars or their default values
[functest.git] / functest / tests / unit / utils / test_env.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2018 Orange and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 # pylint: disable=missing-docstring
11
12 import logging
13 import os
14 import unittest
15
16 from six.moves import reload_module
17
18 from functest.utils import env
19 from functest.utils import constants
20
21
22 class EnvTesting(unittest.TestCase):
23     # pylint: disable=missing-docstring
24
25     def setUp(self):
26         os.environ['FOO'] = 'foo'
27         os.environ['BUILD_TAG'] = 'master'
28         os.environ['CI_LOOP'] = 'weekly'
29
30     def test_get_unset_unknown_env(self):
31         del os.environ['FOO']
32         self.assertEqual(env.get('FOO'), None)
33         # Backward compatibilty (waiting for SDNVPN and SFC)
34         reload_module(env)
35         with self.assertRaises(AttributeError):
36             getattr(env.ENV, 'FOO')
37         reload_module(constants)
38         with self.assertRaises(AttributeError):
39             getattr(constants.CONST, 'FOO')
40
41     def test_get_unknown_env(self):
42         self.assertEqual(env.get('FOO'), 'foo')
43         reload_module(env)
44         # Backward compatibilty (waiting for SDNVPN and SFC)
45         with self.assertRaises(AttributeError):
46             getattr(env.ENV, 'FOO')
47         reload_module(constants)
48         with self.assertRaises(AttributeError):
49             getattr(constants.CONST, 'FOO')
50
51     def test_get_unset_env(self):
52         del os.environ['CI_LOOP']
53         self.assertEqual(
54             env.get('CI_LOOP'), env.INPUTS['CI_LOOP'])
55         # Backward compatibilty (waiting for SDNVPN and SFC)
56         reload_module(env)
57         self.assertEqual(
58             getattr(env.ENV, 'CI_LOOP'), env.INPUTS['CI_LOOP'])
59         reload_module(constants)
60         self.assertEqual(
61             getattr(constants.CONST, 'CI_LOOP'),
62             env.INPUTS['CI_LOOP'])
63
64     def test_get_env(self):
65         self.assertEqual(
66             env.get('CI_LOOP'), 'weekly')
67         # Backward compatibilty (waiting for SDNVPN and SFC)
68         reload_module(env)
69         self.assertEqual(getattr(env.ENV, 'CI_LOOP'), 'weekly')
70         reload_module(constants)
71         self.assertEqual(getattr(constants.CONST, 'CI_LOOP'), 'weekly')
72
73     def test_get_unset_env2(self):
74         del os.environ['BUILD_TAG']
75         self.assertEqual(
76             env.get('BUILD_TAG'), env.INPUTS['BUILD_TAG'])
77         # Backward compatibilty (waiting for SDNVPN and SFC)
78         reload_module(env)
79         self.assertEqual(
80             getattr(env.ENV, 'BUILD_TAG'), env.INPUTS['BUILD_TAG'])
81         reload_module(constants)
82         self.assertEqual(
83             getattr(constants.CONST, 'BUILD_TAG'), env.INPUTS['BUILD_TAG'])
84
85     def test_get_env2(self):
86         self.assertEqual(env.get('BUILD_TAG'), 'master')
87         # Backward compatibilty (waiting for SDNVPN and SFC)
88         reload_module(env)
89         self.assertEqual(getattr(env.ENV, 'BUILD_TAG'), 'master')
90         reload_module(env)
91         self.assertEqual(getattr(constants.CONST, 'BUILD_TAG'), 'master')
92
93
94 if __name__ == "__main__":
95     logging.disable(logging.CRITICAL)
96     unittest.main(verbosity=2)