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

b/snf-cyclades-app/synnefo/volume/tests/volumes.py
12 12
    def setUp(self):
13 13
        self.userid = "test_user"
14 14
        self.size = 1
15
        self.vm = mf.VirtualMachineFactory(userid=self.userid)
15
        self.vm = mf.VirtualMachineFactory(
16
            userid=self.userid,
17
            flavor__disk_template="ext_archipelago")
16 18
        self.kwargs = {"user_id": self.userid,
17 19
                       "size": self.size,
18 20
                       "server_id": self.vm.id}
......
41 43
        self.assertEqual(vol.name, None)
42 44
        self.assertEqual(vol.description, None)
43 45
        self.assertEqual(vol.source_snapshot_id, None)
44
        self.assertEqual(vol.source_volume, None)
46
        self.assertEqual(vol.source, None)
47
        self.assertEqual(vol.origin, None)
48
        self.assertEqual(vol.source_volume_id, None)
45 49
        self.assertEqual(vol.source_image_id, None)
46 50
        self.assertEqual(vol.machine, self.vm)
47 51

  
......
54 58
        self.assertFalse("origin" in disk_info)
55 59

  
56 60
    def test_create_from_volume(self, mrapi):
57
        # Wrong source
61
        # Check permissions
58 62
        svol = mf.VolumeFactory(userid="other_user")
59 63
        self.assertRaises(faults.BadRequest,
60 64
                          volumes.create,
61 65
                          source_volume_id=svol.id,
62 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)
63 73

  
64
        svol = mf.VolumeFactory(userid=self.userid)
74
        svol.status = "AVAILABLE"
75
        svol.save()
65 76
        mrapi().ModifyInstance.return_value = 42
66 77
        vol = volumes.create(source_volume_id=svol.id, **self.kwargs)
67 78

  
......
69 80
        self.assertEqual(vol.userid, self.userid)
70 81
        self.assertEqual(vol.name, None)
71 82
        self.assertEqual(vol.description, None)
72
        self.assertEqual(vol.source_snapshot_id, None)
73
        self.assertEqual(vol.source_volume, svol)
74
        self.assertEqual(vol.source_image_id, None)
83
        self.assertEqual(vol.source, "volume:%s" % svol.id)
84
        self.assertEqual(vol.origin, svol.backend_volume_uuid)
75 85

  
76 86
        name, args, kwargs = mrapi().ModifyInstance.mock_calls[0]
77 87
        self.assertEqual(kwargs["instance"], self.vm.backend_vm_id)
......
81 91
        self.assertEqual(disk_info["volume_name"], vol.backend_volume_uuid)
82 92
        self.assertEqual(disk_info["origin"], svol.backend_volume_uuid)
83 93

  
84
    @patch("synnefo.api.util.get_image")
85
    def test_create_from_snapshot(self, mocked_image, mrapi):
94
    @patch("synnefo.plankton.backend.ImageBackend")
95
    def test_create_from_snapshot(self, mimage, mrapi):
86 96
        # Wrong source
87
        mocked_image.side_effect = faults.ItemNotFound
97
        mimage().get_snapshot.side_effect = faults.ItemNotFound
88 98
        self.assertRaises(faults.BadRequest,
89 99
                          volumes.create,
90 100
                          source_snapshot_id=421,
91 101
                          **self.kwargs)
92 102

  
93
        mocked_image.side_effect = None
94
        mocked_image.return_value = {'location': 'pithos://foo',
95
                                     'checksum': 'snf-snapshot-43',
96
                                     'id': 12,
97
                                     'name': "test_image",
98
                                     'size': 1242,
99
                                     'disk_format': 'diskdump',
100
                                     'properties': {'source_volume': 42}}
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}}
101 112

  
102 113
        mrapi().ModifyInstance.return_value = 42
103 114
        vol = volumes.create(source_snapshot_id=12, **self.kwargs)
......
106 117
        self.assertEqual(vol.userid, self.userid)
107 118
        self.assertEqual(vol.name, None)
108 119
        self.assertEqual(vol.description, None)
109
        self.assertEqual(vol.source_snapshot_id, 12)
110
        self.assertEqual(vol.source_volume, None)
120
        self.assertEqual(int(vol.source_snapshot_id), 12)
121
        self.assertEqual(vol.source_volume_id, None)
111 122
        self.assertEqual(vol.source_image_id, None)
123
        self.assertEqual(vol.origin, "snf-snapshot-43")
112 124

  
113 125
        name, args, kwargs = mrapi().ModifyInstance.mock_calls[0]
114 126
        self.assertEqual(kwargs["instance"], self.vm.backend_vm_id)
......
119 131
        self.assertEqual(disk_info["origin"], "snf-snapshot-43")
120 132

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

  
124 140
        vm = mf.VirtualMachineFactory()
141
        # Also we cannot delete root volume
142
        vol.index = 0
125 143
        vol.machine = vm
126
        vol.save()
127 144
        self.assertRaises(faults.BadRequest,
128 145
                          volumes.delete,
129 146
                          vol)
130
        self.assertFalse(vol.deleted)
131 147

  
132
        vol.machine = None
133
        vol.save()
148
        # We can delete everything else
149
        vol.index = 1
150
        mrapi().ModifyInstance.return_value = 42
134 151
        volumes.delete(vol)
135
        self.assertTrue(vol.deleted)
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, {}))

Also available in: Unified diff