def test_httperror_initial_attrs(self, arg_names, args):
        """Test initial attributes of HTTPError."""

        body = args[0]
        posargs, kwargs = func_args(args, arg_names)

        # Execute the code to be tested
        exc = HTTPError(*posargs, **kwargs)

        assert isinstance(exc, Error)
        assert len(exc.args) == 1
        assert exc.args[0] == body.get('message', None)
        assert exc.http_status == body.get('http-status', None)
        assert exc.reason == body.get('reason', None)
        assert exc.message == body.get('message', None)
        assert exc.request_method == body.get('request-method', None)
        assert exc.request_uri == body.get('request-uri', None)
        assert exc.request_query_parms == body.get('request-query-parms', None)
        assert exc.request_headers == body.get('request-headers', None)
        assert exc.request_authenticated_as == \
            body.get('request-authenticated-as', None)
        assert exc.request_body == body.get('request-body', None)
        assert exc.request_body_as_string == \
            body.get('request-body-as-string', None)
        assert exc.request_body_as_string_partial == \
            body.get('request-body-as-string-partial', None)
        assert exc.stack == body.get('stack', None)
        assert exc.error_details == body.get('error-details', None)
    def test_httperror_str(self, body):
        """All tests for HTTPError.__str__()."""

        exc = HTTPError(body)

        exp_str = "{http-status},{reason}: {message} [{request-method} "\
                  "{request-uri}]".format(**body)

        # Execute the code to be tested
        str_str = str(exc)

        assert str_str == exp_str
    def test_httperror_repr(self, body):
        """All tests for HTTPError.__repr__()."""

        exc = HTTPError(body)

        classname = exc.__class__.__name__

        # Execute the code to be tested
        repr_str = repr(exc)

        # We check the one-lined string just roughly
        repr_str = repr_str.replace('\n', '\\n')
        assert re.match(r'^{}\s*\(.*\)$'.format(classname), repr_str)
Пример #4
0
def test_httperror_str_def(body):
    """All tests for HTTPError.str_def()."""

    exc = HTTPError(body)

    classname = exc.__class__.__name__
    request_method = exc.request_method
    request_uri = exc.request_uri
    http_status = exc.http_status
    reason = exc.reason
    msg = exc.message

    # Execute the code to be tested
    str_def = exc.str_def()

    str_def = ' ' + str_def
    assert str_def.find(' classname={!r};'.format(classname)) >= 0
    assert str_def.find(' request_method={!r};'.format(request_method)) >= 0
    assert str_def.find(' request_uri={!r};'.format(request_uri)) >= 0
    assert str_def.find(' http_status={!r};'.format(http_status)) >= 0
    assert str_def.find(' reason={!r};'.format(reason)) >= 0
    assert str_def.find(' message={!r};'.format(msg)) >= 0
Пример #5
0
class TestHba(object):
    """All tests for Hba and HbaManager classes."""
    def setup_method(self):
        """
        Set up a faked session, and add a faked CPC in DPM mode with one
        partition that has no HBAs.
        Add one FCP adapter and port.
        """

        self.session = FakedSession('fake-host', 'fake-hmc', '2.13.1', '1.8')
        self.client = Client(self.session)

        # Add a CPC in DPM mode
        self.faked_cpc = self.session.hmc.cpcs.add({
            'element-id': 'fake-cpc1-oid',
            # element-uri is set up automatically
            'parent': None,
            'class': 'cpc',
            'name': 'fake-cpc1-name',
            'description': 'CPC #1 (DPM mode)',
            'status': 'active',
            'dpm-enabled': True,
            'is-ensemble-member': False,
            'iml-mode': 'dpm',
        })
        self.cpc = self.client.cpcs.find(name='fake-cpc1-name')

        # Add a partition to the CPC
        self.faked_partition = self.faked_cpc.partitions.add({
            'element-id':
            'fake-part1-oid',
            # element-uri will be automatically set
            'parent':
            self.faked_cpc.uri,
            'class':
            'partition',
            'name':
            'fake-part1-name',
            'description':
            'Partition #1',
            'status':
            'active',
            'initial-memory':
            1024,
            'maximum-memory':
            2048,
        })
        self.partition = self.cpc.partitions.find(name='fake-part1-name')

        # Add an FCP adapter and port to the CPC
        self.faked_fcp1 = self.faked_cpc.adapters.add({
            'object-id':
            FCP1_OID,
            'parent':
            self.faked_cpc.uri,
            'class':
            'adapter',
            'name':
            'fcp1',
            'description':
            'FCP #1',
            'status':
            'active',
            'type':
            'fcp',
            'adapter-id':
            '123',
            'detected-card-type':
            '10gbe-roce-express',
            'card-location':
            '1234-5678-J.01',
            'port-count':
            1,
            'network-port-uris': [],
            'state':
            'online',
            'configured-capacity':
            80,
            'used-capacity':
            0,
            'allowed-capacity':
            80,
            'maximum-total-capacity':
            80,
            'physical-channel-status':
            'operating',
        })
        self.faked_port11 = self.faked_fcp1.ports.add({
            'element-id':
            PORT11_OID,
            'parent':
            self.faked_fcp1.uri,
            'class':
            'storage-port',
            'index':
            1,
            'name':
            'fake-port11-name',
            'description':
            'FCP #1 Port #1',
        })
        assert PORT11_URI == self.faked_port11.uri

    def add_hba1(self):
        """Add a faked HBA 1 to the faked partition."""
        faked_hba = self.faked_partition.hbas.add({
            'element-id':
            HBA1_OID,
            # element-uri will be automatically set
            'parent':
            self.faked_partition.uri,
            'class':
            'hba',
            'name':
            HBA1_NAME,
            'description':
            'HBA ' + HBA1_NAME,
            'adapter-port-uri':
            PORT11_URI,
            'wwpn':
            'AABBCCDDEEFF0011',
            'device-number':
            '1111',
        })
        return faked_hba

    def add_hba2(self):
        """Add a faked HBA 2 to the faked partition."""
        faked_hba = self.faked_partition.hbas.add({
            'element-id':
            HBA2_OID,
            # element-uri will be automatically set
            'parent':
            self.faked_partition.uri,
            'class':
            'hba',
            'name':
            HBA2_NAME,
            'description':
            'HBA ' + HBA2_NAME,
            'adapter-port-uri':
            PORT11_URI,
            'wwpn':
            'AABBCCDDEEFF0012',
            'device-number':
            '1112',
        })
        return faked_hba

    def test_hbamanager_initial_attrs(self):
        """Test initial attributes of HbaManager."""

        hba_mgr = self.partition.hbas

        # Verify all public properties of the manager object
        assert hba_mgr.resource_class == Hba
        assert hba_mgr.session == self.session
        assert hba_mgr.parent == self.partition
        assert hba_mgr.partition == self.partition

    # TODO: Test for HbaManager.__repr__()

    @pytest.mark.parametrize("full_properties_kwargs, prop_names", [
        (dict(), ['element-uri']),
        (dict(full_properties=False), ['element-uri']),
        (dict(full_properties=True), None),
    ])
    def test_hbamanager_list_full_properties(self, full_properties_kwargs,
                                             prop_names):
        """Test HbaManager.list() with full_properties."""

        # Add two faked HBAs
        faked_hba1 = self.add_hba1()
        faked_hba2 = self.add_hba2()

        exp_faked_hbas = [faked_hba1, faked_hba2]
        hba_mgr = self.partition.hbas

        # Execute the code to be tested
        hbas = hba_mgr.list(**full_properties_kwargs)

        assert_resources(hbas, exp_faked_hbas, prop_names)

    @pytest.mark.parametrize("filter_args, exp_oids", [
        ({
            'element-id': HBA1_OID
        }, [HBA1_OID]),
        ({
            'element-id': HBA2_OID
        }, [HBA2_OID]),
        ({
            'element-id': [HBA1_OID, HBA2_OID]
        }, [HBA1_OID, HBA2_OID]),
        ({
            'element-id': [HBA1_OID, HBA1_OID]
        }, [HBA1_OID]),
        ({
            'element-id': HBA1_OID + 'foo'
        }, []),
        ({
            'element-id': [HBA1_OID, HBA2_OID + 'foo']
        }, [HBA1_OID]),
        ({
            'element-id': [HBA2_OID + 'foo', HBA1_OID]
        }, [HBA1_OID]),
        ({
            'name': HBA1_NAME
        }, [HBA1_OID]),
        ({
            'name': HBA2_NAME
        }, [HBA2_OID]),
        ({
            'name': [HBA1_NAME, HBA2_NAME]
        }, [HBA1_OID, HBA2_OID]),
        ({
            'name': HBA1_NAME + 'foo'
        }, []),
        ({
            'name': [HBA1_NAME, HBA2_NAME + 'foo']
        }, [HBA1_OID]),
        ({
            'name': [HBA2_NAME + 'foo', HBA1_NAME]
        }, [HBA1_OID]),
        ({
            'name': [HBA1_NAME, HBA1_NAME]
        }, [HBA1_OID]),
        ({
            'name': '.*hba 1'
        }, [HBA1_OID]),
        ({
            'name': 'hba 1.*'
        }, [HBA1_OID]),
        ({
            'name': 'hba .'
        }, [HBA1_OID, HBA2_OID]),
        ({
            'name': '.ba 1'
        }, [HBA1_OID]),
        ({
            'name': '.+'
        }, [HBA1_OID, HBA2_OID]),
        ({
            'name': 'hba 1.+'
        }, []),
        ({
            'name': '.+hba 1'
        }, []),
        ({
            'name': HBA1_NAME,
            'element-id': HBA1_OID
        }, [HBA1_OID]),
        ({
            'name': HBA1_NAME,
            'element-id': HBA1_OID + 'foo'
        }, []),
        ({
            'name': HBA1_NAME + 'foo',
            'element-id': HBA1_OID
        }, []),
        ({
            'name': HBA1_NAME + 'foo',
            'element-id': HBA1_OID + 'foo'
        }, []),
    ])
    def test_hbamanager_list_filter_args(self, filter_args, exp_oids):
        """Test HbaManager.list() with filter_args."""

        # Add two faked HBAs
        self.add_hba1()
        self.add_hba2()

        hba_mgr = self.partition.hbas

        # Execute the code to be tested
        hbas = hba_mgr.list(filter_args=filter_args)

        assert len(hbas) == len(exp_oids)
        if exp_oids:
            oids = [hba.properties['element-id'] for hba in hbas]
            assert set(oids) == set(exp_oids)

    @pytest.mark.parametrize("initial_partition_status, exp_status_exc", [
        ('stopped', None),
        ('terminated', None),
        ('starting', HTTPError({
            'http-status': 409,
            'reason': 1
        })),
        ('active', None),
        ('stopping', HTTPError({
            'http-status': 409,
            'reason': 1
        })),
        ('degraded', None),
        ('reservation-error', None),
        ('paused', None),
    ])
    @pytest.mark.parametrize("input_props, exp_prop_names, exp_prop_exc", [
        ({}, None, HTTPError({
            'http-status': 400,
            'reason': 5
        })),
        ({
            'name': 'fake-hba-x'
        }, None, HTTPError({
            'http-status': 400,
            'reason': 5
        })),
        ({
            'adapter-port-uri': PORT11_URI
        }, None, HTTPError({
            'http-status': 400,
            'reason': 5
        })),
        ({
            'name': 'fake-hba-x',
            'adapter-port-uri': PORT11_URI
        }, ['element-uri', 'name', 'adapter-port-uri'], None),
    ])
    def test_hbamanager_create(self, input_props, exp_prop_names, exp_prop_exc,
                               initial_partition_status, exp_status_exc):
        """Test HbaManager.create()."""

        # Set the status of the faked partition
        self.faked_partition.properties['status'] = initial_partition_status

        hba_mgr = self.partition.hbas

        if exp_status_exc:
            exp_exc = exp_status_exc
        elif exp_prop_exc:
            exp_exc = exp_prop_exc
        else:
            exp_exc = None

        if exp_exc:

            with pytest.raises(exp_exc.__class__) as exc_info:

                # Execute the code to be tested
                hba = hba_mgr.create(properties=input_props)

            exc = exc_info.value
            if isinstance(exp_exc, HTTPError):
                assert exc.http_status == exp_exc.http_status
                assert exc.reason == exp_exc.reason

        else:

            # Execute the code to be tested.
            # Note: the Hba object returned by Hba.create() has
            # the input properties plus 'element-uri' plus 'element-id'.
            hba = hba_mgr.create(properties=input_props)

            # Check the resource for consistency within itself
            assert isinstance(hba, Hba)
            hba_name = hba.name
            exp_hba_name = hba.properties['name']
            assert hba_name == exp_hba_name
            hba_uri = hba.uri
            exp_hba_uri = hba.properties['element-uri']
            assert hba_uri == exp_hba_uri

            # Check the properties against the expected names and values
            for prop_name in exp_prop_names:
                assert prop_name in hba.properties
                if prop_name in input_props:
                    value = hba.properties[prop_name]
                    exp_value = input_props[prop_name]
                    assert value == exp_value

    def test_hba_repr(self):
        """Test Hba.__repr__()."""

        # Add a faked hba
        faked_hba = self.add_hba1()

        hba_mgr = self.partition.hbas
        hba = hba_mgr.find(name=faked_hba.name)

        # Execute the code to be tested
        repr_str = repr(hba)

        repr_str = repr_str.replace('\n', '\\n')
        # We check just the begin of the string:
        assert re.match(
            r'^{classname}\s+at\s+0x{id:08x}\s+\(\\n.*'.format(
                classname=hba.__class__.__name__, id=id(hba)), repr_str)

    @pytest.mark.parametrize("initial_partition_status, exp_exc", [
        ('stopped', None),
        ('terminated', None),
        ('starting', HTTPError({
            'http-status': 409,
            'reason': 1
        })),
        ('active', None),
        ('stopping', HTTPError({
            'http-status': 409,
            'reason': 1
        })),
        ('degraded', None),
        ('reservation-error', None),
        ('paused', None),
    ])
    def test_hba_delete(self, initial_partition_status, exp_exc):
        """Test Hba.delete()."""

        # Add a faked HBA to be tested and another one
        faked_hba = self.add_hba1()
        self.add_hba2()

        # Set the status of the faked partition
        self.faked_partition.properties['status'] = initial_partition_status

        hba_mgr = self.partition.hbas

        hba = hba_mgr.find(name=faked_hba.name)

        if exp_exc:

            with pytest.raises(exp_exc.__class__) as exc_info:

                # Execute the code to be tested
                hba.delete()

            exc = exc_info.value
            if isinstance(exp_exc, HTTPError):
                assert exc.http_status == exp_exc.http_status
                assert exc.reason == exp_exc.reason

            # Check that the HBA still exists
            hba_mgr.find(name=faked_hba.name)

        else:

            # Execute the code to be tested.
            hba.delete()

            # Check that the HBA no longer exists
            with pytest.raises(NotFound) as exc_info:
                hba_mgr.find(name=faked_hba.name)

    def test_hba_delete_create_same_name(self):
        """Test Hba.delete() followed by Hba.create() with same name."""

        # Add a faked HBA to be tested and another one
        faked_hba = self.add_hba1()
        hba_name = faked_hba.name
        self.add_hba2()

        # Construct the input properties for a third HBA with same name
        part3_props = copy.deepcopy(faked_hba.properties)
        part3_props['description'] = 'Third HBA'

        # Set the status of the faked partition
        self.faked_partition.properties['status'] = 'stopped'  # deletable

        hba_mgr = self.partition.hbas
        hba = hba_mgr.find(name=hba_name)

        # Execute the deletion code to be tested.
        hba.delete()

        # Check that the HBA no longer exists
        with pytest.raises(NotFound):
            hba_mgr.find(name=hba_name)

        # Execute the creation code to be tested.
        hba_mgr.create(part3_props)

        # Check that the HBA exists again under that name
        hba3 = hba_mgr.find(name=hba_name)
        description = hba3.get_property('description')
        assert description == 'Third HBA'

    @pytest.mark.parametrize("input_props", [
        {},
        {
            'description': 'New HBA description'
        },
        {
            'device-number': 'FEDC',
            'description': 'New HBA description'
        },
    ])
    def test_hba_update_properties(self, input_props):
        """Test Hba.update_properties()."""

        # Add a faked HBA
        faked_hba = self.add_hba1()

        # Set the status of the faked partition
        self.faked_partition.properties['status'] = 'stopped'  # updatable

        hba_mgr = self.partition.hbas
        hba = hba_mgr.find(name=faked_hba.name)

        hba.pull_full_properties()
        saved_properties = copy.deepcopy(hba.properties)

        # Execute the code to be tested
        hba.update_properties(properties=input_props)

        # Verify that the resource object already reflects the property
        # updates.
        for prop_name in saved_properties:
            if prop_name in input_props:
                exp_prop_value = input_props[prop_name]
            else:
                exp_prop_value = saved_properties[prop_name]
            assert prop_name in hba.properties
            prop_value = hba.properties[prop_name]
            assert prop_value == exp_prop_value

        # Refresh the resource object and verify that the resource object
        # still reflects the property updates.
        hba.pull_full_properties()
        for prop_name in saved_properties:
            if prop_name in input_props:
                exp_prop_value = input_props[prop_name]
            else:
                exp_prop_value = saved_properties[prop_name]
            assert prop_name in hba.properties
            prop_value = hba.properties[prop_name]
            assert prop_value == exp_prop_value

    def test_hba_update_name(self):
        """Test Hba.update_properties() with 'name' property."""

        # Add a faked HBA
        faked_hba = self.add_hba1()
        hba_name = faked_hba.name

        # Set the status of the faked partition
        self.faked_partition.properties['status'] = 'stopped'  # updatable

        hba_mgr = self.partition.hbas
        hba = hba_mgr.find(name=hba_name)

        new_hba_name = "new-" + hba_name

        # Execute the code to be tested
        hba.update_properties(properties={'name': new_hba_name})

        # Verify that the resource is no longer found by its old name, using
        # list() (this does not use the name-to-URI cache).
        hbas_list = hba_mgr.list(filter_args=dict(name=hba_name))
        assert len(hbas_list) == 0

        # Verify that the resource is no longer found by its old name, using
        # find() (this uses the name-to-URI cache).
        with pytest.raises(NotFound):
            hba_mgr.find(name=hba_name)

        # Verify that the resource object already reflects the update, even
        # though it has not been refreshed yet.
        assert hba.properties['name'] == new_hba_name

        # Refresh the resource object and verify that it still reflects the
        # update.
        hba.pull_full_properties()
        assert hba.properties['name'] == new_hba_name

        # Verify that the resource can be found by its new name, using find()
        new_hba_find = hba_mgr.find(name=new_hba_name)
        assert new_hba_find.properties['name'] == new_hba_name

        # Verify that the resource can be found by its new name, using list()
        new_hbas_list = hba_mgr.list(filter_args=dict(name=new_hba_name))
        assert len(new_hbas_list) == 1
        new_hba_list = new_hbas_list[0]
        assert new_hba_list.properties['name'] == new_hba_name

    @pytest.mark.parametrize("initial_partition_status, exp_exc", [
        ('stopped', None),
        ('terminated', None),
        ('starting', HTTPError({
            'http-status': 409,
            'reason': 1
        })),
        ('active', None),
        ('stopping', HTTPError({
            'http-status': 409,
            'reason': 1
        })),
        ('degraded', None),
        ('reservation-error', None),
        ('paused', None),
    ])
    def test_hba_reassign_port(self, initial_partition_status, exp_exc):
        """Test Hba.reassign_port()."""

        # Add a faked HBA to be tested.
        # Its port points to a faked URI.
        faked_hba = self.add_hba1()

        # Add a faked FCP with one port that the HBA will be reassigned to
        faked_adapter = self.faked_cpc.adapters.add({
            'object-id':
            'fake-fcp1-oid',
            # object-uri is auto-set based upon object-id
            'parent':
            self.faked_cpc.uri,
            'class':
            'adapter',
            'name':
            'fake-fcp1',
            'description':
            'FCP #1',
            'status':
            'active',
            'type':
            'fcp',
            # adapter-family is auto-set based upon type
            'adapter-id':
            '123',
            'detected-card-type':
            'ficon-express-16s',
            'card-location':
            '1234-5678-J.01',
            'port-count':
            1,
            'storage-port-uris': [],
            'state':
            'online',
            'configured-capacity':
            80,
            'used-capacity':
            0,
            'allowed-capacity':
            80,
            'maximum-total-capacity':
            80,
            'channel-path-id':
            '1B',
            'physical-channel-status':
            'operating',
        })
        adapter = self.cpc.adapters.find(name='fake-fcp1')
        faked_adapter.ports.add({
            'element-id': 'fake-port1-oid',
            # element-uri is auto-set based upon object-id
            'parent': faked_adapter.uri,
            'class': 'storage-port',
            'name': 'fake-port1',
            'description': 'FCP #1 Port 1',
            'index': 0,
            'fabric-id': None,
        })
        port = adapter.ports.find(name='fake-port1')

        # Set the status of the faked partition
        self.faked_partition.properties['status'] = initial_partition_status

        # The HBA object we will perform the test on
        hba = self.partition.hbas.find(name=faked_hba.name)

        # Save the HBA properties for later comparison
        hba.pull_full_properties()
        saved_properties = copy.deepcopy(hba.properties)

        if exp_exc:

            with pytest.raises(exp_exc.__class__) as exc_info:

                # Execute the code to be tested
                hba.reassign_port(port)

            exc = exc_info.value
            if isinstance(exp_exc, HTTPError):
                assert exc.http_status == exp_exc.http_status
                assert exc.reason == exp_exc.reason

            # Check that the port of the HBA is unchanged ...
            prop_name = 'adapter-port-uri'
            # ... in the resource object:
            assert hba.properties[prop_name] == saved_properties[prop_name]
            # ... and again when refreshed from the mock state:
            hba.pull_full_properties()
            assert hba.properties[prop_name] == saved_properties[prop_name]

        else:

            # Execute the code to be tested.
            hba.reassign_port(port)

            # Check that the port of the HBA has been set ...
            # ... in the resource object:
            prop_name = 'adapter-port-uri'
            assert hba.properties[prop_name] == port.uri
            # ... and again when refreshed from the mock state:
            hba.pull_full_properties()
            assert hba.properties[prop_name] == port.uri
class TestLdapServerDefinition(object):
    """All tests for the LdapServerDefinition and LdapServerDefinitionManager
    classes."""

    def setup_method(self):
        """
        Set up a faked session, and add a faked Console without any
        child resources.
        """

        self.session = FakedSession('fake-host', 'fake-hmc', '2.13.1', '1.8')
        self.client = Client(self.session)

        self.faked_console = self.session.hmc.consoles.add({
            'object-id': None,
            # object-uri will be automatically set
            'parent': None,
            'class': 'console',
            'name': 'fake-console1',
            'description': 'Console #1',
        })
        self.console = self.client.consoles.find(name=self.faked_console.name)

    def add_ldap_srv_def(self, name):
        faked_ldap_srv_def = self.faked_console.ldap_server_definitions.add({
            'element-id': 'oid-{}'.format(name),
            # element-uri will be automatically set
            'parent': '/api/console',
            'class': 'ldap-server-definition',
            'name': name,
            'description': 'LDAP Server Definition {}'.format(name),
            'primary-hostname-ipaddr': 'host-{}'.format(name),
        })
        return faked_ldap_srv_def

    def test_ldap_srv_def_manager_repr(self):
        """Test LdapServerDefinitionManager.__repr__()."""

        ldap_srv_def_mgr = self.console.ldap_server_definitions

        # Execute the code to be tested
        repr_str = repr(ldap_srv_def_mgr)

        repr_str = repr_str.replace('\n', '\\n')
        # We check just the begin of the string:
        assert re.match(r'^{classname}\s+at\s+0x{id:08x}\s+\(\\n.*'.
                        format(classname=ldap_srv_def_mgr.__class__.__name__,
                               id=id(ldap_srv_def_mgr)),
                        repr_str)

    def test_ldap_srv_def_manager_initial_attrs(self):
        """Test initial attributes of LdapServerDefinitionManager."""

        ldap_srv_def_mgr = self.console.ldap_server_definitions

        # Verify all public properties of the manager object
        assert ldap_srv_def_mgr.resource_class == LdapServerDefinition
        assert ldap_srv_def_mgr.class_name == 'ldap-server-definition'
        assert ldap_srv_def_mgr.session is self.session
        assert ldap_srv_def_mgr.parent is self.console
        assert ldap_srv_def_mgr.console is self.console

    @pytest.mark.parametrize(
        "full_properties_kwargs, prop_names", [
            (dict(full_properties=False),
             ['element-uri']),
            (dict(full_properties=True),
             ['element-uri', 'name']),
            (dict(),  # test default for full_properties (True)
             ['element-uri', 'name']),
        ]
    )
    @pytest.mark.parametrize(
        "filter_args, exp_names", [
            (None,
             ['a', 'b']),
            ({},
             ['a', 'b']),
            ({'name': 'a'},
             ['a']),
        ]
    )
    def test_ldap_srv_def_manager_list(
            self, filter_args, exp_names, full_properties_kwargs, prop_names):
        """Test LdapServerDefinitionManager.list()."""

        faked_ldap_srv_def1 = self.add_ldap_srv_def(name='a')
        faked_ldap_srv_def2 = self.add_ldap_srv_def(name='b')
        faked_ldap_srv_defs = [faked_ldap_srv_def1, faked_ldap_srv_def2]
        exp_faked_ldap_srv_defs = [u for u in faked_ldap_srv_defs
                                   if u.name in exp_names]
        ldap_srv_def_mgr = self.console.ldap_server_definitions

        # Execute the code to be tested
        ldap_srv_defs = ldap_srv_def_mgr.list(filter_args=filter_args,
                                              **full_properties_kwargs)

        assert_resources(ldap_srv_defs, exp_faked_ldap_srv_defs, prop_names)

    @pytest.mark.parametrize(
        "input_props, exp_prop_names, exp_exc", [
            ({},  # props missing
             None,
             HTTPError({'http-status': 400, 'reason': 5})),
            ({'description': 'fake description X'},  # props missing
             None,
             HTTPError({'http-status': 400, 'reason': 5})),
            ({'description': 'fake description X',
              'name': 'a'},
             ['element-uri', 'name', 'description'],
             None),
        ]
    )
    def test_ldap_srv_def_manager_create(
            self, input_props, exp_prop_names, exp_exc):
        """Test LdapServerDefinitionManager.create()."""

        ldap_srv_def_mgr = self.console.ldap_server_definitions

        if exp_exc is not None:

            with pytest.raises(exp_exc.__class__) as exc_info:

                # Execute the code to be tested
                ldap_srv_def_mgr.create(properties=input_props)

            exc = exc_info.value
            if isinstance(exp_exc, HTTPError):
                assert exc.http_status == exp_exc.http_status
                assert exc.reason == exp_exc.reason

        else:

            # Execute the code to be tested.
            ldap_srv_def = ldap_srv_def_mgr.create(properties=input_props)

            # Check the resource for consistency within itself
            assert isinstance(ldap_srv_def, LdapServerDefinition)
            ldap_srv_def_name = ldap_srv_def.name
            exp_ldap_srv_def_name = ldap_srv_def.properties['name']
            assert ldap_srv_def_name == exp_ldap_srv_def_name
            ldap_srv_def_uri = ldap_srv_def.uri
            exp_ldap_srv_def_uri = ldap_srv_def.properties['element-uri']
            assert ldap_srv_def_uri == exp_ldap_srv_def_uri

            # Check the properties against the expected names and values
            for prop_name in exp_prop_names:
                assert prop_name in ldap_srv_def.properties
                if prop_name in input_props:
                    value = ldap_srv_def.properties[prop_name]
                    exp_value = input_props[prop_name]
                    assert value == exp_value

    def test_ldap_srv_def_repr(self):
        """Test LdapServerDefinition.__repr__()."""

        faked_ldap_srv_def1 = self.add_ldap_srv_def(name='a')
        ldap_srv_def1 = self.console.ldap_server_definitions.find(
            name=faked_ldap_srv_def1.name)

        # Execute the code to be tested
        repr_str = repr(ldap_srv_def1)

        repr_str = repr_str.replace('\n', '\\n')
        # We check just the begin of the string:
        assert re.match(r'^{classname}\s+at\s+0x{id:08x}\s+\(\\n.*'.
                        format(classname=ldap_srv_def1.__class__.__name__,
                               id=id(ldap_srv_def1)),
                        repr_str)

    @pytest.mark.parametrize(
        "input_props, exp_exc", [
            ({'name': 'a'},
             None),
            ({'name': 'b'},
             None),
        ]
    )
    def test_ldap_srv_def_delete(self, input_props, exp_exc):
        """Test LdapServerDefinition.delete()."""

        faked_ldap_srv_def = self.add_ldap_srv_def(name=input_props['name'])

        ldap_srv_def_mgr = self.console.ldap_server_definitions
        ldap_srv_def = ldap_srv_def_mgr.find(name=faked_ldap_srv_def.name)

        if exp_exc is not None:

            with pytest.raises(exp_exc.__class__) as exc_info:

                # Execute the code to be tested
                ldap_srv_def.delete()

            exc = exc_info.value
            if isinstance(exp_exc, HTTPError):
                assert exc.http_status == exp_exc.http_status
                assert exc.reason == exp_exc.reason

            # Check that the LDAP Server Definition still exists
            ldap_srv_def_mgr.find(name=faked_ldap_srv_def.name)

        else:

            # Execute the code to be tested.
            ldap_srv_def.delete()

            # Check that the LDAP Server Definition no longer exists
            with pytest.raises(NotFound) as exc_info:
                ldap_srv_def_mgr.find(name=faked_ldap_srv_def.name)

    def test_ldap_srv_def_delete_create_same_name(self):
        """Test LdapServerDefinition.delete() followed by create() with same
        name."""

        ldap_srv_def_name = 'faked_a'

        # Add the LDAP Server Definition to be tested
        self.add_ldap_srv_def(name=ldap_srv_def_name)

        # Input properties for a LDAP Server Definition with the same name
        sn_ldap_srv_def_props = {
            'name': ldap_srv_def_name,
            'description': 'LDAP Server Definition with same name',
            'type': 'user-defined',
        }

        ldap_srv_def_mgr = self.console.ldap_server_definitions
        ldap_srv_def = ldap_srv_def_mgr.find(name=ldap_srv_def_name)

        # Execute the deletion code to be tested
        ldap_srv_def.delete()

        # Check that the LDAP Server Definition no longer exists
        with pytest.raises(NotFound):
            ldap_srv_def_mgr.find(name=ldap_srv_def_name)

        # Execute the creation code to be tested.
        ldap_srv_def_mgr.create(sn_ldap_srv_def_props)

        # Check that the LDAP Server Definition exists again under that name
        sn_ldap_srv_def = ldap_srv_def_mgr.find(name=ldap_srv_def_name)
        description = sn_ldap_srv_def.get_property('description')
        assert description == sn_ldap_srv_def_props['description']

    @pytest.mark.parametrize(
        "input_props", [
            {},
            {'description': 'New LDAP Server Definition description'},
        ]
    )
    def test_ldap_srv_def_update_properties(self, input_props):
        """Test LdapServerDefinition.update_properties()."""

        ldap_srv_def_name = 'faked_a'

        # Add the LDAP Server Definition to be tested
        self.add_ldap_srv_def(name=ldap_srv_def_name)

        ldap_srv_def_mgr = self.console.ldap_server_definitions
        ldap_srv_def = ldap_srv_def_mgr.find(name=ldap_srv_def_name)

        ldap_srv_def.pull_full_properties()
        saved_properties = copy.deepcopy(ldap_srv_def.properties)

        # Execute the code to be tested
        ldap_srv_def.update_properties(properties=input_props)

        # Verify that the resource object already reflects the property
        # updates.
        for prop_name in saved_properties:
            if prop_name in input_props:
                exp_prop_value = input_props[prop_name]
            else:
                exp_prop_value = saved_properties[prop_name]
            assert prop_name in ldap_srv_def.properties
            prop_value = ldap_srv_def.properties[prop_name]
            assert prop_value == exp_prop_value, \
                "Unexpected value for property {!r}".format(prop_name)

        # Refresh the resource object and verify that the resource object
        # still reflects the property updates.
        ldap_srv_def.pull_full_properties()
        for prop_name in saved_properties:
            if prop_name in input_props:
                exp_prop_value = input_props[prop_name]
            else:
                exp_prop_value = saved_properties[prop_name]
            assert prop_name in ldap_srv_def.properties
            prop_value = ldap_srv_def.properties[prop_name]
            assert prop_value == exp_prop_value
Пример #7
0
class TestLpar(object):
    """All tests for Lpar and LparManager classes."""
    def setup_method(self):
        """
        Set up a faked session, and add a faked CPC in classic mode without any
        child resources.
        """

        self.session = FakedSession('fake-host', 'fake-hmc', '2.13.1', '1.8')
        self.session.retry_timeout_config.status_timeout = 1
        self.client = Client(self.session)

        self.faked_cpc = self.session.hmc.cpcs.add({
            'object-id': 'fake-cpc1-oid',
            # object-uri is set up automatically
            'parent': None,
            'class': 'cpc',
            'name': 'fake-cpc1-name',
            'description': 'CPC #1 (classic mode)',
            'status': 'active',
            'dpm-enabled': False,
            'is-ensemble-member': False,
            'iml-mode': 'lpar',
        })
        self.cpc = self.client.cpcs.find(name='fake-cpc1-name')

    def add_lpar1(self):
        """Add lpar 1 (type linux)."""

        faked_lpar = self.faked_cpc.lpars.add({
            'object-id': LPAR1_OID,
            # object-uri will be automatically set
            'parent': self.faked_cpc.uri,
            'class': 'logical-partition',
            'name': LPAR1_NAME,
            'description': 'LPAR #1 (Linux)',
            'status': 'operating',
            'activation-mode': 'linux',
        })
        return faked_lpar

    def add_lpar2(self):
        """Add lpar 2 (type ssc)."""

        faked_lpar = self.faked_cpc.lpars.add({
            'object-id': LPAR2_OID,
            # object-uri will be automatically set
            'parent': self.faked_cpc.uri,
            'class': 'logical-partition',
            'name': LPAR2_NAME,
            'description': 'LPAR #2 (SSC)',
            'status': 'operating',
            'activation-mode': 'ssc',
        })
        return faked_lpar

    def test_lparmanager_initial_attrs(self):
        """Test initial attributes of LparManager."""

        lpar_mgr = self.cpc.lpars

        # Verify all public properties of the manager object
        assert lpar_mgr.resource_class == Lpar
        assert lpar_mgr.session == self.session
        assert lpar_mgr.parent == self.cpc
        assert lpar_mgr.cpc == self.cpc

    # TODO: Test for LparManager.__repr__()

    @pytest.mark.parametrize("full_properties_kwargs, prop_names", [
        (dict(), ['object-uri', 'name', 'status']),
        (dict(full_properties=False), ['object-uri', 'name', 'status']),
        (dict(full_properties=True), None),
    ])
    def test_lparmanager_list_full_properties(self, full_properties_kwargs,
                                              prop_names):
        """Test LparManager.list() with full_properties."""

        # Add two faked LPARs
        faked_lpar1 = self.add_lpar1()
        faked_lpar2 = self.add_lpar2()

        exp_faked_lpars = [faked_lpar1, faked_lpar2]
        lpar_mgr = self.cpc.lpars

        # Execute the code to be tested
        lpars = lpar_mgr.list(**full_properties_kwargs)

        assert_resources(lpars, exp_faked_lpars, prop_names)

    @pytest.mark.parametrize("filter_args, exp_names", [
        ({
            'object-id': LPAR1_OID
        }, [LPAR1_NAME]),
        ({
            'object-id': LPAR2_OID
        }, [LPAR2_NAME]),
        ({
            'object-id': [LPAR1_OID, LPAR2_OID]
        }, [LPAR1_NAME, LPAR2_NAME]),
        ({
            'object-id': [LPAR1_OID, LPAR1_OID]
        }, [LPAR1_NAME]),
        ({
            'object-id': LPAR1_OID + 'foo'
        }, []),
        ({
            'object-id': [LPAR1_OID, LPAR2_OID + 'foo']
        }, [LPAR1_NAME]),
        ({
            'object-id': [LPAR2_OID + 'foo', LPAR1_OID]
        }, [LPAR1_NAME]),
        ({
            'name': LPAR1_NAME
        }, [LPAR1_NAME]),
        ({
            'name': LPAR2_NAME
        }, [LPAR2_NAME]),
        ({
            'name': [LPAR1_NAME, LPAR2_NAME]
        }, [LPAR1_NAME, LPAR2_NAME]),
        ({
            'name': LPAR1_NAME + 'foo'
        }, []),
        ({
            'name': [LPAR1_NAME, LPAR2_NAME + 'foo']
        }, [LPAR1_NAME]),
        ({
            'name': [LPAR2_NAME + 'foo', LPAR1_NAME]
        }, [LPAR1_NAME]),
        ({
            'name': [LPAR1_NAME, LPAR1_NAME]
        }, [LPAR1_NAME]),
        ({
            'name': '.*lpar 1'
        }, [LPAR1_NAME]),
        ({
            'name': 'lpar 1.*'
        }, [LPAR1_NAME]),
        ({
            'name': 'lpar .'
        }, [LPAR1_NAME, LPAR2_NAME]),
        ({
            'name': '.par 1'
        }, [LPAR1_NAME]),
        ({
            'name': '.+'
        }, [LPAR1_NAME, LPAR2_NAME]),
        ({
            'name': 'lpar 1.+'
        }, []),
        ({
            'name': '.+lpar 1'
        }, []),
        ({
            'name': LPAR1_NAME,
            'object-id': LPAR1_OID
        }, [LPAR1_NAME]),
        ({
            'name': LPAR1_NAME,
            'object-id': LPAR1_OID + 'foo'
        }, []),
        ({
            'name': LPAR1_NAME + 'foo',
            'object-id': LPAR1_OID
        }, []),
        ({
            'name': LPAR1_NAME + 'foo',
            'object-id': LPAR1_OID + 'foo'
        }, []),
    ])
    def test_lparmanager_list_filter_args(self, filter_args, exp_names):
        """Test LparManager.list() with filter_args."""

        # Add two faked LPARs
        self.add_lpar1()
        self.add_lpar2()

        lpar_mgr = self.cpc.lpars

        # Execute the code to be tested
        lpars = lpar_mgr.list(filter_args=filter_args)

        assert len(lpars) == len(exp_names)
        if exp_names:
            names = [p.properties['name'] for p in lpars]
            assert set(names) == set(exp_names)

    def test_lpar_repr(self):
        """Test Lpar.__repr__()."""

        # Add a faked LPAR
        faked_lpar = self.add_lpar1()

        lpar_mgr = self.cpc.lpars
        lpar = lpar_mgr.find(name=faked_lpar.name)

        # Execute the code to be tested
        repr_str = repr(lpar)

        repr_str = repr_str.replace('\n', '\\n')
        # We check just the begin of the string:
        assert re.match(
            r'^{classname}\s+at\s+0x{id:08x}\s+\(\\n.*'.format(
                classname=lpar.__class__.__name__, id=id(lpar)), repr_str)

    @pytest.mark.parametrize("lpar_name", [
        LPAR1_NAME,
        LPAR2_NAME,
    ])
    @pytest.mark.parametrize("input_props", [
        {},
        {
            'description': 'New lpar description'
        },
        {
            'acceptable-status': ['operating', 'not-operating'],
            'description': 'New lpar description'
        },
        {
            'ssc-master-userid': None,
            'ssc-master-pw': None
        },
    ])
    def test_lpar_update_properties(self, input_props, lpar_name):
        """Test Lpar.update_properties()."""

        # Add faked lpars
        self.add_lpar1()
        self.add_lpar2()

        lpar_mgr = self.cpc.lpars
        lpar = lpar_mgr.find(name=lpar_name)

        lpar.pull_full_properties()
        saved_properties = copy.deepcopy(lpar.properties)

        # Execute the code to be tested
        lpar.update_properties(properties=input_props)

        # Verify that the resource object already reflects the property
        # updates.
        for prop_name in saved_properties:
            if prop_name in input_props:
                exp_prop_value = input_props[prop_name]
            else:
                exp_prop_value = saved_properties[prop_name]
            assert prop_name in lpar.properties
            prop_value = lpar.properties[prop_name]
            assert prop_value == exp_prop_value

        # Refresh the resource object and verify that the resource object
        # still reflects the property updates.
        lpar.pull_full_properties()
        for prop_name in saved_properties:
            if prop_name in input_props:
                exp_prop_value = input_props[prop_name]
            else:
                exp_prop_value = saved_properties[prop_name]
            assert prop_name in lpar.properties
            prop_value = lpar.properties[prop_name]
            assert prop_value == exp_prop_value

    @pytest.mark.parametrize(
        "initial_profile, profile_kwargs, exp_profile, exp_profile_exc", [
            ('', dict(), None, HTTPError({
                'http-status': 500,
                'reason': 263
            })),
            (LPAR1_NAME, dict(), LPAR1_NAME, None),
            (LPAR2_NAME, dict(), None,
             HTTPError({
                 'http-status': 500,
                 'reason': 263
             })),
            ('', dict(activation_profile_name=LPAR1_NAME), LPAR1_NAME, None),
            (LPAR1_NAME, dict(activation_profile_name=LPAR1_NAME), LPAR1_NAME,
             None),
            (LPAR2_NAME, dict(activation_profile_name=LPAR1_NAME), LPAR1_NAME,
             None),
            ('', dict(activation_profile_name=LPAR2_NAME), None,
             HTTPError({
                 'http-status': 500,
                 'reason': 263
             })),
            (LPAR1_NAME, dict(activation_profile_name=LPAR2_NAME), None,
             HTTPError({
                 'http-status': 500,
                 'reason': 263
             })),
            (LPAR2_NAME, dict(activation_profile_name=LPAR2_NAME), None,
             HTTPError({
                 'http-status': 500,
                 'reason': 263
             })),
        ])
    @pytest.mark.parametrize(
        "initial_status, status_kwargs, act_exp_status, exp_status_exc",
        [
            (
                'not-activated',
                dict(),  # Verify that force has a default
                'not-operating',
                None),
            ('not-activated', dict(force=False), 'not-operating', None),
            ('not-activated', dict(force=True), 'not-operating', None),
            ('not-operating', dict(force=False), 'not-operating', None),
            ('not-operating', dict(force=True), 'not-operating', None),
            (
                'operating',
                dict(),  # Verify that force default is False
                'not-operating',
                HTTPError({
                    'http-status': 500,
                    'reason': 263
                })),
            ('operating', dict(force=False), 'not-operating',
             HTTPError({
                 'http-status': 500,
                 'reason': 263
             })),
            ('operating', dict(force=True), 'not-operating', None),
            ('exceptions', dict(force=False), 'not-operating', None),
            ('exceptions', dict(force=True), 'not-operating', None),
            ('not-activated', dict(), 'exceptions',
             StatusTimeout(None, None, None, None)),
            ('not-activated', dict(allow_status_exceptions=False),
             'exceptions', StatusTimeout(None, None, None, None)),
            ('not-activated', dict(allow_status_exceptions=True), 'exceptions',
             None),
        ])
    @mock.patch.object(LparActivateHandler, 'get_status')
    def test_lpar_activate(self, get_status_mock, initial_status,
                           status_kwargs, act_exp_status, exp_status_exc,
                           initial_profile, profile_kwargs, exp_profile,
                           exp_profile_exc):
        """Test Lpar.activate()."""

        # Add a faked LPAR
        faked_lpar = self.add_lpar1()
        faked_lpar.properties['status'] = initial_status
        faked_lpar.properties['next-activation-profile-name'] = initial_profile

        lpar_mgr = self.cpc.lpars
        lpar = lpar_mgr.find(name=faked_lpar.name)

        input_kwargs = dict(status_kwargs, **profile_kwargs)

        exp_excs = []
        if exp_status_exc:
            exp_excs.append(exp_status_exc)
        if exp_profile_exc:
            exp_excs.append(exp_profile_exc)

        get_status_mock.return_value = act_exp_status

        if exp_excs:

            with pytest.raises(Exception) as exc_info:

                # Execute the code to be tested
                lpar.activate(**input_kwargs)

            exc = exc_info.value

            exp_exc_classes = [e.__class__ for e in exp_excs]
            assert isinstance(exc, tuple(exp_exc_classes))

            if isinstance(exc, HTTPError):
                exp_httperror = [
                    e for e in exp_excs if isinstance(e, HTTPError)
                ][0]
                assert exc.http_status == exp_httperror.http_status
                assert exc.reason == exp_httperror.reason

        else:

            # Execute the code to be tested.
            ret = lpar.activate(**input_kwargs)

            assert ret is None

            lpar.pull_full_properties()

            status = lpar.get_property('status')
            assert status == act_exp_status

            last_profile_name = lpar.get_property(
                'last-used-activation-profile')
            assert last_profile_name == exp_profile

    @pytest.mark.parametrize(
        "initial_status, input_kwargs, act_exp_status, exp_status_exc",
        [
            (
                'not-activated',
                dict(),  # Verify that force has a default
                'not-activated',
                HTTPError({
                    'http-status': 500,
                    'reason': 263
                })),
            ('not-activated', dict(force=False), 'not-activated',
             HTTPError({
                 'http-status': 500,
                 'reason': 263
             })),
            ('not-activated', dict(force=True), 'not-activated', None),
            ('not-operating', dict(force=False), 'not-activated', None),
            ('not-operating', dict(force=True), 'not-activated', None),
            (
                'operating',
                dict(),  # Verify that force default is False
                'not-activated',
                HTTPError({
                    'http-status': 500,
                    'reason': 263
                })),
            ('operating', dict(force=False), 'not-activated',
             HTTPError({
                 'http-status': 500,
                 'reason': 263
             })),
            ('operating', dict(force=True), 'not-activated', None),
            ('exceptions', dict(force=False), 'not-activated', None),
            ('exceptions', dict(force=True), 'not-activated', None),
            ('not-operating', dict(), 'exceptions',
             StatusTimeout(None, None, None, None)),
            ('not-operating', dict(allow_status_exceptions=False),
             'exceptions', StatusTimeout(None, None, None, None)),
            ('not-operating', dict(allow_status_exceptions=True), 'exceptions',
             None),
        ])
    @mock.patch.object(LparDeactivateHandler, 'get_status')
    def test_lpar_deactivate(self, get_status_mock, initial_status,
                             input_kwargs, act_exp_status, exp_status_exc):
        """Test Lpar.deactivate()."""

        # Add a faked LPAR
        faked_lpar = self.add_lpar1()
        faked_lpar.properties['status'] = initial_status

        lpar_mgr = self.cpc.lpars
        lpar = lpar_mgr.find(name=faked_lpar.name)

        get_status_mock.return_value = act_exp_status

        exp_excs = []
        if exp_status_exc:
            exp_excs.append(exp_status_exc)

        if exp_excs:

            with pytest.raises(Exception) as exc_info:

                # Execute the code to be tested
                lpar.deactivate(**input_kwargs)

            exc = exc_info.value

            exp_exc_classes = [e.__class__ for e in exp_excs]
            assert isinstance(exc, tuple(exp_exc_classes))

            if isinstance(exc, HTTPError):
                exp_httperror = [
                    e for e in exp_excs if isinstance(e, HTTPError)
                ][0]
                assert exc.http_status == exp_httperror.http_status
                assert exc.reason == exp_httperror.reason

        else:

            # Execute the code to be tested.
            ret = lpar.deactivate(**input_kwargs)

            assert ret is None

            lpar.pull_full_properties()

            status = lpar.get_property('status')
            assert status == act_exp_status

    @pytest.mark.parametrize(
        "initial_loadparm, loadparm_kwargs, exp_loadparm, exp_loadparm_exc", [
            (None, dict(), '', None),
            (None, dict(load_parameter='abcd'), 'abcd', None),
            ('abcd', dict(), 'abcd', None),
            ('fooo', dict(load_parameter='abcd'), 'abcd', None),
        ])
    @pytest.mark.parametrize(
        "initial_loadaddr, loadaddr_kwargs, exp_loadaddr, exp_loadaddr_exc", [
            (None, dict(), None, HTTPError({
                'http-status': 400,
                'reason': 5
            })),
            (None, dict(load_address='5176'), '5176', None),
            ('5176', dict(), '5176', None),
            ('1234', dict(load_address='5176'), '5176', None),
        ])
    @pytest.mark.parametrize(
        "initial_status, status_kwargs, act_exp_status, exp_status_exc"
        ", initial_stored_status, exp_stored_status, exp_store_status_exc", [
            ('not-activated', dict(), 'operating',
             HTTPError({
                 'http-status': 409,
                 'reason': 0
             }), None, None, None),
            ('not-activated', dict(force=False), 'operating',
             HTTPError({
                 'http-status': 409,
                 'reason': 0
             }), None, None, None),
            ('not-activated', dict(force=True), 'operating',
             HTTPError({
                 'http-status': 409,
                 'reason': 0
             }), None, None, None),
            ('not-operating', dict(force=False), 'operating', None, None, None,
             None),
            ('not-operating', dict(force=True), 'operating', None, None, None,
             None),
            ('operating', dict(), 'operating',
             HTTPError({
                 'http-status': 500,
                 'reason': 263
             }), None, None, None),
            ('operating', dict(force=False), 'operating',
             HTTPError({
                 'http-status': 500,
                 'reason': 263
             }), None, None, None),
            ('operating', dict(force=True), 'operating', None, None, None,
             None),
            ('exceptions', dict(force=False), 'operating', None, None, None,
             None),
            ('exceptions', dict(force=True), 'operating', None, None, None,
             None),
            ('not-operating', dict(), 'exceptions',
             StatusTimeout(None, None, None, None), None, None, None),
            ('not-operating', dict(allow_status_exceptions=False),
             'exceptions', StatusTimeout(None, None, None,
                                         None), None, None, None),
            ('not-operating', dict(allow_status_exceptions=True), 'exceptions',
             None, None, None, None),
            ('not-operating', dict(store_status_indicator=False), 'operating',
             None, None, None, None),
            ('not-operating', dict(store_status_indicator=True), 'operating',
             None, None, 'not-operating', None),
        ])
    @pytest.mark.parametrize(
        "initial_memory, memory_kwargs, exp_memory, exp_memory_exc", [
            ('foobar', dict(), '', None),
            ('foobar', dict(clear_indicator=False), 'foobar', None),
            ('foobar', dict(clear_indicator=True), '', None),
        ])
    @mock.patch.object(LparLoadHandler, 'get_status')
    def test_lpar_load(self, get_status_mock, initial_status, status_kwargs,
                       act_exp_status, exp_status_exc, initial_loadaddr,
                       loadaddr_kwargs, exp_loadaddr, exp_loadaddr_exc,
                       initial_loadparm, loadparm_kwargs, exp_loadparm,
                       exp_loadparm_exc, initial_memory, memory_kwargs,
                       exp_memory, exp_memory_exc, initial_stored_status,
                       exp_stored_status, exp_store_status_exc):
        """Test Lpar.load()."""

        # Add a faked LPAR
        faked_lpar = self.add_lpar1()
        faked_lpar.properties['status'] = initial_status
        faked_lpar.properties['last-used-load-address'] = initial_loadaddr
        faked_lpar.properties['last-used-load-parameter'] = initial_loadparm
        faked_lpar.properties['memory'] = initial_memory

        lpar_mgr = self.cpc.lpars
        lpar = lpar_mgr.find(name=faked_lpar.name)

        input_kwargs = dict(status_kwargs, **loadaddr_kwargs)
        input_kwargs.update(**loadparm_kwargs)
        input_kwargs.update(**memory_kwargs)

        exp_excs = []
        if exp_status_exc:
            exp_excs.append(exp_status_exc)
        if exp_loadaddr_exc:
            exp_excs.append(exp_loadaddr_exc)
        if exp_loadparm_exc:
            exp_excs.append(exp_loadparm_exc)
        if exp_memory_exc:
            exp_excs.append(exp_memory_exc)
        if exp_store_status_exc:
            exp_excs.append(exp_store_status_exc)

        get_status_mock.return_value = act_exp_status

        if exp_excs:

            with pytest.raises(Exception) as exc_info:

                # Execute the code to be tested
                lpar.load(**input_kwargs)

            exc = exc_info.value

            exp_exc_classes = [e.__class__ for e in exp_excs]
            assert isinstance(exc, tuple(exp_exc_classes))

            if isinstance(exc, HTTPError):
                exp_httperror = [
                    e for e in exp_excs if isinstance(e, HTTPError)
                ][0]
                assert exc.http_status == exp_httperror.http_status
                assert exc.reason == exp_httperror.reason

        else:

            # Execute the code to be tested.
            ret = lpar.load(**input_kwargs)

            assert ret is None

            lpar.pull_full_properties()

            status = lpar.get_property('status')
            assert status == act_exp_status

            last_loadaddr = lpar.get_property('last-used-load-address')
            assert last_loadaddr == exp_loadaddr

            last_loadparm = lpar.get_property('last-used-load-parameter')
            assert last_loadparm == exp_loadparm

            last_memory = lpar.get_property('memory')
            assert last_memory == exp_memory

            stored_status = lpar.get_property('stored-status')
            assert stored_status == exp_stored_status
Пример #8
0
class TestUserRole(object):
    """All tests for the UserRole and UserRoleManager classes."""
    def setup_method(self):
        """
        Set up a faked session, and add a faked Console without any
        child resources.
        """

        self.session = FakedSession('fake-host', 'fake-hmc', '2.13.1', '1.8')
        self.client = Client(self.session)

        self.faked_console = self.session.hmc.consoles.add({
            'object-id':
            None,
            # object-uri will be automatically set
            'parent':
            None,
            'class':
            'console',
            'name':
            'fake-console1',
            'description':
            'Console #1',
        })
        self.console = self.client.consoles.find(name=self.faked_console.name)

    def add_user_role(self, name, type_):
        faked_user_role = self.faked_console.user_roles.add({
            'object-id':
            'oid-{}'.format(name),
            # object-uri will be automatically set
            'parent':
            '/api/console',
            'class':
            'user-role',
            'name':
            name,
            'description':
            'User Role {}'.format(name),
            'type':
            type_,
        })
        return faked_user_role

    def test_user_role_manager_repr(self):
        """Test UserRoleManager.__repr__()."""

        user_role_mgr = self.console.user_roles

        # Execute the code to be tested
        repr_str = repr(user_role_mgr)

        repr_str = repr_str.replace('\n', '\\n')
        # We check just the begin of the string:
        assert re.match(
            r'^{classname}\s+at\s+0x{id:08x}\s+\(\\n.*'.format(
                classname=user_role_mgr.__class__.__name__,
                id=id(user_role_mgr)), repr_str)

    def test_user_role_manager_initial_attrs(self):
        """Test initial attributes of UserRoleManager."""

        user_role_mgr = self.console.user_roles

        # Verify all public properties of the manager object
        assert user_role_mgr.resource_class == UserRole
        assert user_role_mgr.class_name == 'user-role'
        assert user_role_mgr.session is self.session
        assert user_role_mgr.parent is self.console
        assert user_role_mgr.console is self.console

    @pytest.mark.parametrize(
        "full_properties_kwargs, prop_names",
        [
            (dict(full_properties=False), ['object-uri']),
            (dict(full_properties=True), ['object-uri', 'name']),
            (
                dict(),  # test default for full_properties (True)
                ['object-uri', 'name']),
        ])
    @pytest.mark.parametrize("filter_args, exp_names", [
        (None, ['a', 'b']),
        ({}, ['a', 'b']),
        ({
            'name': 'a'
        }, ['a']),
    ])
    def test_user_role_manager_list(self, filter_args, exp_names,
                                    full_properties_kwargs, prop_names):
        """Test UserRoleManager.list()."""

        faked_user_role1 = self.add_user_role(name='a', type_='user-defined')
        faked_user_role2 = self.add_user_role(name='b', type_='user-defined')
        faked_user_roles = [faked_user_role1, faked_user_role2]
        exp_faked_user_roles = [
            u for u in faked_user_roles if u.name in exp_names
        ]
        user_role_mgr = self.console.user_roles

        # Execute the code to be tested
        user_roles = user_role_mgr.list(filter_args=filter_args,
                                        **full_properties_kwargs)

        assert_resources(user_roles, exp_faked_user_roles, prop_names)

    @pytest.mark.parametrize(
        "input_props, exp_prop_names, exp_exc",
        [
            (
                {},  # name missing
                None,
                HTTPError({
                    'http-status': 400,
                    'reason': 5
                })),
            (
                {
                    'description': 'fake description X'
                },  # name missing
                None,
                HTTPError({
                    'http-status': 400,
                    'reason': 5
                })),
            (
                {
                    'name': 'fake-name-x',
                    'type': 'user-defined'
                },  # type not allowed
                None,
                HTTPError({
                    'http-status': 400,
                    'reason': 6
                })),
            (
                {
                    'name': 'fake-name-x',
                    'type': 'system-defined'
                },  # type not allowed
                None,
                HTTPError({
                    'http-status': 400,
                    'reason': 6
                })),
            ({
                'name': 'fake-name-x'
            }, ['object-uri', 'name'], None),
            ({
                'name': 'fake-name-x',
                'description': 'fake description X'
            }, ['object-uri', 'name', 'description'], None),
        ])
    def test_user_role_manager_create(self, input_props, exp_prop_names,
                                      exp_exc):
        """Test UserRoleManager.create()."""

        user_role_mgr = self.console.user_roles

        if exp_exc is not None:

            with pytest.raises(exp_exc.__class__) as exc_info:

                # Execute the code to be tested
                user_role_mgr.create(properties=input_props)

            exc = exc_info.value
            if isinstance(exp_exc, HTTPError):
                assert exc.http_status == exp_exc.http_status
                assert exc.reason == exp_exc.reason

        else:

            # Execute the code to be tested.
            user_role = user_role_mgr.create(properties=input_props)

            # Check the resource for consistency within itself
            assert isinstance(user_role, UserRole)
            user_role_name = user_role.name
            exp_user_role_name = user_role.properties['name']
            assert user_role_name == exp_user_role_name
            user_role_uri = user_role.uri
            exp_user_role_uri = user_role.properties['object-uri']
            assert user_role_uri == exp_user_role_uri

            # Check the properties against the expected names and values
            for prop_name in exp_prop_names:
                assert prop_name in user_role.properties
                if prop_name in input_props:
                    value = user_role.properties[prop_name]
                    exp_value = input_props[prop_name]
                    assert value == exp_value

    def test_user_role_repr(self):
        """Test UserRole.__repr__()."""

        faked_user_role1 = self.add_user_role(name='a', type_='user-defined')
        user_role1 = self.console.user_roles.find(name=faked_user_role1.name)

        # Execute the code to be tested
        repr_str = repr(user_role1)

        repr_str = repr_str.replace('\n', '\\n')
        # We check just the begin of the string:
        assert re.match(
            r'^{classname}\s+at\s+0x{id:08x}\s+\(\\n.*'.format(
                classname=user_role1.__class__.__name__, id=id(user_role1)),
            repr_str)

    @pytest.mark.parametrize(
        "input_name, input_type, exp_exc",
        [
            ('a', 'user-defined', None),
            # ('b', 'system-defined',
            #  HTTPError({'http-status': 400, 'reason': 312})),
            # TODO: Re-enable once rejection for system-defined roles supported
        ])
    def test_user_role_delete(self, input_name, input_type, exp_exc):
        """Test UserRole.delete()."""

        faked_user_role = self.add_user_role(name=input_name, type_=input_type)
        user_role_mgr = self.console.user_roles
        user_role = user_role_mgr.find(name=faked_user_role.name)

        if exp_exc is not None:

            with pytest.raises(exp_exc.__class__) as exc_info:

                # Execute the code to be tested
                user_role.delete()

            exc = exc_info.value
            if isinstance(exp_exc, HTTPError):
                assert exc.http_status == exp_exc.http_status
                assert exc.reason == exp_exc.reason

            # Check that the user role still exists
            user_role_mgr.find(name=faked_user_role.name)

        else:

            # Execute the code to be tested.
            user_role.delete()

            # Check that the user role no longer exists
            with pytest.raises(NotFound) as exc_info:
                user_role_mgr.find(name=faked_user_role.name)

    def test_user_role_delete_create_same_name(self):
        """Test UserRole.delete() followed by create() with same name."""

        user_role_name = 'faked_a'

        # Add the user role to be tested
        self.add_user_role(name=user_role_name, type_='user-defined')

        # Input properties for a user role with the same name
        sn_user_role_props = {
            'name': user_role_name,
            'description': 'User Role with same name',
        }

        user_role_mgr = self.console.user_roles
        user_role = user_role_mgr.find(name=user_role_name)

        # Execute the deletion code to be tested
        user_role.delete()

        # Check that the user role no longer exists
        with pytest.raises(NotFound):
            user_role_mgr.find(name=user_role_name)

        # Execute the creation code to be tested.
        user_role_mgr.create(sn_user_role_props)

        # Check that the user role exists again under that name
        sn_user_role = user_role_mgr.find(name=user_role_name)
        description = sn_user_role.get_property('description')
        assert description == sn_user_role_props['description']

    @pytest.mark.parametrize("input_props", [
        {},
        {
            'description': 'New user role description'
        },
    ])
    def test_user_role_update_properties(self, input_props):
        """Test UserRole.update_properties()."""

        # Add the user role to be tested
        faked_user_role = self.add_user_role(name='a', type_='user-defined')

        user_role_mgr = self.console.user_roles
        user_role = user_role_mgr.find(name=faked_user_role.name)

        user_role.pull_full_properties()
        saved_properties = copy.deepcopy(user_role.properties)

        # Execute the code to be tested
        user_role.update_properties(properties=input_props)

        # Verify that the resource object already reflects the property
        # updates.
        for prop_name in saved_properties:
            if prop_name in input_props:
                exp_prop_value = input_props[prop_name]
            else:
                exp_prop_value = saved_properties[prop_name]
            assert prop_name in user_role.properties
            prop_value = user_role.properties[prop_name]
            assert prop_value == exp_prop_value, \
                "Unexpected value for property {!r}".format(prop_name)

        # Refresh the resource object and verify that the resource object
        # still reflects the property updates.
        user_role.pull_full_properties()
        for prop_name in saved_properties:
            if prop_name in input_props:
                exp_prop_value = input_props[prop_name]
            else:
                exp_prop_value = saved_properties[prop_name]
            assert prop_name in user_role.properties
            prop_value = user_role.properties[prop_name]
            assert prop_value == exp_prop_value

    @pytest.mark.parametrize("role_name, role_type, exp_exc", [
        ('ra', 'user-defined', None),
        ('rb', 'system-defined', HTTPError({
            'http-status': 400,
            'reason': 314
        })),
    ])
    @pytest.mark.parametrize("perm_args", [
        {
            'permitted_object': 'cpc',
            'include_members': True,
            'view_only': False
        },
    ])
    def test_user_role_add_permission(self, perm_args, role_name, role_type,
                                      exp_exc):
        """Test UserRole.add_permission()."""

        faked_user_role = self.add_user_role(name=role_name, type_=role_type)
        user_role_mgr = self.console.user_roles
        user_role = user_role_mgr.find(name=faked_user_role.name)

        permitted_object = perm_args['permitted_object']
        if isinstance(permitted_object, BaseResource):
            perm_obj = permitted_object.uri
            perm_type = 'object'
        else:
            assert isinstance(permitted_object, six.string_types)
            perm_obj = permitted_object
            perm_type = 'object-class'
        permission_parms = {
            'permitted-object': perm_obj,
            'permitted-object-type': perm_type,
            'include-members': perm_args['include_members'],
            'view-only-mode': perm_args['view_only'],
        }

        if exp_exc is not None:

            with pytest.raises(exp_exc.__class__) as exc_info:

                # Execute the code to be tested
                user_role.add_permission(**perm_args)

            exc = exc_info.value
            if isinstance(exp_exc, HTTPError):
                assert exc.http_status == exp_exc.http_status
                assert exc.reason == exp_exc.reason

            # Check that the user role still does not have that permission
            user_role.pull_full_properties()
            if 'permissions' in user_role.properties:
                permissions = user_role.properties['permissions']
                assert permission_parms not in permissions

        else:

            # Execute the code to be tested.
            ret = user_role.add_permission(**perm_args)

            assert ret is None

            # Check that the user role now has that permission
            user_role.pull_full_properties()
            permissions = user_role.properties['permissions']
            assert permission_parms in permissions

    @pytest.mark.parametrize("role_name, role_type, exp_exc", [
        ('ra', 'user-defined', None),
        ('rb', 'system-defined', HTTPError({
            'http-status': 400,
            'reason': 314
        })),
    ])
    @pytest.mark.parametrize("perm_args", [
        {
            'permitted_object': 'cpc',
            'include_members': True,
            'view_only': False
        },
    ])
    def test_user_role_remove_permission(self, perm_args, role_name, role_type,
                                         exp_exc):
        """Test UserRole.remove_permission()."""

        faked_user_role = self.add_user_role(name=role_name, type_=role_type)
        user_role_mgr = self.console.user_roles
        user_role = user_role_mgr.find(name=faked_user_role.name)

        permitted_object = perm_args['permitted_object']
        if isinstance(permitted_object, BaseResource):
            perm_obj = permitted_object.uri
            perm_type = 'object'
        else:
            assert isinstance(permitted_object, six.string_types)
            perm_obj = permitted_object
            perm_type = 'object-class'
        permission_parms = {
            'permitted-object': perm_obj,
            'permitted-object-type': perm_type,
            'include-members': perm_args['include_members'],
            'view-only-mode': perm_args['view_only'],
        }

        # Prepare the user role with the initial permission
        if 'permissions' not in faked_user_role.properties:
            faked_user_role.properties['permissions'] = []
        faked_user_role.properties['permissions'].append(permission_parms)

        if exp_exc is not None:

            with pytest.raises(exp_exc.__class__) as exc_info:

                # Execute the code to be tested
                user_role.remove_permission(**perm_args)

            exc = exc_info.value
            if isinstance(exp_exc, HTTPError):
                assert exc.http_status == exp_exc.http_status
                assert exc.reason == exp_exc.reason

            # Check that the user role still has that permission
            user_role.pull_full_properties()
            permissions = user_role.properties['permissions']
            assert permission_parms in permissions

        else:

            # Execute the code to be tested.
            ret = user_role.remove_permission(**perm_args)

            assert ret is None

            # Check that the user role no longer has that permission
            user_role.pull_full_properties()
            if 'permissions' in user_role.properties:
                permissions = user_role.properties['permissions']
                assert permission_parms not in permissions
Пример #9
0
class TestPasswordRule(object):
    """All tests for the PasswordRule and PasswordRuleManager classes."""

    def setup_method(self):
        """
        Setup that is called by pytest before each test method.

        Set up a faked session, and add a faked Console without any
        child resources.
        """
        # pylint: disable=attribute-defined-outside-init

        self.session = FakedSession('fake-host', 'fake-hmc', '2.13.1', '1.8')
        self.client = Client(self.session)

        self.faked_console = self.session.hmc.consoles.add({
            'object-id': None,
            # object-uri will be automatically set
            'parent': None,
            'class': 'console',
            'name': 'fake-console1',
            'description': 'Console #1',
        })
        self.console = self.client.consoles.find(name=self.faked_console.name)

    def add_password_rule(self, name, type_):
        """
        Add a faked password rule object to the faked Console and return it.
        """
        faked_password_rule = self.faked_console.password_rules.add({
            'element-id': 'oid-{}'.format(name),
            # element-uri will be automatically set
            'parent': '/api/console',
            'class': 'password-rule',
            'name': name,
            'description': 'Password Rule {}'.format(name),
            'type': type_,
        })
        return faked_password_rule

    def add_user(self, name, type_):
        """
        Add a faked user object to the faked Console and return it.
        """
        faked_user = self.faked_console.users.add({
            'object-id': 'oid-{}'.format(name),
            # object-uri will be automatically set
            'parent': '/api/console',
            'class': 'user',
            'name': name,
            'description': 'User {}'.format(name),
            'type': type_,
            'authentication-type': 'local',
        })
        return faked_user

    def test_pr_manager_repr(self):
        """Test PasswordRuleManager.__repr__()."""

        password_rule_mgr = self.console.password_rules

        # Execute the code to be tested
        repr_str = repr(password_rule_mgr)

        repr_str = repr_str.replace('\n', '\\n')
        # We check just the begin of the string:
        assert re.match(r'^{classname}\s+at\s+0x{id:08x}\s+\(\\n.*'.
                        format(classname=password_rule_mgr.__class__.__name__,
                               id=id(password_rule_mgr)),
                        repr_str)

    def test_pr_manager_initial_attrs(self):
        """Test initial attributes of PasswordRuleManager."""

        password_rule_mgr = self.console.password_rules

        # Verify all public properties of the manager object
        assert password_rule_mgr.resource_class == PasswordRule
        assert password_rule_mgr.class_name == 'password-rule'
        assert password_rule_mgr.session is self.session
        assert password_rule_mgr.parent is self.console
        assert password_rule_mgr.console is self.console

    @pytest.mark.parametrize(
        "full_properties_kwargs, prop_names", [
            (dict(full_properties=False),
             ['element-uri']),
            (dict(full_properties=True),
             ['element-uri', 'name']),
            (dict(),  # test default for full_properties (True)
             ['element-uri', 'name']),
        ]
    )
    @pytest.mark.parametrize(
        "filter_args, exp_names", [
            (None,
             ['a', 'b']),
            ({},
             ['a', 'b']),
            ({'name': 'a'},
             ['a']),
        ]
    )
    def test_pr_manager_list(
            self, filter_args, exp_names, full_properties_kwargs, prop_names):
        """Test PasswordRuleManager.list()."""

        faked_password_rule1 = self.add_password_rule(
            name='a', type_='user-defined')
        faked_password_rule2 = self.add_password_rule(
            name='b', type_='system-defined')
        faked_password_rules = [faked_password_rule1, faked_password_rule2]
        exp_faked_password_rules = [u for u in faked_password_rules
                                    if u.name in exp_names]
        password_rule_mgr = self.console.password_rules

        # Execute the code to be tested
        password_rules = password_rule_mgr.list(filter_args=filter_args,
                                                **full_properties_kwargs)

        assert_resources(password_rules, exp_faked_password_rules, prop_names)

    @pytest.mark.parametrize(
        "input_props, exp_prop_names, exp_exc", [
            ({},  # props missing
             None,
             HTTPError({'http-status': 400, 'reason': 5})),
            ({'description': 'fake description X'},  # props missing
             None,
             HTTPError({'http-status': 400, 'reason': 5})),
            ({'description': 'fake description X',
              'name': 'a'},
             ['element-uri', 'name', 'description'],
             None),
        ]
    )
    def test_pr_manager_create(self, input_props, exp_prop_names, exp_exc):
        """Test PasswordRuleManager.create()."""

        password_rule_mgr = self.console.password_rules

        if exp_exc is not None:

            with pytest.raises(exp_exc.__class__) as exc_info:

                # Execute the code to be tested
                password_rule_mgr.create(properties=input_props)

            exc = exc_info.value
            if isinstance(exp_exc, HTTPError):
                assert exc.http_status == exp_exc.http_status
                assert exc.reason == exp_exc.reason

        else:

            # Execute the code to be tested.
            password_rule = password_rule_mgr.create(properties=input_props)

            # Check the resource for consistency within itself
            assert isinstance(password_rule, PasswordRule)
            password_rule_name = password_rule.name
            exp_password_rule_name = password_rule.properties['name']
            assert password_rule_name == exp_password_rule_name
            password_rule_uri = password_rule.uri
            exp_password_rule_uri = password_rule.properties['element-uri']
            assert password_rule_uri == exp_password_rule_uri

            # Check the properties against the expected names and values
            for prop_name in exp_prop_names:
                assert prop_name in password_rule.properties
                if prop_name in input_props:
                    value = password_rule.properties[prop_name]
                    exp_value = input_props[prop_name]
                    assert value == exp_value

    def test_pr_repr(self):
        """Test PasswordRule.__repr__()."""

        faked_password_rule1 = self.add_password_rule(
            name='a', type_='user-defined')
        password_rule1 = self.console.password_rules.find(
            name=faked_password_rule1.name)

        # Execute the code to be tested
        repr_str = repr(password_rule1)

        repr_str = repr_str.replace('\n', '\\n')
        # We check just the begin of the string:
        assert re.match(r'^{classname}\s+at\s+0x{id:08x}\s+\(\\n.*'.
                        format(classname=password_rule1.__class__.__name__,
                               id=id(password_rule1)),
                        repr_str)

    @pytest.mark.parametrize(
        "input_props, exp_exc", [
            ({'name': 'a',
              'type': 'user-defined'},
             None),
            ({'name': 'b',
              'type': 'system-defined'},
             None),
        ]
    )
    def test_pr_delete(self, input_props, exp_exc):
        """Test PasswordRule.delete()."""

        faked_password_rule = self.add_password_rule(
            name=input_props['name'],
            type_=input_props['type'])

        password_rule_mgr = self.console.password_rules
        password_rule = password_rule_mgr.find(name=faked_password_rule.name)

        if exp_exc is not None:

            with pytest.raises(exp_exc.__class__) as exc_info:

                # Execute the code to be tested
                password_rule.delete()

            exc = exc_info.value
            if isinstance(exp_exc, HTTPError):
                assert exc.http_status == exp_exc.http_status
                assert exc.reason == exp_exc.reason

            # Check that the Password Rule still exists
            password_rule_mgr.find(name=faked_password_rule.name)

        else:

            # Execute the code to be tested.
            password_rule.delete()

            # Check that the Password Rule no longer exists
            with pytest.raises(NotFound) as exc_info:
                password_rule_mgr.find(name=faked_password_rule.name)

    def test_pr_delete_create_same_name(self):
        """Test PasswordRule.delete() followed by create() with same name."""

        password_rule_name = 'faked_a'

        # Add the Password Rule to be tested
        self.add_password_rule(name=password_rule_name, type_='user-defined')

        # Input properties for a Password Rule with the same name
        sn_password_rule_props = {
            'name': password_rule_name,
            'description': 'Password Rule with same name',
            'type': 'user-defined',
        }

        password_rule_mgr = self.console.password_rules
        password_rule = password_rule_mgr.find(name=password_rule_name)

        # Execute the deletion code to be tested
        password_rule.delete()

        # Check that the Password Rule no longer exists
        with pytest.raises(NotFound):
            password_rule_mgr.find(name=password_rule_name)

        # Execute the creation code to be tested.
        password_rule_mgr.create(sn_password_rule_props)

        # Check that the Password Rule exists again under that name
        sn_password_rule = password_rule_mgr.find(name=password_rule_name)
        description = sn_password_rule.get_property('description')
        assert description == sn_password_rule_props['description']

    @pytest.mark.parametrize(
        "input_props", [
            {},
            {'description': 'New Password Rule description'},
        ]
    )
    def test_pr_update_properties(self, input_props):
        """Test PasswordRule.update_properties()."""

        password_rule_name = 'faked_a'

        # Add the Password Rule to be tested
        self.add_password_rule(name=password_rule_name, type_='user-defined')

        password_rule_mgr = self.console.password_rules
        password_rule = password_rule_mgr.find(name=password_rule_name)

        password_rule.pull_full_properties()
        saved_properties = copy.deepcopy(password_rule.properties)

        # Execute the code to be tested
        password_rule.update_properties(properties=input_props)

        # Verify that the resource object already reflects the property
        # updates.
        for prop_name in saved_properties:
            if prop_name in input_props:
                exp_prop_value = input_props[prop_name]
            else:
                exp_prop_value = saved_properties[prop_name]
            assert prop_name in password_rule.properties
            prop_value = password_rule.properties[prop_name]
            assert prop_value == exp_prop_value, \
                "Unexpected value for property {!r}".format(prop_name)

        # Refresh the resource object and verify that the resource object
        # still reflects the property updates.
        password_rule.pull_full_properties()
        for prop_name in saved_properties:
            if prop_name in input_props:
                exp_prop_value = input_props[prop_name]
            else:
                exp_prop_value = saved_properties[prop_name]
            assert prop_name in password_rule.properties
            prop_value = password_rule.properties[prop_name]
            assert prop_value == exp_prop_value
Пример #10
0
    classname = exc.__class__.__name__

    # Execute the code to be tested
    str_def = exc.str_def()

    str_def = ' ' + str_def
    assert str_def.find(' classname={!r};'.format(classname)) >= 0
    assert str_def.find(' message={!r};'.format(msg)) >= 0


@pytest.mark.parametrize(
    # Input and expected arguments.
    "args",
    [
        # (msg, details)
        ("fake msg", HTTPError(HTTP_ERROR_1)),
        ("", HTTPError(HTTP_ERROR_1)),
        (None, HTTPError(HTTP_ERROR_1)),
    ])
@pytest.mark.parametrize(
    # Whether each input arg is passed as pos.arg (None) or keyword arg
    # (arg name), or is defaulted (omitted from right).
    "arg_names",
    [
        (None, None),
        (None, 'details'),
        ('msg', 'details'),
    ])
def test_serverautherror_initial_attrs(arg_names, args):
    """Test initial attributes of ServerAuthError."""
class TestStorageGroup(object):
    """All tests for the StorageGroup and StorageGroupManager classes."""

    def setup_method(self):
        """
        Setup that is called by pytest before each test method.

        Set up a faked session, and add a faked CPC in DPM mode without any
        child resources.
        """
        # pylint: disable=attribute-defined-outside-init

        self.session = FakedSession('fake-host', 'fake-hmc', '2.14.1', '1.9')
        self.client = Client(self.session)
        self.faked_cpc = self.session.hmc.cpcs.add({
            'object-id': CPC_OID,
            # object-uri is set up automatically
            'parent': None,
            'class': 'cpc',
            'name': 'fake-cpc1-name',
            'description': 'CPC #1 (DPM mode, storage mgmt feature enabled)',
            'status': 'active',
            'dpm-enabled': True,
            'is-ensemble-member': False,
            'iml-mode': 'dpm',
            'available-features-list': [
                dict(name='dpm-storage-management', state=True),
            ],
        })
        assert self.faked_cpc.uri == CPC_URI
        self.cpc = self.client.cpcs.find(name='fake-cpc1-name')
        self.faked_console = self.session.hmc.consoles.add({
            # object-id is set up automatically
            # object-uri is set up automatically
            # parent will be automatically set
            # class will be automatically set
            'name': 'fake-console-name',
            'description': 'The HMC',
        })
        self.console = self.client.consoles.console

    def add_storage_group1(self):
        """Add storage group 1 (type fcp)."""

        faked_storage_group = self.faked_console.storage_groups.add({
            'object-id': SG1_OID,
            # object-uri will be automatically set
            # parent will be automatically set
            # class will be automatically set
            'cpc-uri': CPC_URI,
            'name': SG1_NAME,
            'description': 'Storage Group #1',
            'type': 'fcp',
            'shared': False,
            'fulfillment-state': 'complete',
            'connectivity': 4,
        })
        return faked_storage_group

    def add_storage_group2(self):
        """Add storage group 2 (type fc)."""

        faked_storage_group = self.faked_console.storage_groups.add({
            'object-id': SG2_OID,
            # object-uri will be automatically set
            # parent will be automatically set
            # class will be automatically set
            'cpc-uri': CPC_URI,
            'name': SG2_NAME,
            'description': 'Storage Group #2',
            'type': 'fc',
            'shared': False,
            'fulfillment-state': 'complete',
            'connectivity': 4,
        })
        return faked_storage_group

    def test_sm_initial_attrs(self):
        """Test initial attributes of StorageGroupManager."""

        storage_group_mgr = self.console.storage_groups

        assert isinstance(storage_group_mgr, StorageGroupManager)

        # Verify all public properties of the manager object
        assert storage_group_mgr.resource_class == StorageGroup
        assert storage_group_mgr.session == self.session
        assert storage_group_mgr.parent == self.console
        assert storage_group_mgr.console == self.console

    # TODO: Test for StorageGroupManager.__repr__()

    testcases_sm_list_full_properties = (
        "full_properties_kwargs, prop_names", [
            (dict(),
             ['object-uri', 'cpc-uri', 'name', 'fulfillment-state', 'type']),
            (dict(full_properties=False),
             ['object-uri', 'cpc-uri', 'name', 'fulfillment-state', 'type']),
            (dict(full_properties=True),
             None),
        ]
    )

    @pytest.mark.parametrize(
        *testcases_sm_list_full_properties
    )
    def test_sm_list_full_properties(
            self, full_properties_kwargs, prop_names):
        """Test StorageGroupManager.list() with full_properties."""

        # Add two faked storage groups
        faked_storage_group1 = self.add_storage_group1()
        faked_storage_group2 = self.add_storage_group2()

        exp_faked_storage_groups = [faked_storage_group1, faked_storage_group2]
        storage_group_mgr = self.console.storage_groups

        # Execute the code to be tested
        storage_groups = storage_group_mgr.list(**full_properties_kwargs)

        assert_resources(storage_groups, exp_faked_storage_groups, prop_names)

    testcases_sm_list_filter_args = (
        "filter_args, exp_names", [
            ({'object-id': SG1_OID},
             [SG1_NAME]),
            ({'object-id': SG2_OID},
             [SG2_NAME]),
            ({'object-id': [SG1_OID, SG2_OID]},
             [SG1_NAME, SG2_NAME]),
            ({'object-id': [SG1_OID, SG1_OID]},
             [SG1_NAME]),
            ({'object-id': SG1_OID + 'foo'},
             []),
            ({'object-id': [SG1_OID, SG2_OID + 'foo']},
             [SG1_NAME]),
            ({'object-id': [SG2_OID + 'foo', SG1_OID]},
             [SG1_NAME]),
            ({'name': SG1_NAME},
             [SG1_NAME]),
            ({'name': SG2_NAME},
             [SG2_NAME]),
            ({'name': [SG1_NAME, SG2_NAME]},
             [SG1_NAME, SG2_NAME]),
            ({'name': SG1_NAME + 'foo'},
             []),
            ({'name': [SG1_NAME, SG2_NAME + 'foo']},
             [SG1_NAME]),
            ({'name': [SG2_NAME + 'foo', SG1_NAME]},
             [SG1_NAME]),
            ({'name': [SG1_NAME, SG1_NAME]},
             [SG1_NAME]),
            ({'name': '.*sg 1'},
             [SG1_NAME]),
            ({'name': 'sg 1.*'},
             [SG1_NAME]),
            ({'name': 'sg .'},
             [SG1_NAME, SG2_NAME]),
            ({'name': '.g 1'},
             [SG1_NAME]),
            ({'name': '.+'},
             [SG1_NAME, SG2_NAME]),
            ({'name': 'sg 1.+'},
             []),
            ({'name': '.+sg 1'},
             []),
            ({'name': SG1_NAME,
              'object-id': SG1_OID},
             [SG1_NAME]),
            ({'name': SG1_NAME,
              'object-id': SG1_OID + 'foo'},
             []),
            ({'name': SG1_NAME + 'foo',
              'object-id': SG1_OID},
             []),
            ({'name': SG1_NAME + 'foo',
              'object-id': SG1_OID + 'foo'},
             []),
        ]
    )

    @pytest.mark.parametrize(
        *testcases_sm_list_filter_args
    )
    def test_sm_list_filter_args(
            self, filter_args, exp_names):
        """Test StorageGroupManager.list() with filter_args."""

        # Add two faked storage_groups
        self.add_storage_group1()
        self.add_storage_group2()

        storage_group_mgr = self.console.storage_groups

        # Execute the code to be tested
        storage_groups = storage_group_mgr.list(filter_args=filter_args)

        assert len(storage_groups) == len(exp_names)
        if exp_names:
            names = [p.properties['name'] for p in storage_groups]
            assert set(names) == set(exp_names)

    testcases_sm_create_no_volumes = (
        "input_props, exp_prop_names, exp_exc", [
            ({},
             None,
             HTTPError({'http-status': 400, 'reason': 5})),
            ({'description': 'fake description X'},
             None,
             HTTPError({'http-status': 400, 'reason': 5})),
            ({'name': 'fake-sg-x'},
             None,
             HTTPError({'http-status': 400, 'reason': 5})),
            ({'name': 'fake-sg-x',
              'cpc-uri': CPC_URI,
              'type': 'fcp'},
             ['object-uri', 'name', 'cpc-uri'],
             None),
        ]
    )

    @pytest.mark.parametrize(
        *testcases_sm_create_no_volumes
    )
    def test_sm_create(
            self, input_props, exp_prop_names, exp_exc):
        """Test StorageGroupManager.create()."""

        # TODO: Add logic and test cases for creating initial storage volumes

        storage_group_mgr = self.console.storage_groups

        if exp_exc is not None:

            with pytest.raises(exp_exc.__class__) as exc_info:

                # Execute the code to be tested
                storage_group = storage_group_mgr.create(
                    properties=input_props)

            exc = exc_info.value
            if isinstance(exp_exc, HTTPError):
                assert exc.http_status == exp_exc.http_status
                assert exc.reason == exp_exc.reason

        else:

            # Execute the code to be tested.
            # Note: the StorageGroup object returned by StorageGroup.create()
            # has the input properties plus 'object-uri'.
            storage_group = storage_group_mgr.create(properties=input_props)

            # Check the resource for consistency within itself
            assert isinstance(storage_group, StorageGroup)
            storage_group_name = storage_group.name
            exp_storage_group_name = storage_group.properties['name']
            assert storage_group_name == exp_storage_group_name
            storage_group_uri = storage_group.uri
            exp_storage_group_uri = storage_group.properties['object-uri']
            assert storage_group_uri == exp_storage_group_uri

            # Check the properties against the expected names and values
            for prop_name in exp_prop_names:
                assert prop_name in storage_group.properties
                if prop_name in input_props:
                    value = storage_group.properties[prop_name]
                    exp_value = input_props[prop_name]
                    assert value == exp_value

    def test_sm_resource_object(self):
        """
        Test StorageGroupManager.resource_object().

        This test exists for historical reasons, and by now is covered by the
        test for BaseManager.resource_object().
        """

        # Add a faked storage_group
        faked_storage_group = self.add_storage_group1()
        storage_group_oid = faked_storage_group.oid

        storage_group_mgr = self.console.storage_groups

        # Execute the code to be tested
        storage_group = storage_group_mgr.resource_object(storage_group_oid)

        storage_group_uri = "/api/storage-groups/" + storage_group_oid

        sv_mgr = storage_group.storage_volumes
        vsr_mgr = storage_group.virtual_storage_resources

        assert isinstance(storage_group, StorageGroup)
        assert isinstance(sv_mgr, StorageVolumeManager)
        assert isinstance(vsr_mgr, VirtualStorageResourceManager)

        sg_cpc = storage_group.cpc
        assert isinstance(sg_cpc, Cpc)
        assert sg_cpc.uri == storage_group.properties['cpc-uri']

        # Note: Properties inherited from BaseResource are tested there,
        # but we test them again:
        assert storage_group.properties['object-uri'] == storage_group_uri
        assert storage_group.properties['object-id'] == storage_group_oid
        assert storage_group.properties['class'] == 'storage-group'
        assert storage_group.properties['parent'] == self.console.uri

    def test_sg_repr(self):
        """Test StorageGroup.__repr__()."""

        # Add a faked storage_group
        faked_storage_group = self.add_storage_group1()

        storage_group_mgr = self.console.storage_groups
        storage_group = storage_group_mgr.find(name=faked_storage_group.name)

        # Execute the code to be tested
        repr_str = repr(storage_group)

        repr_str = repr_str.replace('\n', '\\n')
        # We check just the begin of the string:
        assert re.match(r'^{classname}\s+at\s+0x{id:08x}\s+\(\\n.*'.
                        format(classname=storage_group.__class__.__name__,
                               id=id(storage_group)),
                        repr_str)

    def test_sg_delete_non_associated(self):
        """Test StorageGroup.delete() of non-associated storage group."""

        # Add a faked storage group to be tested and another one
        faked_storage_group = self.add_storage_group1()
        self.add_storage_group2()

        storage_group_mgr = self.console.storage_groups

        storage_group = storage_group_mgr.find(name=faked_storage_group.name)

        # Execute the code to be tested.
        storage_group.delete()

        # Check that the storage group no longer exists
        with pytest.raises(NotFound):
            storage_group_mgr.find(name=faked_storage_group.name)

    def test_sg_delete_create_same(self):
        """Test StorageGroup.delete() followed by create() with same name."""

        # Add a faked storage_group to be tested and another one
        faked_storage_group = self.add_storage_group1()
        storage_group_name = faked_storage_group.name
        self.add_storage_group2()

        # Construct the input properties for a third storage_group
        sg3_props = copy.deepcopy(faked_storage_group.properties)
        sg3_props['description'] = 'Third storage_group'

        storage_group_mgr = self.console.storage_groups
        storage_group = storage_group_mgr.find(name=storage_group_name)

        # Execute the deletion code to be tested.
        storage_group.delete()

        # Check that the storage_group no longer exists
        with pytest.raises(NotFound):
            storage_group_mgr.find(name=storage_group_name)

        # Execute the creation code to be tested.
        storage_group_mgr.create(sg3_props)

        # Check that the storage_group exists again under that name
        storage_group3 = storage_group_mgr.find(name=storage_group_name)
        description = storage_group3.get_property('description')
        assert description == 'Third storage_group'

    testcases_sg_update_properties_sgs = (
        "storage_group_name", [
            SG1_NAME,
            SG2_NAME,
        ]
    )

    testcases_sg_update_properties_props = (
        "input_props", [
            {},
            {'description': 'New storage_group description'},
            {'shared': True},
            {'connectivity': 8},
        ]
    )

    @pytest.mark.parametrize(
        *testcases_sg_update_properties_sgs
    )
    @pytest.mark.parametrize(
        *testcases_sg_update_properties_props
    )
    def test_sg_update_properties(
            self, input_props, storage_group_name):
        """Test StorageGroup.update_properties()."""

        # Add faked storage_groups
        self.add_storage_group1()
        self.add_storage_group2()

        storage_group_mgr = self.console.storage_groups
        storage_group = storage_group_mgr.find(name=storage_group_name)

        storage_group.pull_full_properties()
        saved_properties = copy.deepcopy(storage_group.properties)

        # Execute the code to be tested
        storage_group.update_properties(properties=input_props)

        # Verify that the resource object already reflects the property
        # updates.
        for prop_name in saved_properties:
            if prop_name in input_props:
                exp_prop_value = input_props[prop_name]
            else:
                exp_prop_value = saved_properties[prop_name]
            assert prop_name in storage_group.properties
            prop_value = storage_group.properties[prop_name]
            assert prop_value == exp_prop_value

        # Refresh the resource object and verify that the resource object
        # still reflects the property updates.
        storage_group.pull_full_properties()
        for prop_name in saved_properties:
            if prop_name in input_props:
                exp_prop_value = input_props[prop_name]
            else:
                exp_prop_value = saved_properties[prop_name]
            assert prop_name in storage_group.properties
            prop_value = storage_group.properties[prop_name]
            assert prop_value == exp_prop_value

    def test_sg_update_name(self):
        """
        Test StorageGroup.update_properties() with 'name' property.
        """

        # Add a faked storage_group
        faked_storage_group = self.add_storage_group1()
        storage_group_name = faked_storage_group.name

        storage_group_mgr = self.console.storage_groups
        storage_group = storage_group_mgr.find(name=storage_group_name)

        new_storage_group_name = "new-" + storage_group_name

        # Execute the code to be tested
        storage_group.update_properties(
            properties={'name': new_storage_group_name})

        # Verify that the resource is no longer found by its old name, using
        # list() (this does not use the name-to-URI cache).
        storage_groups_list = storage_group_mgr.list(
            filter_args=dict(name=storage_group_name))
        assert len(storage_groups_list) == 0

        # Verify that the resource is no longer found by its old name, using
        # find() (this uses the name-to-URI cache).
        with pytest.raises(NotFound):
            storage_group_mgr.find(name=storage_group_name)

        # Verify that the resource object already reflects the update, even
        # though it has not been refreshed yet.
        assert storage_group.properties['name'] == new_storage_group_name

        # Refresh the resource object and verify that it still reflects the
        # update.
        storage_group.pull_full_properties()
        assert storage_group.properties['name'] == new_storage_group_name

        # Verify that the resource can be found by its new name, using find()
        new_storage_group_find = storage_group_mgr.find(
            name=new_storage_group_name)
        assert new_storage_group_find.properties['name'] == \
            new_storage_group_name

        # Verify that the resource can be found by its new name, using list()
        new_storage_groups_list = storage_group_mgr.list(
            filter_args=dict(name=new_storage_group_name))
        assert len(new_storage_groups_list) == 1
        new_storage_group_list = new_storage_groups_list[0]
        assert new_storage_group_list.properties['name'] == \
            new_storage_group_name

    # TODO: Adjust to invoke a SG method
    @pytest.mark.parametrize(
        "initial_status, exp_exc", [
            ('stopped', None),
            ('terminated', HTTPError({'http-status': 409, 'reason': 1})),
            ('starting', HTTPError({'http-status': 409, 'reason': 1})),
            ('active', HTTPError({'http-status': 409, 'reason': 1})),
            ('stopping', HTTPError({'http-status': 409, 'reason': 1})),
            ('degraded', HTTPError({'http-status': 409, 'reason': 1})),
            ('reservation-error',
             HTTPError({'http-status': 409, 'reason': 1})),
            ('paused', HTTPError({'http-status': 409, 'reason': 1})),
        ]
    )
    def xtest_storagegroup_start(self, initial_status, exp_exc):
        """Test StorageGroup.start()."""

        # Add a faked storage_group
        faked_storage_group = self.add_storage_group1()

        # Set the initial status of the faked storage_group
        faked_storage_group.properties['status'] = initial_status

        storage_group_mgr = self.console.storage_groups
        storage_group = storage_group_mgr.find(name=faked_storage_group.name)

        if exp_exc is not None:

            with pytest.raises(exp_exc.__class__) as exc_info:

                # Execute the code to be tested
                storage_group.start()

            exc = exc_info.value
            if isinstance(exp_exc, HTTPError):
                assert exc.http_status == exp_exc.http_status
                assert exc.reason == exp_exc.reason

        else:

            # Execute the code to be tested.
            ret = storage_group.start()

            assert ret == {}

            storage_group.pull_full_properties()
            status = storage_group.get_property('status')
            assert status == 'active'
Пример #12
0
from zhmcclient import Client, Cpc, HTTPError
from zhmcclient_mock import FakedSession
from tests.common.utils import assert_resources

# Object IDs and names of our faked CPCs:
CPC1_NAME = 'cpc 1'  # z13s in DPM mode
CPC1_OID = 'cpc1-oid'
CPC1_MAX_CRYPTO_DOMAINS = 40  # Crypto Express5S on a z13s
CPC2_NAME = 'cpc 2'  # z13s in classic mode
CPC2_OID = 'cpc2-oid'
CPC3_NAME = 'cpc 3'  # zEC12
CPC3_OID = 'cpc3-oid'
CPC4_NAME = 'cpc 4'  # z14-ZR1 in DPM mode
CPC4_OID = 'cpc4-oid'

HTTPError_404_1 = HTTPError({'http-status': 404, 'reason': 1})
HTTPError_409_5 = HTTPError({'http-status': 409, 'reason': 5})
HTTPError_409_4 = HTTPError({'http-status': 409, 'reason': 4})
HTTPError_400_7 = HTTPError({'http-status': 400, 'reason': 7})

# Names of our faked crypto adapters:
CRYPTO1_NAME = 'crypto 1'
CRYPTO2_NAME = 'crypto 2'

CPC1_UNUSED_CRYPTO_DOMAINS = list(range(4, CPC1_MAX_CRYPTO_DOMAINS))

GET_FREE_CRYPTO_DOMAINS_ENVIRONMENTS = {
    'env0-example': {
        'desc':
        "The example from the description of method "
        "Cpc.get_free_crypto_domains()",
Пример #13
0
class TestUser(object):
    """All tests for the User and UserManager classes."""

    def setup_method(self):
        """
        Setup that is called by pytest before each test method.

        Set up a faked session, and add a faked Console without any
        child resources.
        """
        # pylint: disable=attribute-defined-outside-init

        self.session = FakedSession('fake-host', 'fake-hmc', '2.13.1', '1.8')
        self.client = Client(self.session)

        self.faked_console = self.session.hmc.consoles.add({
            'object-id': None,
            # object-uri will be automatically set
            'parent': None,
            'class': 'console',
            'name': 'fake-console1',
            'description': 'Console #1',
        })
        self.console = self.client.consoles.find(name=self.faked_console.name)

    def add_user(self, name, type_):
        """
        Add a faked user object to the faked Console and return it.
        """
        faked_user = self.faked_console.users.add({
            'object-id': 'oid-{}'.format(name),
            # object-uri will be automatically set
            'parent': '/api/console',
            'class': 'user',
            'name': name,
            'description': 'User {}'.format(name),
            'type': type_,
            'authentication-type': 'local',
        })
        return faked_user

    def add_user_role(self, name, type_):
        """
        Add a faked user role object to the faked Console and return it.
        """
        faked_user_role = self.faked_console.user_roles.add({
            'object-id': 'oid-{}'.format(name),
            # object-uri will be automatically set
            'parent': '/api/console',
            'class': 'user-role',
            'name': name,
            'description': 'User Role {}'.format(name),
            'type': type_,
        })
        return faked_user_role

    def test_user_manager_repr(self):
        """Test UserManager.__repr__()."""

        user_mgr = self.console.users

        # Execute the code to be tested
        repr_str = repr(user_mgr)

        repr_str = repr_str.replace('\n', '\\n')
        # We check just the begin of the string:
        assert re.match(r'^{classname}\s+at\s+0x{id:08x}\s+\(\\n.*'.
                        format(classname=user_mgr.__class__.__name__,
                               id=id(user_mgr)),
                        repr_str)

    def test_user_manager_initial_attrs(self):
        """Test initial attributes of UserManager."""

        user_mgr = self.console.users

        # Verify all public properties of the manager object
        assert user_mgr.resource_class == User
        assert user_mgr.class_name == 'user'
        assert user_mgr.session is self.session
        assert user_mgr.parent is self.console
        assert user_mgr.console is self.console

    @pytest.mark.parametrize(
        "full_properties_kwargs, prop_names", [
            (dict(full_properties=False),
             ['object-uri']),
            (dict(full_properties=True),
             ['object-uri', 'name']),
            (dict(),  # test default for full_properties (True)
             ['object-uri', 'name']),
        ]
    )
    @pytest.mark.parametrize(
        "filter_args, exp_names", [
            (None,
             ['a', 'b']),
            ({},
             ['a', 'b']),
            ({'name': 'a'},
             ['a']),
        ]
    )
    def test_user_manager_list(
            self, filter_args, exp_names, full_properties_kwargs, prop_names):
        """Test UserManager.list()."""

        faked_user1 = self.add_user(name='a', type_='standard')
        faked_user2 = self.add_user(name='b', type_='standard')
        faked_users = [faked_user1, faked_user2]
        exp_faked_users = [u for u in faked_users if u.name in exp_names]
        user_mgr = self.console.users

        # Execute the code to be tested
        users = user_mgr.list(filter_args=filter_args,
                              **full_properties_kwargs)

        assert_resources(users, exp_faked_users, prop_names)

    @pytest.mark.parametrize(
        "input_props, exp_prop_names, exp_exc", [
            ({},
             None,
             HTTPError({'http-status': 400, 'reason': 5})),
            ({'description': 'fake description X'},
             None,
             HTTPError({'http-status': 400, 'reason': 5})),
            ({'name': 'fake-name-x'},
             None,
             HTTPError({'http-status': 400, 'reason': 5})),
            ({'name': 'fake-name-x',
              'type': 'standard'},
             None,
             HTTPError({'http-status': 400, 'reason': 5})),
            ({'name': 'fake-name-x',
              'type': 'standard',
              'authentication-type': 'local'},
             ['object-uri', 'name', 'type', 'authentication-type'],
             None),
            ({'name': 'fake-name-x',
              'type': 'standard',
              'authentication-type': 'local',
              'description': 'fake description X'},
             ['object-uri', 'name', 'type', 'authentication-type',
              'description'],
             None),
        ]
    )
    def test_user_manager_create(self, input_props, exp_prop_names, exp_exc):
        """Test UserManager.create()."""

        user_mgr = self.console.users

        if exp_exc is not None:

            with pytest.raises(exp_exc.__class__) as exc_info:

                # Execute the code to be tested
                user_mgr.create(properties=input_props)

            exc = exc_info.value
            if isinstance(exp_exc, HTTPError):
                assert exc.http_status == exp_exc.http_status
                assert exc.reason == exp_exc.reason

        else:

            # Execute the code to be tested.
            user = user_mgr.create(properties=input_props)

            # Check the resource for consistency within itself
            assert isinstance(user, User)
            user_name = user.name
            exp_user_name = user.properties['name']
            assert user_name == exp_user_name
            user_uri = user.uri
            exp_user_uri = user.properties['object-uri']
            assert user_uri == exp_user_uri

            # Check the properties against the expected names and values
            for prop_name in exp_prop_names:
                assert prop_name in user.properties
                if prop_name in input_props:
                    value = user.properties[prop_name]
                    exp_value = input_props[prop_name]
                    assert value == exp_value

    def test_user_repr(self):
        """Test User.__repr__()."""

        faked_user1 = self.add_user(name='a', type_='standard')
        user1 = self.console.users.find(name=faked_user1.name)

        # Execute the code to be tested
        repr_str = repr(user1)

        repr_str = repr_str.replace('\n', '\\n')
        # We check just the begin of the string:
        assert re.match(r'^{classname}\s+at\s+0x{id:08x}\s+\(\\n.*'.
                        format(classname=user1.__class__.__name__,
                               id=id(user1)),
                        repr_str)

    @pytest.mark.parametrize(
        "input_name, input_type, exp_exc", [
            ('a', 'standard', None),
            ('b', 'template', None),
            ('c', 'pattern-based',
             HTTPError({'http-status': 400, 'reason': 312})),
            ('d', 'system-defined', None),
        ]
    )
    def test_user_delete(self, input_name, input_type, exp_exc):
        """Test User.delete()."""

        faked_user = self.add_user(name=input_name, type_=input_type)
        user_mgr = self.console.users
        user = user_mgr.find(name=faked_user.name)

        if exp_exc is not None:

            with pytest.raises(exp_exc.__class__) as exc_info:

                # Execute the code to be tested
                user.delete()

            exc = exc_info.value
            if isinstance(exp_exc, HTTPError):
                assert exc.http_status == exp_exc.http_status
                assert exc.reason == exp_exc.reason

            # Check that the user still exists
            user_mgr.find(name=faked_user.name)

        else:

            # Execute the code to be tested.
            user.delete()

            # Check that the user no longer exists
            with pytest.raises(NotFound) as exc_info:
                user_mgr.find(name=faked_user.name)

    def test_user_delete_create_same_name(self):
        """Test User.delete() followed by create() with same name."""

        user_name = 'faked_a'

        # Add the user to be tested
        self.add_user(name=user_name, type_='standard')

        # Input properties for a user with the same name
        sn_user_props = {
            'name': user_name,
            'description': 'User with same name',
            'type': 'standard',
            'authentication-type': 'local',
        }

        user_mgr = self.console.users
        user = user_mgr.find(name=user_name)

        # Execute the deletion code to be tested
        user.delete()

        # Check that the user no longer exists
        with pytest.raises(NotFound):
            user_mgr.find(name=user_name)

        # Execute the creation code to be tested.
        user_mgr.create(sn_user_props)

        # Check that the user exists again under that name
        sn_user = user_mgr.find(name=user_name)
        description = sn_user.get_property('description')
        assert description == sn_user_props['description']

    @pytest.mark.parametrize(
        "input_props", [
            {},
            {'description': 'New user description'},
            {'authentication-type': 'ldap',
             'description': 'New user description'},
        ]
    )
    def test_user_update_properties(self, input_props):
        """Test User.update_properties()."""

        # Add the user to be tested
        faked_user = self.add_user(name='a', type_='standard')

        user_mgr = self.console.users
        user = user_mgr.find(name=faked_user.name)

        user.pull_full_properties()
        saved_props = copy.deepcopy(user.properties)

        # Execute the code to be tested
        user.update_properties(properties=input_props)

        # Verify that the resource object already reflects the property
        # updates.
        for prop_name in saved_props:
            if prop_name in input_props:
                exp_prop_value = input_props[prop_name]
            else:
                exp_prop_value = saved_props[prop_name]
            assert prop_name in user.properties
            prop_value = user.properties[prop_name]
            assert prop_value == exp_prop_value, \
                "Unexpected value for property {!r}".format(prop_name)

        # Refresh the resource object and verify that the resource object
        # still reflects the property updates.
        user.pull_full_properties()
        for prop_name in saved_props:
            if prop_name in input_props:
                exp_prop_value = input_props[prop_name]
            else:
                exp_prop_value = saved_props[prop_name]
            assert prop_name in user.properties
            prop_value = user.properties[prop_name]
            assert prop_value == exp_prop_value

    @pytest.mark.parametrize(
        "user_name, user_type, exp_exc", [
            ('a', 'standard', None),
            ('b', 'template', None),
            ('c', 'pattern-based',
             HTTPError({'http-status': 400, 'reason': 314})),
            ('d', 'system-defined',
             HTTPError({'http-status': 400, 'reason': 314})),
        ]
    )
    @pytest.mark.parametrize(
        "role_name, role_type", [
            ('ra', 'user-defined'),
            ('rb', 'system-defined'),
        ]
    )
    def test_user_add_user_role(
            self, role_name, role_type, user_name, user_type, exp_exc):
        """Test User.add_user_role()."""

        faked_user = self.add_user(name=user_name, type_=user_type)
        user_mgr = self.console.users
        user = user_mgr.find(name=faked_user.name)

        faked_user_role = self.add_user_role(name=role_name, type_=role_type)
        user_role_mgr = self.console.user_roles
        user_role = user_role_mgr.find(name=faked_user_role.name)

        if exp_exc is not None:

            with pytest.raises(exp_exc.__class__) as exc_info:

                # Execute the code to be tested
                user.add_user_role(user_role)

            exc = exc_info.value
            if isinstance(exp_exc, HTTPError):
                assert exc.http_status == exp_exc.http_status
                assert exc.reason == exp_exc.reason

            # Check that the user does not have that user role
            user.pull_full_properties()
            if 'user-roles' in user.properties:
                user_role_uris = user.properties['user-roles']
                user_role_uri = user_role.uri
                assert user_role_uri not in user_role_uris

        else:

            # Execute the code to be tested.
            ret = user.add_user_role(user_role)

            assert ret is None

            # Check that the user has that user role
            user.pull_full_properties()
            assert 'user-roles' in user.properties
            user_role_uris = user.properties['user-roles']
            user_role_uri = user_role.uri
            assert user_role_uri in user_role_uris

    @pytest.mark.parametrize(
        "user_name, user_type, exp_exc", [
            ('a', 'standard', None),
            ('b', 'template', None),
            ('c', 'pattern-based',
             HTTPError({'http-status': 400, 'reason': 314})),
            ('d', 'system-defined',
             HTTPError({'http-status': 400, 'reason': 314})),
        ]
    )
    @pytest.mark.parametrize(
        "role_name, role_type", [
            ('ra', 'user-defined'),
            ('rb', 'system-defined'),
        ]
    )
    def test_user_remove_user_role(
            self, role_name, role_type, user_name, user_type, exp_exc):
        """Test User.remove_user_role()."""

        faked_user = self.add_user(name=user_name, type_=user_type)
        user_mgr = self.console.users
        user = user_mgr.find(name=faked_user.name)

        faked_user_role = self.add_user_role(name=role_name, type_=role_type)
        user_role_mgr = self.console.user_roles
        user_role = user_role_mgr.find(name=faked_user_role.name)

        # Prepare the user with the initial user role
        if 'user-roles' not in faked_user.properties:
            faked_user.properties['user-roles'] = []
        faked_user.properties['user-roles'].append(faked_user_role.uri)

        if exp_exc is not None:

            with pytest.raises(exp_exc.__class__) as exc_info:

                # Execute the code to be tested
                user.remove_user_role(user_role)

            exc = exc_info.value
            if isinstance(exp_exc, HTTPError):
                assert exc.http_status == exp_exc.http_status
                assert exc.reason == exp_exc.reason

            # Check that the user still has that user role
            user.pull_full_properties()
            if 'user-roles' in user.properties:
                user_role_uris = user.properties['user-roles']
                user_role_uri = user_role.uri
                assert user_role_uri in user_role_uris

        else:

            # Execute the code to be tested.
            ret = user.remove_user_role(user_role)

            assert ret is None

            # Check that the user no longer has that user role
            user.pull_full_properties()
            assert 'user-roles' in user.properties
            user_role_uris = user.properties['user-roles']
            user_role_uri = user_role.uri
            assert user_role_uri not in user_role_uris
Пример #14
0
class TestMetricsContext(object):
    """All tests for the MetricsContext and MetricsContextManager classes."""
    def setup_method(self):
        """
        Set up a faked session, and add a faked CPC in DPM mode without any
        child resources.
        """

        self.session = FakedSession('fake-host', 'fake-hmc', '2.13.1', '1.8')
        self.client = Client(self.session)

    def add_metricgroupdefinition1(self):
        """Add metric group definition 1."""

        faked_metricgroupdefinition = FakedMetricGroupDefinition(
            name=MG1_NAME,
            types=[
                ('faked-metric11', 'integer-metric'),
                ('faked-metric12', 'string-metric'),
            ])
        self.session.hmc.metrics_contexts.add_metric_group_definition(
            faked_metricgroupdefinition)
        return faked_metricgroupdefinition

    def add_metricgroupdefinition2(self):
        """Add metric group definition 2."""

        faked_metricgroupdefinition = FakedMetricGroupDefinition(
            name=MG2_NAME, types=[
                ('faked-metric21', 'boolean-metric'),
            ])
        self.session.hmc.metrics_contexts.add_metric_group_definition(
            faked_metricgroupdefinition)
        return faked_metricgroupdefinition

    def add_metricscontext1(self):
        """Add faked metrics context 1."""

        faked_metricscontext = self.session.hmc.metrics_contexts.add({
            'fake-id':
            MC1_OID,
            'anticipated-frequency-seconds':
            10,
            'metric-groups': [MG1_NAME, MG2_NAME],
        })
        return faked_metricscontext

    def add_metricscontext2(self):
        """Add faked metrics context 2."""

        faked_metricscontext = self.session.hmc.metrics_contexts.add({
            'fake-id':
            MC2_OID,
            'anticipated-frequency-seconds':
            10,
            'metric-groups': [MG1_NAME],
        })
        return faked_metricscontext

    def create_metricscontext1(self):
        """create (non-faked) metrics context 1."""

        mc_props = {
            'anticipated-frequency-seconds': 10,
            'metric-groups': [MG1_NAME, MG2_NAME],
        }
        metricscontext = self.client.metrics_contexts.create(mc_props)
        return metricscontext

    def create_metricscontext2(self):
        """create (non-faked) metrics context 2."""

        mc_props = {
            'anticipated-frequency-seconds': 10,
            'metric-groups': [MG1_NAME],
        }
        metricscontext = self.client.metrics_contexts.create(mc_props)
        return metricscontext

    def test_metricscontextmanager_initial_attrs(self):
        """Test initial attributes of MetricsContextManager."""

        metricscontext_mgr = self.client.metrics_contexts

        # Verify all public properties of the manager object
        assert metricscontext_mgr.resource_class == MetricsContext
        assert metricscontext_mgr.session == self.session
        assert metricscontext_mgr.parent is None
        assert metricscontext_mgr.client == self.client

    # TODO: Test for MetricsContextManager.__repr__()

    @pytest.mark.parametrize("full_properties_kwargs, prop_names", [
        (dict(), ['anticipated-frequency-seconds', 'metric-groups']),
        (dict(full_properties=False),
         ['anticipated-frequency-seconds', 'metric-groups']),
        (dict(full_properties=True),
         ['anticipated-frequency-seconds', 'metric-groups']),
    ])
    def test_metricscontextmanager_list_full_properties(
            self, full_properties_kwargs, prop_names):
        """Test MetricsContextManager.list() with full_properties."""

        # Add faked metric groups
        self.add_metricgroupdefinition1()
        self.add_metricgroupdefinition2()

        # Create (non-faked) metrics contexts (list() will only return those)
        metricscontext1 = self.create_metricscontext1()
        metricscontext2 = self.create_metricscontext2()

        exp_metricscontexts = [metricscontext1, metricscontext2]
        metricscontext_mgr = self.client.metrics_contexts

        # Execute the code to be tested
        metricscontexts = metricscontext_mgr.list(**full_properties_kwargs)

        assert_resources(metricscontexts, exp_metricscontexts, prop_names)

    @pytest.mark.parametrize("filter_args, exp_names", [
        ({
            'object-id': MC1_OID
        }, [MG1_NAME]),
        ({
            'object-id': MC2_OID
        }, [MG2_NAME]),
        ({
            'object-id': [MC1_OID, MC2_OID]
        }, [MG1_NAME, MG2_NAME]),
        ({
            'object-id': [MC1_OID, MC1_OID]
        }, [MG1_NAME]),
        ({
            'object-id': MC1_OID + 'foo'
        }, []),
        ({
            'object-id': [MC1_OID, MC2_OID + 'foo']
        }, [MG1_NAME]),
        ({
            'object-id': [MC2_OID + 'foo', MC1_OID]
        }, [MG1_NAME]),
        ({
            'name': MG1_NAME
        }, [MG1_NAME]),
        ({
            'name': MG2_NAME
        }, [MG2_NAME]),
        ({
            'name': [MG1_NAME, MG2_NAME]
        }, [MG1_NAME, MG2_NAME]),
        ({
            'name': MG1_NAME + 'foo'
        }, []),
        ({
            'name': [MG1_NAME, MG2_NAME + 'foo']
        }, [MG1_NAME]),
        ({
            'name': [MG2_NAME + 'foo', MG1_NAME]
        }, [MG1_NAME]),
        ({
            'name': [MG1_NAME, MG1_NAME]
        }, [MG1_NAME]),
        ({
            'name': '.*part 1'
        }, [MG1_NAME]),
        ({
            'name': 'part 1.*'
        }, [MG1_NAME]),
        ({
            'name': 'part .'
        }, [MG1_NAME, MG2_NAME]),
        ({
            'name': '.art 1'
        }, [MG1_NAME]),
        ({
            'name': '.+'
        }, [MG1_NAME, MG2_NAME]),
        ({
            'name': 'part 1.+'
        }, []),
        ({
            'name': '.+part 1'
        }, []),
        ({
            'name': MG1_NAME,
            'object-id': MC1_OID
        }, [MG1_NAME]),
        ({
            'name': MG1_NAME,
            'object-id': MC1_OID + 'foo'
        }, []),
        ({
            'name': MG1_NAME + 'foo',
            'object-id': MC1_OID
        }, []),
        ({
            'name': MG1_NAME + 'foo',
            'object-id': MC1_OID + 'foo'
        }, []),
    ])
    @pytest.mark.skip  # TODO: Test for MetricsContextManager.list() w/ filter
    def test_metricscontextmanager_list_filter_args(self, filter_args,
                                                    exp_names):
        """Test MetricsContextManager.list() with filter_args."""

        # Add faked metric groups
        self.add_metricgroupdefinition1()
        self.add_metricgroupdefinition2()

        # Create (non-faked) metrics contexts (list() will only return those)
        self.create_metricscontext1()
        self.create_metricscontext2()

        metricscontext_mgr = self.client.metrics_contexts

        # Execute the code to be tested
        metricscontexts = metricscontext_mgr.list(filter_args=filter_args)

        names = [p.properties['name'] for p in metricscontexts]
        assert set(names) == set(exp_names)

    @pytest.mark.parametrize("input_props, exp_prop_names, exp_exc", [
        ({}, None, HTTPError({
            'http-status': 400,
            'reason': 5
        })),
        ({
            'description': 'fake description X'
        }, None, HTTPError({
            'http-status': 400,
            'reason': 5
        })),
        ({
            'name': 'fake-part-x'
        }, None, HTTPError({
            'http-status': 400,
            'reason': 5
        })),
        ({
            'name': 'fake-part-x',
            'initial-memory': 1024
        }, None, HTTPError({
            'http-status': 400,
            'reason': 5
        })),
        ({
            'name': 'fake-part-x',
            'initial-memory': 1024,
            'maximum-memory': 1024
        }, ['object-uri', 'name', 'initial-memory', 'maximum-memory'], None),
        ({
            'name': 'fake-part-x',
            'initial-memory': 1024,
            'maximum-memory': 1024,
            'description': 'fake description X'
        }, [
            'object-uri', 'name', 'initial-memory', 'maximum-memory',
            'description'
        ], None),
    ])
    @pytest.mark.skip  # TODO: Test for MetricsContextManager.create()
    def test_metricscontextmanager_create(self, input_props, exp_prop_names,
                                          exp_exc):
        """Test MetricsContextManager.create()."""

        metricscontext_mgr = self.client.metrics_contexts

        if exp_exc is not None:

            with pytest.raises(exp_exc.__class__) as exc_info:

                # Execute the code to be tested
                metricscontext = metricscontext_mgr.create(
                    properties=input_props)

            exc = exc_info.value
            if isinstance(exp_exc, HTTPError):
                assert exc.http_status == exp_exc.http_status
                assert exc.reason == exp_exc.reason

        else:

            # Execute the code to be tested.
            # Note: the MetricsContext object returned by
            # MetricsContext.create() has the input properties plus
            # 'object-uri'.
            metricscontext = metricscontext_mgr.create(properties=input_props)

            # Check the resource for consistency within itself
            assert isinstance(metricscontext, MetricsContext)
            metricscontext_name = metricscontext.name
            exp_metricscontext_name = metricscontext.properties['name']
            assert metricscontext_name == exp_metricscontext_name
            metricscontext_uri = metricscontext.uri
            exp_metricscontext_uri = metricscontext.properties['object-uri']
            assert metricscontext_uri == exp_metricscontext_uri

            # Check the properties against the expected names and values
            for prop_name in exp_prop_names:
                assert prop_name in metricscontext.properties
                if prop_name in input_props:
                    value = metricscontext.properties[prop_name]
                    exp_value = input_props[prop_name]
                    assert value == exp_value

    @pytest.mark.skip  # TODO: Test for MetricsContextManager.__repr__()
    def test_metricscontext_repr(self):
        """Test MetricsContext.__repr__()."""

        # Add a faked metrics context
        faked_metricscontext = self.add_metricscontext1()

        metricscontext_mgr = self.client.metrics_contexts
        metricscontext = metricscontext_mgr.find(
            name=faked_metricscontext.name)

        # Execute the code to be tested
        repr_str = repr(metricscontext)

        repr_str = repr_str.replace('\n', '\\n')
        # We check just the begin of the string:
        assert re.match(
            r'^{classname}\s+at\s+0x{id:08x}\s+\(\\n.*'.format(
                classname=metricscontext.__class__.__name__,
                id=id(metricscontext)), repr_str)

    @pytest.mark.parametrize("initial_status, exp_exc", [
        ('stopped', None),
        ('terminated', HTTPError({
            'http-status': 409,
            'reason': 1
        })),
        ('starting', HTTPError({
            'http-status': 409,
            'reason': 1
        })),
        ('active', HTTPError({
            'http-status': 409,
            'reason': 1
        })),
        ('stopping', HTTPError({
            'http-status': 409,
            'reason': 1
        })),
        ('degraded', HTTPError({
            'http-status': 409,
            'reason': 1
        })),
        ('reservation-error', HTTPError({
            'http-status': 409,
            'reason': 1
        })),
        ('paused', HTTPError({
            'http-status': 409,
            'reason': 1
        })),
    ])
    @pytest.mark.skip  # TODO: Test for MetricsContext.delete()
    def test_metricscontext_delete(self, initial_status, exp_exc):
        """Test MetricsContext.delete()."""

        # Add a faked metrics context
        faked_metricscontext = self.add_metricscontext1()

        # Set the initial status of the faked metrics context
        faked_metricscontext.properties['status'] = initial_status

        metricscontext_mgr = self.client.metrics_contexts

        metricscontext = metricscontext_mgr.find(
            name=faked_metricscontext.name)

        if exp_exc is not None:

            with pytest.raises(exp_exc.__class__) as exc_info:

                # Execute the code to be tested
                metricscontext.delete()

            exc = exc_info.value
            if isinstance(exp_exc, HTTPError):
                assert exc.http_status == exp_exc.http_status
                assert exc.reason == exp_exc.reason

            # Check that the metrics context still exists
            metricscontext_mgr.find(name=faked_metricscontext.name)

        else:

            # Execute the code to be tested.
            metricscontext.delete()

            # Check that the metrics context no longer exists
            with pytest.raises(NotFound) as exc_info:
                metricscontext_mgr.find(name=faked_metricscontext.name)
Пример #15
0
class TestAdapter(object):
    """All tests for the Adapter and AdapterManager classes."""
    def setup_method(self):
        """
        Setup that is called by pytest before each test method.

        Set up a faked session, and add a faked CPC in DPM mode without any
        child resources.
        """
        # pylint: disable=attribute-defined-outside-init

        self.session = FakedSession('fake-host', 'fake-hmc', '2.13.1', '1.8')
        self.client = Client(self.session)

        self.faked_cpc = self.session.hmc.cpcs.add({
            'object-id': 'fake-cpc1-oid',
            # object-uri is set up automatically
            'parent': None,
            'class': 'cpc',
            'name': 'fake-cpc1-name',
            'description': 'CPC #1 (DPM mode)',
            'status': 'active',
            'dpm-enabled': True,
            'is-ensemble-member': False,
            'iml-mode': 'dpm',
            'machine-type': '2964',  # z13
        })
        self.cpc = self.client.cpcs.find(name='fake-cpc1-name')

    def add_standard_osa(self):
        """Add a standard OSA adapter with one port to the faked HMC."""

        # Adapter properties that will be auto-set:
        # - object-uri
        # - adapter-family
        # - network-port-uris (to empty array)
        faked_osa1 = self.faked_cpc.adapters.add({
            'object-id':
            OSA1_OID,
            'parent':
            self.faked_cpc.uri,
            'class':
            'adapter',
            'name':
            OSA1_NAME,
            'description':
            'OSA #1',
            'status':
            'active',
            'type':
            'osd',
            'adapter-id':
            '123',
            'detected-card-type':
            'osa-express-5s-10gb',
            'card-location':
            '1234-5678-J.01',
            'port-count':
            1,
            'network-port-uris': [],
            'state':
            'online',
            'configured-capacity':
            80,
            'used-capacity':
            0,
            'allowed-capacity':
            80,
            'maximum-total-capacity':
            80,
            'physical-channel-status':
            'operating',
        })

        # Port properties that will be auto-set:
        # - element-uri
        # Properties in parent adapter that will be auto-set:
        # - network-port-uris
        faked_osa1.ports.add({
            'element-id': 'fake-port11-oid',
            'parent': faked_osa1.uri,
            'class': 'network-port',
            'index': 0,
            'name': 'fake-port11-name',
            'description': 'OSA #1 Port #1',
        })
        return faked_osa1

    def add_standard_hipersocket(self):
        """Add a standard Hipersocket adapter with one port to the faked
        HMC."""

        # Adapter properties that will be auto-set:
        # - object-uri
        # - adapter-family
        # - network-port-uris (to empty array)
        faked_hs2 = self.faked_cpc.adapters.add({
            'object-id':
            HS2_OID,
            'parent':
            self.faked_cpc.uri,
            'class':
            'adapter',
            'name':
            HS2_NAME,
            'description':
            'Hipersocket #2',
            'status':
            'active',
            'type':
            'hipersockets',
            'adapter-id':
            '123',
            'detected-card-type':
            'hipersockets',
            'port-count':
            1,
            'network-port-uris': [],
            'state':
            'online',
            'configured-capacity':
            32,
            'used-capacity':
            0,
            'allowed-capacity':
            32,
            'maximum-total-capacity':
            32,
            'physical-channel-status':
            'operating',
            'maximum-transmission-unit-size':
            56,
        })

        # Port properties that will be auto-set:
        # - element-uri
        # Properties in parent adapter that will be auto-set:
        # - network-port-uris
        faked_hs2.ports.add({
            'element-id': 'fake-port21-oid',
            'parent': faked_hs2.uri,
            'class': 'network-port',
            'index': 0,
            'name': 'fake-port21-name',
            'description': 'Hipersocket #2 Port #1',
        })
        return faked_hs2

    @staticmethod
    def add_crypto_ce5s(faked_cpc):
        """Add a Crypto Express 5S adapter to a faked CPC."""

        # Adapter properties that will be auto-set:
        # - object-uri
        # - adapter-family
        faked_adapter = faked_cpc.adapters.add({
            'object-id': 'fake-ce5s-oid',
            'parent': faked_cpc.uri,
            'class': 'adapter',
            'name': 'fake-ce5s-name',
            'description': 'Crypto Express 5S #1',
            'status': 'active',
            'type': 'crypto',
            'adapter-id': '123',
            'detected-card-type': 'crypto-express-5s',
            'card-location': 'vvvv-wwww',
            'state': 'online',
            'physical-channel-status': 'operating',
            'crypto-number': 7,
            'crypto-type': 'ep11-coprocessor',
            'udx-loaded': False,
            'tke-commands-enabled': False,
        })
        return faked_adapter

    @staticmethod
    def add_ficon_fe6sp(faked_cpc):
        """Add a not-configured FICON Express 6S+ adapter to a faked CPC."""

        # Adapter properties that will be auto-set:
        # - object-uri
        # - storage-port-uris
        faked_ficon_adapter = faked_cpc.adapters.add({
            'object-id':
            'fake-ficon6s-oid',
            'parent':
            faked_cpc.uri,
            'class':
            'adapter',
            'name':
            'fake-ficon6s-name',
            'description':
            'FICON Express 6S+ #1',
            'status':
            'active',
            'type':
            'not-configured',
            'adapter-id':
            '124',
            'adapter-family':
            'ficon',
            'detected-card-type':
            'ficon-express-16s-plus',
            'card-location':
            'vvvv-wwww',
            'port-count':
            1,
            'state':
            'online',
            'configured-capacity':
            254,
            'used-capacity':
            0,
            'allowed-capacity':
            254,
            'maximum-total-capacity':
            254,
            'channel-path-id':
            None,
            'physical-channel-status':
            'not-defined',
        })

        # Port properties that will be auto-set:
        # - element-uri
        # Properties in parent adapter that will be auto-set:
        # - storage-port-uris
        faked_ficon_adapter.ports.add({
            'element-id': 'fake-port11-oid',
            'parent': faked_ficon_adapter.uri,
            'class': 'storage-port',
            'index': 0,
            'name': 'fake-port11-name',
            'description': 'FICON #1 Port #1',
        })
        return faked_ficon_adapter

    def add_cpc_z13s(self):
        """Add a CPC #2 of type z13s to the faked HMC."""

        # CPC properties that will be auto-set:
        # - object-uri
        faked_cpc = self.session.hmc.cpcs.add({
            'object-id': 'fake-cpc-2-oid',
            'parent': None,
            'class': 'cpc',
            'name': 'fake-cpc-2-name',
            'description': 'CPC z13s #2',
            'status': 'active',
            'dpm-enabled': True,
            'is-ensemble-member': False,
            'iml-mode': 'dpm',
            'machine-type': '2965',  # z13s
        })
        return faked_cpc

    def test_adaptermanager_initial_attrs(self):
        """Test initial attributes of AdapterManager."""

        adapter_mgr = self.cpc.adapters

        # Verify all public properties of the manager object
        assert adapter_mgr.resource_class == Adapter
        assert adapter_mgr.session == self.session
        assert adapter_mgr.parent == self.cpc
        assert adapter_mgr.cpc == self.cpc

    # TODO: Test for AdapterManager.__repr__()

    @pytest.mark.parametrize("full_properties_kwargs, prop_names", [
        (dict(), ['object-uri', 'name', 'status']),
        (dict(full_properties=False), ['object-uri', 'name', 'status']),
        (dict(full_properties=True), None),
    ])
    def test_adaptermanager_list_full_properties(self, full_properties_kwargs,
                                                 prop_names):
        """Test AdapterManager.list() with full_properties."""

        # Add two faked adapters
        faked_osa1 = self.add_standard_osa()
        faked_hs2 = self.add_standard_hipersocket()

        exp_faked_adapters = [faked_osa1, faked_hs2]
        adapter_mgr = self.cpc.adapters

        # Execute the code to be tested
        adapters = adapter_mgr.list(**full_properties_kwargs)

        assert_resources(adapters, exp_faked_adapters, prop_names)

    @pytest.mark.parametrize("filter_args, exp_names", [
        ({
            'object-id': OSA1_OID
        }, [OSA1_NAME]),
        ({
            'object-id': HS2_OID
        }, [HS2_NAME]),
        ({
            'object-id': [OSA1_OID, HS2_OID]
        }, [OSA1_NAME, HS2_NAME]),
        ({
            'object-id': [OSA1_OID, OSA1_OID]
        }, [OSA1_NAME]),
        ({
            'object-id': OSA1_OID + 'foo'
        }, []),
        ({
            'object-id': [OSA1_OID, HS2_OID + 'foo']
        }, [OSA1_NAME]),
        ({
            'object-id': [HS2_OID + 'foo', OSA1_OID]
        }, [OSA1_NAME]),
        ({
            'name': OSA1_NAME
        }, [OSA1_NAME]),
        ({
            'name': HS2_NAME
        }, [HS2_NAME]),
        ({
            'name': [OSA1_NAME, HS2_NAME]
        }, [OSA1_NAME, HS2_NAME]),
        ({
            'name': OSA1_NAME + 'foo'
        }, []),
        ({
            'name': [OSA1_NAME, HS2_NAME + 'foo']
        }, [OSA1_NAME]),
        ({
            'name': [HS2_NAME + 'foo', OSA1_NAME]
        }, [OSA1_NAME]),
        ({
            'name': [OSA1_NAME, OSA1_NAME]
        }, [OSA1_NAME]),
        ({
            'name': '.*osa 1'
        }, [OSA1_NAME]),
        ({
            'name': 'osa 1.*'
        }, [OSA1_NAME]),
        ({
            'name': 'osa .'
        }, [OSA1_NAME]),
        ({
            'name': '.sa 1'
        }, [OSA1_NAME]),
        ({
            'name': '.+'
        }, [OSA1_NAME, HS2_NAME]),
        ({
            'name': 'osa 1.+'
        }, []),
        ({
            'name': '.+osa 1'
        }, []),
        ({
            'name': OSA1_NAME,
            'object-id': OSA1_OID
        }, [OSA1_NAME]),
        ({
            'name': OSA1_NAME,
            'object-id': OSA1_OID + 'foo'
        }, []),
        ({
            'name': OSA1_NAME + 'foo',
            'object-id': OSA1_OID
        }, []),
        ({
            'name': OSA1_NAME + 'foo',
            'object-id': OSA1_OID + 'foo'
        }, []),
    ])
    def test_adaptermanager_list_filter_args(self, filter_args, exp_names):
        """Test AdapterManager.list() with filter_args."""

        # Add two faked adapters
        self.add_standard_osa()
        self.add_standard_hipersocket()

        adapter_mgr = self.cpc.adapters

        # Execute the code to be tested
        adapters = adapter_mgr.list(filter_args=filter_args)

        assert len(adapters) == len(exp_names)
        if exp_names:
            names = [ad.properties['name'] for ad in adapters]
            assert set(names) == set(exp_names)

    def test_adaptermanager_create_hipersocket(self):
        """Test AdapterManager.create_hipersocket()."""

        hs_properties = {
            'name': 'hs 3',
            'description': 'Hipersocket #3',
            'port-description': 'Hipersocket #3 Port',
            'maximum-transmission-unit-size': 56,
        }

        adapter_mgr = self.cpc.adapters

        # Execute the code to be tested
        adapter = adapter_mgr.create_hipersocket(properties=hs_properties)

        assert isinstance(adapter, Adapter)
        assert adapter.name == adapter.properties['name']
        assert adapter.uri == adapter.properties['object-uri']

        # We expect the input properties to be in the resource object
        assert adapter.properties['name'] == 'hs 3'
        assert adapter.properties['description'] == 'Hipersocket #3'
        assert adapter.properties['port-description'] == 'Hipersocket #3 Port'
        assert adapter.properties['maximum-transmission-unit-size'] == 56

    # TODO: Test for initial Adapter attributes (ports, port_uris_prop,
    #       port_uri_segment)

    def test_adapter_repr(self):
        """Test Adapter.__repr__()."""

        # Add a faked adapter
        faked_osa = self.add_standard_osa()

        adapter_mgr = self.cpc.adapters
        adapter = adapter_mgr.find(name=faked_osa.name)

        # Execute the code to be tested
        repr_str = repr(adapter)

        repr_str = repr_str.replace('\n', '\\n')
        # We check just the begin of the string:
        assert re.match(
            r'^{classname}\s+at\s+0x{id:08x}\s+\(\\n.*'.format(
                classname=adapter.__class__.__name__, id=id(adapter)),
            repr_str)

    def test_max_crypto_domains(self):
        """Test Adapter.maximum_crypto_domains on z13 and z13s."""

        faked_cpc = self.faked_cpc
        faked_crypto = self.add_crypto_ce5s(faked_cpc)
        self._one_test_max_crypto_domains(faked_cpc, faked_crypto, 85)

        faked_cpc = self.add_cpc_z13s()
        faked_crypto = self.add_crypto_ce5s(faked_cpc)
        self._one_test_max_crypto_domains(faked_cpc, faked_crypto, 40)

    def _one_test_max_crypto_domains(self, faked_cpc, faked_adapter,
                                     exp_max_domains):
        """Test Adapter.maximum_crypto_domains on a CPC"""

        cpc = self.client.cpcs.find(name=faked_cpc.name)
        adapter = cpc.adapters.find(name=faked_adapter.name)

        # Exercise code to be tested
        max_domains = adapter.maximum_crypto_domains

        assert max_domains == exp_max_domains

    def test_adapter_delete(self):
        """Test Adapter.delete() for Hipersocket adapter."""

        # Add two faked adapters
        self.add_standard_osa()
        faked_hs = self.add_standard_hipersocket()

        adapter_mgr = self.cpc.adapters
        hs_adapter = adapter_mgr.find(name=faked_hs.name)

        # Execute the code to be tested
        hs_adapter.delete()

        with pytest.raises(NotFound):
            hs_adapter = adapter_mgr.find(type='hipersockets')

        with pytest.raises(NotFound):
            hs_adapter = adapter_mgr.find(name=faked_hs.name)

        adapters = adapter_mgr.list()
        assert len(adapters) == 1

        with pytest.raises(HTTPError) as exc_info:
            hs_adapter.pull_full_properties()
        exc = exc_info.value
        assert exc.http_status == 404
        assert exc.reason == 1

    @pytest.mark.parametrize("input_props", [
        {},
        {
            'description': 'New adapter description'
        },
        {
            'channel-path-id': '1A',
            'description': 'New adapter description'
        },
    ])
    def test_adapter_update_properties(self, input_props):
        """Test Adapter.update_properties()."""

        # Add a faked adapter
        faked_adapter = self.add_standard_osa()

        adapter_mgr = self.cpc.adapters
        adapter = adapter_mgr.find(name=faked_adapter.name)

        adapter.pull_full_properties()
        saved_properties = copy.deepcopy(adapter.properties)

        # Execute the code to be tested
        adapter.update_properties(properties=input_props)

        # Verify that the resource object already reflects the property
        # updates.
        for prop_name in saved_properties:
            if prop_name in input_props:
                exp_prop_value = input_props[prop_name]
            else:
                exp_prop_value = saved_properties[prop_name]
            assert prop_name in adapter.properties
            prop_value = adapter.properties[prop_name]
            assert prop_value == exp_prop_value

        # Refresh the resource object and verify that the resource object
        # still reflects the property updates.
        adapter.pull_full_properties()
        for prop_name in saved_properties:
            if prop_name in input_props:
                exp_prop_value = input_props[prop_name]
            else:
                exp_prop_value = saved_properties[prop_name]
            assert prop_name in adapter.properties
            prop_value = adapter.properties[prop_name]
            assert prop_value == exp_prop_value

    def test_adapter_update_name(self):
        """
        Test Adapter.update_properties() with 'name' property.
        """

        # Add a faked adapter
        faked_adapter = self.add_standard_osa()
        adapter_name = faked_adapter.name

        adapter_mgr = self.cpc.adapters
        adapter = adapter_mgr.find(name=adapter_name)

        new_adapter_name = "new-" + adapter_name

        # Execute the code to be tested
        adapter.update_properties(properties={'name': new_adapter_name})

        # Verify that the resource is no longer found by its old name, using
        # list() (this does not use the name-to-URI cache).
        adapters_list = adapter_mgr.list(filter_args=dict(name=adapter_name))
        assert len(adapters_list) == 0

        # Verify that the resource is no longer found by its old name, using
        # find() (this uses the name-to-URI cache).
        with pytest.raises(NotFound):
            adapter_mgr.find(name=adapter_name)

        # Verify that the resource object already reflects the update, even
        # though it has not been refreshed yet.
        assert adapter.properties['name'] == new_adapter_name

        # Refresh the resource object and verify that it still reflects the
        # update.
        adapter.pull_full_properties()
        assert adapter.properties['name'] == new_adapter_name

        # Verify that the resource can be found by its new name, using find()
        new_adapter_find = adapter_mgr.find(name=new_adapter_name)
        assert new_adapter_find.properties['name'] == new_adapter_name

        # Verify that the resource can be found by its new name, using list()
        new_adapters_list = adapter_mgr.list(filter_args=dict(
            name=new_adapter_name))
        assert len(new_adapters_list) == 1
        new_adapter_list = new_adapters_list[0]
        assert new_adapter_list.properties['name'] == new_adapter_name

    # TODO: Test for Adapter.change_crypto_type()

    @pytest.mark.parametrize("init_type", ['not-configured', 'fc', 'fcp'])
    @pytest.mark.parametrize("new_type", ['not-configured', 'fc', 'fcp'])
    def test_change_adapter_type_success(self, init_type, new_type):
        """Test Adapter.change_adapter_type() on ficon adapter with success."""

        faked_cpc = self.faked_cpc
        faked_adapter = self.add_ficon_fe6sp(faked_cpc)

        # Set the desired initial adapter type for the test
        faked_adapter.properties['type'] = init_type

        adapter_mgr = self.cpc.adapters
        adapter = adapter_mgr.find(name=faked_adapter.name)

        if new_type == init_type:
            with pytest.raises(HTTPError) as exc_info:

                # Execute the code to be tested
                adapter.change_adapter_type(new_type)

            exc = exc_info.value
            assert exc.http_status == 400
            assert exc.reason == 8
        else:

            # Execute the code to be tested.
            adapter.change_adapter_type(new_type)

            act_type = adapter.get_property('type')
            assert act_type == new_type

    @pytest.mark.parametrize("desc, family, init_type, new_type, exp_exc", [
        ("Invalid adapter family: 'osa'", 'osa', 'osd', None,
         HTTPError({
             'http-status': 400,
             'reason': 18
         })),
        ("Invalid new type value: 'xxx'", 'ficon', 'fcp', 'xxx',
         HTTPError({
             'http-status': 400,
             'reason': 8
         })),
    ])
    def test_change_adapter_type_error(self, desc, family, init_type, new_type,
                                       exp_exc):
        # pylint: disable=unused-argument
        """Test Adapter.change_adapter_type()."""

        faked_cpc = self.faked_cpc
        if family == 'ficon':
            faked_adapter = self.add_ficon_fe6sp(faked_cpc)
        else:
            assert family == 'osa'
            faked_adapter = self.add_standard_osa()

        faked_adapter.properties['type'] == init_type

        adapter_mgr = self.cpc.adapters
        adapter = adapter_mgr.find(name=faked_adapter.name)

        with pytest.raises(exp_exc.__class__) as exc_info:

            # Execute the code to be tested
            adapter.change_adapter_type(new_type)

        exc = exc_info.value
        if isinstance(exp_exc, HTTPError):
            assert exc.http_status == exp_exc.http_status
            assert exc.reason == exp_exc.reason
class TestPartition(object):
    """All tests for the Partition and PartitionManager classes."""
    def setup_method(self):
        """
        Set up a faked session, and add a faked CPC in DPM mode without any
        child resources.
        """

        self.session = FakedSession('fake-host', 'fake-hmc', '2.13.1', '1.8')
        self.client = Client(self.session)

        self.faked_cpc = self.session.hmc.cpcs.add({
            'object-id': 'fake-cpc1-oid',
            # object-uri is set up automatically
            'parent': None,
            'class': 'cpc',
            'name': 'fake-cpc1-name',
            'description': 'CPC #1 (DPM mode)',
            'status': 'active',
            'dpm-enabled': True,
            'is-ensemble-member': False,
            'iml-mode': 'dpm',
        })
        self.cpc = self.client.cpcs.find(name='fake-cpc1-name')

    def add_partition1(self):
        """Add partition 1 (type linux)."""

        faked_partition = self.faked_cpc.partitions.add({
            'object-id':
            PART1_OID,
            # object-uri will be automatically set
            'parent':
            self.faked_cpc.uri,
            'class':
            'partition',
            'name':
            PART1_NAME,
            'description':
            'Partition #1',
            'status':
            'active',
            'type':
            'linux',
            'initial-memory':
            1024,
            'maximum-memory':
            2048,
        })
        return faked_partition

    def add_partition2(self):
        """Add partition 2 (type ssc)."""

        faked_partition = self.faked_cpc.partitions.add({
            'object-id':
            PART2_OID,
            # object-uri will be automatically set
            'parent':
            self.faked_cpc.uri,
            'class':
            'partition',
            'name':
            PART2_NAME,
            'description':
            'Partition #2',
            'status':
            'active',
            'type':
            'ssc',
            'initial-memory':
            1024,
            'maximum-memory':
            2048,
        })
        return faked_partition

    def add_partition3(self):
        """Add partition 3 (support for firmware features)."""

        faked_partition = self.faked_cpc.partitions.add({
            'object-id':
            PART3_OID,
            # object-uri will be automatically set
            'parent':
            self.faked_cpc.uri,
            'class':
            'partition',
            'name':
            PART3_NAME,
            'description':
            'Partition #3',
            'status':
            'active',
            'type':
            'linux',
            'initial-memory':
            1024,
            'maximum-memory':
            2048,
            'available-features-list': [],
        })
        return faked_partition

    def add_partition(self, part_name):
        """Add a partition (using one of the known names)."""

        if part_name == PART1_NAME:
            faked_partition = self.add_partition1()
        elif part_name == PART2_NAME:
            faked_partition = self.add_partition2()
        elif part_name == PART3_NAME:
            faked_partition = self.add_partition3()
        return faked_partition

    def test_partitionmanager_initial_attrs(self):
        """Test initial attributes of PartitionManager."""

        partition_mgr = self.cpc.partitions

        # Verify all public properties of the manager object
        assert partition_mgr.resource_class == Partition
        assert partition_mgr.session == self.session
        assert partition_mgr.parent == self.cpc
        assert partition_mgr.cpc == self.cpc

    # TODO: Test for PartitionManager.__repr__()

    @pytest.mark.parametrize("full_properties_kwargs, prop_names", [
        (dict(), ['object-uri', 'name', 'status']),
        (dict(full_properties=False), ['object-uri', 'name', 'status']),
        (dict(full_properties=True), None),
    ])
    def test_partitionmanager_list_full_properties(self,
                                                   full_properties_kwargs,
                                                   prop_names):
        """Test PartitionManager.list() with full_properties."""

        # Add two faked partitions
        faked_partition1 = self.add_partition1()
        faked_partition2 = self.add_partition2()

        exp_faked_partitions = [faked_partition1, faked_partition2]
        partition_mgr = self.cpc.partitions

        # Execute the code to be tested
        partitions = partition_mgr.list(**full_properties_kwargs)

        assert_resources(partitions, exp_faked_partitions, prop_names)

    @pytest.mark.parametrize("filter_args, exp_names", [
        ({
            'object-id': PART1_OID
        }, [PART1_NAME]),
        ({
            'object-id': PART2_OID
        }, [PART2_NAME]),
        ({
            'object-id': [PART1_OID, PART2_OID]
        }, [PART1_NAME, PART2_NAME]),
        ({
            'object-id': [PART1_OID, PART1_OID]
        }, [PART1_NAME]),
        ({
            'object-id': PART1_OID + 'foo'
        }, []),
        ({
            'object-id': [PART1_OID, PART2_OID + 'foo']
        }, [PART1_NAME]),
        ({
            'object-id': [PART2_OID + 'foo', PART1_OID]
        }, [PART1_NAME]),
        ({
            'name': PART1_NAME
        }, [PART1_NAME]),
        ({
            'name': PART2_NAME
        }, [PART2_NAME]),
        ({
            'name': [PART1_NAME, PART2_NAME]
        }, [PART1_NAME, PART2_NAME]),
        ({
            'name': PART1_NAME + 'foo'
        }, []),
        ({
            'name': [PART1_NAME, PART2_NAME + 'foo']
        }, [PART1_NAME]),
        ({
            'name': [PART2_NAME + 'foo', PART1_NAME]
        }, [PART1_NAME]),
        ({
            'name': [PART1_NAME, PART1_NAME]
        }, [PART1_NAME]),
        ({
            'name': '.*part 1'
        }, [PART1_NAME]),
        ({
            'name': 'part 1.*'
        }, [PART1_NAME]),
        ({
            'name': 'part .'
        }, [PART1_NAME, PART2_NAME]),
        ({
            'name': '.art 1'
        }, [PART1_NAME]),
        ({
            'name': '.+'
        }, [PART1_NAME, PART2_NAME]),
        ({
            'name': 'part 1.+'
        }, []),
        ({
            'name': '.+part 1'
        }, []),
        ({
            'name': PART1_NAME,
            'object-id': PART1_OID
        }, [PART1_NAME]),
        ({
            'name': PART1_NAME,
            'object-id': PART1_OID + 'foo'
        }, []),
        ({
            'name': PART1_NAME + 'foo',
            'object-id': PART1_OID
        }, []),
        ({
            'name': PART1_NAME + 'foo',
            'object-id': PART1_OID + 'foo'
        }, []),
    ])
    def test_partitionmanager_list_filter_args(self, filter_args, exp_names):
        """Test PartitionManager.list() with filter_args."""

        # Add two faked partitions
        self.add_partition1()
        self.add_partition2()

        partition_mgr = self.cpc.partitions

        # Execute the code to be tested
        partitions = partition_mgr.list(filter_args=filter_args)

        assert len(partitions) == len(exp_names)
        if exp_names:
            names = [p.properties['name'] for p in partitions]
            assert set(names) == set(exp_names)

    @pytest.mark.parametrize("input_props, exp_prop_names, exp_exc", [
        ({}, None, HTTPError({
            'http-status': 400,
            'reason': 5
        })),
        ({
            'description': 'fake description X'
        }, None, HTTPError({
            'http-status': 400,
            'reason': 5
        })),
        ({
            'name': 'fake-part-x'
        }, None, HTTPError({
            'http-status': 400,
            'reason': 5
        })),
        ({
            'name': 'fake-part-x',
            'initial-memory': 1024
        }, None, HTTPError({
            'http-status': 400,
            'reason': 5
        })),
        ({
            'name': 'fake-part-x',
            'initial-memory': 1024,
            'maximum-memory': 1024
        }, ['object-uri', 'name', 'initial-memory', 'maximum-memory'], None),
        ({
            'name': 'fake-part-x',
            'initial-memory': 1024,
            'maximum-memory': 1024,
            'description': 'fake description X'
        }, [
            'object-uri', 'name', 'initial-memory', 'maximum-memory',
            'description'
        ], None),
    ])
    def test_partitionmanager_create(self, input_props, exp_prop_names,
                                     exp_exc):
        """Test PartitionManager.create()."""

        partition_mgr = self.cpc.partitions

        if exp_exc is not None:

            with pytest.raises(exp_exc.__class__) as exc_info:

                # Execute the code to be tested
                partition = partition_mgr.create(properties=input_props)

            exc = exc_info.value
            if isinstance(exp_exc, HTTPError):
                assert exc.http_status == exp_exc.http_status
                assert exc.reason == exp_exc.reason

        else:

            # Execute the code to be tested.
            # Note: the Partition object returned by Partition.create() has
            # the input properties plus 'object-uri'.
            partition = partition_mgr.create(properties=input_props)

            # Check the resource for consistency within itself
            assert isinstance(partition, Partition)
            partition_name = partition.name
            exp_partition_name = partition.properties['name']
            assert partition_name == exp_partition_name
            partition_uri = partition.uri
            exp_partition_uri = partition.properties['object-uri']
            assert partition_uri == exp_partition_uri

            # Check the properties against the expected names and values
            for prop_name in exp_prop_names:
                assert prop_name in partition.properties
                if prop_name in input_props:
                    value = partition.properties[prop_name]
                    exp_value = input_props[prop_name]
                    assert value == exp_value

    def test_partitionmanager_resource_object(self):
        """
        Test PartitionManager.resource_object().

        This test exists for historical reasons, and by now is covered by the
        test for BaseManager.resource_object().
        """
        partition_mgr = self.cpc.partitions

        partition_oid = 'fake-partition-id42'

        # Execute the code to be tested
        partition = partition_mgr.resource_object(partition_oid)

        partition_uri = "/api/partitions/" + partition_oid

        assert isinstance(partition, Partition)
        assert partition.uri == partition_uri
        assert partition.properties['object-uri'] == partition_uri
        assert partition.properties['object-id'] == partition_oid
        assert partition.properties['class'] == 'partition'
        assert partition.properties['parent'] == self.cpc.uri

    # TODO: Test for initial Partition attributes (nics, hbas,
    #       virtual_functions)

    def test_partition_repr(self):
        """Test Partition.__repr__()."""

        # Add a faked partition
        faked_partition = self.add_partition1()

        partition_mgr = self.cpc.partitions
        partition = partition_mgr.find(name=faked_partition.name)

        # Execute the code to be tested
        repr_str = repr(partition)

        repr_str = repr_str.replace('\n', '\\n')
        # We check just the begin of the string:
        assert re.match(
            r'^{classname}\s+at\s+0x{id:08x}\s+\(\\n.*'.format(
                classname=partition.__class__.__name__, id=id(partition)),
            repr_str)

    @pytest.mark.parametrize("initial_status, exp_exc", [
        ('stopped', None),
        ('terminated', HTTPError({
            'http-status': 409,
            'reason': 1
        })),
        ('starting', HTTPError({
            'http-status': 409,
            'reason': 1
        })),
        ('active', HTTPError({
            'http-status': 409,
            'reason': 1
        })),
        ('stopping', HTTPError({
            'http-status': 409,
            'reason': 1
        })),
        ('degraded', HTTPError({
            'http-status': 409,
            'reason': 1
        })),
        ('reservation-error', HTTPError({
            'http-status': 409,
            'reason': 1
        })),
        ('paused', HTTPError({
            'http-status': 409,
            'reason': 1
        })),
    ])
    def test_partition_delete(self, initial_status, exp_exc):
        """Test Partition.delete()."""

        # Add a faked partition to be tested and another one
        faked_partition = self.add_partition1()
        self.add_partition2()

        # Set the initial status of the faked partition
        faked_partition.properties['status'] = initial_status

        partition_mgr = self.cpc.partitions

        partition = partition_mgr.find(name=faked_partition.name)

        if exp_exc is not None:

            with pytest.raises(exp_exc.__class__) as exc_info:

                # Execute the code to be tested
                partition.delete()

            exc = exc_info.value
            if isinstance(exp_exc, HTTPError):
                assert exc.http_status == exp_exc.http_status
                assert exc.reason == exp_exc.reason

            # Check that the partition still exists
            partition_mgr.find(name=faked_partition.name)

        else:

            # Execute the code to be tested.
            partition.delete()

            # Check that the partition no longer exists
            with pytest.raises(NotFound) as exc_info:
                partition_mgr.find(name=faked_partition.name)

    def test_partition_delete_create_same_name(self):
        """Test Partition.delete() followed by create() with same name."""

        # Add a faked partition to be tested and another one
        faked_partition = self.add_partition1()
        partition_name = faked_partition.name
        self.add_partition2()

        # Construct the input properties for a third partition
        part3_props = copy.deepcopy(faked_partition.properties)
        part3_props['description'] = 'Third partition'

        # Set the initial status of the faked partition
        faked_partition.properties['status'] = 'stopped'  # deletable

        partition_mgr = self.cpc.partitions
        partition = partition_mgr.find(name=partition_name)

        # Execute the deletion code to be tested.
        partition.delete()

        # Check that the partition no longer exists
        with pytest.raises(NotFound):
            partition_mgr.find(name=partition_name)

        # Execute the creation code to be tested.
        partition_mgr.create(part3_props)

        # Check that the partition exists again under that name
        partition3 = partition_mgr.find(name=partition_name)
        description = partition3.get_property('description')
        assert description == 'Third partition'

    @pytest.mark.parametrize(
        "desc, partition_name, available_features, feature_name, "
        "exp_feature_enabled, exp_exc", [
            ("No feature support on the CPC", PART1_NAME, None,
             'fake-feature1', None, ValueError()),
            ("Feature not available on the partition (empty feature list)",
             PART3_NAME, [], 'fake-feature1', None, ValueError()),
            ("Feature not available on the part (one other feature avail)",
             PART3_NAME, [
                 dict(name='fake-feature-foo', state=True),
             ], 'fake-feature1', None, ValueError()),
            ("Feature disabled (the only feature available)", PART3_NAME, [
                dict(name='fake-feature1', state=False),
            ], 'fake-feature1', False, None),
            ("Feature enabled (the only feature available)", PART3_NAME, [
                dict(name='fake-feature1', state=True),
            ], 'fake-feature1', True, None),
        ])
    def test_partition_feature_enabled(self, desc, partition_name,
                                       available_features, feature_name,
                                       exp_feature_enabled, exp_exc):
        """Test Partition.feature_enabled()."""

        # Add a faked Partition
        faked_partition = self.add_partition(partition_name)

        # Set up the firmware feature list
        if available_features is not None:
            faked_partition.properties['available-features-list'] = \
                available_features

        partition_mgr = self.cpc.partitions
        partition = partition_mgr.find(name=partition_name)

        if exp_exc:
            with pytest.raises(exp_exc.__class__):

                # Execute the code to be tested
                partition.feature_enabled(feature_name)

        else:

            # Execute the code to be tested
            act_feature_enabled = partition.feature_enabled(feature_name)

            assert act_feature_enabled == exp_feature_enabled

    @pytest.mark.parametrize(
        "desc, partition_name, available_features, exp_exc", [
            ("No feature support on the CPC", PART1_NAME, None, ValueError()),
            ("Feature not available on the partition (empty feature list)",
             PART3_NAME, [], None),
            ("Feature not available on the part (one other feature avail)",
             PART3_NAME, [
                 dict(name='fake-feature-foo', state=True),
             ], None),
            ("Feature disabled (the only feature available)", PART3_NAME, [
                dict(name='fake-feature1', state=False),
            ], None),
            ("Feature enabled (the only feature available)", PART3_NAME, [
                dict(name='fake-feature1', state=True),
            ], None),
        ])
    def test_partition_feature_info(self, desc, partition_name,
                                    available_features, exp_exc):
        """Test Partition.feature_info()."""

        # Add a faked Partition
        faked_partition = self.add_partition(partition_name)

        # Set up the firmware feature list
        if available_features is not None:
            faked_partition.properties['available-features-list'] = \
                available_features

        partition_mgr = self.cpc.partitions
        partition = partition_mgr.find(name=partition_name)

        if exp_exc:
            with pytest.raises(exp_exc.__class__):

                # Execute the code to be tested
                partition.feature_info()

        else:

            # Execute the code to be tested
            act_features = partition.feature_info()

            assert act_features == available_features

    @pytest.mark.parametrize("partition_name", [
        PART1_NAME,
        PART2_NAME,
    ])
    @pytest.mark.parametrize("input_props", [
        {},
        {
            'description': 'New partition description'
        },
        {
            'initial-memory': 512,
            'description': 'New partition description'
        },
        {
            'autogenerate-partition-id': True,
            'partition-id': None
        },
        {
            'boot-device': 'none',
            'boot-ftp-host': None,
            'boot-ftp-username': None,
            'boot-ftp-password': None,
            'boot-ftp-insfile': None
        },
        {
            'boot-device': 'none',
            'boot-network-device': None
        },
        {
            'boot-device': 'none',
            'boot-removable-media': None,
            'boot-removable-media-type': None
        },
        {
            'boot-device': 'none',
            'boot-storage-device': None,
            'boot-logical-unit-number': None,
            'boot-world-wide-port-name': None
        },
        {
            'boot-device': 'none',
            'boot-iso-image-name': None,
            'boot-iso-insfile': None
        },
        {
            'ssc-ipv4-gateway': None,
            'ssc-ipv6-gateway': None,
            'ssc-master-userid': None,
            'ssc-master-pw': None
        },
    ])
    def test_partition_update_properties(self, input_props, partition_name):
        """Test Partition.update_properties()."""

        # Add faked partitions
        self.add_partition1()
        self.add_partition2()

        partition_mgr = self.cpc.partitions
        partition = partition_mgr.find(name=partition_name)

        partition.pull_full_properties()
        saved_properties = copy.deepcopy(partition.properties)

        # Execute the code to be tested
        partition.update_properties(properties=input_props)

        # Verify that the resource object already reflects the property
        # updates.
        for prop_name in saved_properties:
            if prop_name in input_props:
                exp_prop_value = input_props[prop_name]
            else:
                exp_prop_value = saved_properties[prop_name]
            assert prop_name in partition.properties
            prop_value = partition.properties[prop_name]
            assert prop_value == exp_prop_value

        # Refresh the resource object and verify that the resource object
        # still reflects the property updates.
        partition.pull_full_properties()
        for prop_name in saved_properties:
            if prop_name in input_props:
                exp_prop_value = input_props[prop_name]
            else:
                exp_prop_value = saved_properties[prop_name]
            assert prop_name in partition.properties
            prop_value = partition.properties[prop_name]
            assert prop_value == exp_prop_value

    def test_partition_update_name(self):
        """
        Test Partition.update_properties() with 'name' property.
        """

        # Add a faked partition
        faked_partition = self.add_partition1()
        partition_name = faked_partition.name

        partition_mgr = self.cpc.partitions
        partition = partition_mgr.find(name=partition_name)

        new_partition_name = "new-" + partition_name

        # Execute the code to be tested
        partition.update_properties(properties={'name': new_partition_name})

        # Verify that the resource is no longer found by its old name, using
        # list() (this does not use the name-to-URI cache).
        partitions_list = partition_mgr.list(filter_args=dict(
            name=partition_name))
        assert len(partitions_list) == 0

        # Verify that the resource is no longer found by its old name, using
        # find() (this uses the name-to-URI cache).
        with pytest.raises(NotFound):
            partition_mgr.find(name=partition_name)

        # Verify that the resource object already reflects the update, even
        # though it has not been refreshed yet.
        assert partition.properties['name'] == new_partition_name

        # Refresh the resource object and verify that it still reflects the
        # update.
        partition.pull_full_properties()
        assert partition.properties['name'] == new_partition_name

        # Verify that the resource can be found by its new name, using find()
        new_partition_find = partition_mgr.find(name=new_partition_name)
        assert new_partition_find.properties['name'] == new_partition_name

        # Verify that the resource can be found by its new name, using list()
        new_partitions_list = partition_mgr.list(filter_args=dict(
            name=new_partition_name))
        assert len(new_partitions_list) == 1
        new_partition_list = new_partitions_list[0]
        assert new_partition_list.properties['name'] == new_partition_name

    @pytest.mark.parametrize("initial_status, exp_exc", [
        ('stopped', None),
        ('terminated', HTTPError({
            'http-status': 409,
            'reason': 1
        })),
        ('starting', HTTPError({
            'http-status': 409,
            'reason': 1
        })),
        ('active', HTTPError({
            'http-status': 409,
            'reason': 1
        })),
        ('stopping', HTTPError({
            'http-status': 409,
            'reason': 1
        })),
        ('degraded', HTTPError({
            'http-status': 409,
            'reason': 1
        })),
        ('reservation-error', HTTPError({
            'http-status': 409,
            'reason': 1
        })),
        ('paused', HTTPError({
            'http-status': 409,
            'reason': 1
        })),
    ])
    def test_partition_start(self, initial_status, exp_exc):
        """Test Partition.start()."""

        # Add a faked partition
        faked_partition = self.add_partition1()

        # Set the initial status of the faked partition
        faked_partition.properties['status'] = initial_status

        partition_mgr = self.cpc.partitions
        partition = partition_mgr.find(name=faked_partition.name)

        if exp_exc is not None:

            with pytest.raises(exp_exc.__class__) as exc_info:

                # Execute the code to be tested
                partition.start()

            exc = exc_info.value
            if isinstance(exp_exc, HTTPError):
                assert exc.http_status == exp_exc.http_status
                assert exc.reason == exp_exc.reason

        else:

            # Execute the code to be tested.
            ret = partition.start()

            assert ret == {}

            partition.pull_full_properties()
            status = partition.get_property('status')
            assert status == 'active'

    @pytest.mark.parametrize("initial_status, exp_exc", [
        ('stopped', HTTPError({
            'http-status': 409,
            'reason': 1
        })),
        ('terminated', None),
        ('starting', HTTPError({
            'http-status': 409,
            'reason': 1
        })),
        ('active', None),
        ('stopping', HTTPError({
            'http-status': 409,
            'reason': 1
        })),
        ('degraded', HTTPError({
            'http-status': 409,
            'reason': 1
        })),
        ('reservation-error', HTTPError({
            'http-status': 409,
            'reason': 1
        })),
        ('paused', None),
    ])
    def test_partition_stop(self, initial_status, exp_exc):
        """Test Partition.stop()."""

        # Add a faked partition
        faked_partition = self.add_partition1()

        # Set the initial status of the faked partition
        faked_partition.properties['status'] = initial_status

        partition_mgr = self.cpc.partitions
        partition = partition_mgr.find(name=faked_partition.name)

        if exp_exc is not None:

            with pytest.raises(exp_exc.__class__) as exc_info:

                # Execute the code to be tested
                partition.stop()

            exc = exc_info.value
            if isinstance(exp_exc, HTTPError):
                assert exc.http_status == exp_exc.http_status
                assert exc.reason == exp_exc.reason

        else:

            # Execute the code to be tested.
            ret = partition.stop()

            assert ret == {}

            partition.pull_full_properties()
            status = partition.get_property('status')
            assert status == 'stopped'

    # TODO: Re-enable test_partition_dump_partition() once supported in hdlr
    def xtest_partition_dump_partition(self):
        """Test Partition.dump_partition()."""

        # Add a faked partition
        faked_partition = self.add_partition1()

        partition_mgr = self.cpc.partitions
        partition = partition_mgr.find(name=faked_partition.name)

        parameters = {
            'dump-load-hba-uri': 'fake-hba-uri',
            'dump-world-wide-port-name': 'fake-wwpn',
            'dump-logical-unit-number': 'fake-lun',
        }

        # Execute the code to be tested.
        ret = partition.dump_partition(parameters=parameters)

        assert ret == {}

    # TODO: Re-enable test_partition_psw_restart() once supported in hdlr
    def xtest_partition_psw_restart(self):
        """Test Partition.psw_restart()."""

        # Add a faked partition
        faked_partition = self.add_partition1()

        partition_mgr = self.cpc.partitions
        partition = partition_mgr.find(name=faked_partition.name)

        # Execute the code to be tested.
        ret = partition.psw_restart()

        assert ret == {}

    # TODO: Re-enable test_partition_mount_iso_image() once supported in hdlr
    def xtest_partition_mount_iso_image(self):
        """Test Partition.mount_iso_image()."""

        # Add a faked partition
        faked_partition = self.add_partition1()

        partition_mgr = self.cpc.partitions
        partition = partition_mgr.find(name=faked_partition.name)

        image = b'fake-image-data'
        image_name = 'fake-image-name'
        ins_file_name = 'fake-ins-file-name'

        # Execute the code to be tested.
        ret = partition.mount_iso_image(image=image,
                                        image_name=image_name,
                                        ins_file_name=ins_file_name)

        assert ret is None

    # TODO: Re-enable test_partition_unmount_iso_image() once supported in hdlr
    def xtest_partition_unmount_iso_image(self):
        """Test Partition.unmount_iso_image()."""

        # Add a faked partition
        faked_partition = self.add_partition1()

        partition_mgr = self.cpc.partitions
        partition = partition_mgr.find(name=faked_partition.name)

        # Execute the code to be tested.
        ret = partition.unmount_iso_image()

        assert ret is None
class TestServerAuthError(object):
    """All tests for exception class ServerAuthError."""
    @pytest.mark.parametrize(
        # Input and expected arguments.
        "args",
        [
            # (msg, details)
            ("fake msg", HTTPError(HTTP_ERROR_1)),
            ("", HTTPError(HTTP_ERROR_1)),
            (None, HTTPError(HTTP_ERROR_1)),
        ])
    @pytest.mark.parametrize(
        # Whether each input arg is passed as pos.arg (None) or keyword arg
        # (arg name), or is defaulted (omitted from right).
        "arg_names",
        [
            (None, None),
            (None, 'details'),
            ('msg', 'details'),
        ])
    def test_serverautherror_initial_attrs(self, arg_names, args):
        """Test initial attributes of ServerAuthError."""

        msg, details = args
        posargs, kwargs = func_args(args, arg_names)

        # Execute the code to be tested
        exc = ServerAuthError(*posargs, **kwargs)

        assert isinstance(exc, AuthError)
        assert len(exc.args) == 1
        assert exc.args[0] == msg
        assert exc.details == details

    @pytest.mark.parametrize("msg, details", [
        ("fake msg", HTTPError(HTTP_ERROR_1)),
        ("", HTTPError(HTTP_ERROR_1)),
        (None, HTTPError(HTTP_ERROR_1)),
    ])
    def test_serverautherror_repr(self, msg, details):
        """All tests for ServerAuthError.__repr__()."""

        exc = ServerAuthError(msg, details)

        classname = exc.__class__.__name__

        # Execute the code to be tested
        repr_str = repr(exc)

        # We check the one-lined string just roughly
        repr_str = repr_str.replace('\n', '\\n')
        assert re.match(r'^{}\s*\(.*\)$'.format(classname), repr_str)

    @pytest.mark.parametrize("msg, details", [
        ("fake msg", HTTPError(HTTP_ERROR_1)),
        ("", HTTPError(HTTP_ERROR_1)),
        (None, HTTPError(HTTP_ERROR_1)),
    ])
    def test_serverautherror_str(self, msg, details):
        """All tests for ServerAuthError.__str__()."""

        exc = ServerAuthError(msg, details)

        exp_str = str(exc.args[0])

        # Execute the code to be tested
        str_str = str(exc)

        assert str_str == exp_str

    @pytest.mark.parametrize("msg, details", [
        ("fake msg", HTTPError(HTTP_ERROR_1)),
        ("", HTTPError(HTTP_ERROR_1)),
        (None, HTTPError(HTTP_ERROR_1)),
    ])
    def test_serverautherror_str_def(self, msg, details):
        """All tests for ServerAuthError.str_def()."""

        exc = ServerAuthError(msg, details)

        classname = exc.__class__.__name__
        request_method = details.request_method
        request_uri = details.request_uri
        http_status = details.http_status
        reason = details.reason

        # Execute the code to be tested
        str_def = exc.str_def()

        str_def = ' ' + str_def
        assert str_def.find(' classname={!r};'.format(classname)) >= 0
        assert str_def.find(
            ' request_method={!r};'.format(request_method)) >= 0
        assert str_def.find(' request_uri={!r};'.format(request_uri)) >= 0
        assert str_def.find(' http_status={!r};'.format(http_status)) >= 0
        assert str_def.find(' reason={!r};'.format(reason)) >= 0
        assert str_def.find(' message={!r};'.format(msg)) >= 0
Пример #18
0
class TestUserPattern(object):
    """All tests for the UserPattern and UserPatternManager classes."""
    def setup_method(self):
        """
        Setup that is called by pytest before each test method.

        Set up a faked session, and add a faked Console without any
        child resources.
        """
        # pylint: disable=attribute-defined-outside-init

        self.session = FakedSession('fake-host', 'fake-hmc', '2.13.1', '1.8')
        self.client = Client(self.session)

        self.faked_console = self.session.hmc.consoles.add({
            'object-id':
            None,
            # object-uri will be automatically set
            'parent':
            None,
            'class':
            'console',
            'name':
            'fake-console1',
            'description':
            'Console #1',
        })
        self.console = self.client.consoles.find(name=self.faked_console.name)

    def add_user_pattern(self, name, pattern, type_, user_template_uri):
        """
        Add a faked user pattern object to the faked Console and return it.
        """
        faked_user_pattern = self.faked_console.user_patterns.add({
            'element-id':
            'oid-{}'.format(name),
            # element-uri will be automatically set
            'parent':
            '/api/console',
            'class':
            'user-pattern',
            'name':
            name,
            'description':
            'User Pattern {}'.format(name),
            'pattern':
            pattern,
            'type':
            type_,
            'retention-time':
            0,
            'user-template-uri':
            user_template_uri,
        })
        return faked_user_pattern

    def add_user(self, name, type_):
        """
        Add a faked user object to the faked Console and return it.
        """
        faked_user = self.faked_console.users.add({
            'object-id':
            'oid-{}'.format(name),
            # object-uri will be automatically set
            'parent':
            '/api/console',
            'class':
            'user',
            'name':
            name,
            'description':
            'User {}'.format(name),
            'type':
            type_,
            'authentication-type':
            'local',
        })
        return faked_user

    def test_upm_repr(self):
        """Test UserPatternManager.__repr__()."""

        user_pattern_mgr = self.console.user_patterns

        # Execute the code to be tested
        repr_str = repr(user_pattern_mgr)

        repr_str = repr_str.replace('\n', '\\n')
        # We check just the begin of the string:
        assert re.match(
            r'^{classname}\s+at\s+0x{id:08x}\s+\(\\n.*'.format(
                classname=user_pattern_mgr.__class__.__name__,
                id=id(user_pattern_mgr)), repr_str)

    def test_upm_initial_attrs(self):
        """Test initial attributes of UserPatternManager."""

        user_pattern_mgr = self.console.user_patterns

        # Verify all public properties of the manager object
        assert user_pattern_mgr.resource_class == UserPattern
        assert user_pattern_mgr.class_name == 'user-pattern'
        assert user_pattern_mgr.session is self.session
        assert user_pattern_mgr.parent is self.console
        assert user_pattern_mgr.console is self.console

    @pytest.mark.parametrize(
        "full_properties_kwargs, prop_names",
        [
            (dict(full_properties=False), ['element-uri']),
            (dict(full_properties=True), ['element-uri', 'name']),
            (
                dict(),  # test default for full_properties (True)
                ['element-uri', 'name']),
        ])
    @pytest.mark.parametrize("filter_args, exp_names", [
        (None, ['a', 'b']),
        ({}, ['a', 'b']),
        ({
            'name': 'a'
        }, ['a']),
    ])
    def test_upm_list(self, filter_args, exp_names, full_properties_kwargs,
                      prop_names):
        """Test UserPatternManager.list()."""

        faked_user1 = self.add_user(name='a', type_='standard')
        faked_user2 = self.add_user(name='b', type_='standard')

        faked_user_pattern1 = self.add_user_pattern(
            name='a',
            pattern='a_*',
            type_='glob-like',
            user_template_uri=faked_user1.uri)
        faked_user_pattern2 = self.add_user_pattern(
            name='b',
            pattern='b_.*',
            type_='regular-expression',
            user_template_uri=faked_user2.uri)
        faked_user_patterns = [faked_user_pattern1, faked_user_pattern2]
        exp_faked_user_patterns = [
            u for u in faked_user_patterns if u.name in exp_names
        ]
        user_pattern_mgr = self.console.user_patterns

        # Execute the code to be tested
        user_patterns = user_pattern_mgr.list(filter_args=filter_args,
                                              **full_properties_kwargs)

        assert_resources(user_patterns, exp_faked_user_patterns, prop_names)

    @pytest.mark.parametrize(
        "input_props, exp_prop_names, exp_exc",
        [
            (
                {},  # props missing
                None,
                HTTPError({
                    'http-status': 400,
                    'reason': 5
                })),
            (
                {
                    'description': 'fake description X'
                },  # props missing
                None,
                HTTPError({
                    'http-status': 400,
                    'reason': 5
                })),
            (
                {
                    'description': 'fake description X',
                    'name': 'a',
                    'pattern': 'a*'
                },  # several missing
                None,
                HTTPError({
                    'http-status': 400,
                    'reason': 5
                })),
            (
                {
                    'description': 'fake description X',
                    'name': 'a',
                    'pattern': 'a*'
                },  # several missing
                None,
                HTTPError({
                    'http-status': 400,
                    'reason': 5
                })),
            (
                {
                    'description': 'fake description X',
                    'name': 'a',
                    'pattern': 'a*',
                    'type': 'glob-like'
                },  # props missing
                None,
                HTTPError({
                    'http-status': 400,
                    'reason': 5
                })),
            (
                {
                    'description': 'fake description X',
                    'name': 'a',
                    'pattern': 'a*',
                    'type': 'glob-like',
                    'retention-time': 0
                },  # props missing
                None,
                HTTPError({
                    'http-status': 400,
                    'reason': 5
                })),
            ({
                'description': 'fake description X',
                'name': 'a',
                'pattern': 'a*',
                'type': 'glob-like',
                'retention-time': 28,
                'user-template-uri': '/api/users/oid-tpl'
            }, [
                'element-uri', 'name', 'description', 'pattern', 'type',
                'retention-time', 'user-template-uri'
            ], None),
        ])
    def test_upm_create(self, input_props, exp_prop_names, exp_exc):
        """Test UserPatternManager.create()."""

        faked_user_template = self.add_user(name='tpl', type_='template')
        assert faked_user_template.uri == '/api/users/oid-tpl'

        user_pattern_mgr = self.console.user_patterns

        if exp_exc is not None:

            with pytest.raises(exp_exc.__class__) as exc_info:

                # Execute the code to be tested
                user_pattern_mgr.create(properties=input_props)

            exc = exc_info.value
            if isinstance(exp_exc, HTTPError):
                assert exc.http_status == exp_exc.http_status
                assert exc.reason == exp_exc.reason

        else:

            # Execute the code to be tested.
            user_pattern = user_pattern_mgr.create(properties=input_props)

            # Check the resource for consistency within itself
            assert isinstance(user_pattern, UserPattern)
            user_pattern_name = user_pattern.name
            exp_user_pattern_name = user_pattern.properties['name']
            assert user_pattern_name == exp_user_pattern_name
            user_pattern_uri = user_pattern.uri
            exp_user_pattern_uri = user_pattern.properties['element-uri']
            assert user_pattern_uri == exp_user_pattern_uri

            # Check the properties against the expected names and values
            for prop_name in exp_prop_names:
                assert prop_name in user_pattern.properties
                if prop_name in input_props:
                    value = user_pattern.properties[prop_name]
                    exp_value = input_props[prop_name]
                    assert value == exp_value

    def test_up_repr(self):
        """Test UserPattern.__repr__()."""

        faked_user1 = self.add_user(name='a', type_='standard')
        faked_user_pattern1 = self.add_user_pattern(
            name='a',
            pattern='a_*',
            type_='glob-like',
            user_template_uri=faked_user1.uri)
        user_pattern1 = self.console.user_patterns.find(
            name=faked_user_pattern1.name)

        # Execute the code to be tested
        repr_str = repr(user_pattern1)

        repr_str = repr_str.replace('\n', '\\n')
        # We check just the begin of the string:
        assert re.match(
            r'^{classname}\s+at\s+0x{id:08x}\s+\(\\n.*'.format(
                classname=user_pattern1.__class__.__name__,
                id=id(user_pattern1)), repr_str)

    @pytest.mark.parametrize("input_props, exp_exc", [
        ({
            'name': 'a',
            'description': 'fake description X',
            'pattern': 'a*',
            'type': 'glob-like',
            'retention-time': 28,
            'user-template-uri': '/api/users/oid-tpl'
        }, None),
    ])
    def test_up_delete(self, input_props, exp_exc):
        """Test UserPattern.delete()."""

        faked_user_pattern = self.add_user_pattern(
            name=input_props['name'],
            pattern=input_props['pattern'],
            type_=input_props['type'],
            user_template_uri=input_props['user-template-uri'])

        user_pattern_mgr = self.console.user_patterns
        user_pattern = user_pattern_mgr.find(name=faked_user_pattern.name)

        if exp_exc is not None:

            with pytest.raises(exp_exc.__class__) as exc_info:

                # Execute the code to be tested
                user_pattern.delete()

            exc = exc_info.value
            if isinstance(exp_exc, HTTPError):
                assert exc.http_status == exp_exc.http_status
                assert exc.reason == exp_exc.reason

            # Check that the user pattern still exists
            user_pattern_mgr.find(name=faked_user_pattern.name)

        else:

            # Execute the code to be tested.
            user_pattern.delete()

            # Check that the user pattern no longer exists
            with pytest.raises(NotFound) as exc_info:
                user_pattern_mgr.find(name=faked_user_pattern.name)

    def test_up_delete_create_same(self):
        """Test UserPattern.delete() followed by create() with same name."""

        user_pattern_name = 'faked_a'

        faked_user1 = self.add_user(name='a', type_='standard')

        # Add the user pattern to be tested
        self.add_user_pattern(name=user_pattern_name,
                              pattern='a_*',
                              type_='glob-like',
                              user_template_uri=faked_user1.uri)

        # Input properties for a user pattern with the same name
        sn_user_pattern_props = {
            'name': user_pattern_name,
            'description': 'User Pattern with same name',
            'pattern': 'a*',
            'type': 'glob-like',
            'retention-time': 28,
            'user-template-uri': '/api/users/oid-tpl',
        }

        user_pattern_mgr = self.console.user_patterns
        user_pattern = user_pattern_mgr.find(name=user_pattern_name)

        # Execute the deletion code to be tested
        user_pattern.delete()

        # Check that the user pattern no longer exists
        with pytest.raises(NotFound):
            user_pattern_mgr.find(name=user_pattern_name)

        # Execute the creation code to be tested.
        user_pattern_mgr.create(sn_user_pattern_props)

        # Check that the user pattern exists again under that name
        sn_user_pattern = user_pattern_mgr.find(name=user_pattern_name)
        description = sn_user_pattern.get_property('description')
        assert description == sn_user_pattern_props['description']

    @pytest.mark.parametrize("input_props", [
        {},
        {
            'description': 'New user pattern description'
        },
    ])
    def test_up_update_properties(self, input_props):
        """Test UserPattern.update_properties()."""

        user_pattern_name = 'faked_a'

        faked_user1 = self.add_user(name='a', type_='standard')

        # Add the user pattern to be tested
        self.add_user_pattern(name=user_pattern_name,
                              pattern='a_*',
                              type_='glob-like',
                              user_template_uri=faked_user1.uri)

        user_pattern_mgr = self.console.user_patterns
        user_pattern = user_pattern_mgr.find(name=user_pattern_name)

        user_pattern.pull_full_properties()
        saved_properties = copy.deepcopy(user_pattern.properties)

        # Execute the code to be tested
        user_pattern.update_properties(properties=input_props)

        # Verify that the resource object already reflects the property
        # updates.
        for prop_name in saved_properties:
            if prop_name in input_props:
                exp_prop_value = input_props[prop_name]
            else:
                exp_prop_value = saved_properties[prop_name]
            assert prop_name in user_pattern.properties
            prop_value = user_pattern.properties[prop_name]
            assert prop_value == exp_prop_value, \
                "Unexpected value for property {!r}".format(prop_name)

        # Refresh the resource object and verify that the resource object
        # still reflects the property updates.
        user_pattern.pull_full_properties()
        for prop_name in saved_properties:
            if prop_name in input_props:
                exp_prop_value = input_props[prop_name]
            else:
                exp_prop_value = saved_properties[prop_name]
            assert prop_name in user_pattern.properties
            prop_value = user_pattern.properties[prop_name]
            assert prop_value == exp_prop_value
Пример #19
0
class TestNic(object):
    """All tests for Nic and NicManager classes."""
    def setup_method(self):
        """
        Set up a faked session, and add a faked CPC in DPM mode with one
        partition that has no NICs.
        Add one OSA adapter, port and vswitch, for tests with OSA-backed NICs.
        Add one ROSE adapter and port, for tests with ROCE-backed NICs.
        """

        self.session = FakedSession('fake-host', 'fake-hmc', '2.13.1', '1.8')
        self.client = Client(self.session)

        # Add a CPC in DPM mode
        self.faked_cpc = self.session.hmc.cpcs.add({
            'element-id': 'fake-cpc1-oid',
            # element-uri is set up automatically
            'parent': None,
            'class': 'cpc',
            'name': 'fake-cpc1-name',
            'description': 'CPC #1 (DPM mode)',
            'status': 'active',
            'dpm-enabled': True,
            'is-ensemble-member': False,
            'iml-mode': 'dpm',
        })
        self.cpc = self.client.cpcs.find(name='fake-cpc1-name')

        # Add a partition to the CPC
        self.faked_partition = self.faked_cpc.partitions.add({
            'element-id':
            'fake-part1-oid',
            # element-uri will be automatically set
            'parent':
            self.faked_cpc.uri,
            'class':
            'partition',
            'name':
            'fake-part1-name',
            'description':
            'Partition #1',
            'status':
            'active',
            'initial-memory':
            1024,
            'maximum-memory':
            2048,
        })
        self.partition = self.cpc.partitions.find(name='fake-part1-name')

        # Add an OSA adapter, port and vswitch to the CPC
        self.faked_osa1 = self.faked_cpc.adapters.add({
            'object-id':
            'osa1-oid',
            'parent':
            self.faked_cpc.uri,
            'class':
            'adapter',
            'name':
            'osa1',
            'description':
            'OSA #1',
            'status':
            'active',
            'type':
            'osd',
            'adapter-id':
            '123',
            'detected-card-type':
            'osa-express-5s-10gb',
            'card-location':
            '1234-5678-J.01',
            'port-count':
            1,
            'network-port-uris': [],
            'state':
            'online',
            'configured-capacity':
            80,
            'used-capacity':
            0,
            'allowed-capacity':
            80,
            'maximum-total-capacity':
            80,
            'channel-path-id':
            '1D',
            'physical-channel-status':
            'operating',
        })
        self.faked_port11 = self.faked_osa1.ports.add({
            'element-id':
            'fake-port11-oid',
            'parent':
            self.faked_osa1.uri,
            'class':
            'network-port',
            'index':
            0,
            'name':
            'fake-port11-name',
            'description':
            'OSA #1 Port #1',
        })
        self.faked_vswitch11 = self.faked_cpc.virtual_switches.add({
            'object-id':
            VSWITCH11_OID,
            'parent':
            self.faked_cpc.uri,
            'class':
            'virtual-switch',
            'name':
            'fake-vswitch11-name',
            'description':
            'Vswitch for OSA #1 Port #1',
            'type':
            'osa',
            'backing-adapter-uri':
            self.faked_osa1.uri,
            'port':
            self.faked_port11.properties['index'],
            'connected-vnic-uris': [],
        })
        assert VSWITCH11_URI == self.faked_vswitch11.uri

        # Add a ROCE adapter and port to the CPC
        self.faked_roce2 = self.faked_cpc.adapters.add({
            'object-id':
            ROCE2_OID,
            'parent':
            self.faked_cpc.uri,
            'class':
            'adapter',
            'name':
            'roce2',
            'description':
            'ROCE #2',
            'status':
            'active',
            'type':
            'roce',
            'adapter-id':
            '123',
            'detected-card-type':
            '10gbe-roce-express',
            'card-location':
            '1234-5678-J.01',
            'port-count':
            1,
            'network-port-uris': [],
            'state':
            'online',
            'configured-capacity':
            80,
            'used-capacity':
            0,
            'allowed-capacity':
            80,
            'maximum-total-capacity':
            80,
            'physical-channel-status':
            'operating',
        })
        self.faked_port21 = self.faked_roce2.ports.add({
            'element-id':
            PORT21_OID,
            'parent':
            self.faked_roce2.uri,
            'class':
            'network-port',
            'index':
            1,
            'name':
            'fake-port21-name',
            'description':
            'ROCE #2 Port #1',
        })
        assert PORT21_URI == self.faked_port21.uri

    def add_nic1(self):
        """Add a faked OSA NIC 1 to the faked partition."""
        faked_nic = self.faked_partition.nics.add({
            'element-id':
            NIC1_OID,
            # element-uri will be automatically set
            'parent':
            self.faked_partition.uri,
            'class':
            'nic',
            'name':
            NIC1_NAME,
            'description':
            'NIC ' + NIC1_NAME,
            'type':
            'osd',
            'virtual-switch-uri':
            VSWITCH11_URI,
            'device-number':
            '1111',
            'ssc-management-nic':
            False,
        })
        return faked_nic

    def add_nic2(self):
        """Add a faked ROCE NIC 2 to the faked partition."""
        faked_nic = self.faked_partition.nics.add({
            'element-id':
            NIC2_OID,
            # element-uri will be automatically set
            'parent':
            self.faked_partition.uri,
            'class':
            'nic',
            'name':
            NIC2_NAME,
            'description':
            'NIC ' + NIC2_NAME,
            'type':
            'roce',
            'network-adapter-port-uri':
            PORT21_URI,
            'device-number':
            '1112',
            'ssc-management-nic':
            False,
        })
        return faked_nic

    def test_nicmanager_initial_attrs(self):
        """Test initial attributes of NicManager."""

        nic_mgr = self.partition.nics

        # Verify all public properties of the manager object
        assert nic_mgr.resource_class == Nic
        assert nic_mgr.session == self.session
        assert nic_mgr.parent == self.partition
        assert nic_mgr.partition == self.partition

    # TODO: Test for NicManager.__repr__()

    @pytest.mark.parametrize("full_properties_kwargs, prop_names", [
        (dict(), ['element-uri']),
        (dict(full_properties=False), ['element-uri']),
        (dict(full_properties=True), None),
    ])
    def test_nicmanager_list_full_properties(self, full_properties_kwargs,
                                             prop_names):
        """Test NicManager.list() with full_properties."""

        # Add two faked NICs
        faked_nic1 = self.add_nic1()
        faked_nic2 = self.add_nic2()

        exp_faked_nics = [faked_nic1, faked_nic2]
        nic_mgr = self.partition.nics

        # Execute the code to be tested
        nics = nic_mgr.list(**full_properties_kwargs)

        assert_resources(nics, exp_faked_nics, prop_names)

    @pytest.mark.parametrize("filter_args, exp_oids", [
        ({
            'element-id': NIC1_OID
        }, [NIC1_OID]),
        ({
            'element-id': NIC2_OID
        }, [NIC2_OID]),
        ({
            'element-id': [NIC1_OID, NIC2_OID]
        }, [NIC1_OID, NIC2_OID]),
        ({
            'element-id': [NIC1_OID, NIC1_OID]
        }, [NIC1_OID]),
        ({
            'element-id': NIC1_OID + 'foo'
        }, []),
        ({
            'element-id': [NIC1_OID, NIC2_OID + 'foo']
        }, [NIC1_OID]),
        ({
            'element-id': [NIC2_OID + 'foo', NIC1_OID]
        }, [NIC1_OID]),
        ({
            'name': NIC1_NAME
        }, [NIC1_OID]),
        ({
            'name': NIC2_NAME
        }, [NIC2_OID]),
        ({
            'name': [NIC1_NAME, NIC2_NAME]
        }, [NIC1_OID, NIC2_OID]),
        ({
            'name': NIC1_NAME + 'foo'
        }, []),
        ({
            'name': [NIC1_NAME, NIC2_NAME + 'foo']
        }, [NIC1_OID]),
        ({
            'name': [NIC2_NAME + 'foo', NIC1_NAME]
        }, [NIC1_OID]),
        ({
            'name': [NIC1_NAME, NIC1_NAME]
        }, [NIC1_OID]),
        ({
            'name': '.*nic 1'
        }, [NIC1_OID]),
        ({
            'name': 'nic 1.*'
        }, [NIC1_OID]),
        ({
            'name': 'nic .'
        }, [NIC1_OID, NIC2_OID]),
        ({
            'name': '.ic 1'
        }, [NIC1_OID]),
        ({
            'name': '.+'
        }, [NIC1_OID, NIC2_OID]),
        ({
            'name': 'nic 1.+'
        }, []),
        ({
            'name': '.+nic 1'
        }, []),
        ({
            'name': NIC1_NAME,
            'element-id': NIC1_OID
        }, [NIC1_OID]),
        ({
            'name': NIC1_NAME,
            'element-id': NIC1_OID + 'foo'
        }, []),
        ({
            'name': NIC1_NAME + 'foo',
            'element-id': NIC1_OID
        }, []),
        ({
            'name': NIC1_NAME + 'foo',
            'element-id': NIC1_OID + 'foo'
        }, []),
    ])
    def test_nicmanager_list_filter_args(self, filter_args, exp_oids):
        """Test NicManager.list() with filter_args."""

        # Add two faked NICs
        self.add_nic1()
        self.add_nic2()

        nic_mgr = self.partition.nics

        # Execute the code to be tested
        nics = nic_mgr.list(filter_args=filter_args)

        assert len(nics) == len(exp_oids)
        if exp_oids:
            oids = [nic.properties['element-id'] for nic in nics]
            assert set(oids) == set(exp_oids)

    @pytest.mark.parametrize("initial_partition_status, exp_status_exc", [
        ('stopped', None),
        ('terminated', None),
        ('starting', HTTPError({
            'http-status': 409,
            'reason': 1
        })),
        ('active', None),
        ('stopping', HTTPError({
            'http-status': 409,
            'reason': 1
        })),
        ('degraded', None),
        ('reservation-error', None),
        ('paused', None),
    ])
    @pytest.mark.parametrize("input_props, exp_prop_names, exp_prop_exc", [
        ({}, None, HTTPError({
            'http-status': 400,
            'reason': 5
        })),
        ({
            'name': 'fake-nic-x'
        }, None, HTTPError({
            'http-status': 400,
            'reason': 5
        })),
        ({
            'network-adapter-port-uri': PORT21_URI
        }, None, HTTPError({
            'http-status': 400,
            'reason': 5
        })),
        ({
            'virtual-switch-uri': VSWITCH11_URI
        }, None, HTTPError({
            'http-status': 400,
            'reason': 5
        })),
        ({
            'name': 'fake-nic-x',
            'network-adapter-port-uri': PORT21_URI
        }, ['element-uri', 'name', 'network-adapter-port-uri'], None),
        ({
            'name': 'fake-nic-x',
            'virtual-switch-uri': VSWITCH11_URI
        }, ['element-uri', 'name', 'virtual-switch-uri'], None),
    ])
    def test_nicmanager_create(self, input_props, exp_prop_names, exp_prop_exc,
                               initial_partition_status, exp_status_exc):
        """Test NicManager.create()."""

        # Set the status of the faked partition
        self.faked_partition.properties['status'] = initial_partition_status

        nic_mgr = self.partition.nics

        if exp_status_exc:
            exp_exc = exp_status_exc
        elif exp_prop_exc:
            exp_exc = exp_prop_exc
        else:
            exp_exc = None

        if exp_exc:

            with pytest.raises(exp_exc.__class__) as exc_info:

                # Execute the code to be tested
                nic = nic_mgr.create(properties=input_props)

            exc = exc_info.value
            if isinstance(exp_exc, HTTPError):
                assert exc.http_status == exp_exc.http_status
                assert exc.reason == exp_exc.reason

        else:

            # Execute the code to be tested.
            # Note: the Nic object returned by Nic.create() has
            # the input properties plus 'element-uri' plus 'element-id'.
            nic = nic_mgr.create(properties=input_props)

            # Check the resource for consistency within itself
            assert isinstance(nic, Nic)
            nic_name = nic.name
            exp_nic_name = nic.properties['name']
            assert nic_name == exp_nic_name
            nic_uri = nic.uri
            exp_nic_uri = nic.properties['element-uri']
            assert nic_uri == exp_nic_uri

            # Check the properties against the expected names and values
            for prop_name in exp_prop_names:
                assert prop_name in nic.properties
                if prop_name in input_props:
                    value = nic.properties[prop_name]
                    exp_value = input_props[prop_name]
                    assert value == exp_value

    def test_nic_repr(self):
        """Test Nic.__repr__()."""

        # Add a faked nic
        faked_nic = self.add_nic1()

        nic_mgr = self.partition.nics
        nic = nic_mgr.find(name=faked_nic.name)

        # Execute the code to be tested
        repr_str = repr(nic)

        repr_str = repr_str.replace('\n', '\\n')
        # We check just the begin of the string:
        assert re.match(
            r'^{classname}\s+at\s+0x{id:08x}\s+\(\\n.*'.format(
                classname=nic.__class__.__name__, id=id(nic)), repr_str)

    @pytest.mark.parametrize("initial_partition_status, exp_exc", [
        ('stopped', None),
        ('terminated', None),
        ('starting', HTTPError({
            'http-status': 409,
            'reason': 1
        })),
        ('active', None),
        ('stopping', HTTPError({
            'http-status': 409,
            'reason': 1
        })),
        ('degraded', None),
        ('reservation-error', None),
        ('paused', None),
    ])
    def test_nic_delete(self, initial_partition_status, exp_exc):
        """Test Nic.delete()."""

        # Add a faked NIC to be tested and another one
        faked_nic = self.add_nic1()
        self.add_nic2()

        # Set the status of the faked partition
        self.faked_partition.properties['status'] = initial_partition_status

        nic_mgr = self.partition.nics

        nic = nic_mgr.find(name=faked_nic.name)

        if exp_exc:

            with pytest.raises(exp_exc.__class__) as exc_info:

                # Execute the code to be tested
                nic.delete()

            exc = exc_info.value
            if isinstance(exp_exc, HTTPError):
                assert exc.http_status == exp_exc.http_status
                assert exc.reason == exp_exc.reason

            # Check that the NIC still exists
            nic_mgr.find(name=faked_nic.name)

        else:

            # Execute the code to be tested.
            nic.delete()

            # Check that the NIC no longer exists
            with pytest.raises(NotFound) as exc_info:
                nic_mgr.find(name=faked_nic.name)

    def test_nic_delete_create_same_name(self):
        """Test Nic.delete() followed by Nic.create() with same name."""

        # Add a faked NIC to be tested and another one
        faked_nic = self.add_nic1()
        nic_name = faked_nic.name
        self.add_nic2()

        # Construct the input properties for a third NIC with same name
        part3_props = copy.deepcopy(faked_nic.properties)
        part3_props['description'] = 'Third NIC'

        # Set the status of the faked partition
        self.faked_partition.properties['status'] = 'stopped'  # deletable

        nic_mgr = self.partition.nics
        nic = nic_mgr.find(name=nic_name)

        # Execute the deletion code to be tested.
        nic.delete()

        # Check that the NIC no longer exists
        with pytest.raises(NotFound):
            nic_mgr.find(name=nic_name)

        # Execute the creation code to be tested.
        nic_mgr.create(part3_props)

        # Check that the NIC exists again under that name
        nic3 = nic_mgr.find(name=nic_name)
        description = nic3.get_property('description')
        assert description == 'Third NIC'

    @pytest.mark.parametrize("input_props", [
        {},
        {
            'description': 'New NIC description'
        },
        {
            'device-number': 'FEDC',
            'description': 'New NIC description'
        },
    ])
    def test_nic_update_properties(self, input_props):
        """Test Nic.update_properties()."""

        # Add a faked NIC
        faked_nic = self.add_nic1()

        # Set the status of the faked partition
        self.faked_partition.properties['status'] = 'stopped'  # updatable

        nic_mgr = self.partition.nics
        nic = nic_mgr.find(name=faked_nic.name)

        nic.pull_full_properties()
        saved_properties = copy.deepcopy(nic.properties)

        # Execute the code to be tested
        nic.update_properties(properties=input_props)

        # Verify that the resource object already reflects the property
        # updates.
        for prop_name in saved_properties:
            if prop_name in input_props:
                exp_prop_value = input_props[prop_name]
            else:
                exp_prop_value = saved_properties[prop_name]
            assert prop_name in nic.properties
            prop_value = nic.properties[prop_name]
            assert prop_value == exp_prop_value

        # Refresh the resource object and verify that the resource object
        # still reflects the property updates.
        nic.pull_full_properties()
        for prop_name in saved_properties:
            if prop_name in input_props:
                exp_prop_value = input_props[prop_name]
            else:
                exp_prop_value = saved_properties[prop_name]
            assert prop_name in nic.properties
            prop_value = nic.properties[prop_name]
            assert prop_value == exp_prop_value

    def test_nic_update_name(self):
        """Test Nic.update_properties() with 'name' property."""

        # Add a faked NIC
        faked_nic = self.add_nic1()
        nic_name = faked_nic.name

        # Set the status of the faked partition
        self.faked_partition.properties['status'] = 'stopped'  # updatable

        nic_mgr = self.partition.nics
        nic = nic_mgr.find(name=nic_name)

        new_nic_name = "new-" + nic_name

        # Execute the code to be tested
        nic.update_properties(properties={'name': new_nic_name})

        # Verify that the resource is no longer found by its old name, using
        # list() (this does not use the name-to-URI cache).
        nics_list = nic_mgr.list(filter_args=dict(name=nic_name))
        assert len(nics_list) == 0

        # Verify that the resource is no longer found by its old name, using
        # find() (this uses the name-to-URI cache).
        with pytest.raises(NotFound):
            nic_mgr.find(name=nic_name)

        # Verify that the resource object already reflects the update, even
        # though it has not been refreshed yet.
        assert nic.properties['name'] == new_nic_name

        # Refresh the resource object and verify that it still reflects the
        # update.
        nic.pull_full_properties()
        assert nic.properties['name'] == new_nic_name

        # Verify that the resource can be found by its new name, using find()
        new_nic_find = nic_mgr.find(name=new_nic_name)
        assert new_nic_find.properties['name'] == new_nic_name

        # Verify that the resource can be found by its new name, using list()
        new_nics_list = nic_mgr.list(filter_args=dict(name=new_nic_name))
        assert len(new_nics_list) == 1
        new_nic_list = new_nics_list[0]
        assert new_nic_list.properties['name'] == new_nic_name

    def test_nicmanager_resource_object(self):
        """
        Test NicManager.resource_object().

        This test exists for historical reasons, and by now is covered by the
        test for BaseManager.resource_object().
        """

        nic_mgr = self.partition.nics
        nic_oid = 'fake-nic-id0711'

        # Execute the code to be tested
        nic = nic_mgr.resource_object(nic_oid)

        nic_uri = self.partition.uri + "/nics/" + nic_oid

        assert isinstance(nic, Nic)
        assert nic.uri == nic_uri
        assert nic.properties['element-uri'] == nic_uri
        assert nic.properties['element-id'] == nic_oid
        assert nic.properties['class'] == 'nic'
        assert nic.properties['parent'] == self.partition.uri