Add router lib unittests

Change-Id: I86ac6564d4df1f3d0ee76da6d49b7476c642ffaf
This commit is contained in:
Adit Sarfaty 2018-07-23 14:30:12 +03:00
parent 12c8ed0639
commit 18b03b993f
3 changed files with 197 additions and 2 deletions

View File

@ -140,7 +140,8 @@ FAKE_ROUTER_LINKT1_PORT = {
"revision": 0,
"id": FAKE_ROUTER_LINKT1_PORT_UUID,
"display_name": FAKE_NAME,
"logical_router_id": FAKE_ROUTER_UUID
"logical_router_id": FAKE_ROUTER_UUID,
"linked_logical_router_port_id": {'target_id': uuidutils.generate_uuid()}
}
FAKE_QOS_PROFILE = {
@ -531,3 +532,59 @@ FAKE_VPN_SESS = {
"enabled": True,
}],
}
FAKE_EDGE_CLUSTER_ID = "69c6bc48-0590-4ff5-87b6-9b49e20b67e0"
FAKE_EDGE_CLUSTER = {
"resource_type": "EdgeCluster",
"description": "edgecluster1",
"id": FAKE_EDGE_CLUSTER_ID,
"display_name": "edgecluster1",
"deployment_type": "VIRTUAL_MACHINE",
"member_node_type": "EDGE_NODE",
"members": [{
"member_index": 0,
"transport_node_id": "321d2746-898e-11e8-9723-000c29391f21"
}],
"cluster_profile_bindings": [{
"profile_id": "15d3485e-0474-4511-bd79-1506ce777baa",
"resource_type": "EdgeHighAvailabilityProfile"
}],
}
FAKE_TIERO_ROUTER_ID = "67927d95-18d3-4763-9eb1-a45ff0e63bbe"
FAKE_TIERO_ROUTER = {
"resource_type": "LogicalRouter",
"description": "Provider Logical Router(Tier0)",
"id": FAKE_TIERO_ROUTER_ID,
"display_name": "PLR-1 LogicalRouterTier0",
"edge_cluster_id": FAKE_EDGE_CLUSTER_ID,
"firewall_sections": [{
"is_valid": True,
"target_type": "FirewallSection",
"target_id": "c3d80576-e340-403d-a2d0-f4a72a1db6e3"
}],
"advanced_config": {
"external_transit_networks": ["100.64.0.0/16"],
"internal_transit_network": "169.254.0.0/28"
},
"router_type": "TIER0",
"high_availability_mode": "ACTIVE_STANDBY",
"failover_mode": "NON_PREEMPTIVE",
}
FAKE_TRANS_NODE_ID = "f5a2b5ca-8dba-11e8-9799-020039422cc8"
FAKE_TRANS_NODE = {
"resource_type": "TransportNode",
"id": FAKE_TRANS_NODE_ID,
"display_name": FAKE_TRANS_NODE_ID,
"maintenance_mode": "DISABLED",
"transport_zone_endpoints": [{
"transport_zone_id": FAKE_TZ_UUID,
"transport_zone_profile_ids": [{
"profile_id": "52035bb3-ab02-4a08-9884-18631312e50a",
"resource_type": "BfdHealthMonitoringProfile"
}]
}],
"node_id": "f5a2b5ca-8dba-11e8-9799-020039422cc8"
}

View File

@ -0,0 +1,138 @@
# Copyright 2018 VMware, Inc.
# 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 copy
import mock
from oslo_utils import uuidutils
from vmware_nsxlib.tests.unit.v3 import nsxlib_testcase
from vmware_nsxlib.tests.unit.v3 import test_constants
from vmware_nsxlib.v3 import exceptions as nsxlib_exc
from vmware_nsxlib.v3 import nsx_constants
class TestRouter(nsxlib_testcase.NsxClientTestCase):
def test_validate_tier0(self):
tier0_groups_dict = {}
tier0_uuid = uuidutils.generate_uuid()
rtr = {'edge_cluster_id': test_constants.FAKE_EDGE_CLUSTER_ID}
with mock.patch.object(self.nsxlib.router._router_client, 'get',
return_value=rtr),\
mock.patch.object(
self.nsxlib.edge_cluster, 'get',
return_value=test_constants.FAKE_EDGE_CLUSTER):
self.nsxlib.router.validate_tier0(tier0_groups_dict, tier0_uuid)
self.assertEqual(
tier0_groups_dict[tier0_uuid]['edge_cluster_uuid'],
test_constants.FAKE_EDGE_CLUSTER_ID)
self.assertEqual(
tier0_groups_dict[tier0_uuid]['member_index_list'], [0])
def test_validate_tier0_fail(self):
tier0_groups_dict = {}
tier0_uuid = uuidutils.generate_uuid()
edge_cluster = copy.copy(test_constants.FAKE_EDGE_CLUSTER)
edge_cluster['members'] = []
with mock.patch.object(self.nsxlib.router._router_client, 'get'),\
mock.patch.object(self.nsxlib.edge_cluster, 'get',
return_value=edge_cluster):
self.assertRaises(
nsxlib_exc.NsxLibInvalidInput,
self.nsxlib.router.validate_tier0,
tier0_groups_dict, tier0_uuid)
def test_add_router_link_port(self):
tags = [{'scope': 'a', 'tag': 'b'}]
tier0_uuid = uuidutils.generate_uuid()
tier1_uuid = uuidutils.generate_uuid()
with mock.patch.object(self.nsxlib.router._router_port_client,
'create') as port_create:
self.nsxlib.router.add_router_link_port(
tier1_uuid, tier0_uuid, tags)
self.assertEqual(port_create.call_count, 2)
def test_remove_router_link_port(self):
tier1_uuid = uuidutils.generate_uuid()
with mock.patch.object(
self.nsxlib.router._router_port_client, 'get_tier1_link_port',
return_value=test_constants.FAKE_ROUTER_LINKT1_PORT) as port_get,\
mock.patch.object(self.nsxlib.router._router_port_client,
'delete') as port_delete:
self.nsxlib.router.remove_router_link_port(tier1_uuid)
self.assertEqual(port_get.call_count, 1)
self.assertEqual(port_delete.call_count, 2)
def test_create_logical_router_intf_port_by_ls_id(self):
logical_router_id = uuidutils.generate_uuid()
display_name = 'dummy'
tags = []
ls_id = uuidutils.generate_uuid()
logical_switch_port_id = uuidutils.generate_uuid()
address_groups = []
with mock.patch.object(
self.nsxlib.router._router_port_client,
"get_by_lswitch_id",
side_effect=nsxlib_exc.ResourceNotFound()) as get_port,\
mock.patch.object(self.nsxlib.router._router_port_client,
"create") as create_port:
self.nsxlib.router.create_logical_router_intf_port_by_ls_id(
logical_router_id,
display_name,
tags,
ls_id,
logical_switch_port_id,
address_groups)
get_port.assert_called_once_with(ls_id)
create_port.assert_called_once_with(
logical_router_id, display_name, tags,
nsx_constants.LROUTERPORT_DOWNLINK,
logical_switch_port_id, address_groups, urpf_mode=None,
relay_service_uuid=None)
def test_add_fip_nat_rules(self):
with mock.patch.object(self.nsxlib.logical_router,
"add_nat_rule") as add_rule:
self.nsxlib.router.add_fip_nat_rules(
test_constants.FAKE_ROUTER_UUID,
'1.1.1.1', '2.2.2.2')
self.assertEqual(add_rule.call_count, 2)
def test_get_tier0_router_tz(self):
tier0_uuid = uuidutils.generate_uuid()
with mock.patch.object(self.nsxlib.router._router_client, 'get',
return_value=test_constants.FAKE_TIERO_ROUTER),\
mock.patch.object(self.nsxlib.edge_cluster, 'get',
return_value=test_constants.FAKE_EDGE_CLUSTER),\
mock.patch.object(self.nsxlib.transport_node, 'get',
return_value=test_constants.FAKE_TRANS_NODE):
tzs = self.nsxlib.router.get_tier0_router_tz(tier0_uuid)
self.assertEqual(tzs, [test_constants.FAKE_TZ_UUID])
def test_get_tier0_router_overlay_tz(self):
tier0_uuid = uuidutils.generate_uuid()
with mock.patch.object(self.nsxlib.router._router_client, 'get',
return_value=test_constants.FAKE_TIERO_ROUTER),\
mock.patch.object(self.nsxlib.edge_cluster, 'get',
return_value=test_constants.FAKE_EDGE_CLUSTER),\
mock.patch.object(self.nsxlib.transport_node, 'get',
return_value=test_constants.FAKE_TRANS_NODE),\
mock.patch.object(self.nsxlib.transport_zone, 'get_transport_type',
return_value="OVERLAY"):
tz = self.nsxlib.router.get_tier0_router_overlay_tz(tier0_uuid)
self.assertEqual(tz, test_constants.FAKE_TZ_UUID)

View File

@ -66,7 +66,7 @@ class RouterLib(object):
err_msg = _("%(act_num)s edge members found in "
"edge_cluster %(cluster_id)s, however we "
"require at least %(exp_num)s edge nodes "
"in edge cluster for use.") % {
"in edge cluster for use") % {
'act_num': len(member_index_list),
'exp_num': MIN_EDGE_NODE_NUM,
'cluster_id': edge_cluster_uuid}