From f2617cde8a829f0666bf212fea19dcb1b90d0a58 Mon Sep 17 00:00:00 2001 From: zxgao Date: Wed, 19 Dec 2018 10:00:17 +0800 Subject: [PATCH] Add test case for availability zone api. Change-Id: I75d95b0a576c6320f402591b709e985feb176ed8 Closes-Bug: 1809059 --- .../unit/api/v1/test_availability_zone.py | 32 +++++++++++++++++++ mogan/tests/unit/db/utils.py | 22 +++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 mogan/tests/unit/api/v1/test_availability_zone.py diff --git a/mogan/tests/unit/api/v1/test_availability_zone.py b/mogan/tests/unit/api/v1/test_availability_zone.py new file mode 100644 index 00000000..d027ebed --- /dev/null +++ b/mogan/tests/unit/api/v1/test_availability_zone.py @@ -0,0 +1,32 @@ +# +# Copyright 2018 Fiberhome +# +# 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 mogan.tests.functional.api import v1 as v1_test +from mogan.tests.unit.db import utils + + +class TestAvailabilityZoneAuthorization(v1_test.APITestV1): + + def setUp(self): + super(TestAvailabilityZoneAuthorization, self).setUp() + self.agg = utils.create_test_aggregate() + self.metadata = {'availability_zone': 'az1'} + self.agg_meta = utils.create_test_aggregate_metadata( + aggregate_id=self.agg.id, metadata=self.metadata) + + def test_get_availability_zones_by_admin(self): + headers = self.gen_headers(self.context, roles="admin") + resp = self.get_json('/availability_zones', headers=headers) + self.assertIn(self.metadata['availability_zone'], + resp['availability_zones']) diff --git a/mogan/tests/unit/db/utils.py b/mogan/tests/unit/db/utils.py index 69cb0e9c..2ff3847a 100644 --- a/mogan/tests/unit/db/utils.py +++ b/mogan/tests/unit/db/utils.py @@ -194,6 +194,13 @@ def get_test_aggregate(**kw): } +def get_test_aggregate_metadata(**kw): + return { + 'aggregate_id': kw.get('aggregate_id', 123), + 'metadata': kw.get('metadata', {'key1': 'value1'}), + } + + def create_test_aggregate(context={}, **kw): """Create test aggregate entry in DB and return the DB object. @@ -213,6 +220,21 @@ def create_test_aggregate(context={}, **kw): return dbapi.aggregate_create(context, agg) +def create_test_aggregate_metadata(context={}, **kw): + """Create test aggregate metadata entry in DB and return the DB object. + + Function to be used to create test Aggregate metadata objects in the + database. + + :param context: The request context, for access checks. + :param kw: kwargs with overriding values for metadata's attributes. + :returns: metadata dict object. + """ + agg_meta = get_test_aggregate_metadata(**kw) + dbapi = db_api.get_instance() + return dbapi.aggregate_metadata_update_or_create(context, **agg_meta) + + def get_test_server_group(**kw): return { 'id': kw.get('id', 123),