TestConstants.py 1.56 KB
Newer Older
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
# -*- coding: utf-8 -*-

import unittest

from ansible.constants import get_config
import ConfigParser
import random
import string
import os


def random_string(length):
    return ''.join(random.choice(string.ascii_uppercase) for x in range(6))

p = ConfigParser.ConfigParser()
p.read(os.path.join(os.path.dirname(__file__), 'ansible.cfg'))

class TestConstants(unittest.TestCase):

    #####################################
    ### get_config unit tests

    
    def test_configfile_and_env_both_set(self):
        r = random_string(6)
        env_var = 'ANSIBLE_TEST_%s' % r
        os.environ[env_var] = r

        res = get_config(p, 'defaults', 'test_key', env_var, 'default')
        del os.environ[env_var]

        assert res == r


    def test_configfile_set_env_not_set(self):
        r = random_string(6)
        env_var = 'ANSIBLE_TEST_%s' % r
        assert env_var not in os.environ
        
        res = get_config(p, 'defaults', 'test_key', env_var, 'default')

        print res
        assert res == 'test_value'


    def test_configfile_not_set_env_set(self):
        r = random_string(6)
        env_var = 'ANSIBLE_TEST_%s' % r
        os.environ[env_var] = r

        res = get_config(p, 'defaults', 'doesnt_exist', env_var, 'default')
        del os.environ[env_var]

        assert res == r


    def test_configfile_not_set_env_not_set(self):
        r = random_string(6)
        env_var = 'ANSIBLE_TEST_%s' % r
        assert env_var not in os.environ
        
        res = get_config(p, 'defaults', 'doesnt_exist', env_var, 'default')

        assert res == 'default'