Revision 910d960d snf-cyclades-app/synnefo/api/tests/servers.py

b/snf-cyclades-app/synnefo/api/tests/servers.py
858 858
        response = self.mypost('servers/%d/action' % vm.id,
859 859
                               vm.userid, data, 'json')
860 860
        self.assertBadRequest(response)
861

  
862

  
863
@patch('synnefo.logic.rapi_pool.GanetiRapiClient')
864
class ServerAttachments(ComputeAPITest):
865
    def test_list_attachments(self, mrapi):
866
        # Test default volume
867
        vol = mfactory.VolumeFactory()
868
        vm = vol.machine
869

  
870
        response = self.myget("servers/%d/os-volume_attachments" % vm.id,
871
                              vm.userid)
872
        self.assertSuccess(response)
873
        attachments = json.loads(response.content)
874
        self.assertEqual(len(attachments), 1)
875
        self.assertEqual(attachments["volumeAttachments"][0],
876
                         {"volumeId": vol.id,
877
                          "serverId": vm.id,
878
                          "id": vol.id,
879
                          "device": ""})
880

  
881
        # Test deleted Volume
882
        dvol = mfactory.VolumeFactory(machine=vm, deleted=True)
883
        response = self.myget("servers/%d/os-volume_attachments" % vm.id,
884
                              vm.userid)
885
        self.assertSuccess(response)
886
        attachments = json.loads(response.content)["volumeAttachments"]
887
        self.assertEqual(len([d for d in attachments if d["id"] == dvol.id]),
888
                         0)
889

  
890
    def test_attach_detach_volume(self, mrapi):
891
        vol = mfactory.VolumeFactory(status="AVAILABLE")
892
        vm = vol.machine
893
        disk_template = vm.flavor.disk_template
894
        # Test that we cannot detach the root volume
895
        response = self.mydelete("servers/%d/os-volume_attachments/%d" %
896
                                 (vm.id, vol.id), vm.userid)
897
        self.assertBadRequest(response)
898

  
899
        # Test that we cannot attach a used volume
900
        vol1 = mfactory.VolumeFactory(status="IN_USE",
901
                                      disk_template=disk_template,
902
                                      userid=vm.userid)
903
        request = json.dumps({"volumeAttachment": {"volumeId": vol1.id}})
904
        response = self.mypost("servers/%d/os-volume_attachments" %
905
                               vm.id, vm.userid,
906
                               request, "json")
907
        self.assertBadRequest(response)
908

  
909
        # We cannot attach a volume of different disk template
910
        vol1.status = "AVAILABLE"
911
        vol1.disk_template = "lalalal"
912
        vol1.save()
913
        response = self.mypost("servers/%d/os-volume_attachments/" %
914
                               vm.id, vm.userid,
915
                               request, "json")
916
        self.assertBadRequest(response)
917

  
918
        vol1.disk_template = disk_template
919
        vol1.save()
920
        mrapi().ModifyInstance.return_value = 43
921
        response = self.mypost("servers/%d/os-volume_attachments" %
922
                               vm.id, vm.userid,
923
                               request, "json")
924
        self.assertEqual(response.status_code, 202, response.content)
925
        attachment = json.loads(response.content)["volumeAttachment"]
926
        self.assertEqual(attachment, {"volumeId": vol1.id,
927
                                      "serverId": vm.id,
928
                                      "id": vol1.id,
929
                                      "device": ""})
930
        # And we delete it...will fail because of status
931
        response = self.mydelete("servers/%d/os-volume_attachments/%d" %
932
                                 (vm.id, vol1.id), vm.userid)
933
        self.assertBadRequest(response)
934
        vm.task = None
935
        vm.save()
936
        vm.volumes.all().update(status="IN_USE")
937
        response = self.mydelete("servers/%d/os-volume_attachments/%d" %
938
                                 (vm.id, vol1.id), vm.userid)
939
        self.assertEqual(response.status_code, 202, response.content)

Also available in: Unified diff