How to use _env_vars_exposed method in molecule

Best Python code snippet using molecule_python

conftest.py

Source:conftest.py Github

copy

Full Screen

...28from molecule import util29from ..conftest import change_dir_to30LOG = logger.get_logger(__name__)31IS_TRAVIS = os.getenv('TRAVIS') and os.getenv('CI')32def _env_vars_exposed(env_vars, env=os.environ):33 """Check if environment variables are exposed."""34 return all(var in env for var in env_vars)35@pytest.fixture36def with_scenario(request, scenario_to_test, driver_name, scenario_name,37 skip_test):38 scenario_directory = os.path.join(39 os.path.dirname(util.abs_path(__file__)), os.path.pardir, 'scenarios',40 scenario_to_test)41 with change_dir_to(scenario_directory):42 yield43 if scenario_name:44 msg = 'CLEANUP: Destroying instances for all scenario(s)'45 LOG.out(msg)46 options = {47 'driver_name': driver_name,48 'all': True,49 }50 cmd = sh.molecule.bake('destroy', **options)51 pytest.helpers.run_command(cmd)52@pytest.fixture53def skip_test(request, driver_name):54 msg_tmpl = ("Ignoring '{}' tests for now" if driver_name == 'delegated'55 else "Skipped '{}' not supported")56 support_checks_map = {57 'azure': supports_azure,58 'digitalocean': supports_digitalocean,59 'docker': supports_docker,60 'ec2': supports_ec2,61 'gce': supports_gce,62 'linode': supports_linode,63 'lxc': supports_lxc,64 'lxd': supports_lxd,65 'openstack': supports_openstack,66 'vagrant': supports_vagrant_virtualbox,67 'delegated': demands_delegated,68 }69 try:70 check_func = support_checks_map[driver_name]71 if not check_func():72 pytest.skip(msg_tmpl.format(driver_name))73 except KeyError:74 pass75@pytest.helpers.register76def idempotence(scenario_name):77 options = {78 'scenario_name': scenario_name,79 }80 cmd = sh.molecule.bake('create', **options)81 pytest.helpers.run_command(cmd)82 options = {83 'scenario_name': scenario_name,84 }85 cmd = sh.molecule.bake('converge', **options)86 pytest.helpers.run_command(cmd)87 options = {88 'scenario_name': scenario_name,89 }90 cmd = sh.molecule.bake('idempotence', **options)91 pytest.helpers.run_command(cmd)92@pytest.helpers.register93def init_role(temp_dir, driver_name):94 role_directory = os.path.join(temp_dir.strpath, 'test-init')95 cmd = sh.molecule.bake('init', 'role', {96 'driver-name': driver_name,97 'role-name': 'test-init'98 })99 pytest.helpers.run_command(cmd)100 pytest.helpers.metadata_lint_update(role_directory)101 with change_dir_to(role_directory):102 options = {103 'all': True,104 }105 cmd = sh.molecule.bake('test', **options)106 pytest.helpers.run_command(cmd)107@pytest.helpers.register108def init_scenario(temp_dir, driver_name):109 # Create role110 role_directory = os.path.join(temp_dir.strpath, 'test-init')111 cmd = sh.molecule.bake('init', 'role', {112 'driver-name': driver_name,113 'role-name': 'test-init'114 })115 pytest.helpers.run_command(cmd)116 pytest.helpers.metadata_lint_update(role_directory)117 with change_dir_to(role_directory):118 # Create scenario119 molecule_directory = pytest.helpers.molecule_directory()120 scenario_directory = os.path.join(molecule_directory, 'test-scenario')121 options = {122 'scenario_name': 'test-scenario',123 'role_name': 'test-init',124 }125 cmd = sh.molecule.bake('init', 'scenario', **options)126 pytest.helpers.run_command(cmd)127 assert os.path.isdir(scenario_directory)128 options = {129 'scenario_name': 'test-scenario',130 'all': True,131 }132 cmd = sh.molecule.bake('test', **options)133 pytest.helpers.run_command(cmd)134@pytest.helpers.register135def metadata_lint_update(role_directory):136 # By default, ansible-lint will fail on newly-created roles because the137 # fields in this file have not been changed from their defaults. This is138 # good because molecule should create this file using the defaults, and139 # users should receive feedback to change these defaults. However, this140 # blocks the testing of 'molecule init' itself, so ansible-lint should141 # be configured to ignore these metadata lint errors.142 ansible_lint_src = os.path.join(143 os.path.dirname(util.abs_path(__file__)), '.ansible-lint')144 shutil.copy(ansible_lint_src, role_directory)145 # Explicitly lint here to catch any unexpected lint errors before146 # continuining functional testing. Ansible lint is run at the root147 # of the role directory and pointed at the role directory to ensure148 # the customize ansible-lint config is used.149 with change_dir_to(role_directory):150 cmd = sh.ansible_lint.bake('.')151 pytest.helpers.run_command(cmd)152@pytest.helpers.register153def list(x):154 cmd = sh.molecule.bake('list')155 out = pytest.helpers.run_command(cmd, log=False)156 out = out.stdout.decode('utf-8')157 out = util.strip_ansi_color(out)158 for l in x.splitlines():159 assert l in out160@pytest.helpers.register161def list_with_format_plain(x):162 cmd = sh.molecule.bake('list', {'format': 'plain'})163 out = pytest.helpers.run_command(cmd, log=False)164 out = out.stdout.decode('utf-8')165 out = util.strip_ansi_color(out)166 for l in x.splitlines():167 assert l in out168@pytest.helpers.register169def login(login_args, scenario_name='default'):170 options = {171 'scenario_name': scenario_name,172 }173 cmd = sh.molecule.bake('destroy', **options)174 pytest.helpers.run_command(cmd)175 options = {176 'scenario_name': scenario_name,177 }178 cmd = sh.molecule.bake('create', **options)179 pytest.helpers.run_command(cmd)180 for instance, regexp in login_args:181 if len(login_args) > 1:182 child_cmd = 'molecule login --host {} --scenario-name {}'.format(183 instance, scenario_name)184 else:185 child_cmd = 'molecule login --scenario-name {}'.format(186 scenario_name)187 child = pexpect.spawn(child_cmd)188 child.expect(regexp)189 # If the test returns and doesn't hang it succeeded.190 child.sendline('exit')191@pytest.helpers.register192def test(driver_name, scenario_name='default'):193 options = {194 'scenario_name': scenario_name,195 'all': scenario_name is None,196 }197 if driver_name == 'delegated':198 options = {199 'scenario_name': scenario_name,200 }201 cmd = sh.molecule.bake('test', **options)202 pytest.helpers.run_command(cmd)203@pytest.helpers.register204def verify(scenario_name='default'):205 options = {206 'scenario_name': scenario_name,207 }208 cmd = sh.molecule.bake('create', **options)209 pytest.helpers.run_command(cmd)210 options = {211 'scenario_name': scenario_name,212 }213 cmd = sh.molecule.bake('converge', **options)214 pytest.helpers.run_command(cmd)215 options = {216 'scenario_name': scenario_name,217 }218 cmd = sh.molecule.bake('verify', **options)219 pytest.helpers.run_command(cmd)220def get_docker_executable():221 return distutils.spawn.find_executable('docker')222def get_lxc_executable():223 return distutils.spawn.find_executable('lxc-start')224def get_lxd_executable():225 return distutils.spawn.find_executable('lxd')226def get_vagrant_executable():227 return distutils.spawn.find_executable('vagrant')228def get_virtualbox_executable():229 return distutils.spawn.find_executable('VBoxManage')230@pytest.helpers.register231def supports_docker():232 return get_docker_executable()233@pytest.helpers.register234def supports_linode():235 from ansible.modules.cloud.linode.linode import HAS_LINODE236 env_vars = ('LINODE_API_KEY', )237 return _env_vars_exposed(env_vars) and HAS_LINODE238@pytest.helpers.register239def supports_lxc():240 # noqa: E501 # FIXME: Travis CI241 # noqa: E501 # This fixes most of the errors:242 # noqa: E501 # $ mkdir -p ~/.config/lxc243 # noqa: E501 # $ echo "lxc.id_map = u 0 100000 65536" > ~/.config/lxc/default.conf244 # noqa: E501 # $ echo "lxc.id_map = g 0 100000 65536" >> ~/.config/lxc/default.conf245 # noqa: E501 # $ echo "lxc.network.type = veth" >> ~/.config/lxc/default.conf246 # noqa: E501 # $ echo "lxc.network.link = lxcbr0" >> ~/.config/lxc/default.conf247 # noqa: E501 # $ echo "$USER veth lxcbr0 2" | sudo tee -a /etc/lxc/lxc-usernet248 # noqa: E501 # travis veth lxcbr0 2249 # noqa: E501 # But there's still one left:250 # noqa: E501 # $ cat ~/lxc-instance.log251 # noqa: E501 # lxc-create 1542112494.884 INFO lxc_utils - utils.c:get_rundir:229 - XDG_RUNTIME_DIR isn't set in the environment.252 # noqa: E501 # lxc-create 1542112494.884 WARN lxc_log - log.c:lxc_log_init:331 - lxc_log_init called with log already initialized253 # noqa: E501 # lxc-create 1542112494.884 INFO lxc_confile - confile.c:config_idmap:1385 - read uid map: type u nsid 0 hostid 100000 range 65536254 # noqa: E501 # lxc-create 1542112494.884 INFO lxc_confile - confile.c:config_idmap:1385 - read uid map: type g nsid 0 hostid 100000 range 65536255 # noqa: E501 # lxc-create 1542112494.887 ERROR lxc_container - lxccontainer.c:do_create_container_dir:767 - Failed to chown container dir256 # noqa: E501 # lxc-create 1542112494.887 ERROR lxc_create_ui - lxc_create.c:main:274 - Error creating container instance257 return not IS_TRAVIS and get_lxc_executable()258@pytest.helpers.register259def supports_lxd():260 # FIXME: Travis CI261 return not IS_TRAVIS and get_lxd_executable()262@pytest.helpers.register263def supports_vagrant_virtualbox():264 return (get_vagrant_executable() or get_virtualbox_executable())265@pytest.helpers.register266def demands_delegated():267 return pytest.config.getoption('--delegated')268@pytest.helpers.register269def supports_azure():270 from ansible.module_utils.azure_rm_common import HAS_AZURE271 env_vars = (272 'AZURE_SUBSCRIPTION_ID',273 'AZURE_CLIENT_ID',274 'AZURE_SECRET',275 'AZURE_TENANT',276 )277 return _env_vars_exposed(env_vars) and HAS_AZURE278@pytest.helpers.register279def supports_digitalocean():280 from ansible.modules.cloud.digital_ocean.digital_ocean import HAS_DOPY281 env_vars = ('DO_API_KEY', )282 return _env_vars_exposed(env_vars) and HAS_DOPY283@pytest.helpers.register284def supports_ec2():285 from ansible.module_utils.ec2 import HAS_BOTO3286 env_vars = (287 'AWS_ACCESS_KEY',288 'AWS_SECRET_ACCESS_KEY',289 )290 return _env_vars_exposed(env_vars) and HAS_BOTO3291@pytest.helpers.register292def supports_gce():293 from ansible.module_utils.gcp import HAS_GOOGLE_AUTH294 env_vars = (295 'GCE_SERVICE_ACCOUNT_EMAIL',296 'GCE_CREDENTIALS_FILE',297 'GCE_PROJECT_ID',298 )299 return _env_vars_exposed(env_vars) and HAS_GOOGLE_AUTH300@pytest.helpers.register301def supports_openstack():302 pytest.importorskip('shade') # Ansible provides no import303 env_vars = (304 'OS_AUTH_URL',305 'OS_PASSWORD',306 'OS_REGION_NAME',307 'OS_USERNAME',308 'OS_TENANT_NAME',309 )310 return _env_vars_exposed(env_vars)311@pytest.helpers.register312def has_inspec():313 return distutils.spawn.find_executable('inspec')314@pytest.helpers.register315def has_rubocop():316 return distutils.spawn.find_executable('rubocop')317needs_inspec = pytest.mark.skipif(318 not has_inspec(),319 reason='Needs inspec to be pre-installed and available in $PATH')320needs_rubocop = pytest.mark.skipif(321 not has_rubocop(),...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run molecule automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful