Add unit tests for nsxlib-backend methods

Change-Id: I9ef65ab9e16eeb4a30e7af79636ae3b1e8bd7776
This commit is contained in:
Amey Bhide 2015-06-22 13:52:24 -07:00 committed by garyk
parent f28417def7
commit dec3852846
5 changed files with 161 additions and 5 deletions

View File

@ -0,0 +1,25 @@
# Copyright (c) 2015 VMware, Inc.
#
# 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 oslo_config import cfg
import unittest
class NsxLibTestCase(unittest.TestCase):
def setUp(self):
cfg.CONF.set_override('nsx_controllers', ['1.2.3.4'])
cfg.CONF.set_override('nsx_user', 'admin')
cfg.CONF.set_override('nsx_password', 'default')

View File

@ -0,0 +1,81 @@
# Copyright (c) 2015 VMware, Inc.
#
# 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 requests
from oslo_log import log
from vmware_nsx.neutron.plugins.vmware.nsxlib import v3 as nsxlib
from vmware_nsx.neutron.tests.unit.vmware.nsxlib.v3 import nsxlib_testcase
from vmware_nsx.neutron.tests.unit.vmware import test_constants_v3
LOG = log.getLogger(__name__)
class NsxLibPortTestCase(nsxlib_testcase.NsxLibTestCase):
def _create_mock_object(self, fake_object):
"""Construct mock response object"""
mock_response = mock.Mock()
if fake_object is not None:
mock_response.json.return_value = fake_object
return mock_response
@mock.patch("vmware_nsx.neutron.plugins.vmware.nsxlib.v3.requests.post")
def test_create_logical_port(self, mock_post):
"""
Test creating a port returns the correct response and 200 status
"""
mock_post.return_value = self._create_mock_object(
test_constants_v3.FAKE_PORT)
mock_post.return_value.status_code = requests.codes.created
result = nsxlib.create_logical_port(
test_constants_v3.FAKE_PORT['logical_switch_id'],
test_constants_v3.FAKE_PORT['attachment']['id'],
tags={})
self.assertEqual(test_constants_v3.FAKE_PORT, result)
@mock.patch("vmware_nsx.neutron.plugins.vmware.nsxlib.v3.requests.post")
def test_create_logical_port_admin_down(self, mock_post):
"""
Test creating port with admin_state down
"""
fake_port = test_constants_v3.FAKE_PORT
fake_port['admin_state'] = "DOWN"
mock_post.return_value = self._create_mock_object(fake_port)
mock_post.return_value.status_code = requests.codes.created
result = nsxlib.create_logical_port(
test_constants_v3.FAKE_PORT['logical_switch_id'],
test_constants_v3.FAKE_PORT['attachment']['id'],
tags={}, admin_state=False)
self.assertEqual(fake_port, result)
@mock.patch("vmware_nsx.neutron.plugins.vmware.nsxlib.v3.requests.delete")
def test_delete_logical_port(self, mock_delete):
"""
Test deleting port
"""
mock_delete.return_value = self._create_mock_object(None)
mock_delete.return_value.status_code = requests.codes.ok
result = nsxlib.delete_logical_port(
test_constants_v3.FAKE_PORT['id'])
self.assertIsNone(result)

View File

@ -0,0 +1,49 @@
# Copyright (c) 2015 VMware, Inc.
#
# 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 requests
from oslo_log import log
from vmware_nsx.neutron.plugins.vmware.nsxlib import v3 as nsxlib
from vmware_nsx.neutron.tests.unit.vmware.nsxlib.v3 import nsxlib_testcase
from vmware_nsx.neutron.tests.unit.vmware import test_constants_v3
LOG = log.getLogger(__name__)
class NsxLibSwitchTestCase(nsxlib_testcase.NsxLibTestCase):
def _create_mock_object(self, fake_object):
"""Construct mock response object"""
mock_response = mock.Mock()
mock_response.json.return_value = fake_object
return mock_response
@mock.patch("vmware_nsx.neutron.plugins.vmware.nsxlib.v3.requests.post")
def test_create_logical_switch(self, mock_post):
"""
Test creating a switch returns the correct response and 200 status
"""
mock_post.return_value = self._create_mock_object(
test_constants_v3.FAKE_SWITCH)
mock_post.return_value.status_code = requests.codes.created
result = nsxlib.create_logical_switch(
test_constants_v3.FAKE_NAME,
test_constants_v3.FAKE_TZ_UUID, tags={})
self.assertEqual(test_constants_v3.FAKE_SWITCH, result)

View File

@ -1,4 +1,4 @@
# Copyright (c) 2015 OpenStack Foundation.
# Copyright (c) 2015 VMware, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -17,14 +17,15 @@
from oslo_utils import uuidutils
FAKE_NAME = "fake_name"
FAKE_UUID = uuidutils.generate_uuid()
FAKE_TZ_UUID = uuidutils.generate_uuid()
FAKE_SWITCH_UUID = uuidutils.generate_uuid()
FAKE_SWITCH = {
"id": FAKE_UUID,
"id": FAKE_SWITCH_UUID,
"display_name": FAKE_NAME,
"resource_type": "LogicalSwitch",
"address_bindings": [],
"transport_zone_id": "dc9ffa3b-3f7b-4e06-b55e-07ec3ebd5a56",
"transport_zone_id": FAKE_TZ_UUID,
"replication_mode": "MTEP",
"admin_state": "UP",
"vni": 50056,
@ -58,7 +59,7 @@ FAKE_PORT = {
"display_name": FAKE_NAME,
"resource_type": "LogicalPort",
"address_bindings": [],
"logical_switch_id": "e3360f8a-3392-413e-972c-7aa91647693f",
"logical_switch_id": FAKE_SWITCH_UUID,
"admin_state": "UP",
"attachment": {
"id": "9ca8d413-f7bf-4276-b4c9-62f42516bdb2",