Revision a95c82a9 snf-cyclades-app/synnefo/api/test/floating_ips.py

b/snf-cyclades-app/synnefo/api/test/floating_ips.py
35 35

  
36 36
from snf_django.utils.testing import BaseAPITest, mocked_quotaholder
37 37
from synnefo.db.models import FloatingIP
38
from synnefo.db.models_factory import FloatingIPFactory, NetworkFactory
38
from synnefo.db.models_factory import (FloatingIPFactory, NetworkFactory,
39
                                       VirtualMachineFactory,
40
                                       NetworkInterfaceFactory)
41
from mock import patch
39 42

  
40 43

  
41 44
URL = "/api/v1.1/os-floating-ips"
......
107 110

  
108 111
    def test_release_in_use(self):
109 112
        ip = FloatingIPFactory()
113
        vm = ip.machine
114
        vm.operstate = "ACTIVE"
115
        vm.userid = ip.userid
116
        vm.save()
117
        vm.nics.create(index=0, ipv4=ip.ipv4, network=ip.network,
118
                       state="ACTIVE")
119
        with mocked_quotaholder():
120
            response = self.delete(URL + "/%s" % ip.id, ip.userid)
121
        self.assertFault(response, 409, "conflict")
122
        # Also send a notification to remove the NIC and assert that FIP is in
123
        # use until notification from ganeti arrives
124
        request = {"removeFloatingIp": {"address": ip.ipv4}}
125
        url = "/api/v1.1/servers/%s/action" % vm.id
126
        with patch('synnefo.logic.rapi_pool.GanetiRapiClient') as c:
127
            c().ModifyInstance.return_value = 10
128
            response = self.post(url, vm.userid, json.dumps(request),
129
                                 "json")
130
        self.assertEqual(response.status_code, 202)
110 131
        with mocked_quotaholder():
111 132
            response = self.delete(URL + "/%s" % ip.id, ip.userid)
112 133
        self.assertFault(response, 409, "conflict")
......
137 158
        self.assertSuccess(response)
138 159
        self.assertEqual(json.loads(response.content)["floating_ip_pools"],
139 160
                        [{"name": str(net.id)}])
161

  
162

  
163
class FloatingIPActionsTest(BaseAPITest):
164
    def setUp(self):
165
        vm = VirtualMachineFactory()
166
        vm.operstate = "ACTIVE"
167
        vm.save()
168
        self.vm = vm
169

  
170
    def test_bad_request(self):
171
        url = "/api/v1.1/servers/%s/action" % self.vm.id
172
        response = self.post(url, self.vm.userid, json.dumps({}), "json")
173
        self.assertBadRequest(response)
174
        response = self.post(url, self.vm.userid,
175
                             json.dumps({"addFloatingIp": {}}),
176
                             "json")
177
        self.assertBadRequest(response)
178

  
179
    @patch('synnefo.logic.rapi_pool.GanetiRapiClient')
180
    def test_add_floating_ip(self, mock):
181
        # Not exists
182
        url = "/api/v1.1/servers/%s/action" % self.vm.id
183
        request = {"addFloatingIp": {"address": "10.0.0.1"}}
184
        response = self.post(url, self.vm.userid, json.dumps(request), "json")
185
        self.assertItemNotFound(response)
186
        # In use
187
        vm1 = VirtualMachineFactory()
188
        ip1 = FloatingIPFactory(userid=self.vm.userid, machine=vm1)
189
        request = {"addFloatingIp": {"address": ip1.ipv4}}
190
        response = self.post(url, self.vm.userid, json.dumps(request), "json")
191
        self.assertFault(response, 409, "conflict")
192
        # Success
193
        ip1 = FloatingIPFactory(userid=self.vm.userid, machine=None)
194
        request = {"addFloatingIp": {"address": ip1.ipv4}}
195
        response = self.post(url, self.vm.userid, json.dumps(request), "json")
196
        self.assertEqual(response.status_code, 202)
197
        ip1_after = FloatingIP.objects.get(id=ip1.id)
198
        self.assertEqual(ip1_after.machine, self.vm)
199
        self.assertTrue(ip1_after.in_use())
200

  
201
    @patch('synnefo.logic.rapi_pool.GanetiRapiClient')
202
    def test_remove_floating_ip(self, mock):
203
        # Not exists
204
        url = "/api/v1.1/servers/%s/action" % self.vm.id
205
        request = {"removeFloatingIp": {"address": "10.0.0.1"}}
206
        response = self.post(url, self.vm.userid, json.dumps(request), "json")
207
        self.assertItemNotFound(response)
208
        # Not In Use
209
        ip1 = FloatingIPFactory(userid=self.vm.userid, machine=None)
210
        request = {"removeFloatingIp": {"address": ip1.ipv4}}
211
        response = self.post(url, self.vm.userid, json.dumps(request), "json")
212
        self.assertItemNotFound(response)
213
        # Success
214
        ip1 = FloatingIPFactory(userid=self.vm.userid, machine=self.vm)
215
        NetworkInterfaceFactory(machine=self.vm, ipv4=ip1.ipv4)
216
        request = {"removeFloatingIp": {"address": ip1.ipv4}}
217
        response = self.post(url, self.vm.userid, json.dumps(request), "json")
218
        self.assertEqual(response.status_code, 202)
219
        # Yet used. Wait for the callbacks
220
        ip1_after = FloatingIP.objects.get(id=ip1.id)
221
        self.assertEqual(ip1_after.machine, self.vm)
222
        self.assertTrue(ip1_after.in_use())

Also available in: Unified diff