Skip to content
This repository has been archived by the owner on Dec 1, 2021. It is now read-only.

Commit

Permalink
Updated metric_type schema to support custom metrics (#404)
Browse files Browse the repository at this point in the history
* Updated metric_type schema to support custom metrics

 - metric_type can be alphanumeric characters and it can also have
underscores

* Updated apiserver policy validation
  • Loading branch information
rohitsharma04 authored and qibobo committed Nov 21, 2018
1 parent 469a9bf commit 7cb1703
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 20 deletions.
7 changes: 1 addition & 6 deletions api/lib/validation/schemaValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ var getDaysInMonthInISOFormat = function() {
return monthEnum;
};

var getMetricTypes = function() {
var metricTypeEnum = ['memoryused', 'memoryutil', 'responsetime', 'throughput'];
return metricTypeEnum;
};


var getPolicySchema = function() {
var schema = {
Expand Down Expand Up @@ -92,7 +87,7 @@ var getScalingRuleSchema = function() {
'type': 'object',
'id':'/scaling_rules',
'properties' : {
'metric_type':{ 'type':'string' },
'metric_type':{ 'type':'string', 'pattern':'^[a-zA-Z0-9_]+$' },
'breach_duration_secs':{ 'type':'integer','minimum': 60,'maximum': 3600 },
'threshold':{ 'type':'integer'},
'operator':{ 'type':'string','enum': validOperators },
Expand Down
2 changes: 1 addition & 1 deletion api/test/unit/validation/schemaValidator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('Validating Policy JSON schema construction',function(){
var validOperator = schemaValidatorPrivate.__get__('getValidOperators')();
var adjustmentPattern = schemaValidatorPrivate.__get__('getAdjustmentPattern')();
expect(schema.id).to.equal('/scaling_rules');
expect(schema.properties.metric_type).to.deep.equal({ 'type':'string' });
expect(schema.properties.metric_type).to.deep.equal({ 'type':'string','pattern': '^[a-zA-Z0-9_]+$' });
expect(schema.properties.breach_duration_secs).to.deep.equal({ 'type':'integer','minimum': 60,'maximum': 3600 });
expect(schema.properties.threshold).to.deep.equal({ 'type':'integer'});
expect(schema.properties.operator).to.deep.equal({ 'type':'string','enum':validOperator });
Expand Down
21 changes: 17 additions & 4 deletions api/test/unit/validation/validationMiddleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,7 @@ describe('Validate Policy JSON Schema structure', function () {

var requiredProperties = schemaValidatorPrivate.__get__('getScalingRuleSchema')().required;
var allProperties = Object.keys(schemaValidatorPrivate.__get__('getScalingRuleSchema')().properties);

var metricTypes = schemaValidatorPrivate.__get__('getMetricTypes')();
var validMetricTypes = ['memoryutil','memoryused','responsetime','throughput','custom_metric'];
var validMetricThresholdSettings = [
{ metric_type: "memoryutil", thresholds: [30, 100] },
{ metric_type: "memoryused", thresholds: [30, 100, 1000] },
Expand Down Expand Up @@ -259,7 +258,7 @@ describe('Validate Policy JSON Schema structure', function () {
});
});

metricTypes.forEach(function (metricType) {
validMetricTypes.forEach(function (metricType) {
it('succeed with valid metric type ' + metricType + ', policy type ' + type, function (done) {
buildTestPolicy(fakePolicy, type);
for (let i = 0; i < fakePolicy.scaling_rules.length; i++) {
Expand Down Expand Up @@ -374,10 +373,24 @@ describe('Validate Policy JSON Schema structure', function () {
});
});
});
it('should fail with custom metric type. policy type ' + type, function (done) {
buildTestPolicy(fakePolicy, type);
fakePolicy.scaling_rules[0].metric_type = 'my metric $';
request(app)
.put('/v1/apps/fakeID/policy', validationMiddleware)
.send(fakePolicy)
.end(function (error, result) {
expect(result.statusCode).to.equal(400);
expect(result.body.error).to.not.be.null;
expect(result.body.error[0].message).to.equal('does not match pattern "^[a-zA-Z0-9_]+$"');
expect(result.body.error[0].stack).to.equal('instance.scaling_rules[0].metric_type does not match pattern "^[a-zA-Z0-9_]+$"');
done();
});
});

it('should not fail with custom metric type. policy type ' + type, function (done) {
buildTestPolicy(fakePolicy, type);
fakePolicy.scaling_rules[0].metric_type = 'custom-metric';
fakePolicy.scaling_rules[0].metric_type = 'custom_metric';
request(app)
.put('/v1/apps/fakeID/policy', validationMiddleware)
.send(fakePolicy)
Expand Down
9 changes: 2 additions & 7 deletions servicebroker/config/catalog.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,8 @@
"description": "The interval between two successive scaling activity"
},
"metric_type": {
"enum": [
"memoryused",
"memoryutil",
"responsetime",
"throughput"
],
"type": "string"
"type": "string",
"pattern": "^[a-zA-Z0-9_]+$"
},
"operator": {
"enum": [
Expand Down
9 changes: 7 additions & 2 deletions servicebroker/test/routes/catalog_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,16 @@ describe("Validate schema definition",function(){
expect(ajv.validate(catalogSchema,fakePolicy)).to.be.false;
});

it("validate metric_type",function(){
fakePolicy.scaling_rules[0].metric_type = "mymetrictype"
it("validate metric_type with non alphanumeric characters",function(){
fakePolicy.scaling_rules[0].metric_type = "ab cd$"
expect(ajv.validate(catalogSchema,fakePolicy)).to.be.false;
});

it("validate metric_type with underscore",function(){
fakePolicy.scaling_rules[0].metric_type = "my_metric2"
expect(ajv.validate(catalogSchema,fakePolicy)).to.be.true;
});

it("validate operator",function(){
fakePolicy.scaling_rules[0].operator = "$"
expect(ajv.validate(catalogSchema,fakePolicy)).to.be.false;
Expand Down

0 comments on commit 7cb1703

Please sign in to comment.