Exemplo n.º 1
0
  def test_metadata(self, mock_query_iter):
    query = gcm.Query(METRIC_TYPE, hours=1, context=self._create_context())
    query_metadata = query.metadata()

    mock_query_iter.assert_called_once_with(headers_only=True)
    self.assertIsInstance(query_metadata, gcm.QueryMetadata)
    self.assertEqual(query_metadata.metric_type, METRIC_TYPE)
Exemplo n.º 2
0
  def test_constructor_maximal(self):
    UPTIME_METRIC = 'compute.googleapis.com/instance/uptime'
    T1 = datetime.datetime(2016, 4, 7, 2, 30, 30)
    DAYS, HOURS, MINUTES = 1, 2, 3
    T0 = T1 - datetime.timedelta(days=DAYS, hours=HOURS, minutes=MINUTES)

    context = self._create_context(PROJECT)
    query = gcm.Query(UPTIME_METRIC,
                      end_time=T1, days=DAYS, hours=HOURS, minutes=MINUTES,
                      project_id=PROJECT, context=context)

    expected_client = gcm._utils.make_client(
        context=context, project_id=PROJECT)
    self.assertEqual(query._client.project, expected_client.project)
    self.assertEqual(query._client.connection.credentials,
                     expected_client.connection.credentials)

    self.assertEqual(query._filter.metric_type, UPTIME_METRIC)

    self.assertEqual(query._start_time, T0)
    self.assertEqual(query._end_time, T1)

    self.assertIsNone(query._per_series_aligner)
    self.assertIsNone(query._alignment_period_seconds)
    self.assertIsNone(query._cross_series_reducer)
    self.assertEqual(query._group_by_fields, ())
Exemplo n.º 3
0
    def test_labels_as_dataframe(self, mock_query_iter):
        mock_query_iter.return_value = self._query_iter_get_result()
        query = gcm.Query(context=self._create_context(PROJECT))
        dataframe = query.labels_as_dataframe()

        mock_query_iter.assert_called_once_with(headers_only=True)
        NUM_INSTANCES = len(INSTANCE_IDS)

        self.assertEqual(dataframe.shape, (NUM_INSTANCES, 5))

        expected_values = [[
            RESOURCE_TYPE, PROJECT, zone, instance_id, instance_name
        ]
                           for zone, instance_id, instance_name in zip(
                               INSTANCE_ZONES, INSTANCE_IDS, INSTANCE_NAMES)]
        self.assertEqual(dataframe.values.tolist(), expected_values)

        expected_headers = [('resource', 'type'),
                            ('resource.labels', 'project_id'),
                            ('resource.labels', 'zone'),
                            ('resource.labels', 'instance_id'),
                            ('metric.labels', 'instance_name')]
        self.assertEqual(dataframe.columns.tolist(), expected_headers)
        self.assertEqual(dataframe.columns.names, [None, None])

        self.assertEqual(dataframe.index.tolist(), list(range(NUM_INSTANCES)))
        self.assertEqual(dataframe.index.names, [None])
Exemplo n.º 4
0
    def test_labels_as_dataframe_w_no_data(self, mock_query_iter):
        mock_query_iter.return_value = []
        query = gcm.Query(context=self._create_context())
        dataframe = query.labels_as_dataframe()

        mock_query_iter.assert_called_once_with(headers_only=True)
        self.assertEqual(dataframe.shape, (0, 0))
        self.assertIsNone(dataframe.columns.name)
        self.assertIsNone(dataframe.index.name)
Exemplo n.º 5
0
  def test_constructor_minimal(self, mock_context_default):
    default_context = self._create_context()
    mock_context_default.return_value = default_context

    query = gcm.Query()

    expected_client = gcm._utils.make_client(context=default_context)
    self.assertEqual(query._client.project, expected_client.project)
    self.assertEqual(query._client.connection.credentials,
                     expected_client.connection.credentials)

    self.assertEqual(query._filter.metric_type, BaseQuery.DEFAULT_METRIC_TYPE)

    self.assertIsNone(query._start_time)
    self.assertIsNone(query._end_time)

    self.assertIsNone(query._per_series_aligner)
    self.assertIsNone(query._alignment_period_seconds)
    self.assertIsNone(query._cross_series_reducer)
    self.assertEqual(query._group_by_fields, ())
Exemplo n.º 6
0
 def setUp(self):
     creds = AccessTokenCredentials('test_token', 'test_ua')
     context = datalab.context.Context(PROJECT, creds)
     self.query = gcm.Query(METRIC_TYPE, context=context)
Exemplo n.º 7
0
 def setUp(self):
     creds = mock.Mock(spec=google.auth.credentials.Credentials)
     context = datalab.context.Context(PROJECT, creds)
     self.query = gcm.Query(METRIC_TYPE, context=context)