def _test_remove_fixture(self, obj): fixture = tobiko.get_fixture(obj) result = tobiko.remove_fixture(obj) self.assertIs(fixture, result) self.assertIsNot(fixture, tobiko.get_fixture(obj)) fixture.setup_fixture.assert_not_called() fixture.cleanup_fixture.assert_not_called()
def test_cleanup(self, fixture=MyFixture, fixture2=MyFixture2): stdout = self.test_main(subcommand='cleanup') written_lines = stdout.getvalue().splitlines() for obj in [fixture, fixture2, MyRequiredFixture]: self.assertIn(canonical_name(obj), written_lines) tobiko.get_fixture(obj).setup_fixture.assert_not_called() tobiko.get_fixture(obj).cleanup_fixture.assert_called_once_with()
def test_cleanup_with_filters(self, fixture=MyFixture): stdout = self.test_main(subcommand='cleanup', filters=[self.id()]) written_lines = stdout.getvalue().splitlines() self.assertEqual([canonical_name(fixture), canonical_name(MyRequiredFixture)], written_lines) for obj in [fixture, MyRequiredFixture]: tobiko.get_fixture(obj).setup_fixture.assert_not_called() tobiko.get_fixture(obj).cleanup_fixture.assert_called_once_with()
def test_get_fixture_with_fixture_id_1(self): fixture_0 = tobiko.get_fixture(MyStack) fixture_1 = tobiko.get_fixture(MyStack, fixture_id=1) self.assertIsNot(fixture_0, fixture_1) stack_0 = tobiko.setup_fixture(fixture_0).get_stack() stack_1 = tobiko.setup_fixture(fixture_1).get_stack() self.assertNotEqual(stack_0.id, stack_1.id) self.assertEqual(tobiko.get_fixture_name(MyStack), stack_0.stack_name) self.assertEqual( tobiko.get_fixture_name(MyStack) + '-1', stack_1.stack_name)
def _test_get_fixture(self, obj): fixture = tobiko.get_fixture(obj) self.assertIsInstance(fixture, MyFixture) self.assertIs(fixture, tobiko.get_fixture(obj)) if isinstance(obj, fixtures.Fixture): self.assertIs(obj, fixture) else: self.assertIs(fixture, tobiko.get_fixture(canonical_name(MyFixture))) fixture.setup_fixture.assert_not_called() fixture.cleanup_fixture.assert_not_called()
def _test_use_fixture(self, obj, fixture_id=None): fixture: MyFixture = tobiko.get_fixture( obj=obj, fixture_id=fixture_id) # type: ignore class InnerTest(unittest.TestCase): def runTest(self): fixture.setup_fixture.assert_not_called() fixture.cleanup_fixture.assert_not_called() result = tobiko.use_fixture(obj, fixture_id=fixture_id) fixture.setup_fixture.assert_called_once_with() fixture.cleanup_fixture.assert_not_called() self.assertIs(fixture, result) fixture.setup_fixture.assert_not_called() fixture.cleanup_fixture.assert_not_called() result = tobiko.run_test(InnerTest()) fixture.setup_fixture.assert_called_once_with() fixture.cleanup_fixture.assert_called_once_with() self.assertEqual(1, result.testsRun) self.assertEqual([], result.errors) self.assertEqual([], result.failures)
def test_download_file(self, url: str = None, cached: bool = False, download_dir: str = None, **params) \ -> curl.CurlProcessFixture: if url is None: url = tobiko.get_fixture(stacks.CirrosImageFixture).image_url if download_dir is None: download_dir = sh.make_temp_dir() header = self.test_get_url_header(url=url, location=True) process = curl.download_file(url=url, cached=cached, download_dir=download_dir, **params) try: result = process.wait() except RuntimeError: if not cached: raise else: self.assertIsInstance(result, sh.ShellExecuteResult) self.assertEqual(0, result.exit_status) if process.file_name is not None: self.assertEqual(header.content_length, sh.get_file_size(process.file_name)) return process
def _test_reset_fixture(self, obj, should_clean=True): result = tobiko.reset_fixture(obj) self.assertIs(tobiko.get_fixture(obj), result) result.setup_fixture.assert_called_once_with() if should_clean: result.cleanup_fixture.assert_called_once_with() else: result.cleanup_fixture.assert_not_called()
def _test_get_fixture(self, obj): fixture = tobiko.get_fixture(obj) self.assertIsInstance(fixture, MyFixture) self.assertIs(fixture, tobiko.get_fixture(obj)) if isinstance(obj, fixtures.Fixture): self.assertIs(obj, fixture) else: self.assertIs(fixture, tobiko.get_fixture(canonical_name(MyFixture))) fixture.setup_fixture.assert_not_called() fixture.cleanup_fixture.assert_not_called() for fixture_id in range(2): other = tobiko.get_fixture(obj, fixture_id=fixture_id) if isinstance(obj, fixtures.Fixture) or not fixture_id: self.assertIs(fixture, other) else: self.assertIsNot(fixture, other)
def test_setup(self, credentials=None): session = keystone.KeystoneSessionFixture(credentials=credentials) session.setUp() if tobiko.is_fixture(credentials): credentials = tobiko.get_fixture(credentials) self.assertIs(credentials.credentials, session.credentials) else: self.assertIs(credentials or DEFAULT_CREDENTIALS, session.credentials)
def heat_template(obj, template_files=None): if isinstance(obj, collections.Mapping): template = HeatTemplateFixture(template=obj, template_files=template_files) else: template = tobiko.get_fixture(obj) if not isinstance(template, HeatTemplateFixture): msg = "Object {!r} is not an HeatTemplateFixture".format(template) raise TypeError(msg) return template
def keystone_session(obj): if not obj: return default_keystone_session() if tobiko.is_fixture(obj): obj = tobiko.get_fixture(obj) if isinstance(obj, KeystoneSessionFixture): obj = tobiko.setup_fixture(obj).session if isinstance(obj, _session.Session): return obj raise TypeError("Can't get {!r} object from {!r}".format( _session.Session, obj))
def heat_template(obj: HeatTemplateType, template_files: typing.Mapping = None) \ -> HeatTemplateFixture: if isinstance(obj, abc.Mapping): template = HeatTemplateFixture(template=obj, template_files=template_files) else: template = tobiko.get_fixture(obj) tobiko.check_valid_type(template, HeatTemplateFixture) return template
def test_get_url_header(self, url: str = None, location: bool = None, ssh_client: ssh.SSHClientType = None): if url is None: url = tobiko.get_fixture(stacks.CirrosImageFixture).image_url header = curl.get_url_header(url=url, location=location, ssh_client=ssh_client) self.assertIsInstance(header, curl.CurlHeader) self.assertIn('date', header) return header
def get_keystone_credentials(obj: KeystoneCredentialsType = None) -> \ typing.Optional[KeystoneCredentials]: if obj is None: return default_keystone_credentials() if isinstance(obj, KeystoneCredentials): return obj if tobiko.is_fixture(obj): obj = tobiko.get_fixture(obj) if isinstance(obj, KeystoneCredentialsFixture): obj = tobiko.setup_fixture(obj).credentials return get_keystone_credentials(obj) raise TypeError(f"Can't get {KeystoneCredentials} object from {obj}")
def test_setup(self, session=None): client = self.create_client(session=session) client.setUp() if session: if tobiko.is_fixture(session): if inspect.isclass(session): session = tobiko.get_fixture(session) self.assertIs(session.session, client.session) else: self.assertIs(session, client.session) else: self.assertIs(keystone.get_keystone_session(), client.session)
def heat_stack_parameters(obj, stack=None): if isinstance(obj, HeatStackParametersFixture): parameters = obj elif obj is None or isinstance(obj, collections.Mapping): parameters = HeatStackParametersFixture(stack, obj) else: parameters = tobiko.get_fixture(obj) tobiko.check_valid_type(parameters, HeatStackParametersFixture) if stack: parameters.stack = parameters.stack or stack tobiko.check_valid_type(parameters.stack, type(None), HeatStackFixture) return parameters
def run(cls, after: bool, **params): fixture = tobiko.get_fixture(cls) params.setdefault('passive_checks_only', False) params.setdefault('skip_mac_table_size_test', True) skips = frozenset(k for k, v in params.items() if v) if after or skips < fixture.skips: # Force re-check tobiko.cleanup_fixture(fixture) else: LOG.info("Will skip Overcloud health checks if already " f"executed: {params}") fixture.skips = skips tobiko.setup_fixture(fixture)
def get_keystone_credentials(obj=None): if not obj: return default_keystone_credentials() if tobiko.is_fixture(obj): obj = tobiko.get_fixture(obj) if isinstance(obj, KeystoneCredentialsFixture): obj = tobiko.setup_fixture(obj).credentials if isinstance(obj, KeystoneCredentials): return obj message = "Can't get {!r} object from {!r}".format(KeystoneCredentials, obj) raise TypeError(message)
def heat_client(obj: HeatClientType = None) -> HeatClient: if obj is None: return default_heat_client() if isinstance(obj, v1_client.Client): return obj fixture = tobiko.get_fixture(obj) if isinstance(fixture, HeatClientFixture): return tobiko.setup_fixture(fixture).client message = "Object {!r} is not a NeutronClientFixture".format(obj) raise TypeError(message)
def heat_stack_parameters(obj, stack: 'HeatStackFixture' = None) \ -> 'HeatStackParametersFixture': if isinstance(obj, HeatStackParametersFixture): parameters = obj elif obj is None or isinstance(obj, abc.Mapping): parameters = HeatStackParametersFixture(stack, obj) else: parameters = tobiko.get_fixture(obj) tobiko.check_valid_type(parameters, HeatStackParametersFixture) if stack is not None and parameters.stack is None: parameters.stack = stack tobiko.check_valid_type(parameters.stack, type(None), HeatStackFixture) return parameters
def has_rhel_image() -> bool: image_url = tobiko.get_fixture(RhelImageFixture).image_url try: response = requests.get(image_url, stream=True) except requests.exceptions.ConnectionError as ex: LOG.debug(f'RHEL image file not found at {image_url}: {ex}', exc_info=1) return False if response.status_code == 404: LOG.debug(f'RHEL image file not found at {image_url}') return False response.raise_for_status() LOG.debug(f'RHEL image file found at {image_url}') return True
def test_servers_creation(stack=TestServerCreationStack, number_of_servers=2) -> \ tobiko.Selection[_nova.ServerStackFixture]: initial_servers_ids = {server.id for server in nova.list_servers()} pid = os.getpid() fixture_obj = tobiko.get_fixture_class(stack) # Get list of server stack instances fixtures: tobiko.Selection[_nova.ServerStackFixture] = tobiko.select( tobiko.get_fixture(fixture_obj, fixture_id=f'{pid}-{i}') for i in range(number_of_servers or 1)) test_case = tobiko.get_test_case() # Check fixtures types for fixture in fixtures: test_case.assertIsInstance(fixture, _nova.ServerStackFixture) # Delete all servers stacks for fixture in fixtures: tobiko.cleanup_fixture(fixture) # Create all servers stacks for fixture in fixtures: tobiko.use_fixture(fixture) # Check every server ID is unique and new server_ids = {fixture.server_id for fixture in fixtures} test_case.assertEqual(number_of_servers or 1, len(server_ids)) test_case.assertFalse(server_ids & initial_servers_ids) # sleep for 20 sec , ensure no race condition with ssh time.sleep(20) # Test SSH connectivity to floating IP address for fixture in fixtures: test_case.assertTrue(sh.get_hostname(ssh_client=fixture.ssh_client)) # Test pinging to floating IP address ping.assert_reachable_hosts(fixture.floating_ip_address for fixture in fixtures) return fixtures
def _init_ansible_playbook(ssh_client: ssh.SSHClientFixture = None) \ -> 'AnsiblePlaybook': if ssh_client is None: return local_ansible_playbook() else: return tobiko.get_fixture(AnsiblePlaybook(ssh_client=ssh_client))
def local_ansible_playbook() -> 'AnsiblePlaybook': return tobiko.get_fixture(AnsiblePlaybook)
def undercloud_ansible_playbook() -> UndercloudAnsiblePlaybook: return tobiko.get_fixture(UndercloudAnsiblePlaybook)
def run_confing(obj=None) -> RunConfigFixture: if obj is None: return tobiko.setup_fixture(RunConfigFixture) fixture = tobiko.get_fixture(obj) tobiko.check_valid_type(fixture, RunConfigFixture) return tobiko.setup_fixture(fixture)
def test_without_instance(self): fixture = tobiko.get_fixture(MyFixtureWithProperty) self.assertEqual(id(fixture), MyFixtureWithProperty.some_property)
def test_get(self): fixture = MyFixture.get() self.assertIs(tobiko.get_fixture(MyFixture), fixture)
def local_shell_connection() -> 'LocalShellConnection': return tobiko.get_fixture(LocalShellConnection)