Beispiel #1
0
    def test_configuration_cdist_log_level_env_var(self):
        env = {
            '__cdist_log_level': str(logging.DEBUG),
        }
        args = argparse.Namespace()

        expected_config_dict = {
            'GLOBAL': {
                'verbosity': cap.VERBOSE_DEBUG,
            },
        }

        # bypass singleton so we can test further
        cc.Configuration.instance = None

        configuration = cc.Configuration(args, env=env, config_files=())
        self.assertEqual(configuration.config, expected_config_dict)

        # bypass singleton so we can test further
        cc.Configuration.instance = None
        env['__cdist_log_level'] = '80'
        with self.assertRaises(ValueError):
            configuration = cc.Configuration(args, env=env, config_files=())

        # bypass singleton so we can test further
        cc.Configuration.instance = None
        env['__cdist_log_level'] = 'x'
        with self.assertRaises(ValueError):
            configuration = cc.Configuration(args, env=env, config_files=())
Beispiel #2
0
    def test_conf_dir_from_path_linking(self):
        """Ensure that links are correctly created for types in conf
           directories which are defined in CDIST_PATH"""

        test_type = "__cdist_test_type"

        os.environ['CDIST_PATH'] = conf_dir

        # bypass singleton from other tests if any
        cc.Configuration.instance = None

        configuration = cc.Configuration(argparse.Namespace(), env=os.environ)

        link_test_local = local.Local(
            target_host=(
                'localhost',
                'localhost',
                'localhost',
            ),
            target_host_tags=None,
            base_root_path=self.host_base_path,
            host_dir_name=self.hostdir,
            exec_path=test.cdist_exec_path,
            configuration=configuration.get_config(section='GLOBAL'))

        link_test_local._create_conf_path_and_link_conf_dirs()

        our_type_dir = os.path.join(link_test_local.type_path, test_type)

        self.assertTrue(os.path.isdir(our_type_dir))
Beispiel #3
0
    def test_configuration_timestamping_log_4(self):
        config = newConfigParser()
        config['GLOBAL'] = {
            'timestamp': 'True',
        }

        global_config_file = os.path.join(fixtures, 'cdist-global.cfg')
        with open(global_config_file, 'w') as f:
            config.write(f)

        expected_config_dict = {
            'GLOBAL': {
                'timestamp': False,
                'verbosity': 0,
            },
        }

        config_files = (global_config_file, )

        # bypass singleton so we can test further
        cc.Configuration.instance = None

        args = argparse.Namespace()
        args.timestamp = False
        configuration = cc.Configuration(args,
                                         env=None,
                                         config_files=config_files)
        self.assertEqual(configuration.config, expected_config_dict)
Beispiel #4
0
    def test_configuration_empty_value_in_file(self):
        config = newConfigParser()
        config['GLOBAL'] = {
            'inventory_dir': '',
            'conf_dir': '',
        }

        config_file = os.path.join(fixtures, 'cdist-local.cfg')
        with open(config_file, 'w') as f:
            config.write(f)

        expected_config_dict = {
            'GLOBAL': {
                'inventory_dir': None,
                'conf_dir': None,
                'verbosity': 0,
            },
        }

        config_files = (config_file, )

        # bypass singleton so we can test further
        cc.Configuration.instance = None

        args = argparse.Namespace()
        configuration = cc.Configuration(args,
                                         env={},
                                         config_files=config_files)
        self.assertEqual(configuration.config, expected_config_dict)
Beispiel #5
0
 def test_update_config_dict_section(self):
     config = {
         'GLOBAL': {
             'conf_dir': ['/usr/local/cdist', ],
             'parallel': -1,
         },
     }
     newconfig = {
         'conf_dir': ['~/.cdist', ],
         'parallel': 2,
         'local_shell': '/usr/local/bin/sh',
     }
     expected = {
         'GLOBAL': {
             'conf_dir': ['/usr/local/cdist', '~/.cdist', ],
             'parallel': 2,
             'local_shell': '/usr/local/bin/sh',
         },
     }
     configuration = cc.Configuration(None, env={}, config_files=())
     configuration._update_config_dict_section('GLOBAL', config, newconfig,
                                               update_appends=True)
     self.assertEqual(config, expected)
     expected = {
         'GLOBAL': {
             'conf_dir': ['~/.cdist', ],
             'parallel': 2,
             'local_shell': '/usr/local/bin/sh',
         },
     }
     configuration._update_config_dict_section('GLOBAL', config, newconfig,
                                               update_appends=False)
     self.assertEqual(config, expected)
    def test_configuration_disable_saving_output_streams3(self):
        config = configparser.ConfigParser()
        config['GLOBAL'] = {
            'save_output_streams': 'False',
        }

        global_config_file = os.path.join(fixtures, 'cdist-global.cfg')
        with open(global_config_file, 'w') as f:
            config.write(f)

        expected_config_dict = {
            'GLOBAL': {
                'save_output_streams': False,
                'verbosity': 0,
            },
        }

        config_files = (global_config_file, )

        # bypass singleton so we can test further
        cc.Configuration.instance = None

        args = argparse.Namespace()
        args.save_output_streams = False
        configuration = cc.Configuration(args, env=None,
                                         config_files=config_files)
        self.assertEqual(configuration.config, expected_config_dict)
Beispiel #7
0
    def test_configuration2(self):
        env = {
            'PATH': '/usr/local/bin:/usr/bin:/bin',
            'TEST': 'test',
        }
        args = argparse.Namespace()

        config = newConfigParser()
        config['GLOBAL'] = {
            'beta': 'off',
            'local_shell': '/bin/sh',
            'remote_shell': '/bin/sh',
            'inventory_dir': '',
            'cache_path_pattern': '',
            'conf_dir': '',
            'init_manifest': '',
            'out_path': '',
            'remote_out_path': '',
            'remote_copy': '',
            'remote_exec': '',
            'jobs': '0',
            'parallel': '-1',
            'verbosity': 'INFO',
            'archiving': 'none',
        }

        global_config_file = os.path.join(fixtures, 'cdist-global.cfg')
        with open(global_config_file, 'w') as f:
            config.write(f)

        expected_config_dict = {
            'GLOBAL': {
                'beta': False,
                'local_shell': '/bin/sh',
                'remote_shell': '/bin/sh',
                'inventory_dir': None,
                'cache_path_pattern': None,
                'conf_dir': None,
                'init_manifest': None,
                'out_path': None,
                'remote_out_path': None,
                'remote_copy': None,
                'remote_exec': None,
                'jobs': 0,
                'parallel': multiprocessing.cpu_count(),
                'verbosity': cap.VERBOSE_INFO,
                'archiving': None,
            },
        }
        config_files = (global_config_file, )

        # bypass singleton so we can test further
        cc.Configuration.instance = None

        configuration = cc.Configuration(args,
                                         env=env,
                                         config_files=config_files)
        self.assertEqual(configuration.config, expected_config_dict)
Beispiel #8
0
    def test_read_config_file(self):
        config = cc.Configuration(None, env={}, config_files=())
        d = config._read_config_file(self.config_file)
        self.assertEqual(d, self.expected_config_dict)

        for x in range(1, 4):
            config_file = getattr(self, 'invalid_config_file' + str(x))
            with self.assertRaises(ValueError):
                config._read_config_file(config_file)
Beispiel #9
0
 def test_read_config_file_with_interpolation(self):
     try:
         config = cc.Configuration(None, env={}, config_files=())
         d = config._read_config_file(interpolation_config_file)
         val = d['GLOBAL']['cache_path_pattern']
         self.assertIsNotNone(val)
         self.assertEqual(val, '%N')
     except configparser.InterpolationSyntaxError as e:
         self.fail("Exception should not have been raised: {}".format(e))
Beispiel #10
0
 def test_update_defaults_for_unset(self):
     config = {
         'GLOBAL': {},
     }
     expected_config = {
         'GLOBAL': {
             'verbosity': 0,
         },
     }
     cfg = cc.Configuration(None, env={}, config_files=())
     cfg._update_defaults_for_unset(config)
     self.assertEqual(config, expected_config)
Beispiel #11
0
    def test_read_args_config(self):
        config = cc.Configuration(None, env={}, config_files=())
        args = argparse.Namespace()
        args.beta = False
        args.conf_dir = ['/usr/local/cdist1', ]
        args.verbose = 3
        args.tag = 'test'

        expected = {
            'conf_dir': ['/usr/local/cdist1', ],
            'verbosity': 3,
        }
        args_dict = vars(args)
        d = config._read_args_config(args_dict)
        self.assertEqual(d, expected)
        self.assertNotEqual(d, args_dict)
Beispiel #12
0
    def test_read_env_var_config(self):
        config = cc.Configuration(None, env={}, config_files=())
        env = {
            'a': 'a',
            'CDIST_BETA': '1',
            'CDIST_PATH': '/usr/local/cdist:~/.cdist',
        }
        expected = {
            'beta': True,
            'conf_dir': ['/usr/local/cdist', '~/.cdist', ],
        }
        section = 'GLOBAL'
        d = config._read_env_var_config(env, section)
        self.assertEqual(d, expected)

        del env['CDIST_BETA']
        del expected['beta']
        d = config._read_env_var_config(env, section)
        self.assertEqual(d, expected)
Beispiel #13
0
    def test_configuration1(self):
        env = {
            'PATH': '/usr/local/bin:/usr/bin:/bin',
            'TEST': 'test',
        }
        args = argparse.Namespace()
        expected_config_dict = {
            'GLOBAL': {
                'verbosity': 0,
            },
        }

        # bypass singleton so we can test further
        cc.Configuration.instance = None
        configuration = cc.Configuration(args, env=env,
                                         config_files=('cdist.cfg'))
        self.assertIsNotNone(configuration.args)
        self.assertIsNotNone(configuration.env)
        self.assertIsNotNone(configuration.config_files)
        self.assertEqual(configuration.config, expected_config_dict)
Beispiel #14
0
    def test_configuration7(self):
        env = {
            'PATH': '/usr/local/bin:/usr/bin:/bin',
            'TEST': 'test',
            'CDIST_PATH': '/opt/cdist/conf:/usr/local/share/cdist/conf',
            'REMOTE_COPY': 'scp',
            'REMOTE_EXEC': 'ssh',
            'CDIST_BETA': '1',
            'CDIST_LOCAL_SHELL': '/usr/bin/sh',
            'CDIST_REMOTE_SHELL': '/usr/bin/sh',
        }
        args = argparse.Namespace()

        config = newConfigParser()
        config['GLOBAL'] = {
            'beta': 'off',
            'local_shell': '/bin/sh',
            'remote_shell': '/bin/sh',
            'inventory_dir': '',
            'cache_path_pattern': '',
            'conf_dir': '',
            'init_manifest': '',
            'out_path': '',
            'remote_out_path': '',
            'remote_copy': '',
            'remote_exec': '',
            'jobs': '0',
            'parallel': '-1',
            'verbosity': 'INFO',
            'archiving': 'none',
        }

        global_config_file = os.path.join(fixtures, 'cdist-global.cfg')
        with open(global_config_file, 'w') as f:
            config.write(f)

        config = newConfigParser()
        config['GLOBAL'] = {
            'beta': 'on',
            'local_shell': '/usr/bin/sh',
            'remote_shell': '/usr/bin/sh',
            'inventory_dir': '/var/db/cdist/inventory',
            'conf_dir': '/opt/cdist',
            'remote_copy': 'myscp',
            'remote_exec': 'myexec',
            'parallel': '-1',
            'archiving': 'tar',
        }

        local_config_file = os.path.join(fixtures, 'cdist-local.cfg')
        with open(local_config_file, 'w') as f:
            config.write(f)

        config = newConfigParser()
        config['GLOBAL'] = {
            'conf_dir': '/opt/conf/cdist',
            'remote_copy': 'scpcustom',
            'remote_exec': 'sshcustom',
            'parallel': '15',
            'archiving': 'txz',
        }

        custom_config_file = os.path.join(fixtures, 'cdist-custom.cfg')
        with open(custom_config_file, 'w') as f:
            config.write(f)

        expected_config_dict = {
            'GLOBAL': {
                'beta': True,
                'local_shell': '/usr/bin/sh',
                'remote_shell': '/usr/bin/sh',
                'inventory_dir': '/var/db/cdist/inventory',
                'cache_path_pattern': None,
                'conf_dir': [
                    '/opt/conf/cdist',
                ],
                'init_manifest': None,
                'out_path': None,
                'remote_out_path': None,
                'remote_copy': 'scpcustom',
                'remote_exec': 'sshcustom',
                'jobs': 0,
                'parallel': 15,
                'verbosity': cap.VERBOSE_INFO,
                'archiving': 'txz',
            },
        }

        config_files = (
            global_config_file,
            local_config_file,
        )

        args.config_file = custom_config_file

        # bypass singleton so we can test further
        cc.Configuration.instance = None

        configuration = cc.Configuration(args,
                                         env=env,
                                         config_files=config_files)
        self.assertEqual(configuration.config, expected_config_dict)
Beispiel #15
0
 def test_non_singleton(self):
     x = cc.Configuration(None, env={}, config_files=(), singleton=False)
     args = argparse.Namespace()
     args.a = 'a'
     y = cc.Configuration(args, env={}, config_files=(), singleton=False)
     self.assertIsNot(x, y)