Package cm_api_tests :: Module test_timeseries
[hide private]
[frames] | no frames]

Source Code for Module cm_api_tests.test_timeseries

  1  # Licensed to Cloudera, Inc. under one 
  2  # or more contributor license agreements.  See the NOTICE file 
  3  # distributed with this work for additional information 
  4  # regarding copyright ownership.  Cloudera, Inc. licenses this file 
  5  # to you under the Apache License, Version 2.0 (the 
  6  # "License"); you may not use this file except in compliance 
  7  # with the License.  You may obtain a copy of the License at 
  8  # 
  9  #     http://www.apache.org/licenses/LICENSE-2.0 
 10  # 
 11  # Unless required by applicable law or agreed to in writing, software 
 12  # distributed under the License is distributed on an "AS IS" BASIS, 
 13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 14  # See the License for the specific language governing permissions and 
 15  # limitations under the License. 
 16   
 17   
 18  import unittest 
 19  from cm_api.endpoints.timeseries import * 
 20  from cm_api.endpoints.types import ApiList 
 21  from cm_api_tests import utils 
 22   
 23  try: 
 24    import json 
 25  except ImportError: 
 26    import simplejson as json 
 27   
28 -class TestTimeSeries(unittest.TestCase):
29
30 - def test_query_timeseries(self):
31 TIME_SERIES = '''{ 32 "items" : [ { 33 "timeSeries" : [ { 34 "metadata" : { 35 "metricName" : "cpu_percent", 36 "entityName" : "localhost", 37 "startTime" : "2013-05-08T22:58:28.868Z", 38 "endTime" : "2013-05-08T23:03:28.868Z", 39 "attributes" : { 40 "hostname" : "localhost", 41 "hostId" : "localhost", 42 "rackId" : "/default", 43 "category" : "HOST", 44 "entityName" : "localhost" 45 }, 46 "unitNumerators" : [ "percent" ], 47 "unitDenominators" : [ ] 48 }, 49 "data" : [ { 50 "timestamp" : "2013-05-08T22:59:06.000Z", 51 "value" : 1.7, 52 "type" : "SAMPLE" 53 }, { 54 "timestamp" : "2013-05-08T23:00:06.000Z", 55 "value" : 3.5, 56 "type" : "SAMPLE" 57 }, { 58 "timestamp" : "2013-05-08T23:01:06.000Z", 59 "value" : 2.1, 60 "type" : "SAMPLE" 61 }, { 62 "timestamp" : "2013-05-08T23:02:06.000Z", 63 "value" : 1.5, 64 "type" : "SAMPLE" 65 }, { 66 "timestamp" : "2013-05-08T23:03:06.000Z", 67 "value" : 1.7, 68 "type" : "SAMPLE" 69 } ] 70 } ], 71 "warnings" : [ ], 72 "timeSeriesQuery" : "select cpu_percent" 73 } ] 74 }''' 75 76 api_resource = utils.MockResource(self) 77 api_resource.expect("GET", "/timeseries", 78 retdata=json.loads(TIME_SERIES)) 79 responses = query_timeseries(api_resource, 80 "select cpu_percent", 81 None, 82 None) 83 84 self.assertIsInstance(responses, ApiList) 85 self.assertEqual(1, len(responses)) 86 response = responses[0] 87 self.assertIsInstance(response, ApiTimeSeriesResponse) 88 self.assertEqual("select cpu_percent", response.timeSeriesQuery) 89 self.assertFalse(response.warnings) 90 self.assertFalse(response.errors) 91 self.assertEqual(1, len(response.timeSeries)) 92 timeseries = response.timeSeries[0] 93 self.assertIsInstance(timeseries, ApiTimeSeries) 94 metadata = timeseries.metadata 95 self.assertIsInstance(metadata, ApiTimeSeriesMetadata) 96 self.assertEqual("cpu_percent", metadata.metricName) 97 self.assertEqual("localhost", metadata.entityName) 98 self.assertIsInstance(metadata.attributes, dict) 99 self.assertEqual("cpu_percent", metadata.metricName) 100 self.assertEqual(5, len(timeseries.data)) 101 for data in timeseries.data: 102 self.assertIsInstance(data, ApiTimeSeriesData) 103 self.assertEqual("SAMPLE", data.type) 104 self.assertIsInstance(data.timestamp, datetime.datetime) 105 self.assertTrue(data.value)
106 107
108 - def test_get_metric_schema(self):
109 METRICS = '''{ 110 "items" : [ { 111 "name" : "event_drain_success_count_flume_sink_min_rate", 112 "isCounter" : false, 113 "unitNumerator" : "events", 114 "unitDenominator" : "seconds", 115 "aliases" : [ ], 116 "sources" : { 117 "CLUSTER" : [ "cdh3", "cdh4" ], 118 "FLUME" : [ "cdh3", "cdh4" ] 119 } 120 }, { 121 "name" : "drop_receive", 122 "isCounter" : true, 123 "unitNumerator" : "packets", 124 "aliases" : [ "network_interface_drop_receive" ], 125 "sources" : { 126 "NETWORK_INTERFACE" : [ "enterprise" ] 127 } 128 } ] 129 }''' 130 131 api_resource = utils.MockResource(self) 132 api_resource.expect("GET", "/timeseries/schema", 133 retdata=json.loads(METRICS)) 134 metrics = get_metric_schema(api_resource) 135 136 self.assertIsInstance(metrics, ApiList) 137 self.assertEqual(2, len(metrics)) 138 metric = metrics[0] 139 self.assertIsInstance(metric, ApiMetricSchema) 140 self.assertEqual("event_drain_success_count_flume_sink_min_rate", 141 metric.name) 142 self.assertFalse(metric.isCounter) 143 self.assertEqual("events", metric.unitNumerator) 144 self.assertEqual("seconds", metric.unitDenominator) 145 self.assertFalse(metric.aliases) 146 self.assertIsInstance(metric.sources, dict) 147 metric = metrics[1] 148 self.assertIsInstance(metric, ApiMetricSchema) 149 self.assertEqual("drop_receive", metric.name) 150 self.assertTrue(metric.isCounter) 151 self.assertEqual("packets", metric.unitNumerator) 152 self.assertFalse( metric.unitDenominator) 153 self.assertIsInstance(metric.aliases, list) 154 self.assertEquals("network_interface_drop_receive", metric.aliases[0]) 155 self.assertIsInstance(metric.sources, dict) 156 self.assertEquals("enterprise", metric.sources["NETWORK_INTERFACE"][0])
157