nova/nova/tests/unit/api/openstack/test_requestlog.py
Eric Fried 52fe8c0285 Stop using NoAuthMiddleware in tests
This rips NoAuthMiddleware completely out of functional tests by:
- Removing our override of [api]auth_strategy in ConfFixture, allowing
  it to default to ``keystone``. This causes the fake wsgi setup to go
  through the same pipeline as real API requests; so making that work
  entails...
- Mocking out the keystonecontext piece of the paste pipeline to create
  the context previously mashed (somewhat inappropriately) into
  NoAuthMiddleware. In the future we may want to mock this more
  shallowly or find a way to make the req more realistic so it needn't
  be mocked at all, but for now this is close to what the noauth2
  pipeline used to do.
- Stubbing out the keystonemiddleware piece of the paste pipeline. In
  the future we should try to use keystonemiddleware's AuthTokenFixture
  so our tests can occur in a more realistic environment, but for now
  this is just mimicking what the noauth2 pipeline used to do; except
  for...
- Removing the authentication portion of the TestOpenStackClient. This
  used to make an actual request(), which landed in NoAuthMiddleware,
  which was hacking together some headers (based, it appears, on a
  protocol which is many years out of date and no longer approximates
  what keystone does, which should be the point if it's going to exist
  at all). So now we just hack up the necessary headers inline.
- Doing the addition of project_id in request URIs in OSAPIFixture.
  This is another thing that NoAuthMiddleware was doing inappropriately
  (IRL the project_id will either be part of the request from the start,
  or it won't). It was also only doing it part of the time; as a result,
  a couple of tests requesting version documents, which were previously
  not expecting the project ID to be present, needed to be modified to
  expect it. This better reflects reality.

Change-Id: I459a605b4a9390f0e36356ca1fe432948159acd4
2019-10-14 11:43:52 -05:00

146 lines
5.5 KiB
Python

# Copyright 2017 IBM Corp.
# 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.
import mock
import fixtures as fx
import testtools
from nova.tests import fixtures
from nova.tests.unit import conf_fixture
"""Test request logging middleware under various conditions.
The request logging middleware is needed when running under something
other than eventlet. While Nova grew up on eventlet, and it's wsgi
server, it meant that our user facing data (the log stream) was a mix
of what Nova was emitting, and what eventlet.wsgi was emitting on our
behalf. When running under uwsgi we want to make sure that we have
equivalent coverage.
All these tests use GET / to hit an endpoint that doesn't require the
database setup. We have to do a bit of mocking to make that work.
"""
class TestRequestLogMiddleware(testtools.TestCase):
def setUp(self):
super(TestRequestLogMiddleware, self).setUp()
# this is the minimal set of magic mocks needed to convince
# the API service it can start on it's own without a database.
mocks = ['nova.objects.Service.get_by_host_and_binary',
'nova.objects.Service.create']
self.stdlog = fixtures.StandardLogging()
self.useFixture(self.stdlog)
for m in mocks:
p = mock.patch(m)
self.addCleanup(p.stop)
p.start()
@mock.patch('nova.api.openstack.requestlog.RequestLog._should_emit')
def test_logs_requests(self, emit):
"""Ensure requests are logged.
Make a standard request for / and ensure there is a log entry.
"""
emit.return_value = True
self.useFixture(conf_fixture.ConfFixture())
self.useFixture(fixtures.RPCFixture('nova.test'))
api = self.useFixture(fixtures.OSAPIFixture()).api
resp = api.api_request('/', strip_version=True)
# the content length might vary, but the important part is
# what we log is what we return to the user (which turns out
# to excitingly not be the case with eventlet!)
content_length = resp.headers['content-length']
log1 = ('INFO [nova.api.openstack.requestlog] 127.0.0.1 '
'"GET /" status: 200 len: %s' % content_length)
self.assertIn(log1, self.stdlog.logger.output)
@mock.patch('nova.api.openstack.requestlog.RequestLog._should_emit')
def test_logs_mv(self, emit):
"""Ensure logs register microversion if passed.
This makes sure that microversion logging actually shows up
when appropriate.
"""
emit.return_value = True
self.useFixture(conf_fixture.ConfFixture())
# NOTE(sdague): all these tests are using the
self.useFixture(
fx.MonkeyPatch(
'nova.api.openstack.compute.versions.'
'Versions.support_api_request_version',
True))
self.useFixture(fixtures.RPCFixture('nova.test'))
api = self.useFixture(fixtures.OSAPIFixture()).api
api.microversion = '2.25'
resp = api.api_request('/', strip_version=True)
content_length = resp.headers['content-length']
log1 = ('INFO [nova.api.openstack.requestlog] 127.0.0.1 '
'"GET /" status: 200 len: %s microversion: 2.25 time:' %
content_length)
self.assertIn(log1, self.stdlog.logger.output)
@mock.patch('nova.api.openstack.compute.versions.Versions.index')
@mock.patch('nova.api.openstack.requestlog.RequestLog._should_emit')
def test_logs_under_exception(self, emit, v_index):
"""Ensure that logs still emit under unexpected failure.
If we get an unexpected failure all the way up to the top, we should
still have a record of that request via the except block.
"""
emit.return_value = True
v_index.side_effect = Exception("Unexpected Error")
self.useFixture(conf_fixture.ConfFixture())
self.useFixture(fixtures.RPCFixture('nova.test'))
api = self.useFixture(fixtures.OSAPIFixture()).api
api.api_request('/', strip_version=True)
log1 = ('INFO [nova.api.openstack.requestlog] 127.0.0.1 "GET /"'
' status: 500 len: 0 microversion: - time:')
self.assertIn(log1, self.stdlog.logger.output)
@mock.patch('nova.api.openstack.requestlog.RequestLog._should_emit')
def test_no_log_under_eventlet(self, emit):
"""Ensure that logs don't end up under eventlet.
We still set the _should_emit return value directly to prevent
the situation where eventlet is removed from tests and this
preventing that.
NOTE(sdague): this test can be deleted when eventlet is no
longer supported for the wsgi stack in Nova.
"""
emit.return_value = False
self.useFixture(conf_fixture.ConfFixture())
self.useFixture(fixtures.RPCFixture('nova.test'))
api = self.useFixture(fixtures.OSAPIFixture()).api
api.api_request('/', strip_version=True)
self.assertNotIn("nova.api.openstack.requestlog",
self.stdlog.logger.output)