From 7d8aae08e7fb90db153699ca0179c92072f9d3fb Mon Sep 17 00:00:00 2001 From: Chris Dent Date: Thu, 25 Jun 2015 16:14:57 +0000 Subject: [PATCH] Make ceilometer work correctly when hosted with a SCRIPT_NAME In the past ceilometer was generaly hosted at / and URL generation was relatively straightforward. It should also be possible to host at /foobar or /telemetry and for URL generation to still work. This changeset adds tests to confirm prefixed requests can work and fixes the small number of issues that were found. Note that because the ceilometer api has the temerity to not set location headers, we can't test that those are working, because there aren't any. In general that's the place where SCRIPT_NAME mounting would show bugs. But we can confirm that content-location headers are okay. Closes-Bug: #1468796 Change-Id: I6e5cf440bc3bd8ffb4cc5c86aa19225b3c5f87ed --- .testr.conf | 2 +- ceilometer/api/controllers/root.py | 2 +- ceilometer/api/controllers/v2/resources.py | 4 +- .../gabbits_prefix/api_events_with_data.yaml | 13 +++++ .../tests/gabbi/gabbits_prefix/basic.yaml | 20 ++++++++ .../gabbi/gabbits_prefix/clean-samples.yaml | 51 +++++++++++++++++++ .../gabbits_prefix/resources-fixtured.yaml | 24 +++++++++ ceilometer/tests/gabbi/test_gabbi_prefix.py | 35 +++++++++++++ 8 files changed, 147 insertions(+), 4 deletions(-) create mode 100644 ceilometer/tests/gabbi/gabbits_prefix/api_events_with_data.yaml create mode 100644 ceilometer/tests/gabbi/gabbits_prefix/basic.yaml create mode 100644 ceilometer/tests/gabbi/gabbits_prefix/clean-samples.yaml create mode 100644 ceilometer/tests/gabbi/gabbits_prefix/resources-fixtured.yaml create mode 100644 ceilometer/tests/gabbi/test_gabbi_prefix.py diff --git a/.testr.conf b/.testr.conf index 4b4a9d6551..0a8a677b42 100644 --- a/.testr.conf +++ b/.testr.conf @@ -6,4 +6,4 @@ test_command=OS_STDOUT_CAPTURE=${OS_STDOUT_CAPTURE:-1} \ test_id_option=--load-list $IDFILE test_list_option=--list # NOTE(chdent): Only used/matches on gabbi-related tests. -group_regex=(gabbi\.driver\.test_gabbi_[^_]+)_ +group_regex=(gabbi\.driver\.test_gabbi_(?:prefix_|)[^_]+)_ diff --git a/ceilometer/api/controllers/root.py b/ceilometer/api/controllers/root.py index 4f22862aa5..2198c40618 100644 --- a/ceilometer/api/controllers/root.py +++ b/ceilometer/api/controllers/root.py @@ -27,7 +27,7 @@ class RootController(object): @pecan.expose('json') def index(self): - base_url = pecan.request.host_url + base_url = pecan.request.application_url available = [{'tag': 'v2', 'date': '2013-02-13T00:00:00Z', }] collected = [version_descriptor(base_url, v['tag'], v['date']) for v in available] diff --git a/ceilometer/api/controllers/v2/resources.py b/ceilometer/api/controllers/v2/resources.py index 3b489f186d..b90255155b 100644 --- a/ceilometer/api/controllers/v2/resources.py +++ b/ceilometer/api/controllers/v2/resources.py @@ -100,14 +100,14 @@ class ResourcesController(rest.RestController): rel=rel_name) def _resource_links(self, resource_id, meter_links=1): - links = [self._make_link('self', pecan.request.host_url, + links = [self._make_link('self', pecan.request.application_url, 'resources', resource_id)] if meter_links: for meter in pecan.request.storage_conn.get_meters( resource=resource_id): query = {'field': 'resource_id', 'value': resource_id} links.append(self._make_link(meter.name, - pecan.request.host_url, + pecan.request.application_url, 'meters', meter.name, query=query)) return links diff --git a/ceilometer/tests/gabbi/gabbits_prefix/api_events_with_data.yaml b/ceilometer/tests/gabbi/gabbits_prefix/api_events_with_data.yaml new file mode 100644 index 0000000000..cab9f3274a --- /dev/null +++ b/ceilometer/tests/gabbi/gabbits_prefix/api_events_with_data.yaml @@ -0,0 +1,13 @@ +# This test runs against the Events API and confirms the +# content-location header includes a prefix. +fixtures: +- ConfigFixture +- EventDataFixture + +tests: + +- name: get all events + url: /v2/events + response_headers: + content-type: application/json; charset=UTF-8 + content-location: /$SCHEME://.*/telemetry/v2/events/ diff --git a/ceilometer/tests/gabbi/gabbits_prefix/basic.yaml b/ceilometer/tests/gabbi/gabbits_prefix/basic.yaml new file mode 100644 index 0000000000..d9d2815ba7 --- /dev/null +++ b/ceilometer/tests/gabbi/gabbits_prefix/basic.yaml @@ -0,0 +1,20 @@ +# +# Confirm root reports the right data including a prefixed URL +# +fixtures: + - ConfigFixture + +tests: + +# Root gives us some information on where to go from here. +- name: quick root check + url: / + response_headers: + content-type: application/json; charset=UTF-8 + response_strings: + - '"base": "application/json"' + response_json_paths: + versions.values.[0].status: stable + versions.values.[0].media-types.[0].base: application/json + response_strings: + - /telemetry/ diff --git a/ceilometer/tests/gabbi/gabbits_prefix/clean-samples.yaml b/ceilometer/tests/gabbi/gabbits_prefix/clean-samples.yaml new file mode 100644 index 0000000000..61f7c816ae --- /dev/null +++ b/ceilometer/tests/gabbi/gabbits_prefix/clean-samples.yaml @@ -0,0 +1,51 @@ +# Post a simple sample and confirm the created resource has +# reasonable URLs +fixtures: + - ConfigFixture + +tests: + +# POST one sample and verify its existence. + + - name: post sample for meter + desc: post a single sample + url: /v2/meters/apples?direct=True + method: POST + request_headers: + content-type: application/json + data: | + [ + { + "counter_name": "apples", + "project_id": "35b17138-b364-4e6a-a131-8f3099c5be68", + "user_id": "efd87807-12d2-4b38-9c70-5f5c2ac427ff", + "counter_unit": "instance", + "counter_volume": 1, + "resource_id": "bd9431c1-8d69-4ad3-803a-8d4a6b89fd36", + "resource_metadata": { + "name2": "value2", + "name1": "value1" + }, + "counter_type": "gauge" + } + ] + + response_json_paths: + $.[0].counter_name: apples + status: 201 + response_headers: + content-type: application/json; charset=UTF-8 + + - name: get resources + desc: get the resources that exist because of the sample + url: /v2/resources + response_json_paths: + $.[0].metadata.name2: value2 + + - name: get resource + desc: get just one of those resources via self + url: $RESPONSE['$[0].links[0].href'] + response_json_paths: + $.metadata.name2: value2 + response_strings: + - /telemetry/ diff --git a/ceilometer/tests/gabbi/gabbits_prefix/resources-fixtured.yaml b/ceilometer/tests/gabbi/gabbits_prefix/resources-fixtured.yaml new file mode 100644 index 0000000000..ce4811de8e --- /dev/null +++ b/ceilometer/tests/gabbi/gabbits_prefix/resources-fixtured.yaml @@ -0,0 +1,24 @@ +# +# Explore and cover resources API with gabbi tests when there are a +# small number of pre-existing resources +# + +fixtures: + - ConfigFixture + - SampleDataFixture + +tests: + + - name: list all resources + url: /v2/resources + response_json_paths: + $[0].user_id: farmerjon + $[0].links[1].rel: livestock + response_strings: + - /telemetry/ + + - name: get one resource + desc: get a resource via the links in the first resource listed above + url: $RESPONSE['$[0].links[0].href'] + response_json_paths: + $.resource_id: $RESPONSE['$[0].resource_id'] diff --git a/ceilometer/tests/gabbi/test_gabbi_prefix.py b/ceilometer/tests/gabbi/test_gabbi_prefix.py new file mode 100644 index 0000000000..ae7045b122 --- /dev/null +++ b/ceilometer/tests/gabbi/test_gabbi_prefix.py @@ -0,0 +1,35 @@ +# +# Copyright 2015 Red Hat. All Rights Reserved. +# +# 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. + +"""A module to exercise the Ceilometer API with gabbi with a URL prefix""" + +import os + +from gabbi import driver + +from ceilometer.api import app +from ceilometer.tests.gabbi import fixtures as fixture_module + + +TESTS_DIR = 'gabbits_prefix' + + +def load_tests(loader, tests, pattern): + """Provide a TestSuite to the discovery process.""" + test_dir = os.path.join(os.path.dirname(__file__), TESTS_DIR) + return driver.build_tests(test_dir, loader, host=None, + prefix='/telemetry', + intercept=app.VersionSelectorApplication, + fixture_module=fixture_module)