Ejemplo n.º 1
0
    def test_vios_busy_helper(self, mock_sleep, mock_sess):
        # Try with 1 retries
        hlp = functools.partial(vios_busy.vios_busy_retry_helper,
                                max_retries=1)
        error = pvmex.Error('yo', response=self.http_error.response)
        mock_sess.request.side_effect = error
        adpt = adp.Adapter(mock_sess, use_cache=False, helpers=hlp)
        self.assertRaises(pvmex.Error,
                          adpt._request,
                          'method',
                          'path',
                          body='the body')
        # Test that the request method was called twice and sleep was called
        self.assertEqual(mock_sess.request.call_count, 2)
        mock_sleep.assert_called_once_with(5 * 1)

        # Test with more retries and sleep values
        retries = 10
        hlp = functools.partial(vios_busy.vios_busy_retry_helper,
                                max_retries=retries,
                                delay=15)
        mock_sess.reset_mock()
        self.assertRaises(pvmex.Error,
                          adpt._request,
                          'method',
                          'path',
                          body='the body',
                          helpers=hlp)
        # Should have tried 'retries' times plus the initial one
        self.assertEqual(mock_sess.request.call_count, retries + 1)

        # Test with None response
        mock_sess.reset_mock()
        error = pvmex.Error('yo', response=None)
        mock_sess.request.side_effect = error
        hlp = functools.partial(vios_busy.vios_busy_retry_helper,
                                max_retries=1,
                                delay=15)
        self.assertRaises(pvmex.Error,
                          adpt._request,
                          'method',
                          'path',
                          body='the body',
                          helpers=hlp)
        # There should be no retries since the response was None
        self.assertEqual(mock_sess.request.call_count, 1)

        # Test with a Service Unavailable exception
        mock_sess.reset_mock()
        hlp = functools.partial(vios_busy.vios_busy_retry_helper,
                                max_retries=1)
        error = pvmex.Error('yo', response=self.http_error_sa.response)
        mock_sess.request.side_effect = error
        adpt = adp.Adapter(mock_sess, use_cache=False, helpers=hlp)
        self.assertRaises(pvmex.Error,
                          adpt._request,
                          'method',
                          'path',
                          body='the body')
        self.assertEqual(mock_sess.request.call_count, 2)
Ejemplo n.º 2
0
    def test_invalid_helper(self, mock_sess):

        hlp = "bad helper, shame on you"

        mock_sess.request.return_value = 'Should not get returned'
        adpt = adp.Adapter(mock_sess, helpers=hlp)
        with self.assertRaises(TypeError):
            adpt._request('method', 'path')

        adpt = adp.Adapter(mock_sess)
        with self.assertRaises(TypeError):
            adpt._request('method', 'path', helpers=[hlp])
Ejemplo n.º 3
0
    def init_host(self, host):
        """Initialize anything that is necessary for the driver to function.

        Includes catching up with currently running VMs on the given host.
        """
        # Build the adapter. May need to attempt the connection multiple times
        # in case the PowerVM management API service is starting.
        # TODO(efried): Implement async compute service enable/disable like
        # I73a34eb6e0ca32d03e54d12a5e066b2ed4f19a61
        self.adapter = pvm_apt.Adapter(
            pvm_apt.Session(conn_tries=60),
            helpers=[log_hlp.log_helper, vio_hlp.vios_busy_retry_helper])
        # Make sure the Virtual I/O Server(s) are available.
        pvm_par.validate_vios_ready(self.adapter)
        self.host_wrapper = pvm_ms.System.get(self.adapter)[0]

        # Do a scrub of the I/O plane to make sure the system is in good shape
        LOG.info("Clearing stale I/O connections on driver init.")
        pvm_stor.ComprehensiveScrub(self.adapter).execute()

        # Initialize the disk adapter
        # TODO(efried): Other disk adapters (localdisk), by conf selection.
        self.disk_dvr = ssp.SSPDiskAdapter(self.adapter,
                                           self.host_wrapper.uuid)
        self.image_api = image.API()

        LOG.info("The PowerVM compute driver has been initialized.")
Ejemplo n.º 4
0
 def test_multi_list(self):
     hlp1 = functools.partial(cat_string_helper, string="1")
     hlp2 = functools.partial(cat_string_helper, string="2")
     adpt = adp.Adapter('mock_session',
                        use_cache=False,
                        helpers=[hlp1, hlp2])
     self.assertEqual([hlp1, hlp2], adpt.helpers)
Ejemplo n.º 5
0
    def test_create(self, mock_session):
        """Test create() method found in the Adapter class."""
        # Init test data
        adapter = adp.Adapter(self.sess, use_cache=False)
        new_scsi = pvm_stor.VSCSIClientAdapterElement.bld(adapter)

        element = new_scsi
        root_type = 'ManagedSystem'
        root_id = 'id'
        child_type = 'LogicalPartition'

        create_response = self._mk_response(200, response_text)

        # Mock out the method and class we are not currently testing
        session = mock_session.return_value
        session.request.return_value = create_response

        # Run the actual test
        ret_create_value = adapter.create(element, root_type, root_id,
                                          child_type)

        # Verify Correct path was built in build_path()
        reqpath = adp.Adapter.build_path('uom',
                                         root_type,
                                         root_id,
                                         child_type,
                                         xag=[])

        # Verify the return value
        # self.assertIsInstance(ret_create_value, adp.Response)
        self.assertEqual('PUT', ret_create_value.reqmethod)
        self.assertEqual(200, ret_create_value.status)
        self.assertEqual(reqpath, ret_create_value.reqpath)
Ejemplo n.º 6
0
    def test_read(self, mock_session):
        """Test read() method found in the Adapter class."""
        # Init test data
        root_type = 'ManagedSystem'
        root_id = 'caae9209-25e5-35cd-a71a-ed55c03f294d'
        child_type = 'child'
        child_id = 'child'
        suffix_type = 'quick'
        adapter = adp.Adapter(self.sess, use_cache=False)

        # Create a Response object, that will serve as a mock return value
        read_response = self._mk_response(200, response_text)

        # Mock out the method and class we are not currently testing
        session = mock_session.return_value
        session.request.return_value = read_response

        # Run the actual test
        ret_read_value = adapter.read(root_type, root_id, child_type, child_id,
                                      suffix_type)

        # Verify Correct path was built in build_path()
        reqpath = adp.Adapter.build_path('uom', root_type, root_id, child_type,
                                         child_id, suffix_type)
        # Verify the return value
        # self.assertIsInstance(ret_read_value, adp.Response)
        self.assertEqual('GET', ret_read_value.reqmethod)
        self.assertEqual(200, ret_read_value.status)
        self.assertEqual(reqpath, ret_read_value.reqpath)
Ejemplo n.º 7
0
    def init_host(self, host):
        """Initialize anything that is necessary for the driver to function.

        Includes catching up with currently running VMs on the given host.
        """
        LOG.warning(
            'The powervm virt driver is deprecated and may be removed in a '
            'future release. The driver is not tested by the OpenStack '
            'project nor does it have clear maintainers and thus its quality'
            'can not be ensured. If you are using the driver in production '
            'please let us know the openstack-discuss mailing list or on IRC'
        )

        # Build the adapter. May need to attempt the connection multiple times
        # in case the PowerVM management API service is starting.
        # TODO(efried): Implement async compute service enable/disable like
        # I73a34eb6e0ca32d03e54d12a5e066b2ed4f19a61
        self.adapter = pvm_apt.Adapter(
            pvm_apt.Session(conn_tries=60),
            helpers=[log_hlp.log_helper, vio_hlp.vios_busy_retry_helper])
        # Make sure the Virtual I/O Server(s) are available.
        pvm_par.validate_vios_ready(self.adapter)
        self.host_wrapper = pvm_ms.System.get(self.adapter)[0]

        # Do a scrub of the I/O plane to make sure the system is in good shape
        LOG.info("Clearing stale I/O connections on driver init.")
        pvm_stor.ComprehensiveScrub(self.adapter).execute()

        # Initialize the disk adapter
        self.disk_dvr = importutils.import_object_ns(
            DISK_ADPT_NS, DISK_ADPT_MAPPINGS[CONF.powervm.disk_driver.lower()],
            self.adapter, self.host_wrapper.uuid)
        self.image_api = glance.API()

        LOG.info("The PowerVM compute driver has been initialized.")
Ejemplo n.º 8
0
    def test_read_by_href(self, mock_read_by_path, mock_log):
        """Ensure read_by_href correctly extends, preserves query strings."""
        def validate_read_by_path(expected):
            def _read_by_path(path, etag, timeout, auditmemento, age,
                              sensitive, helpers):
                self._assert_paths_equivalent(expected, path)
                for param in (etag, auditmemento, helpers):
                    self.assertIsNone(param)
                for param2 in (age, timeout):
                    self.assertEqual(-1, param2)
                self.assertFalse(sensitive)

            return _read_by_path

        self.sess.host = 'foo'
        self.sess.port = 123
        adapter = adp.Adapter(self.sess)
        mock_read_by_path.side_effect = validate_read_by_path(
            '/rest/api/uom/Bar?k=v&group=None#frag')
        adapter.read_by_href('http://foo:123/rest/api/uom/Bar?k=v#frag')
        self.assertFalse(mock_log.debug.called)

        self.sess.host = 'bar'
        mock_read_by_path.side_effect = validate_read_by_path(
            '/rest/api/uom/Bar?k=v&group=None#frag')
        adapter.read_by_href('http://foo:123/rest/api/uom/Bar?k=v#frag')
        self.assertTrue(mock_log.debug.called)

        mock_read_by_path.side_effect = validate_read_by_path(
            '/rest/api/uom/Bar?k=v&group=RealGroup#frag')
        adapter.read_by_href(
            'http://foo:123/rest/api/uom/Bar?k=v&group=RealGroup#frag')
Ejemplo n.º 9
0
    def test_upload(self, mock_session):
        # Build the adapter
        adapter = adp.Adapter(self.sess, use_cache=False)

        # Mock data
        filedesc_mock = mock.MagicMock()
        filedesc_mock.findtext.side_effect = ['uuid', 'mime']

        mock_request = mock.MagicMock()
        adapter._request = mock_request

        # Invoke
        adapter.upload_file(filedesc_mock, None)

        # Validate
        expected_headers = {
            'Accept': 'application/vnd.ibm.powervm.web+xml',
            'Content-Type': 'mime'
        }
        expected_path = '/rest/api/web/File/contents/uuid'
        mock_request.assert_called_with('PUT',
                                        expected_path,
                                        helpers=None,
                                        headers=expected_headers,
                                        timeout=-1,
                                        auditmemento=None,
                                        filehandle=None,
                                        chunksize=65536)
Ejemplo n.º 10
0
 def test_single_list(self):
     hlp = functools.partial(cat_string_helper, string="purple!")
     hlp_list = [hlp]
     adpt = adp.Adapter('mock_session', helpers=hlp_list)
     self.assertEqual(hlp_list, adpt.helpers)
     # Use this test to ensure the list returned is a copy
     self.assertNotEqual(id(hlp_list), id(adpt.helpers))
Ejemplo n.º 11
0
    def test_update(self, mock_session):
        """Test update() method found in the Adapter class."""
        # Init test data
        data = 'data'
        etag = 'etag'
        root_type = 'root type'
        root_id = 'root id'
        adapter = adp.Adapter(self.sess, use_cache=False)

        update_response = self._mk_response(200, response_text)

        # Mock out the method and class we are not currently testing
        session = mock_session.return_value
        session.request.return_value = update_response

        # Run the actual test
        ret_update_value = adapter.update(data, etag, root_type, root_id)

        # Verify Correct path was built in build_path()
        reqpath = adp.Adapter.build_path('uom', root_type, root_id)

        # Verify the return value
        # self.assertIsInstance(ret_update_value, adp.Response)
        self.assertEqual('POST', ret_update_value.reqmethod)
        self.assertEqual(200, ret_update_value.status)
        self.assertEqual(reqpath, ret_update_value.reqpath)
Ejemplo n.º 12
0
    def test_sample_helper(self, mock_sleep):

        helpers = smpl_hlp.sample_retry_helper
        fake_resp1 = adp.Response('GET',
                                  '/some/path',
                                  200,
                                  'OK', ['headers'],
                                  body='Some Text HSCL3205 More Text')
        self.sess.request.side_effect = pvmex.Error('yo', response=fake_resp1)
        adpt = adp.Adapter(self.sess, helpers=helpers)
        self.assertRaises(pvmex.Error,
                          adpt._request,
                          'method',
                          'path',
                          body='the body')

        # Test that the request method was called twice and sleep was called
        self.assertEqual(self.sess.request.call_count, 2)
        mock_sleep.assert_called_once_with(5 * 1)

        hlp = functools.partial(smpl_hlp.sample_retry_helper, max_retries=5)
        self.sess.reset_mock()
        try:
            adpt._request('method', 'path', body='the body', helpers=hlp)
        except Exception:
            # Should have tried 6 times total
            self.assertEqual(self.sess.request.call_count, 6)
Ejemplo n.º 13
0
 def test_crt_related_href(self, mock_sess):
     """Tests to make sure that related elements are well formed."""
     mock_sess.dest = 'root'
     adapter = adpt.Adapter(mock_sess)
     href = vios.VStorageMapping.crt_related_href(adapter, 'host', 'lpar')
     self.assertEqual(
         'root/rest/api/uom/ManagedSystem/host/'
         'LogicalPartition/lpar', href)
Ejemplo n.º 14
0
    def test_headers(self, mock_request):
        def validate_hdrs_func(acc=None, inm=None):
            expected_headers = {}
            if acc is not None:
                expected_headers['Accept'] = acc
            if inm is not None:
                expected_headers['If-None-Match'] = inm

            def validate_request(meth, path, **kwargs):
                self.assertEqual(expected_headers, kwargs['headers'])

            return validate_request

        adpt = adp.Adapter(mock.Mock())
        basepath = c.API_BASE_PATH + 'uom/SomeRootObject'
        uuid = "abcdef01-2345-2345-2345-67890abcdef0"
        hdr_xml = 'application/atom+xml'
        hdr_json = 'application/json'
        etag = 'abc123'

        # Root feed
        mock_request.side_effect = validate_hdrs_func(acc=hdr_xml)
        adpt._read_by_path(basepath, None, None, None, None)

        # Root instance with etag
        mock_request.side_effect = validate_hdrs_func(acc=hdr_xml, inm=etag)
        adpt._read_by_path(basepath + '/' + uuid, etag, None, None, None)

        # Quick root anchor (produces XML report of available quick properties
        mock_request.side_effect = validate_hdrs_func(acc=hdr_xml)
        adpt._read_by_path(basepath + '/quick', None, None, None, None)

        # Quick root instance (JSON of all quick properties)
        mock_request.side_effect = validate_hdrs_func(acc=hdr_json)
        adpt._read_by_path('/'.join([basepath, uuid, 'quick']), None, None,
                           None, None)

        # Specific quick property
        mock_request.side_effect = validate_hdrs_func(acc=hdr_json)
        adpt._read_by_path('/'.join([basepath, uuid, 'quick', 'property']),
                           None, None, None, None)

        # Explicit JSON file
        mock_request.side_effect = validate_hdrs_func(acc=hdr_json)
        adpt._read_by_path('/'.join([basepath, 'somefile.json']), None, None,
                           None, None)

        # Object that happens to end in 'json'
        mock_request.side_effect = validate_hdrs_func(acc=hdr_xml)
        adpt._read_by_path('/'.join([basepath, 'xml_about_json']), None, None,
                           None, None)

        # Quick with query params and fragments
        mock_request.side_effect = validate_hdrs_func(acc=hdr_json)
        adpt._read_by_path(
            '/'.join([basepath, uuid, 'quick']) + '?group=None#frag', None,
            None, None, None)
Ejemplo n.º 15
0
    def test_runs(self, mock_sess):

        hlp1 = functools.partial(cat_string_helper, string="1")
        hlp2 = functools.partial(cat_string_helper, string="2")
        hlp3 = functools.partial(cat_string_helper, string="3")

        mock_sess.request.return_value = 'countdown:'
        adpt = adp.Adapter(mock_sess, helpers=[hlp1, hlp2, hlp3])
        self.assertEqual('countdown:321', adpt._request('method', 'path'))

        # Override adapter helpers
        self.assertEqual('countdown:2',
                         adpt._request('method', 'path', helpers=hlp2))

        # No adapter helpers, but request helper
        adpt = adp.Adapter(mock_sess)
        self.assertEqual('countdown:1',
                         adpt._request('method', 'path', helpers=[hlp1]))
Ejemplo n.º 16
0
    def refresh(self):
        """Do the query and get the response."""

        print("Connecting.")
        adap = adp.Adapter()

        print("Reading path:  " + self.path)
        self.response = adap.read(self.path)

        print("Received " + str(self.response))
Ejemplo n.º 17
0
def main(argv):

    new_response = pvmhttp.PVMResp()
    output_file = None
    file_to_refresh = None

    aindex = 0
    while aindex < len(argv):
        if argv[aindex] == '-path':
            aindex += 1
            new_response.path = argv[aindex]
        elif argv[aindex] == '-comment':
            aindex += 1
            new_response.comment = argv[aindex]
        elif argv[aindex] == '-output':
            aindex += 1
            output_file = argv[aindex]
        elif argv[aindex] == '-refresh':
            aindex += 1
            file_to_refresh = argv[aindex]
        else:
            print("Unknown argument ", argv[aindex])
            usage()

        aindex += 1

    if file_to_refresh:
        rc = refresh_response(file_to_refresh)
        exit(rc)

    if new_response.path is None or output_file is None:
        usage()

    print("Connecting.")
    adap = adp.Adapter()
    print("Reading path:  ", new_response.path)
    new_response.response = adap.read(new_response.path)

    print("Received ", new_response.response)

    orig_file_name = output_file

    dirname = os.path.dirname(output_file)
    if dirname is None or dirname == '':
        dirname = os.path.dirname(__file__)
        output_file = os.path.join(dirname, output_file)

    new_response.save(output_file)

    print("Response has been saved in ", output_file)
    print("Use the pvmhttp.load_pvm_resp('%s') method "
          "to load it in your testcase " % orig_file_name)

    print("You can have the %s file rebuilt by running: "
          "create_httpresp -refresh %s" % (orig_file_name, orig_file_name))
Ejemplo n.º 18
0
    def init_host(self, host):
        """Initialize anything that is necessary for the driver to function.

        Includes catching up with currently running VMs on the given host.
        """
        # Build the adapter.  May need to attempt the connection multiple times
        # in case the PowerVM management API service is starting.
        # TODO(efried): Implement async compute service enable/disable like
        # I73a34eb6e0ca32d03e54d12a5e066b2ed4f19a61
        self.adapter = pvm_apt.Adapter(
            pvm_apt.Session(conn_tries=60),
            helpers=[log_hlp.log_helper, vio_hlp.vios_busy_retry_helper])
        # Make sure the Virtual I/O Server(s) are available.
        pvm_par.validate_vios_ready(self.adapter)
        self.host_wrapper = pvm_ms.System.get(self.adapter)[0]
        LOG.info("The PowerVM compute driver has been initialized.")
Ejemplo n.º 19
0
    def __init__(self):
        super(PowerVMInspector, self).__init__()

        # Build the adapter to the PowerVM API.
        self.adpt = pvm_adpt.Adapter(
            pvm_adpt.Session(),
            helpers=[log_hlp.log_helper, vio_hlp.vios_busy_retry_helper])

        # Get the host system UUID
        host_uuid = self._get_host_uuid(self.adpt)

        # Ensure that metrics gathering is running for the host.
        pvm_mon_util.ensure_ltm_monitors(self.adpt, host_uuid)

        # Get the VM Metric Utility
        self.vm_metrics = pvm_mon_util.LparMetricCache(self.adpt, host_uuid)
Ejemplo n.º 20
0
    def __init__(self, conf):
        super(PowerVMInspector, self).__init__(conf)

        # Build the adapter.  May need to attempt the connection multiple times
        # in case the REST server is starting.
        session = pvm_adpt.Session(conn_tries=300)
        self.adpt = pvm_adpt.Adapter(
            session,
            helpers=[log_hlp.log_helper, vio_hlp.vios_busy_retry_helper])

        # Get the host system UUID
        host_uuid = self._get_host_uuid(self.adpt)

        # Ensure that metrics gathering is running for the host.
        pvm_mon_util.ensure_ltm_monitors(self.adpt, host_uuid)

        # Get the VM Metric Utility
        self.vm_metrics = pvm_mon_util.LparMetricCache(self.adpt, host_uuid)
Ejemplo n.º 21
0
    def test_unauthorized_error(self, mock_session, mock_log):
        """401 (unauthorized) calling Adapter.create()."""

        # Init test data
        adapter = adp.Adapter(self.sess, use_cache=False)
        new_scsi = pvm_stor.VSCSIClientAdapterElement.bld(adapter)

        element = new_scsi
        root_type = 'ManagedSystem'
        root_id = 'id'
        child_type = 'LogicalPartition'

        create_response = self._mk_response(401)

        # Mock out the method and class we are not currently testing
        session = mock_session.return_value
        session.request.return_value = create_response

        # Run the actual test
        self.assertRaises(pvmex.HttpError, adapter.create, element, root_type,
                          root_id, child_type)
        mock_log.warn.assert_called_once_with(mock.ANY)
Ejemplo n.º 22
0
    def test_delete(self, mock_session):
        """Test delete() method found in the Adapter class."""
        # Init test data
        root_type = 'ManagedSystem'
        root_id = 'id'
        adapter = adp.Adapter(self.sess, use_cache=False)

        delete_response = self._mk_response(204)

        # Mock out the method and class we are not currently testing
        session = mock_session.return_value
        session.request.return_value = delete_response

        # Run the actual test
        ret_delete_value = adapter.delete(root_type, root_id)

        # Verify Correct path was built in build_path()
        reqpath = adp.Adapter.build_path('uom', root_type, root_id, xag=[])

        # Verify the return value
        # self.assertIsInstance(ret_delete_value, adp.Response)
        self.assertEqual('DELETE', ret_delete_value.reqmethod)
        self.assertEqual(204, ret_delete_value.status)
        self.assertEqual(reqpath, ret_delete_value.reqpath)
Ejemplo n.º 23
0
    def test_log_helper(self, mock_log):

        helpers = log_hlp.log_helper
        response = adp.Response('GET', '/some/path', 200, 'OK', ['headers'])
        self.sess.request.return_value = response
        adpt = adp.Adapter(self.sess, helpers=helpers)

        # Test that we get the response we expect passed back unharmed
        self.assertEqual(response,
                         adpt._request('method', 'path', body='the body'))

        # Should be 1 req/resp in the log now, which would be 4 info messages
        mock_log.reset_mock()
        log_hlp._write_thread_log()
        self.assertEqual(mock_log.info.call_count, 4)

        # Should be empty now
        mock_log.reset_mock()
        log_hlp._write_thread_log()
        self.assertEqual(mock_log.info.call_count, 0)

        # Test that we limit the number of entries
        mock_log.reset_mock()
        for x in range(0, 30):
            adpt._request('method1', 'path', body='the body %d' % x)
        log_hlp._write_thread_log()
        # Each req/resp pair is 2 log entries but headers and body
        # are logged separately, so with maxlogs=3, it's 3 * 2 * 2.
        self.assertEqual(mock_log.info.call_count, (3 * 2 * 2))

        mock_log.reset_mock()
        # Add a few records
        adpt._request('method1', 'path', body='the body')
        # Ensure a 412 (special case) doesn't dump, but does raise
        self.sess.request.side_effect = pvmex.HttpError(
            mock.Mock(status=c.HTTPStatus.ETAG_MISMATCH))
        self.assertRaises(pvmex.HttpError,
                          adpt._request,
                          'method2',
                          'path',
                          body='the body')
        self.assertEqual(0, mock_log.info.call_count)
        # Ensure a non-412 exception dumps the logs and is then raised
        self.sess.request.side_effect = pvmex.HttpError(
            mock.Mock(status=c.HTTPStatus.INTERNAL_ERROR))
        mock_log.reset_mock()
        self.assertRaises(pvmex.Error,
                          adpt._request,
                          'method',
                          'path',
                          body='the body')
        # Should be 10 entries. 4 * 2 req/resp, 2 for this req.
        self.assertEqual(mock_log.info.call_count, 10)

        # Ensure the log storage is initialized correctly, and we can change
        # the default value
        hlp_size = functools.partial(log_hlp.log_helper, max_logs=12)
        adpt1 = adp.Adapter(self.sess, helpers=hlp_size)
        self.sess.request.side_effect = None
        with mock.patch('pypowervm.helpers.log_helper.'
                        '_init_thread_stg') as mock_init:
            adpt1._request('method1', 'path', body='the body')
            # Should be called with 24 since 12 * 2 entries.
            self.assertEqual(mock_init.call_args_list,
                             [mock.call(max_entries=24)])
Ejemplo n.º 24
0
 def setup_adapter(self):
     """Configures the pypowervm adapter and utilities."""
     self.adapter = pvm_adpt.Adapter(
         pvm_adpt.Session(),
         helpers=[log_hlp.log_helper, vio_hlp.vios_busy_retry_helper])
     self.host_uuid = utils.get_host_uuid(self.adapter)
Ejemplo n.º 25
0
    def test_traits_into_wrappers(self, mock_request):
        # Note traits param is None, which reflects the real value of
        # self.traits during _logon's request.
        httpresp = req_mod.Response()
        httpresp._content = _logon_response_text
        httpresp.status_code = 200
        httpresp.headers = req_struct.CaseInsensitiveDict({
            'X-MC-Type':
            'PVM',
            'content-type':
            'application/vnd.ibm.powervm.web+xml; type=LogonResponse'
        })
        mock_request.return_value = httpresp
        sess = adp.Session()
        self.assertEqual('PVM', sess.mc_type)
        self.assertIsNotNone(sess.traits)
        self.assertTrue(sess.traits.local_api)
        self.assertFalse(sess.traits._is_hmc)
        adapter = adp.Adapter(sess)
        self.assertEqual(sess.traits, adapter.traits)

        # Response => Feed => Entrys => EntryWrappers => sub-ElementWrappers
        httpresp._content = _feed_file
        resp = adapter.read('NetworkBridge')
        self.assertEqual(sess.traits, resp.adapter.traits)
        nblist = net.NetBridge.wrap(resp)
        for nb in nblist:
            self.assertIsInstance(nb, net.NetBridge)
            self.assertEqual(sess.traits, nb.traits)
        seas = nblist[0].seas
        for sea in seas:
            self.assertIsInstance(sea, net.SEA)
            self.assertEqual(sess.traits, sea.traits)
        trunk = seas[0].primary_adpt
        self.assertIsInstance(trunk, net.TrunkAdapter)
        self.assertEqual(sess.traits, trunk.traits)

        # Response => Entry => EntryWrapper => sub-EntryWrappers
        # => sub-sub-ElementWrapper
        httpresp._content = _entry_file
        resp = adapter.read('VolumeGroup', root_id='abc123')
        self.assertEqual(sess.traits, resp.adapter.traits)
        vgent = stor.VG.wrap(resp)
        self.assertIsInstance(vgent, stor.VG)
        self.assertEqual(sess.traits, vgent.traits)
        pvs = vgent.phys_vols
        for pvent in pvs:
            self.assertIsInstance(pvent, stor.PV)
            self.assertEqual(sess.traits, pvent.traits)

        # Building raw wrappers from scratch
        class MyEntryWrapper(ewrap.EntryWrapper):
            schema_type = 'SomeObject'

            @classmethod
            def bld(cls, adpt):
                return super(MyEntryWrapper, cls)._bld(adpt)

        mew = MyEntryWrapper.bld(adapter)
        self.assertIsInstance(mew, MyEntryWrapper)
        self.assertEqual(sess.traits, mew.traits)

        class MyElementWrapper(ewrap.ElementWrapper):
            schema_type = 'SomeObject'

            @classmethod
            def bld(cls, adpt):
                return super(MyElementWrapper, cls)._bld(adpt)

        mew = MyElementWrapper.bld(adapter)
        self.assertIsInstance(mew, MyElementWrapper)
        self.assertEqual(sess.traits, mew.traits)
Ejemplo n.º 26
0
 def test_none(self):
     adpt = adp.Adapter('mock_session', helpers=None)
     self.assertEqual([], adpt.helpers)
Ejemplo n.º 27
0
 def test_single(self):
     hlp = functools.partial(cat_string_helper, string="purple!")
     adpt = adp.Adapter('mock_session', helpers=hlp)
     self.assertEqual([hlp], adpt.helpers)
Ejemplo n.º 28
0
    def test_no_helpers(self, mock_sess):

        mock_sess.request.return_value = 'ReturnValue'
        adpt = adp.Adapter(mock_sess)
        self.assertEqual('ReturnValue', adpt._request('method', 'path'))
Ejemplo n.º 29
0
    def test_extend_path(self, mock_session):
        # Init test data
        adapter = adp.Adapter(self.sess, use_cache=False)

        path = adapter.extend_path('basepath',
                                   suffix_type='suffix',
                                   suffix_parm='suffix_parm',
                                   detail='detail',
                                   xag=[pvm_vios.VIOS.xags.FC_MAPPING])

        expected_path = ('basepath/suffix/suffix_parm?detail=detail&'
                         'group=ViosFCMapping')
        self._assert_paths_equivalent(expected_path, path)

        # Multiple XAGs in a set
        path = adapter.extend_path(
            'basepath',
            suffix_type='suffix',
            suffix_parm='suffix_parm',
            detail='detail',
            xag={pvm_vios.VIOS.xags.FC_MAPPING, pvm_vios.VIOS.xags.NETWORK})

        expected_path = ('basepath/suffix/suffix_parm?detail=detail&'
                         'group=ViosFCMapping,ViosNetwork')
        self._assert_paths_equivalent(expected_path, path)

        # Verify sorting
        path = adapter.extend_path(
            'basepath',
            suffix_type='suffix',
            suffix_parm='suffix_parm',
            detail='detail',
            xag=[pvm_vios.VIOS.xags.NETWORK, pvm_vios.VIOS.xags.FC_MAPPING])

        expected_path = ('basepath/suffix/suffix_parm?detail=detail&'
                         'group=ViosFCMapping,ViosNetwork')
        self._assert_paths_equivalent(expected_path, path)

        # Explicitly no XAG
        path = adapter.extend_path('basepath',
                                   suffix_type='suffix',
                                   suffix_parm='suffix_parm',
                                   detail='detail',
                                   xag=[])

        expected_path = 'basepath/suffix/suffix_parm?detail=detail'
        self._assert_paths_equivalent(expected_path, path)

        # Ensure unspecified XAG defaults to group=None
        path = adapter.extend_path('basepath',
                                   suffix_type='suffix',
                                   suffix_parm='suffix_parm')

        expected_path = 'basepath/suffix/suffix_parm?group=None'
        self._assert_paths_equivalent(expected_path, path)

        # ...except for specific suffix types 'quick' and 'do'
        path = adapter.extend_path('basepath',
                                   suffix_type='quick',
                                   suffix_parm='suffix_parm')

        expected_path = 'basepath/quick/suffix_parm'
        self._assert_paths_equivalent(expected_path, path)

        path = adapter.extend_path('basepath',
                                   suffix_type='do',
                                   suffix_parm='suffix_parm')

        expected_path = 'basepath/do/suffix_parm'
        self._assert_paths_equivalent(expected_path, path)

        # Ensure arg xags and path xags interact correctly
        # path_xag=None, arg_xag=None => group=None
        self._assert_paths_equivalent('basepath?group=None',
                                      adapter.extend_path('basepath'))
        # path_xag='None', arg_xag=None => group=None
        self._assert_paths_equivalent(
            'basepath?group=None', adapter.extend_path('basepath?group=None'))
        # path_xag='a,b,c', arg_xag=None => group=a,b,c
        self._assert_paths_equivalent(
            'basepath?group=a,b,c',
            adapter.extend_path('basepath?group=a,b,c'))
        # path_xag=None, arg_xag=() => no group=
        self._assert_paths_equivalent('basepath',
                                      adapter.extend_path('basepath', xag=()))
        # path_xag='None', arg_xag={} => no group=
        self._assert_paths_equivalent(
            'basepath', adapter.extend_path('basepath?group=None', xag={}))
        # path_xag='a,b,c', arg_xag=[] => ValueError
        self.assertRaises(ValueError,
                          adapter.extend_path,
                          'basepath?group=a,b,c',
                          xag=[])
        # path_xag=None, arg_xag='a,b,c' => group='a,b,c'
        self._assert_paths_equivalent(
            'basepath?group=a,b,c',
            adapter.extend_path('basepath', xag={'a', 'b', 'c'}))
        # path_xag='None', arg_xag='a,b,c' => group='a,b,c'
        self._assert_paths_equivalent(
            'basepath?group=a,b,c',
            adapter.extend_path('basepath?group=None', xag=('a', 'b', 'c')))
        # path_xag='a,b,c', arg_xag='a,b,c' => group='a,b,c'
        self._assert_paths_equivalent(
            'basepath?group=a,b,c',
            adapter.extend_path('basepath?group=a,b,c', xag=['a', 'b', 'c']))
        # path_xag='a,b,c', arg_xag='d,e,f' => ValueError
        self.assertRaises(ValueError,
                          adapter.extend_path,
                          'basepath?group=a,b,c',
                          xag=['d', 'e', 'f'])
        # Multi-instance query params properly reassembled.
        self._assert_paths_equivalent(
            'basepath?foo=1,2,3&group=a,b,c&foo=4,5,6',
            adapter.extend_path('basepath?foo=4,5,6&group=None&foo=1,2,3',
                                xag=['a', 'b', 'c']))
Ejemplo n.º 30
0
 def test_empty_init(self, mock_sess):
     adp.Adapter()
     mock_sess.assert_called_with()