def test_no_shared_folders_for_distributable_image(config_files, monkeypatch): with mocked_executable('lxc'): with mocked_lxd_version_check(): with open(config_files, "r") as main_file: with command_context({'edi_create_distributable_image': True}): parser = ConfigurationParser(main_file) coordinator = SharedFolderCoordinator(parser) def fake_os_path_exists(*_): return False monkeypatch.setattr(os.path, 'exists', fake_os_path_exists) def fake_run(*popenargs, **kwargs): # We should not run anything! assert False monkeypatch.setattr(mockablerun, 'run_mockable', fake_run) coordinator.create_host_folders() coordinator.verify_container_mountpoints('does-not-exist') assert coordinator.get_mountpoints() == [] assert coordinator.get_pre_config_profiles() == [] assert coordinator.get_post_config_profiles() == []
def test_verify_container_mountpoints_failure(config_files, monkeypatch): with mocked_executable('lxc', '/here/is/no/lxc'): with mocked_lxd_version_check(): with open(config_files, "r") as main_file: def fake_lxc_exec_command(*popenargs, **kwargs): if get_command(popenargs).endswith( 'lxc') and get_sub_command(popenargs) == 'exec': if get_command_parameter(popenargs, '--') == 'test': return subprocess.CompletedProcess( "failure", 1, 'failure') else: return subprocess.CompletedProcess( "fakerun", 0, '') else: return subprocess.run(*popenargs, **kwargs) monkeypatch.setattr(mockablerun, 'run_mockable', fake_lxc_exec_command) parser = ConfigurationParser(main_file) coordinator = SharedFolderCoordinator(parser) with pytest.raises(FatalError) as error: coordinator.verify_container_mountpoints('fake-container') assert 'fake-container' in error.value.message assert '/foo/bar/target_mountpoint' in error.value.message
def test_plugins(monkeypatch, config_files, capsys, command, command_args, has_templates, has_profiles, has_playbooks, has_postprocessing_commands): with mocked_executable('lxc'): with mocked_lxd_version_check(): def fake_lxc_config_command(*popenargs, **kwargs): if 'images.compression_algorithm' in popenargs[0]: return subprocess.CompletedProcess("fakerun", 0, '') else: return subprocess.run(*popenargs, **kwargs) monkeypatch.setattr(mockablerun, 'run_mockable', fake_lxc_config_command) parser = edi._setup_command_line_interface() command_args.append(config_files) cli_args = parser.parse_args(command_args) command().run_cli(cli_args) out, err = capsys.readouterr() assert err == '' result = yaml.safe_load(out) if has_templates: assert result.get('lxc_templates') else: assert not result.get('lxc_templates') if has_profiles: assert result.get('lxc_profiles') else: assert not result.get('lxc_profiles') if has_playbooks: assert len(result.get('playbooks')) == 3 base_system = result.get('playbooks')[0].get('10_base_system') assert 'plugins/playbooks/foo.yml' in base_system.get('path') assert base_system.get('dictionary').get( 'kernel_package') == 'linux-image-amd64-rt' assert base_system.get('dictionary').get( 'edi_project_directory') == os.path.dirname(config_files) assert base_system.get('dictionary').get("param1") == "keep" assert base_system.get('dictionary').get( "param2") == "overwritten" assert base_system.get('dictionary').get( "param3") == "customized" else: assert not result.get('playbooks') if has_postprocessing_commands: assert result.get('postprocessing_commands') else: assert not result.get('postprocessing_commands')
def test_invalid_version(monkeypatch): patch_lxd_get_version(monkeypatch, '2.2.0') with mocked_executable('lxd', '/here/is/no/lxd'): with clear_lxd_version_check_cache(): check_method = LxdVersion.check with pytest.raises(FatalError) as error: check_method() assert '2.2.0' in error.value.message assert '>=3.0.0' in error.value.message assert 'xenial-backports' in error.value.message
def test_get_server_image_compression_bzip2(monkeypatch): with mocked_executable('lxc', '/here/is/no/lxc'): with mocked_lxd_version_check(): def fake_lxc_config_command(*popenargs, **kwargs): if get_command(popenargs).endswith('lxc') and get_sub_command(popenargs) == 'config': return subprocess.CompletedProcess("fakerun", 0, stdout='bzip2') else: return subprocess.run(*popenargs, **kwargs) monkeypatch.setattr(mockablerun, 'run_mockable', fake_lxc_config_command) result = get_server_image_compression_algorithm() assert result == 'bzip2'
def test_verify_container_mountpoints(config_files, monkeypatch): with mocked_executable('lxc', '/here/is/no/lxc'): with mocked_lxd_version_check(): with open(config_files, "r") as main_file: def fake_lxc_exec_command(*popenargs, **kwargs): if get_command(popenargs).endswith('lxc') and get_sub_command(popenargs) == 'exec': return subprocess.CompletedProcess("fakerun", 0, '') else: return subprocess.run(*popenargs, **kwargs) monkeypatch.setattr(mockablerun, 'run_mockable', fake_lxc_exec_command) parser = ConfigurationParser(main_file) coordinator = SharedFolderCoordinator(parser) coordinator.verify_container_mountpoints('fake-container')
def test_valid_version(monkeypatch): patch_lxd_get_version(monkeypatch, '3.0.0+bingo') with mocked_executable('lxd', '/here/is/no/lxd'): with clear_lxd_version_check_cache(): LxdVersion.check()