From d59fa2b65e23b243806d82929d59c2bf79a91570 Mon Sep 17 00:00:00 2001 From: "hobo.kengo" Date: Fri, 5 Aug 2016 05:11:28 +0000 Subject: [PATCH] Disallow specifying too long name for meter-label Change-Id: Id916192f0ae38434de7d86790e056e48764b0916 Closes-Bug: #1609725 --- neutron/extensions/metering.py | 1 + .../tempest/api/test_metering_extensions.py | 14 ++++++++ .../tempest/api/test_metering_negative.py | 36 +++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 neutron/tests/tempest/api/test_metering_negative.py diff --git a/neutron/extensions/metering.py b/neutron/extensions/metering.py index 6f2de1494ff..1ef299875fe 100644 --- a/neutron/extensions/metering.py +++ b/neutron/extensions/metering.py @@ -49,6 +49,7 @@ RESOURCE_ATTRIBUTE_MAP = { 'is_visible': True, 'primary_key': True}, 'name': {'allow_post': True, 'allow_put': False, + 'validate': {'type:string': attr.NAME_MAX_LEN}, 'is_visible': True, 'default': ''}, 'description': {'allow_post': True, 'allow_put': False, 'validate': { diff --git a/neutron/tests/tempest/api/test_metering_extensions.py b/neutron/tests/tempest/api/test_metering_extensions.py index 756cd5a35d4..7b03386e984 100644 --- a/neutron/tests/tempest/api/test_metering_extensions.py +++ b/neutron/tests/tempest/api/test_metering_extensions.py @@ -15,8 +15,11 @@ from tempest.lib.common.utils import data_utils from tempest import test +from neutron.api.v2 import attributes as attr from neutron.tests.tempest.api import base +LONG_NAME_OK = 'x' * (attr.NAME_MAX_LEN) + class MeteringTestJSON(base.BaseAdminNetworkTest): @@ -81,6 +84,17 @@ class MeteringTestJSON(base.BaseAdminNetworkTest): id=metering_label['id'])) self.assertEqual(len(labels['metering_labels']), 1) + @test.idempotent_id('46608f8d-2e27-4eb6-a0b4-dbe405144c4d') + def test_create_delete_metering_label_with_name_max_length(self): + name = LONG_NAME_OK + body = self.admin_client.create_metering_label(name=name) + metering_label = body['metering_label'] + self.addCleanup(self._delete_metering_label, + metering_label['id']) + labels = (self.admin_client.list_metering_labels( + id=metering_label['id'])) + self.assertEqual(len(labels['metering_labels']), 1) + @test.idempotent_id('cfc500d9-9de6-4847-8803-62889c097d45') def test_show_metering_label(self): # Verifies the details of a label diff --git a/neutron/tests/tempest/api/test_metering_negative.py b/neutron/tests/tempest/api/test_metering_negative.py new file mode 100644 index 00000000000..39fdae8d87d --- /dev/null +++ b/neutron/tests/tempest/api/test_metering_negative.py @@ -0,0 +1,36 @@ +# Copyright 2016 FUJITSU LIMITED +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from tempest.lib import exceptions as lib_exc +from tempest import test + +from neutron.api.v2 import attributes as attr +from neutron.tests.tempest.api import base + +LONG_NAME_NG = 'x' * (attr.NAME_MAX_LEN + 1) + + +class MeteringNegativeTestJSON(base.BaseAdminNetworkTest): + + @classmethod + @test.requires_ext(extension="metering", service="network") + def resource_setup(cls): + super(MeteringNegativeTestJSON, cls).resource_setup() + + @test.attr(type='negative') + @test.idempotent_id('8b3f7c84-9d37-4771-8681-bfd2c07f3c2d') + def test_create_metering_label_with_too_long_name(self): + self.assertRaises(lib_exc.BadRequest, + self.admin_client.create_metering_label, + name=LONG_NAME_NG)