From 4898f5b7a8c8b45201c620021a13fb3f8221ce72 Mon Sep 17 00:00:00 2001 From: Salvatore Orlando Date: Thu, 11 Aug 2011 00:52:15 +0100 Subject: [PATCH] Stubout work in progress --- quantum/client.py | 11 ++++++++-- tests/unit/client_tools/stubs.py | 37 ++++++++++++++++++++++++++++++++ tests/unit/test_cli.py | 18 ++++++++++++++++ 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 tests/unit/client_tools/stubs.py diff --git a/quantum/client.py b/quantum/client.py index ca05236fa..3bc04ec45 100644 --- a/quantum/client.py +++ b/quantum/client.py @@ -19,6 +19,7 @@ import httplib import socket import urllib + from quantum.common.wsgi import Serializer @@ -91,6 +92,13 @@ class Client(object): else: return httplib.HTTPConnection + def _send_request(self, conn, method, action, body, headers): + # Salvatore: Isolating this piece of code in its own method to + # facilitate stubout for testing + conn.request(method, action, body, headers) + return conn.getresponse() + + def do_request(self, method, action, body=None, headers=None, params=None): """ @@ -132,8 +140,7 @@ class Client(object): else: c = connection_type(self.host, self.port) - c.request(method, action, body, headers) - res = c.getresponse() + res = self._send_request(c, method, action, body, headers) status_code = self.get_status_code(res) if status_code in (httplib.OK, httplib.CREATED, diff --git a/tests/unit/client_tools/stubs.py b/tests/unit/client_tools/stubs.py new file mode 100644 index 000000000..04a2e3cc4 --- /dev/null +++ b/tests/unit/client_tools/stubs.py @@ -0,0 +1,37 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2011 OpenStack LLC +# +# 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. + +""" Stubs for client tools unit tests """ + +from quantum import client +from tests.unit import testlib_api + + +def stubout_send_request(stubs, api): + """Simulates a failure in fetch image_glance_disk.""" + + def fake_send_request(self, conn, method, action, body, headers): + # ignore headers and connection + req = testlib_api.create_request(action, body, + "application/json", method) + res = req.get_response(api) + return res + + stubs.Set(client.Client, '_send_request', fake_send_request) + +class FakeHTTPConnection: + """ stub HTTP connection class for CLI testing """ + pass diff --git a/tests/unit/test_cli.py b/tests/unit/test_cli.py index b662d6715..d772ed32d 100644 --- a/tests/unit/test_cli.py +++ b/tests/unit/test_cli.py @@ -23,10 +23,14 @@ import logging +import stubout import sys import unittest from quantum import api as server +from quantum import cli +from quantum.db import api as db +from tests.unit.client_tools import stubs as client_stubs LOG = logging.getLogger('quantum.tests.test_cli') @@ -34,8 +38,22 @@ class CLITest(unittest.TestCase): def setUp(self): """Prepare the test environment""" + options = {} + options['plugin_provider'] = 'quantum.plugins.SamplePlugin.FakePlugin' + self.api = server.APIRouterV01(options) + self.tenant_id = "test_tenant" + self.network_name_1 = "test_network_1" + self.network_name_2 = "test_network_2" + # Stubout do_request + self.stubs = stubout.StubOutForTesting() + client_stubs.stubout_send_request(self.stubs, self.api) + # Redirect stdout + # Pre-populate data pass def tearDown(self): """Clear the test environment""" + db.clear_db() + + def test_list_networks_api(self): pass -- 2.45.2