1
# Copyright 2011 OpenStack LLC.
4
# Licensed under the Apache License, Version 2.0 (the "License"); you may
5
# not use this file except in compliance with the License. You may obtain
6
# a copy of the License at
8
# http://www.apache.org/licenses/LICENSE-2.0
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
# License for the specific language governing permissions and limitations
20
from nova import compute
21
from nova import context
23
from nova.tests.api.openstack import fakes
26
UUID = '70f6db34-de8d-4fbd-aafb-4065bdfa6114'
27
last_add_fixed_ip = (None, None)
28
last_remove_fixed_ip = (None, None)
31
def compute_api_add_fixed_ip(self, context, instance, network_id):
32
global last_add_fixed_ip
34
last_add_fixed_ip = (instance['uuid'], network_id)
37
def compute_api_remove_fixed_ip(self, context, instance, address):
38
global last_remove_fixed_ip
40
last_remove_fixed_ip = (instance['uuid'], address)
43
def compute_api_get(self, context, instance_id):
44
return {'id': 1, 'uuid': instance_id}
47
class FixedIpTest(test.TestCase):
49
super(FixedIpTest, self).setUp()
50
fakes.stub_out_networking(self.stubs)
51
fakes.stub_out_rate_limiting(self.stubs)
52
self.stubs.Set(compute.api.API, "add_fixed_ip",
53
compute_api_add_fixed_ip)
54
self.stubs.Set(compute.api.API, "remove_fixed_ip",
55
compute_api_remove_fixed_ip)
56
self.stubs.Set(compute.api.API, 'get', compute_api_get)
57
self.context = context.get_admin_context()
59
def test_add_fixed_ip(self):
60
global last_add_fixed_ip
61
last_add_fixed_ip = (None, None)
63
body = dict(addFixedIp=dict(networkId='test_net'))
64
req = webob.Request.blank('/v2/123/servers/%s/action' % UUID)
66
req.body = json.dumps(body)
67
req.headers['content-type'] = 'application/json'
69
resp = req.get_response(fakes.wsgi_app())
70
self.assertEqual(resp.status_int, 202)
71
self.assertEqual(last_add_fixed_ip, (UUID, 'test_net'))
73
def test_add_fixed_ip_no_network(self):
74
global last_add_fixed_ip
75
last_add_fixed_ip = (None, None)
77
body = dict(addFixedIp=dict())
78
req = webob.Request.blank('/v2/123/servers/%s/action' % UUID)
80
req.body = json.dumps(body)
81
req.headers['content-type'] = 'application/json'
83
resp = req.get_response(fakes.wsgi_app())
84
self.assertEqual(resp.status_int, 422)
85
self.assertEqual(last_add_fixed_ip, (None, None))
87
def test_remove_fixed_ip(self):
88
global last_remove_fixed_ip
89
last_remove_fixed_ip = (None, None)
91
body = dict(removeFixedIp=dict(address='10.10.10.1'))
92
req = webob.Request.blank('/v2/123/servers/%s/action' % UUID)
94
req.body = json.dumps(body)
95
req.headers['content-type'] = 'application/json'
97
resp = req.get_response(fakes.wsgi_app())
98
self.assertEqual(resp.status_int, 202)
99
self.assertEqual(last_remove_fixed_ip, (UUID, '10.10.10.1'))
101
def test_remove_fixed_ip_no_address(self):
102
global last_remove_fixed_ip
103
last_remove_fixed_ip = (None, None)
105
body = dict(removeFixedIp=dict())
106
req = webob.Request.blank('/v2/123/servers/%s/action' % UUID)
108
req.body = json.dumps(body)
109
req.headers['content-type'] = 'application/json'
111
resp = req.get_response(fakes.wsgi_app())
112
self.assertEqual(resp.status_int, 422)
113
self.assertEqual(last_remove_fixed_ip, (None, None))