def test_service_inc_metrics_statistics(self, _mock_nova_client,
                                            _mock_monasca_client,
                                            _mock_endpoints, _mock_serv_end,
                                            _mock_ks_client):
        _mock_nova_client.return_value = self.mock_novaclient
        _mock_serv_end.return_value = None
        _mock_ks_client.return_value = self.mock_ksclient
        _mock_monasca_client.return_value = self.mock_monasca_client
        _mock_endpoints.return_value = self.mock_get_endpoints

        req_data = {
            api.OPERATION: 'instance-list',
            'monasca_metrics': ['some.metric'],
            'monasca_data': {
                'operation': 'metric_statistics'
            }
        }

        svc = NovaSvc(BllRequest(auth_token=get_mock_token(), data=req_data))
        reply = svc.handle()
        self.assertEqual(reply[api.STATUS], api.COMPLETE)

        self.mock_monasca_client.metrics.list_statistics.assert_called_with(
            name='some.metric')

        self.assertIn(api.DATA, reply)
        data = reply[api.DATA]
        self.assertIn('instances', data)
        self.assertIsInstance(data['instances'], list)
        self.assertTrue(len(data['instances']) > 0)
        self.assertIn('metrics', data['instances'][0])
        self.assertIn('some.metric', data['instances'][0]['metrics'])
        some_metric = data['instances'][0]['metrics']['some.metric']
        self.assertEquals(some_metric[0]['statistics'][0][1], 31)
    def test_hypervisor_list(self, _mock_nova_client, _mock_endpoints,
                             _mock_serv_end, _mock_ks_client):
        _mock_nova_client.return_value = self.mock_novaclient
        _mock_serv_end.return_value = None
        _mock_ks_client.return_value = self.mock_ksclient
        _mock_endpoints.return_value = self.mock_get_endpoints

        svc = NovaSvc(
            BllRequest(auth_token=get_mock_token(),
                       data={api.OPERATION: 'hypervisor-list'}))

        # build up a list of ping statuses
        statuses = {}
        for hyp in self.hyp_list:
            statuses[hyp.hypervisor_hostname] = 'up'

        with patch('bll.plugins.service.SvcBase.call_service',
                   return_value=statuses):
            reply = svc.handle()

        self.assertEqual(api.COMPLETE, reply[api.STATUS])

        # 4 hypervisors
        hyp_list = reply[api.DATA]
        self.assertEqual(len(self.hyp_list), len(hyp_list))
        known_id_list = [x.id for x in self.hyp_list]
        for hyp in hyp_list:
            self.assertTrue(hyp['hypervisor_id'] in known_id_list)
            self.assertEqual(hyp['ping_status'], 'up')
    def nova_operation(self, operation, data=None):
        request = {
            api.TARGET: 'nova',
            api.AUTH_TOKEN: self.token or None,
            api.DATA: {
                api.OPERATION: operation
            }
        }
        if data:
            request[api.DATA].update(data)

        svc = NovaSvc(bll_request=BllRequest(request))
        return svc.handle()
    def test_service_delete_good_input(self, _mock_nova_client,
                                       _mock_endpoints, _mock_serv_end,
                                       _mock_ks_client):
        _mock_nova_client.return_value = self.mock_novaclient
        _mock_serv_end.return_value = None
        _mock_ks_client.return_value = self.mock_ksclient
        _mock_endpoints.return_value = self.mock_get_endpoints

        svc = NovaSvc(
            BllRequest(auth_token=get_mock_token(),
                       operation='service-delete',
                       data={'hostname': 'myhost1'}))
        reply = svc.handle()
        self.assertEqual(reply[api.STATUS], api.COMPLETE)
    def test_service_delete_bad_input(self, _mock_nova_client, _mock_endpoints,
                                      _mock_serv_end, _mock_ks_client):
        _mock_nova_client.return_value = self.mock_novaclient
        _mock_serv_end.return_value = None
        _mock_ks_client.return_value = self.mock_ksclient
        _mock_endpoints.return_value = self.mock_get_endpoints

        with log_level(logging.CRITICAL, 'bll'):
            svc = NovaSvc(
                BllRequest(auth_token=get_mock_token(),
                           operation='service-delete',
                           data={'hostname': 'badhost'}))
            with self.assertRaises(Exception):
                svc.handle()
        def test_service_inc_metrics_dimension_prop(self,
                                                    _mock_nova_client,
                                                    _mock_monasca_client,
                                                    _mock_endpoints,
                                                    _mock_serv_end,
                                                    _mock_ks_client):
            _mock_nova_client.return_value = self.mock_novaclient
            _mock_serv_end.return_value = None
            _mock_ks_client.return_value = self.mock_ksclient
            _mock_monasca_client.return_value = self.mock_monasca_client
            _mock_endpoints.return_value = self.mock_get_endpoints

            req_data = {
                api.OPERATION: 'instance-list',
                'project_id': self.project_list[0].id,
                'monasca_metrics': ['some.metric'],
                'monasca_dimensions': {
                    'resource_id': {
                        'property': 'tenant_id'
                    }
                },
                'monasca_data': {
                    'operation': 'metric_statistics'
                }
            }

            svc = NovaSvc(BllRequest(auth_token=get_mock_token(),
                                     data=req_data))
            reply = svc.handle()
            self.assertEqual(reply[api.STATUS], api.COMPLETE)

            self.mock_monasca_client.metrics.list_statistics \
                .assert_called_with(
                    name='some.metric',
                    dimensions={
                        'resource_id': self.server_list[4].id
                    }
                )

            self.assertIn(api.DATA, reply)
            data = reply[api.DATA]
            self.assertIn('instances', data)
            self.assertIsInstance(data['instances'], list)
            self.assertTrue(len(data['instances']) > 0)
            self.assertIn('metrics', data['instances'][0])
            self.assertIn('some.metric', data['instances'][0]['metrics'])
    def test_service_delete_no_input(self, _mock_nova_client, _mock_serv_end,
                                     _mock_ks_client):
        _mock_nova_client.return_value = self.mock_novaclient
        _mock_serv_end.return_value = None
        _mock_ks_client.return_value = self.mock_ksclient

        with log_level(logging.CRITICAL, 'bll'):
            svc = NovaSvc(
                BllRequest(auth_token=get_mock_token(),
                           data={api.OPERATION: 'service-delete'}))
            # In production the service is called via the handle method of the
            # base, SvcBase, which uses pykka and catches exceptions and
            # returns an appropriate error response to the caller.  But using
            # pykka in unit tests conflicts with the mock library causing any
            # such tests to hang as both libraries are attempting to
            # automagically wrap function calls in proxies.  Therefore we have
            # to directly expect and handle the exception ourselves.
            with self.assertRaises(Exception):
                svc.handle()
    def test_full_instance_list(self, _mock_nova_client, _mock_endpoints,
                                _mock_serv_end, _mock_ks_client):
        _mock_nova_client.return_value = self.mock_novaclient

        # Pretend the baremetal endpoint does not exist
        _mock_serv_end.return_value = None
        _mock_endpoints.return_value = self.mock_get_endpoints

        # Pretend there's one tenant called "some_tenant"
        _mock_ks_client.return_value = self.mock_ksclient

        svc = NovaSvc(
            BllRequest(auth_token=get_mock_token(),
                       data={api.OPERATION: 'instance-list'}))

        reply = svc.handle()
        self.assertEqual(api.COMPLETE, reply[api.STATUS])
        self.assertEqual(len(self.server_list),
                         len(reply[api.DATA]['instances']))