Exemplo n.º 1
0
 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()
Exemplo n.º 2
0
 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()
Exemplo n.º 3
0
 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()
Exemplo n.º 4
0
 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)
Exemplo n.º 5
0
 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()
Exemplo n.º 6
0
    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)
Exemplo n.º 7
0
    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
Exemplo n.º 8
0
 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()
Exemplo n.º 9
0
    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)
Exemplo n.º 10
0
 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)
Exemplo n.º 11
0
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
Exemplo n.º 12
0
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))
Exemplo n.º 13
0
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
Exemplo n.º 14
0
 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
Exemplo n.º 15
0
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}")
Exemplo n.º 16
0
 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)
Exemplo n.º 17
0
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
Exemplo n.º 18
0
 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)
Exemplo n.º 19
0
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)
Exemplo n.º 20
0
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)
Exemplo n.º 21
0
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
Exemplo n.º 22
0
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
Exemplo n.º 23
0
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
Exemplo n.º 24
0
 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))
Exemplo n.º 25
0
def local_ansible_playbook() -> 'AnsiblePlaybook':
    return tobiko.get_fixture(AnsiblePlaybook)
Exemplo n.º 26
0
def undercloud_ansible_playbook() -> UndercloudAnsiblePlaybook:
    return tobiko.get_fixture(UndercloudAnsiblePlaybook)
Exemplo n.º 27
0
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)
Exemplo n.º 28
0
 def test_without_instance(self):
     fixture = tobiko.get_fixture(MyFixtureWithProperty)
     self.assertEqual(id(fixture), MyFixtureWithProperty.some_property)
Exemplo n.º 29
0
 def test_get(self):
     fixture = MyFixture.get()
     self.assertIs(tobiko.get_fixture(MyFixture), fixture)
Exemplo n.º 30
0
def local_shell_connection() -> 'LocalShellConnection':
    return tobiko.get_fixture(LocalShellConnection)