Revision b66e4833 snf-cyclades-app/synnefo/api/tests.py

b/snf-cyclades-app/synnefo/api/tests.py
249 249
        """Test if a server details are returned."""
250 250
        db_vm = self.vm2
251 251
        user = self.vm2.userid
252
        net = mfactory.NetworkFactory()
253
        nic = mfactory.NetworkInterfaceFactory(machine=self.vm2, network=net)
254

  
252 255
        db_vm_meta = mfactory.VirtualMachineMetadataFactory(vm=db_vm)
253 256

  
254 257
        response = self.get('/api/v1.1/servers/%d' % db_vm.id, user)
......
260 263
        self.assertEqual(server['imageRef'], db_vm.imageid)
261 264
        self.assertEqual(server['name'], db_vm.name)
262 265
        self.assertEqual(server['status'], get_rsapi_state(db_vm))
266
        api_nic = server['attachments']['values'][0]
267
        self.assertEqual(api_nic['network_id'], str(net.id))
268
        self.assertEqual(api_nic['mac_address'], nic.mac)
269
        self.assertEqual(api_nic['firewallProfile'], nic.firewall_profile)
270
        self.assertEqual(api_nic['ipv4'], nic.ipv4)
271
        self.assertEqual(api_nic['ipv6'], nic.ipv6)
272
        self.assertEqual(api_nic['id'], 'nic-%s-%s' % (db_vm.id, nic.index))
263 273

  
264 274
        metadata = server['metadata']['values']
265 275
        self.assertEqual(len(metadata), 1)
......
285 295
        response = self.post('/api/v1.1/servers', params={})
286 296
        self.assertBadRequest(response)
287 297

  
298
    def test_rename_server(self):
299
        vm = self.vm2
300
        request = {'server': {'name': 'new_name'}}
301
        response = self.put('/api/v1.1/servers/%d' % vm.id, vm.userid,
302
                            json.dumps(request), 'json')
303
        self.assertSuccess(response)
304
        self.assertEqual(VirtualMachine.objects.get(id=vm.id).name, "new_name")
305

  
288 306

  
289 307
@patch('synnefo.api.util.get_image')
290 308
@patch('synnefo.logic.rapi_pool.GanetiRapiClient')
......
359 377
        vm = mfactory.VirtualMachineFactory()
360 378
        response = self.delete('/api/v1.1/servers/%d' % 42, vm.userid)
361 379
        self.assertItemNotFound(response)
362
        mrapi().DeleteInstance.assert_not_called()
380
        self.assertFalse(mrapi.mock_calls)
363 381

  
364 382

  
365 383
@patch('synnefo.api.util.get_image')
......
383 401
                self.assertEqual(VirtualMachine.objects.get(id=vm.id).action,
384 402
                                 action.upper())
385 403

  
404
    def test_action_in_building_vm(self, mrapi, mimage):
405
        """Test building in progress"""
406
        vm = mfactory.VirtualMachineFactory()
407
        request = {'start': '{}'}
408
        response = self.post('/api/v1.1/servers/%d/action' % vm.id,
409
                             vm.userid, json.dumps(request), 'json')
410
        self.assertEqual(response.status_code, 409)
411
        self.assertFalse(mrapi.mock_calls)
412

  
413
    def test_destroy_build_vm(self, mrapi, mimage):
414
        """Test building in progress"""
415
        vm = mfactory.VirtualMachineFactory()
416
        response = self.delete('/api/v1.1/servers/%d' % vm.id,
417
                             vm.userid)
418
        self.assertSuccess(response)
419
        mrapi().RemoveInstance.assert_called_once()
420

  
386 421
    def test_firewall(self, mrapi, mimage):
387 422
        vm = mfactory.VirtualMachineFactory()
388 423
        vm.operstate = "STOPPED"
......
393 428
        self.assertEqual(response.status_code, 202)
394 429
        mrapi().ModifyInstance.assert_called_once()
395 430

  
431
    def test_unsupported_firewall(self, mrapi, mimage):
432
        vm = mfactory.VirtualMachineFactory()
433
        vm.operstate = "STOPPED"
434
        vm.save()
435
        request = {'firewallProfile': {'profile': 'FOO'}}
436
        response = self.post('/api/v1.1/servers/%d/action' % vm.id,
437
                             vm.userid, json.dumps(request), 'json')
438
        self.assertBadRequest(response)
439
        self.assertFalse(mrapi.mock_calls)
440

  
396 441

  
397 442
class ServerMetadataAPITest(BaseAPITest):
398 443
    def setUp(self):
......
590 635
        response = self.delete('/api/v1.1/networks/%d' % net.id,
591 636
                                self.net2.userid)
592 637
        self.assertFault(response, 403, 'forbidden')
593
        mrapi.DeleteNetwork.assert_not_called()
638
        self.assertFalse(mrapi.mock_calls)
639

  
640
    def test_delete_network_in_use(self, mrapi):
641
        net = mfactory.NetworkFactory(deleted=False)
642
        vm = mfactory.VirtualMachineFactory()
643
        mfactory.NetworkInterfaceFactory(machine=vm, network=net)
644
        response = self.delete('/api/v1.1/networks/%d' % net.id,
645
                                net.userid)
646
        self.assertFault(response, 421, 'networkInUse')
647
        self.assertFalse(mrapi.mock_calls)
594 648

  
595 649
    def test_add_nic(self, mrapi):
596 650
        user = 'userr'
......
601 655
                             net.userid, json.dumps(request), 'json')
602 656
        self.assertEqual(response.status_code, 202)
603 657

  
658
    def test_add_nic_to_public_network(self, mrapi):
659
        user = 'userr'
660
        vm = mfactory.VirtualMachineFactory(name='yo', userid=user)
661
        net = mfactory.NetworkFactory(state='ACTIVE', userid=user, public=True)
662
        request = {'add': {'serverRef': vm.id}}
663
        response = self.post('/api/v1.1/networks/%d/action' % net.id,
664
                             net.userid, json.dumps(request), 'json')
665
        self.assertFault(response, 403, 'forbidden')
666

  
604 667
    def test_add_nic_malformed(self, mrapi):
605 668
        user = 'userr'
606 669
        vm = mfactory.VirtualMachineFactory(name='yo', userid=user)
......
610 673
                             net.userid, json.dumps(request), 'json')
611 674
        self.assertBadRequest(response)
612 675

  
676
    def test_add_nic_not_active(self, mrapi):
677
        """Test connecting VM to non-active network"""
678
        user = 'dummy'
679
        vm = mfactory.VirtualMachineFactory(name='yo', userid=user)
680
        net = mfactory.NetworkFactory(state='PENDING', subnet='10.0.0.0/31',
681
                                      userid=user)
682
        request = {'add': {'serveRef': vm.id}}
683
        response = self.post('/api/v1.1/networks/%d/action' % net.id,
684
                             net.userid, json.dumps(request), 'json')
685
        # Test that returns BuildInProgress
686
        self.assertEqual(response.status_code, 409)
687

  
688
    def test_add_nic_full_network(self, mrapi):
689
        """Test connecting VM to a full network"""
690
        user = 'userr'
691
        vm = mfactory.VirtualMachineFactory(name='yo', userid=user)
692
        net = mfactory.NetworkFactory(state='ACTIVE', subnet='10.0.0.0/30',
693
                                      userid=user, dhcp=True)
694
        pool = net.get_pool()
695
        while not pool.empty():
696
            pool.get()
697
        pool.save()
698
        pool = net.get_pool()
699
        self.assertTrue(pool.empty())
700
        request = {'add': {'serverRef': vm.id}}
701
        response = self.post('/api/v1.1/networks/%d/action' % net.id,
702
                             net.userid, json.dumps(request), 'json')
703
        # Test that returns OverLimit
704
        self.assertEqual(response.status_code, 413)
705

  
613 706
    def test_remove_nic(self, mrapi):
614 707
        user = 'userr'
615 708
        vm = mfactory.VirtualMachineFactory(name='yo', userid=user)
......
637 730
                             net.userid, json.dumps(request), 'json')
638 731
        self.assertBadRequest(response)
639 732

  
733
    def test_remove_nic_malformed_2(self, mrapi):
734
        user = 'userr'
735
        vm = mfactory.VirtualMachineFactory(name='yo', userid=user)
736
        net = mfactory.NetworkFactory(state='ACTIVE', userid=user)
737
        request = {'remove':
738
                    {'attachment': 'nic-%s' % vm.id}
739
                  }
740
        response = self.post('/api/v1.1/networks/%d/action' % net.id,
741
                             net.userid, json.dumps(request), 'json')
742
        self.assertBadRequest(response)
743

  
640 744

  
641 745
class ServerVNCConsole(BaseAPITest):
642 746

  
......
665 769
        self.assertEqual(set(console.keys()),
666 770
                         set(['type', 'host', 'port', 'password']))
667 771

  
772
    def test_wrong_console_type(self):
773
        """Test console req for ACTIVE server"""
774
        vm = mfactory.VirtualMachineFactory()
775
        vm.operstate = 'STARTED'
776
        vm.save()
777

  
778
        data = json.dumps({'console': {'type': 'foo'}})
779
        response = self.post('/api/v1.1/servers/%d/action' % vm.id,
780
                             vm.userid, data, 'json')
781
        self.assertBadRequest(response)
668 782

  
669 783
def assert_backend_closed(func):
670 784
    @wraps(func)
......
846 960
                            {'foo': 'bar', 'foo2': 'bar2', 'foo3': 'bar3'}})
847 961

  
848 962
    @assert_backend_closed
963
    def test_create_metadata_malformed_1(self, backend):
964
        backend.return_value.get_image.return_value = self.image
965
        with patch("synnefo.api.images.ImageBackend"):
966
                request = {'met': {'foo3': 'bar3'}}
967
                response = self.put('/api/v1.1/images/42/meta/foo3', 'user',
968
                                    json.dumps(request), 'json')
969
                self.assertBadRequest(response)
970

  
971
    @assert_backend_closed
972
    def test_create_metadata_malformed_2(self, backend):
973
        backend.return_value.get_image.return_value = self.image
974
        with patch("synnefo.api.images.ImageBackend"):
975
                request = {'meta': [('foo3', 'bar3')]}
976
                response = self.put('/api/v1.1/images/42/meta/foo3', 'user',
977
                                    json.dumps(request), 'json')
978
                self.assertBadRequest(response)
979

  
980
    @assert_backend_closed
981
    def test_create_metadata_malformed_3(self, backend):
982
        backend.return_value.get_image.return_value = self.image
983
        with patch("synnefo.api.images.ImageBackend"):
984
            request = {'met': {'foo3': 'bar3', 'foo4': 'bar4'}}
985
            response = self.put('/api/v1.1/images/42/meta/foo3', 'user',
986
                                    json.dumps(request), 'json')
987
            self.assertBadRequest(response)
988

  
989
    @assert_backend_closed
990
    def test_create_metadata_malformed_4(self, backend):
991
        backend.return_value.get_image.return_value = self.image
992
        with patch("synnefo.api.images.ImageBackend"):
993
            request = {'met': {'foo3': 'bar3'}}
994
            response = self.put('/api/v1.1/images/42/meta/foo4', 'user',
995
                                    json.dumps(request), 'json')
996
            self.assertBadRequest(response)
997

  
998
    @assert_backend_closed
849 999
    def test_update_metadata_item(self, backend):
850 1000
        backend.return_value.get_image.return_value = self.image
851 1001
        with patch("synnefo.api.images.ImageBackend") as m:
......
858 1008
                            {'foo': 'bar_new', 'foo2': 'bar2', 'foo4': 'bar4'}
859 1009
                        })
860 1010

  
1011
    @assert_backend_closed
1012
    def test_update_metadata_malformed(self, backend):
1013
        backend.return_value.get_image.return_value = self.image
1014
        with patch("synnefo.api.images.ImageBackend"):
1015
                request = {'meta': {'foo': 'bar_new', 'foo4': 'bar4'}}
1016
                response = self.post('/api/v1.1/images/42/meta', 'user',
1017
                                    json.dumps(request), 'json')
1018
                self.assertBadRequest(response)
1019

  
861 1020

  
862 1021
class APITest(TestCase):
863 1022
    def test_api_version(self):

Also available in: Unified diff