diff --git a/api/lib/validation/schemaValidator.js b/api/lib/validation/schemaValidator.js index 40bb18544..b13eeb5f3 100644 --- a/api/lib/validation/schemaValidator.js +++ b/api/lib/validation/schemaValidator.js @@ -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 = { @@ -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 }, diff --git a/api/test/unit/validation/schemaValidator.test.js b/api/test/unit/validation/schemaValidator.test.js index 1b46c1a93..c20c69311 100644 --- a/api/test/unit/validation/schemaValidator.test.js +++ b/api/test/unit/validation/schemaValidator.test.js @@ -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 }); diff --git a/api/test/unit/validation/validationMiddleware.test.js b/api/test/unit/validation/validationMiddleware.test.js index 9219bd2e4..f202f4931 100644 --- a/api/test/unit/validation/validationMiddleware.test.js +++ b/api/test/unit/validation/validationMiddleware.test.js @@ -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] }, @@ -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++) { @@ -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) diff --git a/servicebroker/config/catalog.schema.json b/servicebroker/config/catalog.schema.json index a94830c20..c34af4c8c 100644 --- a/servicebroker/config/catalog.schema.json +++ b/servicebroker/config/catalog.schema.json @@ -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": [ diff --git a/servicebroker/test/routes/catalog_test.js b/servicebroker/test/routes/catalog_test.js index 3248e0aa3..e15381947 100644 --- a/servicebroker/test/routes/catalog_test.js +++ b/servicebroker/test/routes/catalog_test.js @@ -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;