Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / volume / tests / volumes.py @ fad9d098

History | View | Annotate | Download (6.2 kB)

1
from snf_django.utils.testing import BaseAPITest
2
#from synnefo.db.models import Volume
3
from synnefo.db import models_factory as mf
4
from synnefo.volume import volumes
5
from snf_django.lib.api import faults
6
from mock import patch
7
from copy import deepcopy
8

    
9

    
10
@patch("synnefo.logic.rapi_pool.GanetiRapiClient")
11
class VolumesTest(BaseAPITest):
12
    def setUp(self):
13
        self.userid = "test_user"
14
        self.size = 1
15
        self.vm = mf.VirtualMachineFactory(
16
            userid=self.userid,
17
            flavor__disk_template="ext_archipelago")
18
        self.kwargs = {"user_id": self.userid,
19
                       "size": self.size,
20
                       "server_id": self.vm.id}
21

    
22
    def test_create(self, mrapi):
23
        # No server id
24
        kwargs = deepcopy(self.kwargs)
25
        kwargs["server_id"] = None
26
        self.assertRaises(faults.BadRequest,
27
                          volumes.create,
28
                          **kwargs)
29

    
30
        # Invalid server
31
        vm = mf.VirtualMachineFactory(userid="other_user")
32
        kwargs["server_id"] = vm.id
33
        self.assertRaises(faults.BadRequest,
34
                          volumes.create,
35
                          **kwargs)
36

    
37
        # Create server without source!
38
        mrapi().ModifyInstance.return_value = 42
39
        vol = volumes.create(**self.kwargs)
40

    
41
        self.assertEqual(vol.size, self.size)
42
        self.assertEqual(vol.userid, self.userid)
43
        self.assertEqual(vol.name, None)
44
        self.assertEqual(vol.description, None)
45
        self.assertEqual(vol.source_snapshot_id, None)
46
        self.assertEqual(vol.source, None)
47
        self.assertEqual(vol.origin, None)
48
        self.assertEqual(vol.source_volume_id, None)
49
        self.assertEqual(vol.source_image_id, None)
50
        self.assertEqual(vol.machine, self.vm)
51

    
52
        name, args, kwargs = mrapi().ModifyInstance.mock_calls[0]
53
        self.assertEqual(kwargs["instance"], self.vm.backend_vm_id)
54
        disk_info = kwargs["disks"][0][2]
55
        self.assertEqual(disk_info["size"], self.size)
56
        self.assertEqual(disk_info["name"], vol.backend_volume_uuid)
57
        self.assertEqual(disk_info["volume_name"], vol.backend_volume_uuid)
58
        self.assertFalse("origin" in disk_info)
59

    
60
    def test_create_from_volume(self, mrapi):
61
        # Check permissions
62
        svol = mf.VolumeFactory(userid="other_user")
63
        self.assertRaises(faults.BadRequest,
64
                          volumes.create,
65
                          source_volume_id=svol.id,
66
                          **self.kwargs)
67
        # Invalid volume status
68
        svol = mf.VolumeFactory(userid=self.userid, status="CREATING")
69
        self.assertRaises(faults.BadRequest,
70
                          volumes.create,
71
                          source_volume_id=svol.id,
72
                          **self.kwargs)
73

    
74
        svol.status = "AVAILABLE"
75
        svol.save()
76
        mrapi().ModifyInstance.return_value = 42
77
        vol = volumes.create(source_volume_id=svol.id, **self.kwargs)
78

    
79
        self.assertEqual(vol.size, self.size)
80
        self.assertEqual(vol.userid, self.userid)
81
        self.assertEqual(vol.name, None)
82
        self.assertEqual(vol.description, None)
83
        self.assertEqual(vol.source, "volume:%s" % svol.id)
84
        self.assertEqual(vol.origin, svol.backend_volume_uuid)
85

    
86
        name, args, kwargs = mrapi().ModifyInstance.mock_calls[0]
87
        self.assertEqual(kwargs["instance"], self.vm.backend_vm_id)
88
        disk_info = kwargs["disks"][0][2]
89
        self.assertEqual(disk_info["size"], self.size)
90
        self.assertEqual(disk_info["name"], vol.backend_volume_uuid)
91
        self.assertEqual(disk_info["volume_name"], vol.backend_volume_uuid)
92
        self.assertEqual(disk_info["origin"], svol.backend_volume_uuid)
93

    
94
    @patch("synnefo.plankton.backend.ImageBackend")
95
    def test_create_from_snapshot(self, mimage, mrapi):
96
        # Wrong source
97
        mimage().get_snapshot.side_effect = faults.ItemNotFound
98
        self.assertRaises(faults.BadRequest,
99
                          volumes.create,
100
                          source_snapshot_id=421,
101
                          **self.kwargs)
102

    
103
        mimage().get_snapshot.side_effect = None
104
        mimage().get_snapshot.return_value = {
105
            'location': 'pithos://foo',
106
            'checksum': 'snf-snapshot-43',
107
            'id': 12,
108
            'name': "test_image",
109
            'size': 1242,
110
            'disk_format': 'diskdump',
111
            'properties': {'source_volume': 42}}
112

    
113
        mrapi().ModifyInstance.return_value = 42
114
        vol = volumes.create(source_snapshot_id=12, **self.kwargs)
115

    
116
        self.assertEqual(vol.size, self.size)
117
        self.assertEqual(vol.userid, self.userid)
118
        self.assertEqual(vol.name, None)
119
        self.assertEqual(vol.description, None)
120
        self.assertEqual(int(vol.source_snapshot_id), 12)
121
        self.assertEqual(vol.source_volume_id, None)
122
        self.assertEqual(vol.source_image_id, None)
123
        self.assertEqual(vol.origin, "snf-snapshot-43")
124

    
125
        name, args, kwargs = mrapi().ModifyInstance.mock_calls[0]
126
        self.assertEqual(kwargs["instance"], self.vm.backend_vm_id)
127
        disk_info = kwargs["disks"][0][2]
128
        self.assertEqual(disk_info["size"], self.size)
129
        self.assertEqual(disk_info["name"], vol.backend_volume_uuid)
130
        self.assertEqual(disk_info["volume_name"], vol.backend_volume_uuid)
131
        self.assertEqual(disk_info["origin"], "snf-snapshot-43")
132

    
133
    def test_delete(self, mrapi):
134
        # We can not deleted detached volumes
135
        vol = mf.VolumeFactory(machine=None)
136
        self.assertRaises(faults.BadRequest,
137
                          volumes.delete,
138
                          vol)
139

    
140
        vm = mf.VirtualMachineFactory()
141
        # Also we cannot delete root volume
142
        vol.index = 0
143
        vol.machine = vm
144
        self.assertRaises(faults.BadRequest,
145
                          volumes.delete,
146
                          vol)
147

    
148
        # We can delete everything else
149
        vol.index = 1
150
        mrapi().ModifyInstance.return_value = 42
151
        volumes.delete(vol)
152
        self.assertEqual(vol.backendjobid, 42)
153
        args, kwargs = mrapi().ModifyInstance.call_args
154
        self.assertEqual(kwargs["instance"], vm.backend_vm_id)
155
        self.assertEqual(kwargs["disks"][0], ("remove",
156
                                              vol.backend_volume_uuid, {}))