def testGetInstanceWithDimensionNotFoundEmpty(self, adapterMock): """ Test for Get '/_metrics/cloudwatch/<region-name>/AWS/<namespace>/\ instances/<InstancdId>/Dimenion' with invalid InstancceId response is validated for appropriate headers, body and status Tests scenario when given instance name is not present in ouput from getDimensionsFromRegion """ adapterMock.return_value.describeRegions.return_value = self.regions adapterMock.return_value.describeSupportedMetrics.return_value = ( self.resources) adapterMock.return_value.describeResources.return_value = [{ 'grn': u'aws://us-west-2/Instance/i-548acc3a', 'name': u'Bar', 'resID': u'i-548acc3a' }] response = self.app.get("/us-east-1/AWS/EC2/instances/i-1234567a", headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) self.assertEqual(result, [])
def testMetricDataTimeStampQueryParams(uid): ''' This test makes MetricDataHandler GET calls with from and to params : _models/<uid>/data?from=<>&to=<> ''' with repository.engineFactory().connect() as conn: firstMetricData = conn.execute( sql.select([schema.metric_data ]).where(schema.metric_data.c.uid == uid). order_by(sql.expression.asc( schema.metric_data.c.timestamp)).limit(1)).fetchall() lastMetricData = conn.execute( sql.select([ schema.metric_data ]).where(schema.metric_data.c.uid == uid).order_by( sql.expression.desc( schema.metric_data.c.timestamp)).limit( 1)).fetchall() firstTimeStamp = firstMetricData[0].timestamp lastTimeStamp = lastMetricData[0].timestamp response = self.app.get("/%s/data?from=%s&to=%s" % (uid, firstTimeStamp, lastTimeStamp), headers=self.headers) assertions.assertSuccess(self, response) getAllModelsResult = utils.jsonDecode(response.body) for metricData in getAllModelsResult['data']: self.assertGreaterEqual( datetime.strptime(metricData[0], '%Y-%m-%d %H:%M:%S'), firstTimeStamp) self.assertLessEqual( datetime.strptime(metricData[0], '%Y-%m-%d %H:%M:%S'), lastTimeStamp)
def testMetricsAPIGETCouldWatch(self, adapterMock): adapterMock.return_value.describeRegions.return_value = [] adapterMock.return_value.describeSupportedMetrics.return_value = {} response = self.app.get("/_metrics/cloudwatch", headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) self.assertEqual(result, {'regions': {}, 'namespaces': {}})
def testMetricDataForRandomRowID(uid): ''' This tests if the metric data returned by the GET call : _models/<uid>/data has anomaly_score consistent with what is there in the actual database by asserting it against a dao.MetricData.get() call It repeats the process for 5 random sample rows for each uid in the database. Algorithm : - Query the MetricDataHandler GET call for a certain uid - Check if response is OK - Find the last row id for the uid - Select a random row between 1 and last row id - Find the anomaly score for that row id - Assert on the anomaly score ''' response = self.app.get("/%s/data" % uid, headers=self.headers) assertions.assertSuccess(self, response) getAllModelsResult = utils.jsonDecode(response.body) with repository.engineFactory().connect() as conn: lastRowID = repository.getMetric(conn, uid).last_rowid for _ in range(5): randomRowID = randrange(1, lastRowID) with repository.engineFactory().connect() as conn: singleMetricData = repository.getMetricData( conn, uid, rowid=randomRowID).first() metricData = getMetricDataWithRowID(getAllModelsResult['data'], randomRowID) self.assertEqual(metricData[2], singleMetricData.anomaly_score) self.assertEqual( datetime.strptime(metricData[0], '%Y-%m-%d %H:%M:%S'), singleMetricData.timestamp)
def testGetModelDataWithModelUIdAndAnomalyScore(self): """ test GET /metricId/data?anomaly=testanomalyScore """ getMetricDataWithAnomalyQueryResponse = self.app.get( "/%s/data?anomaly=%s" % (self.uid, self.testAnomalyScore), headers=self.headers) getMetricDataWithAnomalyQueryResult = utils.jsonDecode( getMetricDataWithAnomalyQueryResponse.body) assertions.assertSuccess(self, getMetricDataWithAnomalyQueryResponse) self.assertIsInstance(getMetricDataWithAnomalyQueryResult, dict) self.assertItemsEqual(getMetricDataWithAnomalyQueryResult.keys(), ["data", "names"]) self.assertGreater(len(getMetricDataWithAnomalyQueryResult["data"]), 0) # we are parsing amomaly scores from reponse and chekcing if each of it # is satisfying value condition set with GET request. # If for some reason this parameter is not applied on DB query, we get # full response for this request # We are making sure each value for anomaly_score in result matches with # condition set in GET request, hence the assertion anomalyScores = \ [row[2] for row in getMetricDataWithAnomalyQueryResult["data"]] failedScores = [a for a in anomalyScores if a < self.testAnomalyScore] self.assertEqual(failedScores, [])
def testGETSpecificInstanceFromRegion(self): """ Test for Get '/_metrics/cloudwatch/<region-name>/AWS/<namespace>/instances/<InstancdId>' response is validated for appropriate headers, body and status Test is currently using ec2 box for jenkins-master, this test also validates for retriving all supported metrics with dimensions """ supportedMetrics = ( createCloudwatchDatasourceAdapter().describeSupportedMetrics()) ec2Metrics = supportedMetrics[ResourceTypeNames.EC2_INSTANCE].keys() # Instance used for following test is jenkins-master node response = self.app.get("/us-west-2/AWS/EC2/instances/%s" % VALID_EC2_INSTANCE["InstanceId"], headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) self.assertIsInstance(result, list) self.assertGreater(len(ec2Metrics), 0) self.assertGreater(len(result), 0) self.assertEqual(len(ec2Metrics), len(result)) for res in result: self.assertEqual(res["region"], "us-west-2") self.assertEqual(res["namespace"], "AWS/EC2") self.assertEqual(res["datasource"], "cloudwatch") self.assertIn(res["metric"], ec2Metrics) self.assertIsInstance(res["dimensions"], dict) self.assertEqual(res["dimensions"]["InstanceId"], VALID_EC2_INSTANCE["InstanceId"]) ec2Metrics.remove(res["metric"]) self.assertEqual(ec2Metrics, [])
def testModelHandlerListModelsWithSlashEmptyResponse(self, getAllModelsMock, _engineMock, *args): getAllModelsMock.return_value = [] response = self.app.get("/", headers=self.headers) assertions.assertSuccess(self, response) result = jsonDecode(response.body) self.assertEqual(result, [])
def testMetricsAPIGETDataSources(self, getDatasources): getDatasources.return_value = [] response = self.app.get("/_metrics/datasources", headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) self.assertEqual(result, []) self.assertTrue(getDatasources.called)
def testMetricDataForRandomRowID(uid): ''' This tests if the metric data returned by the GET call : _models/<uid>/data has anomaly_score consistent with what is there in the actual database by asserting it against a dao.MetricData.get() call It repeats the process for 5 random sample rows for each uid in the database. Algorithm : - Query the MetricDataHandler GET call for a certain uid - Check if response is OK - Find the last row id for the uid - Select a random row between 1 and last row id - Find the anomaly score for that row id - Assert on the anomaly score ''' response = self.app.get("/%s/data" %uid, headers=self.headers) assertions.assertSuccess(self, response) getAllModelsResult = utils.jsonDecode(response.body) with repository.engineFactory().connect() as conn: lastRowID = repository.getMetric(conn, uid).last_rowid for _ in range(5): randomRowID = randrange(1, lastRowID) with repository.engineFactory().connect() as conn: singleMetricData = repository.getMetricData( conn, uid, rowid=randomRowID).first() metricData = getMetricDataWithRowID(getAllModelsResult['data'], randomRowID) self.assertEqual(metricData[2], singleMetricData.anomaly_score) self.assertEqual(datetime.strptime(metricData[0], '%Y-%m-%d %H:%M:%S'), singleMetricData.timestamp)
def testMetricDataQueryParams(uid): ''' This test makes MetricDataHandler GET calls with various params : _models/<uid>/data?from=<>&to=<>&anomaly=<> ''' with repository.engineFactory().connect() as conn: firstMetricData = conn.execute( "SELECT * FROM `metric_data` WHERE `uid`='%s' " "and abs(`anomaly_score` - 0) > 1e-5 " "ORDER BY `timestamp` ASC LIMIT 1" % uid).fetchall() lastMetricData = conn.execute( "SELECT * FROM `metric_data` WHERE `uid`='%s' " "and abs(`anomaly_score` - 0) > 1e-5 " "ORDER BY `timestamp` DESC LIMIT 1" % uid).fetchall() firstTimeStamp = firstMetricData[0].timestamp lastTimeStamp = lastMetricData[0].timestamp anomalyScore = firstMetricData[0].anomaly_score response = self.app.get( "/%s/data?from=%s&to=%s&anomaly=%s" % (uid, firstTimeStamp, lastTimeStamp, anomalyScore), headers=self.headers) assertions.assertSuccess(self, response) getAllModelsResult = utils.jsonDecode(response.body) for metricData in getAllModelsResult['data']: self.assertGreaterEqual(metricData[2], anomalyScore) self.assertGreaterEqual( datetime.strptime(metricData[0], '%Y-%m-%d %H:%M:%S'), firstTimeStamp) self.assertLessEqual( datetime.strptime(metricData[0], '%Y-%m-%d %H:%M:%S'), lastTimeStamp)
def testMetricDataTimeStampQueryParams(uid): ''' This test makes MetricDataHandler GET calls with from and to params : _models/<uid>/data?from=<>&to=<> ''' with repository.engineFactory().connect() as conn: firstMetricData = conn.execute( sql.select([schema.metric_data]) .where(schema.metric_data.c.uid == uid) .order_by(sql.expression.asc(schema.metric_data.c.timestamp)) .limit(1)).fetchall() lastMetricData = conn.execute( sql.select([schema.metric_data]) .where(schema.metric_data.c.uid == uid) .order_by(sql.expression.desc(schema.metric_data.c.timestamp)) .limit(1)).fetchall() firstTimeStamp = firstMetricData[0].timestamp lastTimeStamp = lastMetricData[0].timestamp response = self.app.get("/%s/data?from=%s&to=%s" % (uid, firstTimeStamp, lastTimeStamp), headers=self.headers) assertions.assertSuccess(self, response) getAllModelsResult = utils.jsonDecode(response.body) for metricData in getAllModelsResult['data']: self.assertGreaterEqual(datetime.strptime(metricData[0], '%Y-%m-%d %H:%M:%S'), firstTimeStamp) self.assertLessEqual(datetime.strptime(metricData[0], '%Y-%m-%d %H:%M:%S'), lastTimeStamp)
def testGetModelDataWithModelUIdAndAnomalyScore(self): """ test GET /metricId/data?anomaly=testanomalyScore """ getMetricDataWithAnomalyQueryResponse = self.app.get( "/%s/data?anomaly=%s" % (self.uid, self.testAnomalyScore), headers=self.headers) getMetricDataWithAnomalyQueryResult = utils.jsonDecode( getMetricDataWithAnomalyQueryResponse.body) assertions.assertSuccess(self, getMetricDataWithAnomalyQueryResponse) self.assertIsInstance(getMetricDataWithAnomalyQueryResult, dict) self.assertItemsEqual(getMetricDataWithAnomalyQueryResult.keys(), ["data", "names"]) self.assertGreater(len(getMetricDataWithAnomalyQueryResult["data"]), 0) # we are parsing amomaly scores from reponse and chekcing if each of it # is satisfying value condition set with GET request. # If for some reason this parameter is not applied on DB query, we get # full response for this request # We are making sure each value for anomaly_score in result matches with # condition set in GET request, hence the assertion anomalyScores = \ [row[2] for row in getMetricDataWithAnomalyQueryResult["data"]] failedScores = [a for a in anomalyScores if a < self.testAnomalyScore] self.assertEqual(failedScores, [])
def testGetInstanceWithDimension(self, adapterMock): """ Test for Get '/_metrics/cloudwatch/<region-name>/AWS/<namespace>/\ instances/<InstancdId>' with valid InstancceId response is validated for appropriate headers, body and status """ adapterMock.return_value.describeRegions.return_value = self.regions adapterMock.return_value.describeSupportedMetrics.return_value = ( self.resources) adapterMock.return_value.describeResources.return_value = [{ 'grn': u'aws://us-west-2/Instance/i-548acc3a', 'name': u'Bar', 'resID': u'i-548acc3a' }] response = self.app.get("/us-east-1/AWS/EC2/instances/i-548acc3a", headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) self.assertTrue( all(metric["metric"] in self.resources['AWS::EC2::Instance'] for metric in result))
def testMetricsAPIGETCouldWatch(self, adapterMock): adapterMock.return_value.describeRegions.return_value = [] adapterMock.return_value.describeSupportedMetrics.return_value = {} response = self.app.get("/_metrics/cloudwatch", headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) self.assertEqual(result, {'regions': {}, 'namespaces': {}})
def testMetricsAPIGETDataSources(self, getDatasources): getDatasources.return_value = [] response = self.app.get("/_metrics/datasources", headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) self.assertEqual(result, []) self.assertTrue(getDatasources.called)
def testMetricDataQueryParams(uid): ''' This test makes MetricDataHandler GET calls with various params : _models/<uid>/data?from=<>&to=<>&anomaly=<> ''' with repository.engineFactory().connect() as conn: firstMetricData = conn.execute( "SELECT * FROM `metric_data` WHERE `uid`='%s' " "and abs(`anomaly_score` - 0) > 1e-5 " "ORDER BY `timestamp` ASC LIMIT 1" % uid).fetchall() lastMetricData = conn.execute( "SELECT * FROM `metric_data` WHERE `uid`='%s' " "and abs(`anomaly_score` - 0) > 1e-5 " "ORDER BY `timestamp` DESC LIMIT 1" % uid).fetchall() firstTimeStamp = firstMetricData[0].timestamp lastTimeStamp = lastMetricData[0].timestamp anomalyScore = firstMetricData[0].anomaly_score response = self.app.get("/%s/data?from=%s&to=%s&anomaly=%s" % (uid, firstTimeStamp, lastTimeStamp, anomalyScore), headers=self.headers) assertions.assertSuccess(self, response) getAllModelsResult = utils.jsonDecode(response.body) for metricData in getAllModelsResult['data']: self.assertGreaterEqual(metricData[2], anomalyScore) self.assertGreaterEqual(datetime.strptime(metricData[0], '%Y-%m-%d %H:%M:%S'), firstTimeStamp) self.assertLessEqual(datetime.strptime(metricData[0], '%Y-%m-%d %H:%M:%S'), lastTimeStamp)
def testGETSpecificInstanceFromRegion(self): """ Test for Get '/_metrics/cloudwatch/<region-name>/AWS/<namespace>/instances/<InstancdId>' response is validated for appropriate headers, body and status Test is currently using ec2 box for jenkins-master, this test also validates for retriving all supported metrics with dimensions """ supportedMetrics = ( createCloudwatchDatasourceAdapter().describeSupportedMetrics()) ec2Metrics = supportedMetrics[ResourceTypeNames.EC2_INSTANCE].keys() # Instance used for following test is jenkins-master node response = self.app.get("/us-west-2/AWS/EC2/instances/%s" % VALID_EC2_INSTANCE["InstanceId"], headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) self.assertIsInstance(result, list) self.assertGreater(len(ec2Metrics), 0) self.assertGreater(len(result), 0) self.assertEqual(len(ec2Metrics), len(result)) for res in result: self.assertEqual(res["region"], "us-west-2") self.assertEqual(res["namespace"], "AWS/EC2") self.assertEqual(res["datasource"], "cloudwatch") self.assertIn(res["metric"], ec2Metrics) self.assertIsInstance(res["dimensions"], dict) self.assertEqual(res["dimensions"]["InstanceId"], VALID_EC2_INSTANCE["InstanceId"]) ec2Metrics.remove(res["metric"]) self.assertEqual(ec2Metrics, [])
def testCombinationQuota1(self): result = self.cloudwatchApp.get("/us-west-2/AWS/EC2", headers=self.headers) instances = json.loads(result.body) for i in range(0, 10, 5): instance = instances[i] instanceId = "us-west-2/AWS/EC2/%s" % ( instance["dimensions"]["InstanceId"]) postResponse = self.instancesApp.post("/%s" % instanceId, headers=self.headers) assertions.assertSuccess(self, postResponse) self.addCleanup(self.instancesApp.delete, "/", headers=self.headers, params=json.dumps([instanceId])) sock = socket.socket() sock.connect(("localhost", self.plaintextPort)) sock.sendall("CustomMetric1 5.0 1386201600\n") self.gracefullyCloseSocket(sock) self.addCleanup(self.customApp.delete, "/CustomMetric1", headers=self.headers) uid = self.checkMetricCreated("CustomMetric1") with self.assertRaises(AppError) as e: payload = {"uid": uid, "datasource": "custom", "min": 0.0, "max": 100.0} self.modelApp.post("/", json.dumps(payload), headers=self.headers) self.assertIn("Server limit exceeded", e.exception.message)
def testInstanceDefaultsHandlerPOST(self, listMetricIDsMock, _engineFactoryMock): """ Test for POST "/_instances/region/namespace/instanceId" response is validated for appropriate headers, body and status """ listMetricIDsMock.return_value = [] region = "us-west-2" # Currently we are not supporting certain namespaces # unsupportedNamespaces reflects such unsupported namespaces # These namespaces are currently validated for "400 Bad Request" # and expected error message. # Update this list with changes in namespace support unsupportedNamespaces = ("Billing", "StorageGateway") for namespace in unsupportedNamespaces: response = self.app.post("/%s/AWS/%s/abcd1234" % (region, namespace), headers=self.headers, status="*") assertions.assertBadRequest(self, response, "json") result = json.loads(response.body)["result"] self.assertTrue(result.startswith("Not supported.")) cwAdapter = datasource.createDatasourceAdapter("cloudwatch") supportedNamespaces = set() for resourceInfo in cwAdapter.describeSupportedMetrics().values(): for metricInfo in resourceInfo.values(): supportedNamespaces.add(metricInfo["namespace"]) for namespace in supportedNamespaces: response = self.app.post("/%s/%s/abcd1234" % (region, namespace), headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) self.assertIsInstance(result, dict) self.assertEqual(result, {"result": "success"})
def testQuery(self, getMetricDataMock, _engineMock): getMetricDataMock.return_value = self.decodeRowTuples( self.metric_data["datalist"]) response = self.app.get("/be9fab-f416-4845-8dab-02d292244112/data?\ from=2013-08-15 21:34:00&to=2013-08-15 21:24:00&anomaly=0.025", headers=self.headers) assertions.assertSuccess(self, response)
def testGetMetricsWithNonEmptyResponse(self, getDatasourcesMock): """ Test get "/datasources", with non empty response response is validated for appropriate headers, body and status """ getDatasourcesMock.return_value = ("autostack", "cloudwatch", "custom") response = self.app.get("/datasources", headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) self.assertEqual(result, ["autostack", "cloudwatch", "custom"])
def testGetMetricsWithEmptyResponse(self, getDatasourcesMock): """ Test get "/datasources" response is validated for appropriate headers, body and status """ getDatasourcesMock.return_value = tuple() response = self.app.get("/datasources", headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) self.assertEqual(result, [])
def _deleteOneMetric(self): """ Delete one metric from test EC2 instance """ app = TestApp(models_api.app.wsgifunc()) response = app.get("/", headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) self.assertIsInstance(result, list) app.delete("/" + result[0]['uid'], headers=self.headers)
def testGetListRegionsEmptyResponse(self, adapterMock): """ Test for Get '/_metrics/cloudwatch/regions' response is validated for appropriate headers, body and status """ adapterMock.return_value.describeRegions.return_value = [] response = self.app.get("/regions", headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) self.assertEqual(result, {})
def _deleteOneMetric(self): """ Delete one metric from test EC2 instance """ app = TestApp(models_api.app.wsgifunc()) response = app.get("/", headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) self.assertIsInstance(result, list) app.delete("/" + result[0]['uid'], headers=self.headers)
def testModelsAPIGET(self, getAllMetricsMock, getInstanceStatusHistoryMock, engineFactoryMock, *args): #import pdb; pdb.set_trace() getAllMetricsMock.return_value = [] getInstanceStatusHistoryMock.return_value = [] response = self.app.get("/_models", headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) self.assertEqual(result, []) self.assertTrue(getAllMetricsMock.called)
def testGetMetricsWithEmptyResponse(self, getDatasourcesMock): """ Test get "/datasources" response is validated for appropriate headers, body and status """ getDatasourcesMock.return_value = tuple() response = self.app.get("/datasources", headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) self.assertEqual(result, [])
def testGETDatasources(self): """ Test for GET for '/_metrics/datasources' response is validated for appropriate headers and body """ response = self.app.get('/datasources', headers=self.headers) assertions.assertSuccess(self, response) self.assertIsInstance(utils.jsonDecode(response.body), list) self.assertSetEqual(set(utils.jsonDecode(response.body)), set(["autostack", "custom", "cloudwatch"]))
def testGetListRegionsEmptyResponse(self, adapterMock): """ Test for Get '/_metrics/cloudwatch/regions' response is validated for appropriate headers, body and status """ adapterMock.return_value.describeRegions.return_value = [] response = self.app.get("/regions", headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) self.assertEqual(result, {})
def testGetMetricsWithNonEmptyResponse(self, getDatasourcesMock): """ Test get "/datasources", with non empty response response is validated for appropriate headers, body and status """ getDatasourcesMock.return_value = ("autostack", "cloudwatch", "custom") response = self.app.get("/datasources", headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) self.assertEqual(result, ["autostack", "cloudwatch", "custom"])
def testDefaultGetSpecificSection(self): response = self.app.get("/aws", headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) self.assertIsInstance(result, dict) for key in set(self.configurable_options["aws"]): if key in settings_api.HIDDEN_SETTINGS["aws"]: self.assertNotIn(key, result) else: self.assertIn(key, result)
def testGETDatasources(self): """ Test for GET for '/_metrics/datasources' response is validated for appropriate headers and body """ response = self.app.get('/datasources', headers=self.headers) assertions.assertSuccess(self, response) self.assertIsInstance(utils.jsonDecode(response.body), list) self.assertSetEqual(set(utils.jsonDecode(response.body)), set(["autostack", "custom", "cloudwatch"]))
def testGETListRegions(self): """ Test for Get '/_metrics/cloudwatch/regions' response is validated for appropriate headers, body and status response is validated against json for supported regions """ response = self.app.get("/regions", headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) self.assertIsInstance(result, dict) self.assertEqual(json.loads(response.body), self.regions)
def testGetModelDataWithModelUId(self): """ test GET /metricId/data """ getMetricDataResponse = self.app.get("/%s/data" % self.uid, headers=self.headers) assertions.assertSuccess(self, getMetricDataResponse) getMetricDataResult = utils.jsonDecode(getMetricDataResponse.body) self.assertIsInstance(getMetricDataResult, dict) self.assertItemsEqual(getMetricDataResult.keys(), ["data", "names"]) self.assertGreater(len(getMetricDataResult["data"]), 0)
def _deleteInstance(self): """ Delete test EC2 instance created by :py:meth:`_createEC2Instance` """ app = TestApp(instances_api.app.wsgifunc()) response = app.delete("/", params=json.dumps([self.instanceId]), headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) self.assertIsInstance(result, dict) self.assertEqual(result, {"result": "success"})
def testGetModelDataWithModelUId(self): """ test GET /metricId/data """ getMetricDataResponse = self.app.get("/%s/data" % self.uid, headers=self.headers) assertions.assertSuccess(self, getMetricDataResponse) getMetricDataResult = utils.jsonDecode(getMetricDataResponse.body) self.assertIsInstance(getMetricDataResult, dict) self.assertItemsEqual(getMetricDataResult.keys(), ["data", "names"]) self.assertGreater(len(getMetricDataResult["data"]), 0)
def testMetricDataHandlerGetMetricDataWIthAnomaly(self, getMetricDataMock, _engineMock): getMetricDataMock.return_value = self.decodeRowTuples( self.metric_data['withanomaly']) response = self.app.get( "/be9fab-f416-4845-8dab-02d292244112/data?anomaly=0.01", headers=self.headers) assertions.assertSuccess(self, response) result = jsonDecode(response.body) self.assertEqual([row[1:] for row in self.metric_data['withanomaly']], result["data"])
def testGETListRegions(self): """ Test for Get '/_metrics/cloudwatch/regions' response is validated for appropriate headers, body and status response is validated against json for supported regions """ response = self.app.get("/regions", headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) self.assertIsInstance(result, dict) self.assertEqual(json.loads(response.body), self.regions)
def testGETListInstancesForRegion(self): """ Test for Get '/_metrics/cloudwatch/<region-name>/AWS/<namespace>/instances' response is validated for appropriate headers, body and status response is validated against response """ response = self.app.get("/us-west-2/AWS/EC2", headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) self.assertIsInstance(result, list)
def testMetricDataHandlerGetMetricDataWithToTimestamp(self, getMetricDataMock, _engineMock): getMetricDataMock.return_value = self.decodeRowTuples( self.metric_data["withto"]) response = self.app.get( "/be9fab-f416-4845-8dab-02d292244112/data?to=2013-08-15 21:28:00", headers=self.headers) assertions.assertSuccess(self, response) result = jsonDecode(response.body) self.assertEqual([row[1:] for row in self.metric_data['withto']], result["data"])
def testModelsAPIGET(self, getAllMetricsMock, getInstanceStatusHistoryMock, engineFactoryMock, *args): #import pdb; pdb.set_trace() getAllMetricsMock.return_value = [] getInstanceStatusHistoryMock.return_value = [] response = self.app.get("/_models", headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) self.assertEqual(result, []) self.assertTrue(getAllMetricsMock.called)
def _deleteInstance(self): """ Delete test EC2 instance created by :py:meth:`_createEC2Instance` """ app = TestApp(instances_api.app.wsgifunc()) response = app.delete("/", params=json.dumps([self.instanceId]), headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) self.assertIsInstance(result, dict) self.assertEqual(result, {"result": "success"})
def testGetListNamespaceNoRegions(self, adapterMock): """ Test for Get '/_metrics/cloudwatch/namespaces' response is validated for appropriate headers, body and status """ adapterMock.return_value.describeRegions.return_value = [] response = self.app.get("/namespaces", headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) # Added Autostacks namespaces to this list for now, to maintain API # backwards-compatibility during adapter refactor self.assertEqual(result, {"Autostacks": {"metrics": ["InstanceCount"]}})
def testDefaultGetList(self): response = self.app.get("", headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) self.assertIsInstance(result, dict) for config in self.configurable_options: self.assertTrue(result.has_key(config)) for key in self.configurable_options[config]: if key in settings_api.HIDDEN_SETTINGS[config]: self.assertNotIn(key, result[config]) else: self.assertIn(key, result[config])
def testGETListInstancesForRegion(self): """ Test for Get '/_metrics/cloudwatch/<region-name>/AWS/<namespace>/instances' response is validated for appropriate headers, body and status response is validated against response """ response = self.app.get("/us-west-2/AWS/EC2", headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) self.assertIsInstance(result, list)
def testGETSpecificNamespace(self): """ Test for Get '/_metrics/cloudwatch/AWS/<namespace>' response is validated for appropriate headers, body and status response is validated against available metrics for each namespaces supported """ for namespace in self._supportedAWSNamespaces(): response = self.app.get("/%s" % namespace, headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) self.assertIsInstance(result, dict)
def testGETSpecificNamespace(self): """ Test for Get '/_metrics/cloudwatch/AWS/<namespace>' response is validated for appropriate headers, body and status response is validated against available metrics for each namespaces supported """ for namespace in self._supportedAWSNamespaces(): response = self.app.get("/%s" % namespace, headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) self.assertIsInstance(result, dict)
def testMetricDataHandlerGetMetricDataWIthAnomaly(self, getMetricDataMock, _engineMock): getMetricDataMock.return_value = self.decodeRowTuples( self.metric_data['withanomaly']) response = self.app.get( "/be9fab-f416-4845-8dab-02d292244112/data?anomaly=0.01", headers=self.headers) assertions.assertSuccess(self, response) result = jsonDecode(response.body) self.assertEqual([row[1:] for row in self.metric_data['withanomaly']], result["data"])
def _createEC2Instance(self): """ Created EC2 instance to be used by the tests :return: Instance ID :rtype: str """ app = TestApp(instances_api.app.wsgifunc()) response = app.post("/" + self.instanceId, headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) self.assertIsInstance(result, dict) self.assertEqual(result, {"result": "success"})
def testGetMetricValidInputEmptyResponse(self, adapterMock): """ Test for Get '/_metrics/cloudwatch/<region-name>/AWS/<namespace>/metricName' response is validated for appropriate headers, body and status """ adapterMock.return_value.describeRegions.return_value = self.regions adapterMock.return_value.describeSupportedMetrics.return_value = self.resources adapterMock.return_value.describeResources.return_value = [] response = self.app.get("/us-east-1/AWS/EC2/CPUUtilization", headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) self.assertEqual(result, [])
def _createEC2Instance(self): """ Created EC2 instance to be used by the tests :return: Instance ID :rtype: str """ app = TestApp(instances_api.app.wsgifunc()) response = app.post("/" + self.instanceId, headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) self.assertIsInstance(result, dict) self.assertEqual(result, {"result": "success"})
def testGetMetricDimensionWithResponse(self, adapterMock): """ Test for Get '/_metrics/cloudwatch/<region-name>/AWS/<namespace>/metricName' response is validated for appropriate headers, body and status and response is validated with pre-defined responses """ adapterMock.return_value.describeRegions.return_value = self.regions adapterMock.return_value.describeSupportedMetrics.return_value = self.resources adapterMock.return_value.describeResources.return_value = [ {"grn": u"aws://us-west-2/Instance/i-d48ccaba", "name": u"Foo", "resID": u"i-d48ccaba"}, {"grn": u"aws://us-west-2/Instance/i-548acc3a", "name": u"Bar", "resID": u"i-548acc3a"}, ] response = self.app.get("/us-east-1/AWS/EC2/CPUUtilization", headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) self.assertItemsEqual( result, [ { "datasource": "cloudwatch", "dimensions": {"InstanceId": "i-d48ccaba"}, "metric": "CPUUtilization", "namespace": "AWS/EC2", "region": "us-east-1", }, { "datasource": "cloudwatch", "dimensions": {"InstanceId": "i-548acc3a"}, "metric": "CPUUtilization", "namespace": "AWS/EC2", "region": "us-east-1", }, { "datasource": "cloudwatch", "dimensions": {"AutoScalingGroupName": "i-d48ccaba"}, "metric": "CPUUtilization", "namespace": "AWS/EC2", "region": "us-east-1", }, { "datasource": "cloudwatch", "dimensions": {"AutoScalingGroupName": "i-548acc3a"}, "metric": "CPUUtilization", "namespace": "AWS/EC2", "region": "us-east-1", }, ], )
def _getInstancesHandlerCommon(self, instancesMock, route, expectedResult): """ This method wraps around common testing path for all GET routes which falls to listing all available instances instancesMock : Mock for Instances class route : route under test for current testcase expectedResult : expected response from API call """ response = self.app.get(route, headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) self.assertIsInstance(result, list) self.assertEqual(result, expectedResult) self.assertTrue(instancesMock.getInstances.called)
def testGETNamespaces(self): """ Test for Get '/_metrics/cloudwatch/namespaces' response is validated for appropriate headers, body and status response is validated against known namespaces """ response = self.app.get("/namespaces", headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) self.assertIsInstance(result, dict) supportedNamespaces = self._supportedAWSNamespaces() | set(["Autostacks"]) self.assertEqual(supportedNamespaces, set(result.keys()))
def _getInstancesHandlerCommon(self, instancesMock, route, expectedResult): """ This method wraps around common testing path for all GET routes which falls to listing all available instances instancesMock : Mock for Instances class route : route under test for current testcase expectedResult : expected response from API call """ response = self.app.get(route, headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) self.assertIsInstance(result, list) self.assertEqual(result, expectedResult) self.assertTrue(instancesMock.getInstances.called)
def testGetListNamespaceNoRegions(self, adapterMock): """ Test for Get '/_metrics/cloudwatch/namespaces' response is validated for appropriate headers, body and status """ adapterMock.return_value.describeRegions.return_value = [] response = self.app.get("/namespaces", headers=self.headers) assertions.assertSuccess(self, response) result = app_utils.jsonDecode(response.body) # Added Autostacks namespaces to this list for now, to maintain API # backwards-compatibility during adapter refactor self.assertEqual(result, {'Autostacks': { 'metrics': ['InstanceCount'] }})