Convert security group tests to httpretty

Change-Id: Icdc2c2c6f66a014c29241e262d21d50e39128105
blueprint: httpretty-testing
This commit is contained in:
Jamie Lennox 2014-05-26 13:17:41 +10:00
parent d6f59a3016
commit 0170e78454
2 changed files with 121 additions and 20 deletions

View File

@ -0,0 +1,99 @@
# 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 httpretty
from novaclient.openstack.common import jsonutils
from novaclient.tests import fakes
from novaclient.tests.fixture_data import base
class Fixture(base.Fixture):
base_url = 'os-security-groups'
def setUp(self):
super(Fixture, self).setUp()
security_group_1 = {
"name": "test",
"description": "FAKE_SECURITY_GROUP",
"tenant_id": "4ffc664c198e435e9853f2538fbcd7a7",
"id": 1,
"rules": [
{
"id": 11,
"group": {},
"ip_protocol": "TCP",
"from_port": 22,
"to_port": 22,
"parent_group_id": 1,
"ip_range": {"cidr": "10.0.0.0/8"}
},
{
"id": 12,
"group": {
"tenant_id": "272bee4c1e624cd4a72a6b0ea55b4582",
"name": "test2"
},
"ip_protocol": "TCP",
"from_port": 222,
"to_port": 222,
"parent_group_id": 1,
"ip_range": {}
}
]
}
security_group_2 = {
"name": "test2",
"description": "FAKE_SECURITY_GROUP2",
"tenant_id": "272bee4c1e624cd4a72a6b0ea55b4582",
"id": 2,
"rules": []
}
get_groups = {'security_groups': [security_group_1, security_group_2]}
httpretty.register_uri(httpretty.GET, self.url(),
body=jsonutils.dumps(get_groups),
content_type='application/json')
get_group_1 = {'security_group': security_group_1}
httpretty.register_uri(httpretty.GET, self.url(1),
body=jsonutils.dumps(get_group_1),
content_type='application/json')
httpretty.register_uri(httpretty.DELETE, self.url(1), status=202)
def post_os_security_groups(request, url, headers):
body = jsonutils.loads(request.body.decode('utf-8'))
assert list(body) == ['security_group']
fakes.assert_has_keys(body['security_group'],
required=['name', 'description'])
r = jsonutils.dumps({'security_group': security_group_1})
return 202, headers, r
httpretty.register_uri(httpretty.POST, self.url(),
body=post_os_security_groups,
content_type='application/json')
def put_os_security_groups_1(request, url, headers):
body = jsonutils.loads(request.body.decode('utf-8'))
assert list(body) == ['security_group']
fakes.assert_has_keys(body['security_group'],
required=['name', 'description'])
return 205, headers, request.body
httpretty.register_uri(httpretty.PUT, self.url(1),
body=put_os_security_groups_1,
content_type='application/json')

View File

@ -11,18 +11,20 @@
# License for the specific language governing permissions and limitations
# under the License.
from novaclient.tests.fixture_data import client
from novaclient.tests.fixture_data import security_groups as data
from novaclient.tests import utils
from novaclient.tests.v1_1 import fakes
from novaclient.v1_1 import security_groups
cs = fakes.FakeClient()
class SecurityGroupsTest(utils.FixturedTestCase):
client_fixture_class = client.V1
data_fixture_class = data.Fixture
class SecurityGroupsTest(utils.TestCase):
def _do_test_list_security_groups(self, search_opts, path):
sgs = cs.security_groups.list(search_opts=search_opts)
cs.assert_called('GET', path)
sgs = self.cs.security_groups.list(search_opts=search_opts)
self.assert_called('GET', path)
for sg in sgs:
self.assertIsInstance(sg, security_groups.SecurityGroup)
@ -39,34 +41,34 @@ class SecurityGroupsTest(utils.TestCase):
{'all_tenants': 0}, '/os-security-groups')
def test_get_security_groups(self):
sg = cs.security_groups.get(1)
cs.assert_called('GET', '/os-security-groups/1')
sg = self.cs.security_groups.get(1)
self.assert_called('GET', '/os-security-groups/1')
self.assertIsInstance(sg, security_groups.SecurityGroup)
self.assertEqual('1', str(sg))
def test_delete_security_group(self):
sg = cs.security_groups.list()[0]
sg = self.cs.security_groups.list()[0]
sg.delete()
cs.assert_called('DELETE', '/os-security-groups/1')
cs.security_groups.delete(1)
cs.assert_called('DELETE', '/os-security-groups/1')
cs.security_groups.delete(sg)
cs.assert_called('DELETE', '/os-security-groups/1')
self.assert_called('DELETE', '/os-security-groups/1')
self.cs.security_groups.delete(1)
self.assert_called('DELETE', '/os-security-groups/1')
self.cs.security_groups.delete(sg)
self.assert_called('DELETE', '/os-security-groups/1')
def test_create_security_group(self):
sg = cs.security_groups.create("foo", "foo barr")
cs.assert_called('POST', '/os-security-groups')
sg = self.cs.security_groups.create("foo", "foo barr")
self.assert_called('POST', '/os-security-groups')
self.assertIsInstance(sg, security_groups.SecurityGroup)
def test_update_security_group(self):
sg = cs.security_groups.list()[0]
secgroup = cs.security_groups.update(sg, "update", "update")
cs.assert_called('PUT', '/os-security-groups/1')
sg = self.cs.security_groups.list()[0]
secgroup = self.cs.security_groups.update(sg, "update", "update")
self.assert_called('PUT', '/os-security-groups/1')
self.assertIsInstance(secgroup, security_groups.SecurityGroup)
def test_refresh_security_group(self):
sg = cs.security_groups.get(1)
sg2 = cs.security_groups.get(1)
sg = self.cs.security_groups.get(1)
sg2 = self.cs.security_groups.get(1)
self.assertEqual(sg.name, sg2.name)
sg2.name = "should be test"
self.assertNotEqual(sg.name, sg2.name)