|
| 1 | +# Copyright 2017 Massachusetts Open Cloud Contributors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the |
| 5 | +# License. You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, |
| 10 | +# software distributed under the License is distributed on an "AS |
| 11 | +# IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | +# express or implied. See the License for the specific language |
| 13 | +# governing permissions and limitations under the License. |
| 14 | +"""Unit tests for dell switches running Dell Networking OS 9 (with REST API)""" |
| 15 | + |
| 16 | +import pytest |
| 17 | + |
| 18 | +from hil import model, api, config |
| 19 | +from hil.model import db |
| 20 | +from hil.test_common import config_testsuite, config_merge, fresh_database, \ |
| 21 | + fail_on_log_warnings, with_request_context, server_init, \ |
| 22 | + network_create_simple |
| 23 | +from hil.errors import BlockedError |
| 24 | + |
| 25 | +fail_on_log_warnings = pytest.fixture(autouse=True)(fail_on_log_warnings) |
| 26 | +fresh_database = pytest.fixture(fresh_database) |
| 27 | +server_init = pytest.fixture(server_init) |
| 28 | +with_request_context = pytest.yield_fixture(with_request_context) |
| 29 | + |
| 30 | +SWITCH_TYPE = 'http://schema.massopencloud.org/haas/v0/switches/dellnos9' |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture |
| 34 | +def configure(): |
| 35 | + """Configure HIL""" |
| 36 | + config_testsuite() |
| 37 | + config_merge({ |
| 38 | + 'auth': { |
| 39 | + 'require_authentication': 'True', |
| 40 | + }, |
| 41 | + 'extensions': { |
| 42 | + 'hil.ext.auth.null': '', |
| 43 | + 'hil.ext.switches.dellnos9': '', |
| 44 | + 'hil.ext.obm.mock': '', |
| 45 | + 'hil.ext.network_allocators.null': None, |
| 46 | + 'hil.ext.network_allocators.vlan_pool': '', |
| 47 | + }, |
| 48 | + 'hil.ext.network_allocators.vlan_pool': { |
| 49 | + 'vlans': '40-80', |
| 50 | + }, |
| 51 | + }) |
| 52 | + config.load_extensions() |
| 53 | + |
| 54 | + |
| 55 | +default_fixtures = ['fail_on_log_warnings', |
| 56 | + 'configure', |
| 57 | + 'fresh_database', |
| 58 | + 'server_init', |
| 59 | + 'with_request_context'] |
| 60 | + |
| 61 | +pytestmark = pytest.mark.usefixtures(*default_fixtures) |
| 62 | + |
| 63 | + |
| 64 | +def mock_networking_action(): |
| 65 | + """performs the required db operations and clears up the networking action |
| 66 | + queue, so that we can queue more items to test the api.the |
| 67 | +
|
| 68 | + This is useful because calling deferred.apply_networking would require a |
| 69 | + real switch |
| 70 | + """ |
| 71 | + action = db.session.query(model.NetworkingAction) \ |
| 72 | + .order_by(model.NetworkingAction.id).one_or_none() |
| 73 | + |
| 74 | + if action.new_network is None: |
| 75 | + db.session.query(model.NetworkAttachment) \ |
| 76 | + .filter_by(nic=action.nic, channel=action.channel)\ |
| 77 | + .delete() |
| 78 | + else: |
| 79 | + db.session.add(model.NetworkAttachment( |
| 80 | + nic=action.nic, |
| 81 | + network=action.new_network, |
| 82 | + channel=action.channel)) |
| 83 | + |
| 84 | + db.session.delete(action) |
| 85 | + db.session.commit() |
| 86 | + |
| 87 | + |
| 88 | +def test_ensure_legal_operations(): |
| 89 | + """Test to ensure that ensure_legal_operations works as expected""" |
| 90 | + |
| 91 | + # create a project and a network |
| 92 | + api.project_create('anvil-nextgen') |
| 93 | + network_create_simple('hammernet', 'anvil-nextgen') |
| 94 | + network_create_simple('pineapple', 'anvil-nextgen') |
| 95 | + |
| 96 | + # register a switch of type dellnos9 and add a port to it |
| 97 | + api.switch_register('s3048', |
| 98 | + type=SWITCH_TYPE, |
| 99 | + username="switch_user", |
| 100 | + password="switch_pass", |
| 101 | + hostname="switchname", |
| 102 | + interface_type="GigabitEthernet") |
| 103 | + api.switch_register_port('s3048', '1/3') |
| 104 | + switch = api._must_find(model.Switch, 's3048') |
| 105 | + |
| 106 | + # register a ndoe and a nic |
| 107 | + api.node_register('compute-01', obm={ |
| 108 | + "type": "http://schema.massopencloud.org/haas/v0/obm/mock", |
| 109 | + "host": "ipmihost", |
| 110 | + "user": "root", |
| 111 | + "password": "tapeworm"}) |
| 112 | + api.project_connect_node('anvil-nextgen', 'compute-01') |
| 113 | + api.node_register_nic('compute-01', 'eth0', 'DE:AD:BE:EF:20:14') |
| 114 | + nic = api._must_find(model.Nic, 'eth0') |
| 115 | + |
| 116 | + api.port_connect_nic('s3048', '1/3', 'compute-01', 'eth0') |
| 117 | + |
| 118 | + # connecting a trunked network wihtout having a native should fail. |
| 119 | + # call the method directly and test the API too. |
| 120 | + with pytest.raises(BlockedError): |
| 121 | + switch.ensure_legal_operation(nic, 'connect', 'vlan/1212') |
| 122 | + |
| 123 | + with pytest.raises(BlockedError): |
| 124 | + api.node_connect_network('compute-01', 'eth0', 'hammernet', 'vlan/40') |
| 125 | + |
| 126 | + # doing these operations in the correct order, that is native network first |
| 127 | + # and then trunked, should work. |
| 128 | + api.node_connect_network('compute-01', 'eth0', 'hammernet', 'vlan/native') |
| 129 | + mock_networking_action() |
| 130 | + api.node_connect_network('compute-01', 'eth0', 'pineapple', 'vlan/41') |
| 131 | + mock_networking_action() |
| 132 | + |
| 133 | + # removing these networks in the wrong order should not work. |
| 134 | + with pytest.raises(BlockedError): |
| 135 | + switch.ensure_legal_operation(nic, 'detach', 'vlan/native') |
| 136 | + |
| 137 | + with pytest.raises(BlockedError): |
| 138 | + api.node_detach_network('compute-01', 'eth0', 'hammernet') |
| 139 | + |
| 140 | + # removing networks in the right order should work |
| 141 | + api.node_detach_network('compute-01', 'eth0', 'pineapple') |
| 142 | + mock_networking_action() |
| 143 | + api.node_detach_network('compute-01', 'eth0', 'hammernet') |
| 144 | + mock_networking_action() |
| 145 | + db.session.close() |
0 commit comments