def test_guess_flavor_flavor(prepare_os, mock_image_cfg, mock_env_cfg, config): p = prepare_os.PrepOS(mock_image_cfg, mock_env_cfg) with mock.patch.object(p, 'os') as mock_os: mock_os.fuzzy_find_flavor.return_value = sentinel.flavor assert p.guess_flavor(config.Config({'nova': { 'flavor': 'x' }})) == sentinel.flavor
def test_prepare_ssh_empty(prepare_os, config, mock_env_cfg): p = prepare_os.PrepOS(config.Config({'glance': { 'name': 'foo' }}), mock_env_cfg) p.ip = sentinel.ip p.prepare_ssh() assert p.ssh is None
def notest_get_environment_not_ok(config): mock_config = mock.mock_open( read_data='{"env1":{ "os_tenant_name": "good_name"}}') with mock.patch.object(config, "open", mock_config): with mock.patch.object(config.os.path, "isfile", return_value=True): conf = config.Config() with pytest.raises(config.TestEnvironmentNotFoundInConfigError): conf.get_environment('env3')["os_tenant_name"]
def test_add_config_simple(config): with tempfile.NamedTemporaryFile() as mock_cfg: mock_cfg.write('a: b') mock_cfg.flush() c = config.Config() c.add_config(mock_cfg.name) assert c.get('a') == 'b' assert c.config_list == [mock_cfg.name]
def test_RotateCommand_actual(commands, mock_env_cfg, config): parser, obj = create_subparser(commands.RotateCommand) args = parser.parse_args(['rotate', 'uploadlabel']) with mock.patch.object(commands.config, "UploadEnvConfig") as uec: uec.return_value = config.Config({"uploadlabel": mock_env_cfg}) with mock.patch.object(commands.osclient, "OSClient"): args.command(args) assert obj.upload_env
def test_guess_flavor_flavor_id(prepare_os, mock_image_cfg, mock_env_cfg, config): p = prepare_os.PrepOS(mock_image_cfg, mock_env_cfg) with mock.patch.object(p, 'os') as mock_os: mock_os.get_flavor = lambda x: x assert p.guess_flavor(config.Config({'nova': { 'flavor_id': 'x' }})) == 'x'
def test_spawn_instance_no_config_drive(prepare_os, mock_image_cfg, config): env = {'keystone': {}, 'nova': {'flavor': 'mock_flavor'}} prep_os = prepare_os.PrepOS(mock_image_cfg, config.Config(env)) with mock.patch.object(prepare_os.osclient, "OSClient") as mock_os: prep_os.os_key = mock.MagicMock() prep_os.connect() prep_os.spawn_instance(1) assert mock_os.return_value.boot_instance.call_args[1][ 'config_drive'] is False
def test_ObsoleteCommand_actual(commands, mock_env_cfg, config): parser, obj = create_subparser(commands.ObsoleteCommand) args = parser.parse_args(['mark-obsolete', 'uploadlabel', 'myuuid']) assert args.uuid == 'myuuid' with mock.patch.object(commands.config, "UploadEnvConfig") as uec: uec.return_value = config.Config({'uploadlabel': mock_env_cfg}) with mock.patch.object(commands.osclient, "OSClient"): args.command(args) assert obj.upload_env
def test_UploadCommand_no_glance_section(commands, mock_env_cfg, config): img_config = {'filename': 'foobar'} parser, obj = create_subparser(commands.UploadCommand) args = parser.parse_args(['upload', 'label', 'uploadlabel']) with mock.patch.object(commands.config, "UploadEnvConfig") as uec: uec.return_value = config.Config({"uploadlabel": mock_env_cfg}) with mock.patch.object(commands.osclient, "OSClient"): with mock.patch.object(commands.config, "ImageConfig") as ic: ic.return_value.__getitem__.return_value = img_config with pytest.raises(commands.NotFoundInConfigError): args.command(args)
def mock_image_cfg(config): image = config.Config({ 'glance': { 'name': 'foo' }, 'filename': sentinel.filename, 'tests': { 'ssh': { 'username': '******' } } }) return image
def test_add_config_double(config, capfd): with tempfile.NamedTemporaryFile() as mock_cfg1: mock_cfg1.write('some_label: bad!') mock_cfg1.flush() with tempfile.NamedTemporaryFile() as mock_cfg2: mock_cfg2.write('some_label: good') mock_cfg2.flush() c = config.Config() c.add_config(mock_cfg1.name) c.add_config(mock_cfg2.name) assert c.get('some_label') == 'good' assert c.config_list == [mock_cfg1.name, mock_cfg2.name] out = capfd.readouterr()[0] assert "redefines some_label" in out
def test_UploadCommand_actual_with_obsolete(commands, cred, mock_env_cfg, mock_image_cfg, config): parser, obj = create_subparser(commands.UploadCommand) args = parser.parse_args(['upload', 'label', 'uploadlabel']) assert args.imagelabel == 'label' assert args.no_obsolete is False assert args.images_config is None assert args.filename is None with mock.patch.object(commands.config, "UploadEnvConfig", autospec=True, strict=True) as uec: uec.return_value = config.Config({'uploadlabel': mock_env_cfg}) with mock.patch.object(commands.osclient, "OSClient") as mock_os: mock_os.return_value.older_images.return_value = [ sentinel.one, sentinel.two ] with mock.patch.object(commands.config, "ImageConfig", autospec=True, strict=True) as ic: ic.return_value = config.Config({'label': mock_image_cfg}) args.command(args)
def mock_env_cfg(config): env = config.Config({ 'keystone': { 'auth_url': sentinel.auth, 'tenant_name': sentinel.tenant, 'password': sentinel.password, 'username': sentinel.user }, 'nova': { 'flavor': 'example', 'nics': [ {'net_id': sentinel.uuid1}, {'net_id': sentinel.uuid2} ] } }) return env
def test__userdata_with_userdata_file(prepare_os, mock_image_cfg, config): with tempfile.NamedTemporaryFile() as t: t.write('{"foo":"bar"}') t.flush() env = config.Config({ 'keystone': { 'auth_url': sentinel.auth, 'tenant_name': sentinel.tenant, 'password': sentinel.password, 'username': sentinel.user }, 'nova': { 'flavor': 'example', 'nics': [ {'net_id': sentinel.uuid1}, {'net_id': sentinel.uuid2} ], 'userdata_file': t.name } }) prep_os = prepare_os.PrepOS(mock_image_cfg, env) assert prep_os._userdata(env) == '{"foo":"bar"}'
def test_config_repr(config): d = {"a": 1} r = repr(config.Config(d)) assert r == "Config({'a': 1})"
def test_merge_config_snippet_with_overwrite(config, capfd): c = config.Config({'a': 1, 'b': 2, 'c': 3}) c.merge_config_snippet({'a': 2}, 'new_name') assert c.config == {'a': 2, 'b': 2, 'c': 3} out = capfd.readouterr()[0] assert 'redefines' in out
def test__userdata_no_userdata(prepare_os, mock_image_cfg, mock_env_cfg, config): prep_os = prepare_os.PrepOS(mock_image_cfg, mock_env_cfg) assert prep_os._userdata(config.Config({})) is None
def test_add_config_no_file(config): with mock.patch.object(config.os.path, "isfile", return_value=False): c = config.Config() with pytest.raises(config.ConfigNotFound): c.add_config(sentinel.not_a_file)
def test_gather_snippets(config, file_val, res): with mock.patch.object(config.os, 'listdir', return_value=['file3', 'file1', 'file2']): with mock.patch.object(config.os.path, 'isfile', side_effect=file_val): assert config.Config().gather_snippets('path') == res
def test_len(config, data): assert len(config.Config(data)) == len(data)
def _prep_conf(self, config): c = config.Config() c.CONFIG_SEARCH_PATH = ['etc/dibctl', './dibctl', './'] c.DEFAULT_CONFIG_NAME = 'config.yaml' c.CONF_D_NAME = 'config.d' return c
def test_non_zero_true(config): assert bool(config.Config({'a': 1})) is True
def test_empty_is_false(config): assert bool(config.Config()) is False
def test_get_max(config, conf1, conf2, path, result): c1 = config.Config(conf1) c2 = config.Config(conf2) assert config.get_max(c1, c2, path, 99) == result
def test_gather_snippets_empty(config): with mock.patch.object(config.os, 'listdir', return_value=[]): assert config.Config().gather_snippets('path') == []
def test_config_dict_conversion(config): d = {"a": 1, "b": "c"} x = config.Config(d) y = dict(x) assert y == d
def test_config_str(config): d = {"a": 1} assert str(config.Config(d)) == str(d)
def test_config_items(config): d = {"a": 1} l = list(config.Config(d).items()) assert l == [('a', 1)]
def test_guess_flavor_flavor_bad(prepare_os, mock_image_cfg, mock_env_cfg, config, bad): p = prepare_os.PrepOS(mock_image_cfg, mock_env_cfg) with mock.patch.object(p, 'os'): with pytest.raises(prepare_os.FlavorError): assert p.guess_flavor(config.Config(bad))
def test_config_in(config, input, query, result): c = config.Config(input) c.config_file = 'foo' assert (query in c) is result