summaryrefslogtreecommitdiff
path: root/nova/tests/unit/api/openstack/compute/test_networks.py
blob: 595353e7b1dff35da6da873f9b8abd01a26c3562 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# Copyright 2011 Grid Dynamics
# Copyright 2011 OpenStack Foundation
# 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

from oslo_utils.fixture import uuidsentinel as uuids
import webob

from nova.api.openstack.compute import networks as networks_v21
from nova import exception
from nova.network import neutron
from nova import test
from nova.tests.unit.api.openstack import fakes


# NOTE(stephenfin): obviously these aren't complete reponses, but this is all
# we care about
FAKE_NETWORKS = [
    {
        'id': uuids.private,
        'name': 'private',
    },
    {
        'id': uuids.public,
        'name': 'public',
    },
]

USER_RESPONSE_TEMPLATE = {
    field: None for field in (
        'id', 'cidr', 'netmask', 'gateway', 'broadcast', 'dns1', 'dns2',
        'cidr_v6', 'gateway_v6', 'label', 'netmask_v6')
}

ADMIN_RESPONSE_TEMPLATE = {
    field: None for field in (
        'id', 'cidr', 'netmask', 'gateway', 'broadcast', 'dns1', 'dns2',
        'cidr_v6', 'gateway_v6', 'label', 'netmask_v6', 'created_at',
        'updated_at', 'deleted_at', 'deleted', 'injected', 'bridge', 'vlan',
        'vpn_public_address', 'vpn_public_port', 'vpn_private_address',
        'dhcp_start', 'project_id', 'host', 'bridge_interface', 'multi_host',
        'priority', 'rxtx_base', 'mtu', 'dhcp_server', 'enable_dhcp',
        'share_address')
}


class FakeNetworkAPI(object):

    def __init__(self):
        self.networks = copy.deepcopy(FAKE_NETWORKS)

    def get_all(self, context):
        return self.networks

    def get(self, context, network_id):
        for network in self.networks:
            if network['id'] == network_id:
                return network
        raise exception.NetworkNotFound(network_id=network_id)


class NetworksTestV21(test.NoDBTestCase):
    validation_error = exception.ValidationError

    def setUp(self):
        super(NetworksTestV21, self).setUp()
        # TODO(stephenfin): Consider using then NeutronFixture here
        self.fake_network_api = FakeNetworkAPI()
        self._setup()
        fakes.stub_out_networking(self)
        self.non_admin_req = fakes.HTTPRequest.blank(
            '', project_id=fakes.FAKE_PROJECT_ID)
        self.admin_req = fakes.HTTPRequest.blank(
            '', project_id=fakes.FAKE_PROJECT_ID, use_admin_context=True)

    def _setup(self):
        self.controller = networks_v21.NetworkController(
            self.fake_network_api)
        self.neutron_ctrl = networks_v21.NetworkController(
            neutron.API())
        self.req = fakes.HTTPRequest.blank('',
                                           project_id=fakes.FAKE_PROJECT_ID)

    def _check_status(self, res, method, code):
        self.assertEqual(method.wsgi_code, code)

    def test_network_list_all_as_user(self):
        res_dict = self.controller.index(self.non_admin_req)

        expected = []
        for fake_network in FAKE_NETWORKS:
            expected_ = copy.deepcopy(USER_RESPONSE_TEMPLATE)
            expected_['id'] = fake_network['id']
            expected_['label'] = fake_network['name']
            expected.append(expected_)

        self.assertEqual({'networks': expected}, res_dict)

    def test_network_list_all_as_admin(self):
        res_dict = self.controller.index(self.admin_req)

        expected = []
        for fake_network in FAKE_NETWORKS:
            expected_ = copy.deepcopy(ADMIN_RESPONSE_TEMPLATE)
            expected_['id'] = fake_network['id']
            expected_['label'] = fake_network['name']
            expected.append(expected_)

        self.assertEqual({'networks': expected}, res_dict)

    def test_network_get_as_user(self):
        uuid = FAKE_NETWORKS[0]['id']
        res_dict = self.controller.show(self.non_admin_req, uuid)

        expected = copy.deepcopy(USER_RESPONSE_TEMPLATE)
        expected['id'] = FAKE_NETWORKS[0]['id']
        expected['label'] = FAKE_NETWORKS[0]['name']

        self.assertEqual({'network': expected}, res_dict)

    def test_network_get_as_admin(self):
        uuid = FAKE_NETWORKS[0]['id']
        res_dict = self.controller.show(self.admin_req, uuid)

        expected = copy.deepcopy(ADMIN_RESPONSE_TEMPLATE)
        expected['id'] = FAKE_NETWORKS[0]['id']
        expected['label'] = FAKE_NETWORKS[0]['name']

        self.assertEqual({'network': expected}, res_dict)

    def test_network_get_not_found(self):
        self.assertRaises(webob.exc.HTTPNotFound,
                          self.controller.show, self.req, uuids.invalid)


class NetworksDeprecationTest(test.NoDBTestCase):

    def setUp(self):
        super(NetworksDeprecationTest, self).setUp()
        self.controller = networks_v21.NetworkController()
        self.req = fakes.HTTPRequest.blank('', version='2.36')

    def test_all_api_return_not_found(self):
        self.assertRaises(exception.VersionNotFoundForAPIMethod,
            self.controller.show, self.req, fakes.FAKE_UUID)
        self.assertRaises(exception.VersionNotFoundForAPIMethod,
            self.controller.index, self.req)