Example #1
0
 def test_delete_network(self, mock_del_command, mock_connection):
     ovn_north = OvnNorth()
     ovn_north.delete_network(TestOvnNorth.NETWORK_ID10)
     assert mock_del_command.call_count == 1
     expected_del_call = mock.call(ovn_north.idl, TestOvnNorth.NETWORK_ID10,
                                   False)
     assert mock_del_command.mock_calls[0] == expected_del_call
Example #2
0
 def test_list_subnets(self, mock_connection):
     ovn_north = OvnNorth()
     result = ovn_north.list_subnets()
     assert len(result) == 2
     assert result[0]['id'] == str(TestOvnNorth.SUBNET_ID101)
     assert result[0]['network_id'] == str(TestOvnNorth.NETWORK_ID10)
     assert result[0]['tenant_id'] == tenant_id()
Example #3
0
    def test_update_localnet_network(self, mock_lsp_add_command,
                                     mock_set_command, mock_ls_get_command,
                                     mock_connection):
        mock_ls_get_command.return_value.execute.return_value = (
            TestOvnNorth.NETWORK_LOCALNET_12)
        ovn_north = OvnNorth()
        rest_data = {
            NetworkMapper.REST_NETWORK_NAME: TestOvnNorth.NETWORK_NAME12,
            NetworkMapper.REST_PROVIDER_NETWORK_TYPE:
            NetworkMapper.NETWORK_TYPE_VLAN,
            NetworkMapper.REST_PROVIDER_PHYSICAL_NETWORK:
            TestOvnNorth.LOCALNET_NAME,
            NetworkMapper.REST_PROVIDER_SEGMENTATION_ID:
            TestOvnNorth.LOCALNET_VLAN
        }
        result = ovn_north.update_network(rest_data, TestOvnNorth.NETWORK_ID12)

        self.assert_networks_equal(result, TestOvnNorth.NETWORK_LOCALNET_12)
        assert mock_set_command.call_count == 4
        assert mock_set_command.mock_calls[0] == mock.call(
            ovn_north.idl, ovnconst.TABLE_LS, TestOvnNorth.NETWORK_ID12,
            (NetworkMapper.REST_NETWORK_NAME, TestOvnNorth.NETWORK_NAME12))
        assert mock_lsp_add_command.call_count == 1
        assert mock_lsp_add_command.mock_calls[0] == mock.call(
            ovn_north.idl, str(TestOvnNorth.NETWORK_LOCALNET_12.uuid),
            ovnconst.LOCALNET_SWITCH_PORT_NAME, None, None, False)
        assert mock_ls_get_command.call_count == 2
Example #4
0
 def test_delete_port(self, mock_del_command, mock_connection):
     ovn_north = OvnNorth()
     ovn_north.delete_port(TestOvnNorth.PORT_ID01)
     assert mock_del_command.call_count == 1
     expected_del_call = mock.call(ovn_north.idl, TestOvnNorth.PORT_ID01,
                                   None, False)
     assert mock_del_command.mock_calls[0] == expected_del_call
Example #5
0
 def test_get_subnet(self, mock_connection):
     ovn_north = OvnNorth()
     result = ovn_north.get_subnet(TestOvnNorth.SUBNET_ID101)
     assert result['id'] == str(TestOvnNorth.SUBNET_ID101)
     assert result['network_id'] == str(TestOvnNorth.NETWORK_ID10)
     gateway_ip = TestOvnNorth.SUBNET_101.options['router']
     assert result['gateway_ip'] == gateway_ip
Example #6
0
 def test_list_ports(self, mock_connection):
     ovn_north = OvnNorth()
     ports = ovn_north.list_ports()
     assert len(ports) == 2
     self.assert_port_equal(ports[0], TestOvnNorth.PORT_1,
                            str(TestOvnNorth.NETWORK_ID11))
     self.assert_port_equal(ports[1], TestOvnNorth.PORT_2,
                            str(TestOvnNorth.NETWORK_ID11))
Example #7
0
 def test_delete_subnet(self, mock_del_command, mock_connection):
     ovn_north = OvnNorth()
     ovn_north.delete_subnet(TestOvnNorth.SUBNET_ID101)
     assert mock_del_command.call_count == 1
     expected_del_call = mock.call(
         ovn_north.idl,
         TestOvnNorth.SUBNET_ID101,
     )
     assert mock_del_command.mock_calls[0] == expected_del_call
Example #8
0
    def test_get_router(self, mock_lookup, mock_connection):
        mock_lookup.return_value = TestOvnNorth.ROUTER_20
        ovn_north = OvnNorth()
        result = ovn_north.get_router(str(TestOvnNorth.ROUTER_ID20))

        assert result['id'] == str(TestOvnNorth.ROUTER_ID20)
        assert result['name'] == str(TestOvnNorth.ROUTER_NAME20)

        assert mock_lookup.call_args == mock.call(
            ovnconst.TABLE_LR, str(TestOvnNorth.ROUTER_ID20))
    def test_get_networks(self, mock_ls_list, mock_connection):
        mock_ls_list.return_value.execute.return_value = TestOvnNorth.networks

        ovn_north = OvnNorth()
        result = ovn_north.list_networks()

        assert len(result) == 2
        self.assert_networks_equal(result[0], TestOvnNorth.NETWORK_10)
        self.assert_networks_equal(result[1], TestOvnNorth.NETWORK_11)
        assert mock_ls_list.call_count == 1
        assert mock_ls_list.return_value.execute.call_count == 1
Example #10
0
 def test_subnet_add_invalid_network(self, mock_connection):
     ovn_north = OvnNorth()
     rest_data = {
         SubnetMapper.REST_SUBNET_NAME: 'subnet_name',
         SubnetMapper.REST_SUBNET_CIDR: '1.1.1.0/24',
         SubnetMapper.REST_SUBNET_NETWORK_ID: 7,
         SubnetMapper.REST_SUBNET_DNS_NAMESERVERS: ['1.1.1.1'],
         SubnetMapper.REST_SUBNET_GATEWAY_IP: '1.1.1.0',
     }
     with pytest.raises(SubnetConfigError):
         ovn_north.add_subnet(rest_data)
Example #11
0
 def test_subnet_add_duplicate_network(self, mock_connection):
     ovn_north = OvnNorth()
     rest_data = {
         SubnetMapper.REST_SUBNET_NAME: 'subnet_name',
         SubnetMapper.REST_SUBNET_CIDR: '1.1.1.0/24',
         SubnetMapper.REST_SUBNET_NETWORK_ID:
         str(TestOvnNorth.NETWORK_ID10),
         SubnetMapper.REST_SUBNET_GATEWAY_IP: '1.1.1.0',
     }
     with pytest.raises(SubnetConfigError):
         ovn_north.add_subnet(rest_data)
Example #12
0
    def test_get_network(self, mock_ls_get, mock_connection):
        mock_ls_get.return_value.execute.return_value = (
            TestOvnNorth.NETWORK_10)
        ovn_north = OvnNorth()
        result = ovn_north.get_network(str(TestOvnNorth.NETWORK_ID10))

        self.assert_networks_equal(result, TestOvnNorth.NETWORK_10)
        assert mock_ls_get.call_count == 1
        assert mock_ls_get.return_value.execute.call_count == 1
        assert mock_ls_get.mock_calls[0] == mock.call(
            ovn_north.idl, str(TestOvnNorth.NETWORK_ID10))
Example #13
0
 def test_subnet_dhcp_enabled_false(self, mock_connection):
     ovn_north = OvnNorth()
     rest_data = {
         SubnetMapper.REST_SUBNET_NAME: 'subnet_name',
         SubnetMapper.REST_SUBNET_CIDR: '1.1.1.0/24',
         SubnetMapper.REST_SUBNET_NETWORK_ID: '',
         SubnetMapper.REST_SUBNET_DNS_NAMESERVERS: ['1.1.1.1'],
         SubnetMapper.REST_SUBNET_GATEWAY_IP: '1.1.1.0',
         SubnetMapper.REST_SUBNET_ENABLE_DHCP: False
     }
     with pytest.raises(UnsupportedDataValueError):
         ovn_north.add_subnet(rest_data)
Example #14
0
    def test_update_network(self, mock_set_command, mock_connection):
        ovn_north = OvnNorth()
        rest_data = {
            NetworkMapper.REST_NETWORK_NAME: TestOvnNorth.NETWORK_NAME10
        }
        result = ovn_north.update_network(rest_data, TestOvnNorth.NETWORK_ID10)

        self.assert_networks_equal(result, TestOvnNorth.NETWORK_10)
        assert mock_set_command.call_count == 1
        assert mock_set_command.mock_calls[0] == mock.call(
            ovn_north.idl, ovnconst.TABLE_LS, TestOvnNorth.NETWORK_ID10,
            (NetworkMapper.REST_NETWORK_NAME, TestOvnNorth.NETWORK_NAME10))
Example #15
0
    def test_add_network(self, mock_add_command, mock_connection):
        mock_add_command.return_value.execute.return_value = (
            TestOvnNorth.NETWORK_10)
        ovn_north = OvnNorth()
        rest_data = {
            NetworkMapper.REST_NETWORK_NAME: TestOvnNorth.NETWORK_NAME10
        }
        result = ovn_north.add_network(rest_data)

        self.assert_networks_equal(result, TestOvnNorth.NETWORK_10)
        assert mock_add_command.call_count == 1
        assert mock_add_command.mock_calls[0] == mock.call(
            ovn_north.idl, TestOvnNorth.NETWORK_NAME10, False)
Example #16
0
    def test_add_subnet(self, mock_setoptions_command, mock_add_command,
                        mock_dbset_command, mock_connection):
        add_execute = mock_add_command.return_value.execute
        add_execute.return_value = TestOvnNorth.SUBNET_102
        subnet_cidr = '1.1.1.0/24'
        ovn_north = OvnNorth()
        rest_data = {
            SubnetMapper.REST_SUBNET_NAME: 'subnet_name',
            SubnetMapper.REST_SUBNET_CIDR: subnet_cidr,
            SubnetMapper.REST_SUBNET_NETWORK_ID:
            str(TestOvnNorth.NETWORK_ID10),
            SubnetMapper.REST_SUBNET_DNS_NAMESERVERS: ['1.1.1.1'],
            SubnetMapper.REST_SUBNET_GATEWAY_IP: '1.1.1.0',
        }
        result = ovn_north.add_subnet(rest_data)
        assert result['id'] == str(TestOvnNorth.SUBNET_ID102)
        assert mock_dbset_command.call_count == 1
        assert mock_add_command.call_count == 1
        assert mock_setoptions_command.call_count == 1

        expected_dbset_call = mock.call(
            ovn_north.idl,
            ovnconst.TABLE_LS,
            str(TestOvnNorth.NETWORK_ID10),
            (ovnconst.ROW_LS_OTHER_CONFIG, {
                NetworkMapper.OVN_SUBNET: subnet_cidr
            }),
        )
        assert mock_dbset_command.mock_calls[0] == expected_dbset_call

        expected_add_call = mock.call(ovn_north.idl,
                                      subnet_cidr,
                                      ovirt_name='subnet_name',
                                      ovirt_network_id=str(
                                          TestOvnNorth.NETWORK_ID10))
        assert mock_add_command.mock_calls[0] == expected_add_call

        expected_options_call = mock.call(ovn_north.idl,
                                          TestOvnNorth.SUBNET_ID102,
                                          dns_server='1.1.1.1',
                                          lease_time=dhcp_lease_time(),
                                          router='1.1.1.0',
                                          server_id='1.1.1.0',
                                          server_mac=dhcp_server_mac(),
                                          mtu=dhcp_mtu())
        assert mock_setoptions_command.mock_calls[0] == expected_options_call
Example #17
0
    def test_add_port(self, mock_db_set, mock_add_command, mock_connection):
        mock_add_command.return_value.execute.return_value = (
            TestOvnNorth.PORT_1)
        ovn_north = OvnNorth()
        rest_data = {
            PortMapper.REST_PORT_NAME: TestOvnNorth.PORT_NAME01,
            PortMapper.REST_PORT_NETWORK_ID: str(TestOvnNorth.NETWORK_ID10),
            PortMapper.REST_PORT_DEVICE_ID: TestOvnNorth.DEVICE_ID,
            PortMapper.REST_PORT_DEVICE_OWNER: PortMapper.DEVICE_OWNER_OVIRT,
            PortMapper.REST_PORT_ADMIN_STATE_UP: True,
            PortMapper.REST_PORT_MAC_ADDRESS: TestOvnNorth.MAC_ADDRESS
        }
        result = ovn_north.add_port(rest_data)

        # ID11 because this network has the port in TestOvnNorth.networks
        self.assert_port_equal(result, TestOvnNorth.PORT_1,
                               str(TestOvnNorth.NETWORK_ID11))

        assert mock_add_command.call_count == 1
        mock_add_command.assert_called_with(ovn_north.idl,
                                            str(TestOvnNorth.NETWORK_ID10),
                                            TestOvnNorth.PORT_NAME01, None,
                                            None, False)

        assert mock_db_set.call_count == 3

        assert mock_db_set.mock_calls[0] == mock.call(
            ovn_north.idl, ovnconst.TABLE_LSP, str(TestOvnNorth.PORT_ID01),
            (ovnconst.ROW_LSP_NAME, str(TestOvnNorth.PORT_ID01)))

        assert mock_db_set.mock_calls[2] == mock.call(
            ovn_north.idl, ovnconst.TABLE_LSP, TestOvnNorth.PORT_ID01,
            (ovnconst.ROW_LSP_EXTERNAL_IDS, {
                PortMapper.OVN_DEVICE_ID: TestOvnNorth.DEVICE_ID
            }), (ovnconst.ROW_LSP_EXTERNAL_IDS, {
                PortMapper.OVN_NIC_NAME: TestOvnNorth.PORT_NAME01
            }), (ovnconst.ROW_LSP_EXTERNAL_IDS, {
                PortMapper.OVN_DEVICE_OWNER: PortMapper.DEVICE_OWNER_OVIRT
            }), (ovnconst.ROW_LSP_ENABLED, True))

        assert mock_db_set.mock_calls[4] == mock.call(
            ovn_north.idl,
            ovnconst.TABLE_LSP,
            TestOvnNorth.PORT_ID01,
            (ovnconst.ROW_LSP_ADDRESSES, [TestOvnNorth.MAC_ADDRESS]),
        )
Example #18
0
 def test_add_subnet_no_dns(self, mock_setoptions_command, mock_add_command,
                            mock_dbset_command, mock_connection):
     add_execute = mock_add_command.return_value.execute
     add_execute.return_value = TestOvnNorth.SUBNET_102
     subnet_cidr = '1.1.1.0/24'
     ovn_north = OvnNorth()
     rest_data = {
         SubnetMapper.REST_SUBNET_NAME: 'subnet_name',
         SubnetMapper.REST_SUBNET_CIDR: subnet_cidr,
         SubnetMapper.REST_SUBNET_NETWORK_ID:
         str(TestOvnNorth.NETWORK_ID10),
         SubnetMapper.REST_SUBNET_DNS_NAMESERVERS: [],
         SubnetMapper.REST_SUBNET_GATEWAY_IP: '1.1.1.0',
     }
     result = ovn_north.add_subnet(rest_data)
     assert result['id'] == str(TestOvnNorth.SUBNET_ID102)
     assert mock_dbset_command.call_count == 1
     assert mock_add_command.call_count == 1
     assert mock_setoptions_command.call_count == 1
Example #19
0
    def test_post_routers(self, mock_connection):
        nb_db = OvnNorth()
        nb_db._add_router = Mock()
        nb_db._add_router.return_value = OvnRouterRow(
            'uuid', 'router1', {
                RouterMapper.OVN_ROUTER_GATEWAY_NETWORK: 'network_id',
                RouterMapper.OVN_ROUTER_GATEWAY_SUBNET: 'subnet_id',
                RouterMapper.OVN_ROUTER_GATEWAY_IP: '1.1.1.1',
            })
        rest_input = '''{
            "router": {
                "name": "router1",
                "external_gateway_info": {
                    "enable_snat": "true",
                    "external_fixed_ips": [{
                        "ip_address": "172.24.4.6",
                        "subnet_id": "b930d7f6-ceb7-40a0-8b81-a425dd994ccf"
                    }],
                    "network_id": "ae34051f-aa6c-4c75-abf5-50dc9ac99ef3"
                }
            }
        }'''

        handler, params = SelectingHandler.get_response_handler(
            responses(), POST, ROUTERS.split('/'))
        response = handler(nb_db, rest_input, NOT_RELEVANT)

        response_json = json.loads(response.body)
        router = response_json['router']
        assert router[RouterMapper.REST_ROUTER_NAME] == 'router1'
        assert router[RouterMapper.REST_ROUTER_ID] == 'uuid'

        gateway = router[RouterMapper.REST_ROUTER_EXTERNAL_GATEWAY_INFO]
        ips = gateway[RouterMapper.REST_ROUTER_FIXED_IPS][0]
        assert gateway[RouterMapper.REST_ROUTER_NETWORK_ID] == 'network_id'
        assert ips[RouterMapper.REST_ROUTER_SUBNET_ID] == 'subnet_id'
        assert ips[RouterMapper.REST_ROUTER_IP_ADDRESS] == '1.1.1.1'

        nb_db._add_router.assert_called_once_with(
            'router1', True, 'ae34051f-aa6c-4c75-abf5-50dc9ac99ef3',
            'b930d7f6-ceb7-40a0-8b81-a425dd994ccf', '172.24.4.6')
Example #20
0
    def _port_admin_state(self, mock_connection, is_up, is_enabled, result,
                          mock_lsp_get, mock_ls_list):
        port_row = OvnPortRow(TestOvnNorth.PORT_ID01,
                              external_ids={
                                  PortMapper.OVN_NIC_NAME:
                                  TestOvnNorth.PORT_NAME01,
                                  PortMapper.OVN_DEVICE_ID:
                                  str(TestOvnNorth.PORT_ID01),
                                  PortMapper.OVN_DEVICE_OWNER:
                                  PortMapper.DEVICE_OWNER_OVIRT,
                              })
        port_row.up = is_up
        port_row.enabled = is_enabled

        mock_lsp_get.return_value.execute.return_value = port_row
        mock_ls_list.return_value.execute.return_value = [
            OvnNetworkRow(TestOvnNorth.NETWORK_ID11, ports=[port_row])
        ]

        ovn_north = OvnNorth()
        port = ovn_north.get_port(TestOvnNorth.PORT_ID01)
        assert port[PortMapper.REST_PORT_ADMIN_STATE_UP] == result
Example #21
0
 def call_response_handler(self, response_handler, content, parameters):
     if not validate_token(
             self.headers.get(TOKEN_HTTP_HEADER_FIELD_NAME, '')):
         raise Forbidden()
     with OvnNorth() as ovn_north:
         return response_handler(ovn_north, content, parameters)