aboutsummaryrefslogtreecommitdiffstats
path: root/functest/tests/unit/utils/test_env.py
blob: 064ff9880de723b8f723c2309af09f6fda7fb2fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python

# Copyright (c) 2018 Orange and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0

# pylint: disable=missing-docstring

import logging
import os
import unittest

from six.moves import reload_module

from functest.utils import env
from functest.utils import constants


class EnvTesting(unittest.TestCase):
    # pylint: disable=missing-docstring

    def setUp(self):
        os.environ['FOO'] = 'foo'
        os.environ['BUILD_TAG'] = 'master'
        os.environ['CI_LOOP'] = 'weekly'

    def test_get_unset_unknown_env(self):
        del os.environ['FOO']
        self.assertEqual(env.get('FOO'), None)
        # Backward compatibilty (waiting for SDNVPN and SFC)
        reload_module(env)
        with self.assertRaises(AttributeError):
            getattr(env.ENV, 'FOO')
        reload_module(constants)
        with self.assertRaises(AttributeError):
            getattr(constants.CONST, 'FOO')

    def test_get_unknown_env(self):
        self.assertEqual(env.get('FOO'), 'foo')
        reload_module(env)
        # Backward compatibilty (waiting for SDNVPN and SFC)
        with self.assertRaises(AttributeError):
            getattr(env.ENV, 'FOO')
        reload_module(constants)
        with self.assertRaises(AttributeError):
            getattr(constants.CONST, 'FOO')

    def test_get_unset_env(self):
        del os.environ['CI_LOOP']
        self.assertEqual(
            env.get('CI_LOOP'), env.INPUTS['CI_LOOP'])
        # Backward compatibilty (waiting for SDNVPN and SFC)
        reload_module(env)
        self.assertEqual(
            getattr(env.ENV, 'CI_LOOP'), env.INPUTS['CI_LOOP'])
        reload_module(constants)
        self.assertEqual(
            getattr(constants.CONST, 'CI_LOOP'),
            env.INPUTS['CI_LOOP'])

    def test_get_env(self):
        self.assertEqual(
            env.get('CI_LOOP'), 'weekly')
        # Backward compatibilty (waiting for SDNVPN and SFC)
        reload_module(env)
        self.assertEqual(getattr(env.ENV, 'CI_LOOP'), 'weekly')
        reload_module(constants)
        self.assertEqual(getattr(constants.CONST, 'CI_LOOP'), 'weekly')

    def test_get_unset_env2(self):
        del os.environ['BUILD_TAG']
        self.assertEqual(
            env.get('BUILD_TAG'), env.INPUTS['BUILD_TAG'])
        # Backward compatibilty (waiting for SDNVPN and SFC)
        reload_module(env)
        self.assertEqual(
            getattr(env.ENV, 'BUILD_TAG'), env.INPUTS['BUILD_TAG'])
        reload_module(constants)
        self.assertEqual(
            getattr(constants.CONST, 'BUILD_TAG'), env.INPUTS['BUILD_TAG'])

    def test_get_env2(self):
        self.assertEqual(env.get('BUILD_TAG'), 'master')
        # Backward compatibilty (waiting for SDNVPN and SFC)
        reload_module(env)
        self.assertEqual(getattr(env.ENV, 'BUILD_TAG'), 'master')
        reload_module(env)
        self.assertEqual(getattr(constants.CONST, 'BUILD_TAG'), 'master')


if __name__ == "__main__":
    logging.disable(logging.CRITICAL)
    unittest.main(verbosity=2)