def setUp(self): """ Some initializations are made. * We fetch the gatling host from testinfra + Ansible inventory, * We set the user, * We set some information about the gatling distribution * distribution name, * version, * package name (=distribution + version), * zip name """ # We fetch every hosts that fit in the gatling group in a list gatling_hosts = testinfra.get_hosts([ "ansible://gatling?ansible_inventory=.molecule/ansible_inventory" ] ) # We fetch every hosts that fit in the launcher group in a list gatling_launchers = testinfra.get_hosts([ "ansible://launcher?ansible_inventory=.molecule/ansible_inventory" ] ) # We set some information on the remote environment self.gatling = { "hosts": gatling_hosts, "launcher": { "hosts": gatling_launchers, "script": "gatling_scaling_out.sh", "home": ( lambda: "/" + self.gatling.get("user") + "/" ) }, "user": "******", "distribution": "gatling-charts-highcharts-bundle-", "home": ( lambda: "/" + self.gatling.get("user") + "/" + self.gatling.get("distribution") + self.gatling.get("version") + "/" ), "version": "2.2.5", "package": ( lambda: self.gatling.get("distribution") + self.gatling.get("version") ), "zip": { "name": ( lambda: self.gatling['package']() + '-bundle.zip' ), "md5sum": "b2f3448466b815c29d2d81fe2335503e" }, "simulation": { "name": "write.streaming.StaresRamp", "directory": "/user-files/simulations", "runner_script": "/bin/gatling.sh", "report_directory": "/results/", "reports_directory": "/results/merged_reports/" } }
def pytest_generate_tests(metafunc): if "_testinfra_host" in metafunc.fixturenames: if metafunc.config.option.hosts is not None: hosts = metafunc.config.option.hosts.split(",") elif hasattr(metafunc.module, "testinfra_hosts"): hosts = metafunc.module.testinfra_hosts else: hosts = [None] params = testinfra.get_hosts( hosts, connection=metafunc.config.option.connection, ssh_config=metafunc.config.option.ssh_config, ssh_identity_file=metafunc.config.option.ssh_identity_file, sudo=metafunc.config.option.sudo, sudo_user=metafunc.config.option.sudo_user, ansible_inventory=metafunc.config.option.ansible_inventory, force_ansible=metafunc.config.option.force_ansible, ) params = sorted(params, key=lambda x: x.backend.get_pytest_id()) ids = [e.backend.get_pytest_id() for e in params] metafunc.parametrize("_testinfra_host", params, ids=ids, scope="module", indirect=True)
def test_bundle_running(bundle): '''Verify all members of a bundled resource are running''' controllers = testinfra.get_hosts(['ansible://controller']) count = len(controllers) container_names = ['{}-{}'.format(bundle, i) for i in range(count)] containers = [] for name in container_names: for host in controllers: try: container = host.docker(name) if container.is_running: print(f'found container {name} ' f'on host {host.backend.hostname}') containers.append(container) break except AssertionError: continue else: pytest.fail(f'container {name} is not running on any host') for container in containers: info = container.inspect() if 'Health' in info['State']: assert info['State']['Health']['Status'] == 'healthy'
def test_ansible_no_host(): with tempfile.NamedTemporaryFile() as f: f.write(b'host\n') f.flush() assert AnsibleRunner(f.name).get_hosts() == ['host'] hosts = testinfra.get_hosts( [None], connection='ansible', ansible_inventory=f.name) assert [h.backend.get_pytest_id() for h in hosts] == ['ansible://host'] with tempfile.NamedTemporaryFile() as f: # empty or no inventory should not return any hosts except for # localhost assert AnsibleRunner(f.name).get_hosts() == [] assert AnsibleRunner(f.name).get_hosts('local*') == [] assert AnsibleRunner(f.name).get_hosts('localhost') == ['localhost']
def test_ansible_no_host(): with tempfile.NamedTemporaryFile() as f: f.write(b'host\n') f.flush() assert AnsibleRunner(f.name).get_hosts() == ['host'] hosts = testinfra.get_hosts([None], connection='ansible', ansible_inventory=f.name) assert [h.backend.get_pytest_id() for h in hosts] == ['ansible://host'] with tempfile.NamedTemporaryFile() as f: # empty or no inventory should not return any hosts except for # localhost nohost = ('No inventory was parsed (missing file ?), ' 'only implicit localhost is available') with pytest.raises(RuntimeError) as exc: assert AnsibleRunner(f.name).get_hosts() == [] assert str(exc.value) == nohost with pytest.raises(RuntimeError) as exc: assert AnsibleRunner(f.name).get_hosts('local*') == [] assert str(exc.value) == nohost assert AnsibleRunner(f.name).get_hosts('localhost') == ['localhost']
def pytest_generate_tests(metafunc): if "_testinfra_host" in metafunc.fixturenames: if metafunc.config.option.hosts is not None: hosts = metafunc.config.option.hosts.split(",") elif hasattr(metafunc.module, "testinfra_hosts"): hosts = metafunc.module.testinfra_hosts else: hosts = [None] params = testinfra.get_hosts( hosts, connection=metafunc.config.option.connection, ssh_config=metafunc.config.option.ssh_config, ssh_identity_file=metafunc.config.option.ssh_identity_file, sudo=metafunc.config.option.sudo, sudo_user=metafunc.config.option.sudo_user, ansible_inventory=metafunc.config.option.ansible_inventory, ) params = sorted(params, key=lambda x: x.backend.get_pytest_id()) ids = [e.backend.get_pytest_id() for e in params] metafunc.parametrize( "_testinfra_host", params, ids=ids, scope="module", indirect=True)
def __init__(self, *args, **kwargs): super(MetalSwitchPlaneDeployment, self).__init__(*args, **kwargs) self.hosts = testinfra.get_hosts( ["ssh://mini-lab-leaf01", "ssh://mini-lab-leaf02"])
def setUp(self): # We get every hosts that fit in the gatling group in a list self.gatling_hosts = testinfra.get_hosts([ "ansible://gatling?ansible_inventory=.molecule/ansible_inventory" ])
def pcs_status(): hosts = testinfra.get_hosts(['ansible://controller']) c1 = hosts[0] res = c1.run('pcs status xml') doc = lxml.etree.fromstring(res.stdout) return doc
import testinfra host = testinfra.get_hosts('all') # Files to check traefik_service_content = ''' # https://raw.githubusercontent.com/containous/traefik/master/contrib/systemd/traefik.service [Unit] Description=Traefik Documentation=https://docs.traefik.io AssertFileIsExecutable=/usr/bin/traefik_v2.3.2 AssertPathExists=/etc/traefik/traefik.yaml [Service] Environment="AWS_PROFILE=default" Environment="AWS_ACCESS_KEY_ID=token_access" Environment="AWS_SECRET_ACCESS_KEY=token_secret" Type=notify ExecStart=/usr/bin/traefik_v2.3.2 Restart=always WatchdogSec=1s [Install] WantedBy=multi-user.target ''' global_yaml_content = ''' --- http: middlewares: redirect-http-to-https: redirectScheme: