def test_command_init_scenario(temp_dir): with change_dir_to(temp_dir): os.makedirs(os.path.join(temp_dir, "molecule", "default")) scenario_directory = os.path.join(temp_dir, "molecule", "test-scenario") cmd = [ "molecule", "init", "scenario", "test-scenario", "--driver-name", "vagrant", ] result = run_command(cmd) assert result.returncode == 0 assert os.path.isdir(scenario_directory) confpath = os.path.join(scenario_directory, "molecule.yml") conf = util.safe_load_file(confpath) env = os.environ if "TESTBOX" in env: conf["platforms"][0]["box"] = env["TESTBOX"] if not os.path.exists("/dev/kvm"): conf["driver"]["provider"] = {"name": "libvirt"} for p in conf["platforms"]: p["provider_options"] = {"driver": '"qemu"'} util.write_file(confpath, util.safe_dump(conf)) cmd = ["molecule", "--debug", "test", "-s", "test-scenario"] result = run_command(cmd) assert result.returncode == 0
def test_command_init_scenario(temp_dir): role_directory = os.path.join(temp_dir.strpath, "test-init") cmd = ["molecule", "init", "role", "test-init"] result = run_command(cmd) assert result.returncode == 0 with change_dir_to(role_directory): molecule_directory = pytest.helpers.molecule_directory() scenario_directory = os.path.join(molecule_directory, "test-scenario") cmd = [ "molecule", "init", "scenario", "test-scenario", "--role-name", "test-init", "--driver-name", "azure", ] result = run_command(cmd) assert result.returncode == 0 assert os.path.isdir(scenario_directory) # temporary trick to pass on CI/CD if "AZURE_SECRET" in os.environ: cmd = ["molecule", "test", "-s", "test-scenario"] result = run_command(cmd) assert result.returncode == 0
def init_scenario(temp_dir, driver_name): # Create role role_directory = os.path.join(temp_dir.strpath, "test-init") cmd = [ "molecule", "init", "role", "test-init", "--driver-name", driver_name ] assert run_command(cmd).returncode == 0 metadata_lint_update(role_directory) with change_dir_to(role_directory): # Create scenario molecule_dir = molecule_directory() scenario_directory = os.path.join(molecule_dir, "test-scenario") cmd = [ "molecule", "init", "scenario", "test-scenario", "--role-name", "test-init", "--driver-name", driver_name, ] assert run_command(cmd).returncode == 0 assert os.path.isdir(scenario_directory) cmd = ["molecule", "test", "--scenario-name", "test-scenario", "--all"] assert run_command(cmd).returncode == 0
def test_command_init_scenario(temp_dir): role_directory = os.path.join(temp_dir.strpath, "test-init") cmd = ["molecule", "init", "role", "--role-name", "test-init"] assert run_command(cmd).returncode == 0 metadata_lint_update(role_directory) with change_dir_to(role_directory): molecule_directory = pytest.helpers.molecule_directory() scenario_directory = os.path.join(molecule_directory, "test-scenario") cmd = [ "molecule", "init", "scenario", "--scenario-name", "test-scenario", "--role-name", "test-init", "--driver-name", "libvirt", ] assert run_command(cmd).returncode == 0 assert os.path.isdir(scenario_directory) cmd = ["molecule", "test", "-s", "test-scenario"] assert run_command(cmd).returncode == 0
def test_command_init_scenario_custom_template_precedence( temp_dir, resources_folder_path): role_directory = os.path.join(temp_dir.strpath, "test-role") options = {} cmd = sh.molecule.bake("init", "role", "test-role", **options) pytest.helpers.run_command(cmd) pytest.helpers.metadata_lint_update(role_directory) with change_dir_to(role_directory): invalid_template_dir_path = os.path.join(resources_folder_path, "invalid_scenario_template") env = os.environ env.update( {"MOLECULE_SCENARIO_DRIVER_TEMPLATE": invalid_template_dir_path}) custom_template_dir_path = os.path.join(resources_folder_path, "custom_scenario_template") options = { "role_name": "test-role", "driver_template": custom_template_dir_path, } # command line argument takes precedence, or it would fail cmd = sh.molecule.bake("init", "scenario", "test-scenario", **options) pytest.helpers.run_command(cmd, log=False)
def init_scenario(temp_dir, driver_name): # Create role role_directory = os.path.join(temp_dir.strpath, "test-init") cmd = [ "molecule", "init", "role", "test-init", "--driver-name", driver_name ] pytest.helpers.run_command(cmd) pytest.helpers.metadata_lint_update(role_directory) with change_dir_to(role_directory): # Create scenario molecule_directory = pytest.helpers.molecule_directory() scenario_directory = os.path.join(molecule_directory, "test-scenario") cmd = [ "molecule", "init", "scenario", "test-scenario", "--role-name", "test-init", "--driver-name", driver_name, ] pytest.helpers.run_command(cmd) assert os.path.isdir(scenario_directory) cmd = ["molecule", "test", "--scenario-name", "test-scenario", "--all"] pytest.helpers.run_command(cmd)
def test_command_init_scenario_custom_template_precedence( temp_dir, resources_folder_path): role_directory = os.path.join(temp_dir.strpath, 'test-role') options = {'role_name': 'test-role'} cmd = sh.molecule.bake('init', 'role', **options) pytest.helpers.run_command(cmd) pytest.helpers.metadata_lint_update(role_directory) with change_dir_to(role_directory): invalid_template_dir_path = os.path.join(resources_folder_path, 'invalid_scenario_template') env = os.environ env.update( {'MOLECULE_SCENARIO_DRIVER_TEMPLATE': invalid_template_dir_path}) custom_template_dir_path = os.path.join(resources_folder_path, 'custom_scenario_template') options = { 'scenario_name': 'test-scenario', 'role_name': 'test-role', 'driver_template': custom_template_dir_path, } # command line argument takes precedence, or it would fail cmd = sh.molecule.bake('init', 'scenario', **options) pytest.helpers.run_command(cmd, log=False)
def test_command_init_scenario(temp_dir, DRIVER): """Verify that init scenario works.""" role_directory = os.path.join(temp_dir.strpath, "test-init") cmd = ["molecule", "init", "role", "test-init"] result = run_command(cmd) assert result.returncode == 0 with change_dir_to(role_directory): molecule_directory = pytest.helpers.molecule_directory() scenario_directory = os.path.join(molecule_directory, "test-scenario") options = {"role_name": "test-init", "driver-name": DRIVER} cmd = [ "molecule", "init", "scenario", "test-scenario", *util.dict2args(options), ] result = run_command(cmd) assert result.returncode == 0 assert os.path.isdir(scenario_directory) cmd = ["molecule", "--debug", "test", "-s", "test-scenario"] result = run_command(cmd) assert result.returncode == 0
def test_vagrant_root(temp_dir, scenario): options = {"scenario_name": scenario} scenario_directory = os.path.join(os.path.dirname(util.abs_path(__file__)), os.path.pardir, "scenarios") with change_dir_to(scenario_directory): cmd = sh.molecule.bake("test", **options) run_command(cmd)
def init_role(temp_dir, driver_name): role_directory = os.path.join(temp_dir.strpath, "test-init") cmd = ["molecule", "init", "role", "test-init", "--driver-name", driver_name] assert run_command(cmd).returncode == 0 pytest.helpers.metadata_lint_update(role_directory) with change_dir_to(role_directory): cmd = ["molecule", "test", "--all"] assert run_command(cmd).returncode == 0
def test_command_init_role_inspec(temp_dir): role_directory = os.path.join(temp_dir.strpath, 'test-init') options = {'role_name': 'test-init', 'verifier_name': 'inspec'} cmd = sh.molecule.bake('init', 'role', **options) pytest.helpers.run_command(cmd) pytest.helpers.metadata_lint_update(role_directory) with change_dir_to(role_directory): cmd = sh.molecule.bake('test') pytest.helpers.run_command(cmd)
def init_role(temp_dir, driver_name): role_directory = os.path.join(temp_dir.strpath, "test-init") cmd = sh.molecule.bake("init", "role", "test-init", {"driver-name": driver_name}) pytest.helpers.run_command(cmd) pytest.helpers.metadata_lint_update(role_directory) with change_dir_to(role_directory): options = {"all": True} cmd = sh.molecule.bake("test", **options) pytest.helpers.run_command(cmd)
def init_role(temp_dir, driver_name): role_directory = os.path.join(temp_dir.strpath, 'test-init') cmd = sh.molecule.bake('init', 'role', 'test-init', {'driver-name': driver_name}) pytest.helpers.run_command(cmd) pytest.helpers.metadata_lint_update(role_directory) with change_dir_to(role_directory): options = {'all': True} cmd = sh.molecule.bake('test', **options) pytest.helpers.run_command(cmd)
def with_scenario(request, scenario_to_test, driver_name, scenario_name): scenario_directory = os.path.join(os.path.dirname(util.abs_path(__file__)), "scenarios", scenario_to_test) with change_dir_to(scenario_directory): yield if scenario_name: msg = "CLEANUP: Destroying instances for all scenario(s)" LOG.info(msg) cmd = [ "molecule", "destroy", "--driver-name", driver_name, "--all" ] assert run_command(cmd).returncode == 0
def test_vagrant_root(temp_dir, scenario): env = os.environ if not os.path.exists("/dev/kvm"): env.update({"VIRT_DRIVER": "'qemu'"}) scenario_directory = os.path.join(os.path.dirname(util.abs_path(__file__)), os.path.pardir, "scenarios") with change_dir_to(scenario_directory): cmd = ["molecule", "test", "--scenario-name", scenario] result = run_command(cmd) assert result.returncode == 0
def test_command_init_scenario_with_invalid_role_raises(temp_dir): role_directory = os.path.join(temp_dir.strpath, "test-role") options = {} cmd = sh.molecule.bake("init", "role", "test-role", **options) pytest.helpers.run_command(cmd) pytest.helpers.metadata_lint_update(role_directory) with change_dir_to(role_directory): options = {"role_name": "invalid-role-name"} with pytest.raises(sh.ErrorReturnCode) as e: cmd = sh.molecule.bake("init", "scenario", "default", **options) pytest.helpers.run_command(cmd, log=False) msg = ("ERROR: The role 'invalid-role-name' not found. " "Please choose the proper role name.") assert msg in str(e.value.stderr)
def with_scenario(request, scenario_to_test, driver_name, scenario_name, skip_test): scenario_directory = os.path.join( os.path.dirname(util.abs_path(__file__)), os.path.pardir, 'scenarios', scenario_to_test, ) with change_dir_to(scenario_directory): yield if scenario_name: msg = 'CLEANUP: Destroying instances for all scenario(s)' LOG.out(msg) options = {'driver_name': driver_name, 'all': True} cmd = sh.molecule.bake('destroy', **options) pytest.helpers.run_command(cmd)
def test_command_init_scenario_as_default_without_default_scenario(temp_dir): role_directory = os.path.join(temp_dir.strpath, 'test-role') options = {'role_name': 'test-role'} cmd = sh.molecule.bake('init', 'role', **options) pytest.helpers.run_command(cmd) pytest.helpers.metadata_lint_update(role_directory) with change_dir_to(role_directory): molecule_directory = pytest.helpers.molecule_directory() scenario_directory = os.path.join(molecule_directory, 'default') shutil.rmtree(scenario_directory) options = {'scenario_name': 'default', 'role_name': 'test-role'} cmd = sh.molecule.bake('init', 'scenario', **options) pytest.helpers.run_command(cmd) assert os.path.isdir(scenario_directory)
def test_command_init_scenario(temp_dir): role_directory = os.path.join(temp_dir.strpath, "test-init") options = {} cmd = sh.molecule.bake("init", "role", "test-init", **options) run_command(cmd) with change_dir_to(role_directory): molecule_directory = pytest.helpers.molecule_directory() scenario_directory = os.path.join(molecule_directory, "test-scenario") options = {"role_name": "test-init", "driver-name": "vagrant"} cmd = sh.molecule.bake("init", "scenario", "test-scenario", **options) run_command(cmd) assert os.path.isdir(scenario_directory) cmd = sh.molecule.bake("--debug", "test", "-s", "test-scenario") run_command(cmd)
def test_command_init_scenario_as_default_without_default_scenario(temp_dir): role_directory = os.path.join(temp_dir.strpath, "test-role") options = {} cmd = sh.molecule.bake("init", "role", "test-role", **options) pytest.helpers.run_command(cmd) pytest.helpers.metadata_lint_update(role_directory) with change_dir_to(role_directory): molecule_directory = pytest.helpers.molecule_directory() scenario_directory = os.path.join(molecule_directory, "default") shutil.rmtree(scenario_directory) options = {"role_name": "test-role"} cmd = sh.molecule.bake("init", "scenario", "default", **options) pytest.helpers.run_command(cmd) assert os.path.isdir(scenario_directory)
def metadata_lint_update(role_directory): # By default, ansible-lint will fail on newly-created roles because the # fields in this file have not been changed from their defaults. This is # good because molecule should create this file using the defaults, and # users should receive feedback to change these defaults. However, this # blocks the testing of 'molecule init' itself, so ansible-lint should # be configured to ignore these metadata lint errors. ansible_lint_src = os.path.join(os.path.dirname(util.abs_path(__file__)), '.ansible-lint') shutil.copy(ansible_lint_src, role_directory) # Explicitly lint here to catch any unexpected lint errors before # continuining functional testing. Ansible lint is run at the root # of the role directory and pointed at the role directory to ensure # the customize ansible-lint config is used. with change_dir_to(role_directory): cmd = sh.ansible_lint.bake('.') pytest.helpers.run_command(cmd)
def with_scenario(request, scenario_to_test, driver_name, scenario_name, skip_test): scenario_directory = os.path.join( os.path.dirname(util.abs_path(__file__)), os.path.pardir, "scenarios", scenario_to_test, ) with change_dir_to(scenario_directory): yield if scenario_name: msg = "CLEANUP: Destroying instances for all scenario(s)" LOG.out(msg) cmd = [ "molecule", "destroy", "--driver-name", driver_name, "--all" ] pytest.helpers.run_command(cmd)
def test_command_init_scenario_inspec(temp_dir): role_directory = os.path.join(temp_dir.strpath, 'test-init') options = {'role_name': 'test-init'} cmd = sh.molecule.bake('init', 'role', **options) pytest.helpers.run_command(cmd) pytest.helpers.metadata_lint_update(role_directory) with change_dir_to(role_directory): molecule_directory = pytest.helpers.molecule_directory() scenario_directory = os.path.join(molecule_directory, 'test-scenario') options = { 'scenario_name': 'test-scenario', 'role_name': 'test-init', 'verifier_name': 'inspec', } cmd = sh.molecule.bake('init', 'scenario', **options) pytest.helpers.run_command(cmd) assert os.path.isdir(scenario_directory)
def init_scenario(temp_dir, driver_name): # Create role role_directory = os.path.join(temp_dir.strpath, "test-init") cmd = sh.molecule.bake("init", "role", "test-init", {"driver-name": driver_name}) pytest.helpers.run_command(cmd) pytest.helpers.metadata_lint_update(role_directory) with change_dir_to(role_directory): # Create scenario molecule_directory = pytest.helpers.molecule_directory() scenario_directory = os.path.join(molecule_directory, "test-scenario") options = {"role_name": "test-init"} cmd = sh.molecule.bake("init", "scenario", "test-scenario", **options) pytest.helpers.run_command(cmd) assert os.path.isdir(scenario_directory) options = {"scenario_name": "test-scenario", "all": True} cmd = sh.molecule.bake("test", **options) pytest.helpers.run_command(cmd)
def test_command_init_scenario(temp_dir): role_directory = os.path.join(temp_dir.strpath, "test-init") options = {} cmd = sh.molecule.bake("init", "role", "test-init", **options) assert run_command(cmd).returncode == 0 metadata_lint_update(role_directory) with change_dir_to(role_directory): molecule_directory = pytest.helpers.molecule_directory() scenario_directory = os.path.join(molecule_directory, "test-scenario") options = { "role_name": "test-init", "driver-name": "ec2", } cmd = sh.molecule.bake("init", "scenario", "test-scenario", **options) assert run_command(cmd).returncode == 0 assert os.path.isdir(scenario_directory) cmd = sh.molecule.bake("test", "-s", "test-scenario") assert run_command(cmd).returncode == 0
def init_scenario(temp_dir, driver_name): # Create role role_directory = os.path.join(temp_dir.strpath, 'test-init') cmd = sh.molecule.bake('init', 'role', 'test-init', {'driver-name': driver_name}) pytest.helpers.run_command(cmd) pytest.helpers.metadata_lint_update(role_directory) with change_dir_to(role_directory): # Create scenario molecule_directory = pytest.helpers.molecule_directory() scenario_directory = os.path.join(molecule_directory, 'test-scenario') options = {'role_name': 'test-init'} cmd = sh.molecule.bake('init', 'scenario', 'test-scenario', **options) pytest.helpers.run_command(cmd) assert os.path.isdir(scenario_directory) options = {'scenario_name': 'test-scenario', 'all': True} cmd = sh.molecule.bake('test', **options) pytest.helpers.run_command(cmd)
def test_command_init_scenario_without_default_scenario_raises(temp_dir): role_directory = os.path.join(temp_dir.strpath, "test-role") options = {} cmd = sh.molecule.bake("init", "role", "test-role", **options) pytest.helpers.run_command(cmd) pytest.helpers.metadata_lint_update(role_directory) with change_dir_to(role_directory): molecule_directory = pytest.helpers.molecule_directory() scenario_directory = os.path.join(molecule_directory, "default") shutil.rmtree(scenario_directory) options = {"role_name": "test-role"} with pytest.raises(sh.ErrorReturnCode) as e: cmd = sh.molecule.bake("init", "scenario", "invalid-role-name", **options) pytest.helpers.run_command(cmd, log=False) msg = ("The default scenario not found. Please create a scenario " "named 'default' first.") assert msg in str(e.value.stderr)
def test_command_init_scenario(temp_dir): """Verify that we can initialize a new scenario with this driver.""" with change_dir_to(temp_dir): scenario_directory = os.path.join(molecule_directory(), "default") cmd = [ "molecule", "init", "scenario", "default", "--driver-name", "containers", ] result = run_command(cmd) assert result.returncode == 0 assert os.path.isdir(scenario_directory) # we do not run the full "test" sequence because lint will fail, check # is shorter but comprehensive enough to test the most important # functionality: destroy, dependency, create, prepare, converge cmd = ["molecule", "check", "-s", "default"] result = run_command(cmd) assert result.returncode == 0