Beispiel #1
0
    def test_delete_node_404_restore_of(self):
        def raise_exc(*args, **kwargs):
            e = base.NodeError(MockRequest(), URLError("fake 404"))
            e.code = 404
            raise e

        volume = db.models.Volume(node=self.node0, account=self.account,
                                  status='ACTIVE', size=1, restore_of='foo')
        self.db.add(volume)
        self.db.commit()
        self.assertEqual(volume.restore_of, 'foo')
        c = Controller({'account_id': self.account_id, 'id': volume.id},
                       self.mock_app)
        req = Request.blank('')
        with patch(Controller, 'node_request', raise_exc):
            self.assertRaises(base.NodeError, c.delete, req)

        self.assertEqual(volume.restore_of, None)
Beispiel #2
0
 def test_validate_affinity(self):
     c = Controller({'account_id': self.account_id, 'id': 'test'},
             self.mock_app)
     req = Request.blank('?somethingelse=2')
     affinity = c._validate_affinity(req.params)
     self.assertEquals(affinity, '')
     req = Request.blank('?affinity=badformat')
     self.assertRaises(HTTPPreconditionFailed, c._validate_affinity,
                       req.params)
     req = Request.blank('?affinity=badtypeof_affinity:abcde')
     self.assertRaises(HTTPPreconditionFailed, c._validate_affinity,
                       req.params)
     req = Request.blank('?affinity=different_node:volume_id')
     affinity = c._validate_affinity(req.params)
     self.assertEquals(affinity, 'different_node:volume_id')
     req = Request.blank('?affinity=different_group:volume_id')
     affinity = c._validate_affinity(req.params)
     self.assertEquals(affinity, 'different_group:volume_id')
Beispiel #3
0
 def test_recommend_nodes_ordered_by_volumes(self):
     c = Controller({'account_id':  self.account_id}, self.mock_app)
     n = db.models.Node('node3', 13, volume_type=self.vtype,
                        hostname='10.127.0.3', port=8083)
     self.db.add(n)
     self.db.add(db.models.Volume(1, 'vtype', node=self.node1,
                                  account_id=self.account_id,
                                  volume_type=self.vtype))
     self.db.add(db.models.Volume(1, 'vtype', node=n,
                                  account_id=self.account_id))
     n.calc_storage_used()
     self.db.commit()
     nodes = c.get_recommended_nodes(self.vtype.name, 1)
     # node0/2 have 0 volumes, they should be included.
     # node1/3 have 1 volume. 3 should be included because it's bigger.
     node_ids = [node.id for node in nodes]
     expected_ids = [self.node0.id, self.node2.id, n.id]
     self.assertEqual(sorted(node_ids), sorted(expected_ids))
Beispiel #4
0
 def test_create_source_success(self):
     c = Controller({
         'account_id': self.account_id,
         'id': 'test1'
     }, self.mock_app)
     source_vol = db.models.Volume(node=self.node0,
                                   account=self.account,
                                   status='ACTIVE',
                                   size=1)
     self.db.add(source_vol)
     self.db.commit()
     req = Request.blank('?size=2&volume_type_name=vtype&source_volume=%s' %
                         source_vol.id)
     res = c.create(req)
     self.assertEqual(res.body['id'], 'test1')
     self.assertEqual(res.body['size'], 2)
     self.assert_(res.body['node_id'], self.node0.id)
     self.assert_(res.body['status'], 'CLONING')
Beispiel #5
0
 def test_validate_volume_type_limits(self):
     n = db.models.Node('bignode', 10000, volume_type=self.vtype,
                        hostname='10.127.0.72', port=8152)
     self.db.add(n)
     self.vtype.min_size = 100
     self.vtype.max_size = 1000
     self.db.add(self.vtype)
     self.db.commit()
     c = Controller({'account_id': self.account_id, 'id': 'test'},
                    self.mock_app)
     req = Request.blank('?size=1234&volume_type_name=vtype')
     self.assertRaises(HTTPPreconditionFailed, c.create, req)
     req = Request.blank('?size=12&volume_type_name=vtype')
     self.assertRaises(HTTPPreconditionFailed, c.create, req)
     req = Request.blank('?size=123&volume_type_name=vtype')
     res = c.create(req)
     self.assertEqual(res.body['id'], 'test')
     self.assertEqual(res.body['size'], 123)
Beispiel #6
0
 def test_index_filtered_by_restore_of(self):
     volume = db.models.Volume(node=None,
                               account=self.account,
                               status='ACTIVE',
                               size=1,
                               restore_of='foo')
     self.db.add(volume)
     self.db.commit()
     self.assertEqual(volume.restore_of, 'foo')
     c = Controller({
         'account_id': self.account_id,
         'id': volume.id
     }, self.mock_app)
     req = Request.blank('?restore_of=foo')
     res = c.index(req)
     self.assertEqual(len(res.body), 1)
     self.assertEqual(res.body[0]['id'], volume.id)
     self.assertEqual(res.body[0]['restore_of'], 'foo')
Beispiel #7
0
 def test_recommended_nodes_affinity_node(self):
     self.mock_app.fill_strategy = 'deep_fill'
     c = Controller({'account_id': self.account_id}, self.mock_app)
     nodes = c.get_recommended_nodes(self.vtype.name, 1)
     node_ids = [node.id for node in nodes]
     # Our test nodes are in order by size, 0 = 10, 1 = 11, etc.
     expected_ids = [self.node0.id, self.node1.id, self.node2.id]
     self.assertEqual(node_ids, expected_ids)
     # Now add a volume for this account
     v1 = db.models.Volume(1,
                           'vtype',
                           node=self.node0,
                           account_id=self.account_id,
                           volume_type=self.vtype)
     v2 = db.models.Volume(2,
                           'vtype',
                           node=self.node1,
                           account_id=self.account_id,
                           volume_type=self.vtype)
     v3 = db.models.Volume(2,
                           'vtype',
                           node=self.node2,
                           account_id=self.account_id,
                           volume_type=self.vtype)
     self.db.add_all([v1, v2, v3])
     self.db.commit()
     # Should prefer the second node
     affinity = 'different_node:%s' % v1.id
     nodes = c.get_recommended_nodes(self.vtype.name, 3, affinity=affinity)
     self.assertEquals(2, len(nodes))
     self.assertEquals(nodes[0].id, self.node1.id)
     # Add a volume to the second node
     # Should prefer the last node
     affinity = 'different_node:%s,%s' % (v1.id, v2.id)
     nodes = c.get_recommended_nodes(self.vtype.name, 1, affinity=affinity)
     self.assertEquals(1, len(nodes))
     self.assertEquals(nodes[0].id, self.node2.id)
     # Recommendation fails.
     affinity = 'different_node:%s,%s,%s' % (v1.id, v2.id, v3.id)
     self.assertRaises(HTTPInsufficientStorage,
                       c.get_recommended_nodes,
                       self.vtype.name,
                       1,
                       affinity=affinity)
Beispiel #8
0
 def test_get_recommended_nodes_is_random(self):
     c = Controller({'account_id':  self.account_id}, self.mock_app)
     size = 1
     nodes1 = c.get_recommended_nodes(self.vtype.name, size)
     attempts = 0
     while True:
         nodes2 = c.get_recommended_nodes(self.vtype.name, size)
         try:
             self.assertNotEquals(nodes1, nodes2)
         except AssertionError:
             if attempts >= 3:
                 raise
         else:
             break
         attempts += 1
     self.assertEquals(len(nodes1), len(nodes2))
     nodes1.sort()
     nodes2.sort()
     self.assertEqual(nodes1, nodes2)
Beispiel #9
0
 def test_delete_no_node_restore_of(self):
     volume = db.models.Volume(node=None,
                               account=self.account,
                               status='ACTIVE',
                               size=1,
                               restore_of='foo')
     self.db.add(volume)
     self.db.commit()
     self.assertEqual(volume.restore_of, 'foo')
     c = Controller({
         'account_id': self.account_id,
         'id': volume.id
     }, self.mock_app)
     req = Request.blank('')
     res = c.delete(req)
     self.assertEqual(res.body['id'], volume.id)
     self.assertEqual(res.body['status'], 'DELETED')
     self.assertEqual(res.body['restore_of'], None)
     self.assertEqual(volume.restore_of, None)
Beispiel #10
0
 def test_create_backup_validation(self):
     volume = db.models.Volume(node=self.node0,
                               account=self.account,
                               status='ACTIVE',
                               size=1)
     self.db.add(volume)
     self.db.commit()
     c = Controller({
         'account_id': self.account_id,
         'id': 'test1'
     }, self.mock_app)
     req = Request.blank('?size=2&volume_type_name=vtype&backup=%s' %
                         'backupnotfound')
     self.assertRaises(HTTPPreconditionFailed, c.create, req)
     backup = db.models.Backup(volume, status='NOTAVAILABLE')
     self.db.add(backup)
     self.db.commit()
     req = Request.blank('?size=2&volume_type_name=vtype&backup=%s' %
                         backup.id)
     self.assertRaises(HTTPPreconditionFailed, c.create, req)
Beispiel #11
0
 def test_deep_recommended_nodes(self):
     self.mock_app.fill_strategy = 'deep_fill'
     c = Controller({'account_id': self.account_id}, self.mock_app)
     nodes = c.get_recommended_nodes(self.vtype.name, 1)
     node_ids = [node.id for node in nodes]
     # Our test nodes are in order by size, 0 = 10, 1 = 11, etc.
     expected_ids = [self.node0.id, self.node1.id, self.node2.id]
     self.assertEqual(node_ids, expected_ids)
     # Add Volume from a different account,
     self.db.add(
         db.models.Volume(1,
                          'vtype',
                          node=self.node0,
                          account_id=self.account_id2,
                          volume_type=self.vtype))
     # node0 should still be preferred
     nodes = c.get_recommended_nodes(self.vtype.name, 1)
     self.assertEquals(3, len(nodes))
     self.assertEquals(nodes[0].id, self.node0.id)
     # Now add a volume for this account
     self.db.add(
         db.models.Volume(1,
                          'vtype',
                          node=self.node0,
                          account_id=self.account_id,
                          volume_type=self.vtype))
     # Should prefer the second node
     nodes = c.get_recommended_nodes(self.vtype.name, 3)
     self.assertEquals(3, len(nodes))
     self.assertEquals(nodes[0].id, self.node1.id)
     # Add a volume to the second node
     self.db.add(
         db.models.Volume(3,
                          'vtype',
                          node=self.node1,
                          account_id=self.account_id,
                          volume_type=self.vtype))
     # Should prefer the last node
     nodes = c.get_recommended_nodes(self.vtype.name, 1)
     self.assertEquals(3, len(nodes))
     self.assertEquals(nodes[0].id, self.node2.id)
Beispiel #12
0
    def test_create_from_backup_with_new_size(self):
        volume = db.models.Volume(node=self.node0,
                                  account=self.account,
                                  status='ACTIVE',
                                  size=1)
        self.db.add(volume)
        backup = db.models.Backup(volume, status='AVAILABLE')
        self.db.add(backup)
        self.db.commit()

        c = Controller({
            'account_id': self.account_id,
            'id': 'test1'
        }, self.mock_app)
        req = Request.blank('?size=2&volume_type_name=vtype&backup=%s' %
                            backup.id)
        res = c.create(req)
        self.assertEqual(res.body['id'], 'test1')
        self.assertEqual(res.body['size'], 2)
        self.assert_(res.body['node_id'])
        self.assert_(res.body['status'], 'ACTIVE')
Beispiel #13
0
 def test_recommended_nodes_affinity_cab(self):
     self.mock_app.fill_strategy = 'deep_fill'
     c = Controller({'account_id':  self.account_id}, self.mock_app)
     nodes = c.get_recommended_nodes(self.vtype.name, 1)
     node_ids = [node.id for node in nodes]
     # Our test nodes are in order by size, 0 = 10, 1 = 11, etc.
     expected_ids = [self.node0.id, self.node1.id, self.node2.id]
     self.assertEqual(node_ids, expected_ids)
     # Set up cabs.
     self.node0.affinity_group = 'cab1'
     self.node1.affinity_group = 'cab2'
     self.node2.affinity_group = 'cab1'
     # Now add some volumes to these cabs.
     # v1,v3 = cab1, v2 = cab2
     self.db.add_all([self.node0, self.node1, self.node2])
     v1 = db.models.Volume(1, 'vtype', node=self.node0,
         account_id=self.account_id, volume_type=self.vtype)
     v2 = db.models.Volume(1, 'vtype', node=self.node1,
         account_id=self.account_id, volume_type=self.vtype)
     v3 = db.models.Volume(1, 'vtype', node=self.node2,
         account_id=self.account_id, volume_type=self.vtype)
     self.db.add_all([v1, v2, v3])
     self.db.commit()
     # Should only show node1 (cab2)
     affinity = 'different_group:%s' % v1.id
     nodes = c.get_recommended_nodes(self.vtype.name, 3,
                                     affinity=affinity)
     self.assertEquals(1, len(nodes))
     self.assertEquals(nodes[0].id, self.node1.id)
     # Should show node0 and node2 (cab1)
     affinity = 'different_group:%s' % v2.id
     nodes = c.get_recommended_nodes(self.vtype.name, 1,
                                     affinity=affinity)
     self.assertEquals(2, len(nodes))
     self.assertEquals(nodes[0].id, self.node0.id)
     self.assertEquals(nodes[1].id, self.node2.id)
     # Recommendation fails.
     affinity = 'different_group:%s,%s' % (v1.id, v2.id)
     self.assertRaises(HTTPInsufficientStorage, c.get_recommended_nodes,
                       self.vtype.name, 1, affinity=affinity)
Beispiel #14
0
 def test_recommended_nodes_for_image(self):
     self.mock_app.fill_strategy = 'deep_fill'
     self.mock_app.image_convert_limit = 1
     c = Controller({'account_id':  self.account_id}, self.mock_app)
     nodes = c.get_recommended_nodes(self.vtype.name, 1)
     node_ids = [node.id for node in nodes]
     # Our test nodes are in order by size, 0 = 10, 1 = 11, etc.
     expected_ids = [self.node0.id, self.node1.id, self.node2.id]
     self.assertEqual(node_ids, expected_ids)
     # Add an imaging volume to node0
     self.db.add(db.models.Volume(1, 'vtype', node=self.node0,
                                  account_id=self.account_id2,
                                  volume_type=self.vtype, status='IMAGING'))
     nodes = c.get_recommended_nodes(self.vtype.name, 1)
     self.assertEquals(3, len(nodes))
     node_ids = [node.id for node in nodes]
     self.assertIn(self.node0.id, node_ids)
     # Now try recommendations for imaging
     nodes = c.get_recommended_nodes(self.vtype.name, 1, imaging=True)
     self.assertEquals(2, len(nodes))
     node_ids = [node.id for node in nodes]
     self.assertNotIn(self.node0.id, node_ids)
Beispiel #15
0
 def test_validate_name(self):
     c = Controller({'account_id': self.account_id, 'id': 'test'},
                    self.mock_app)
     req = Request.blank('')
     valid_name = c._validate_name(req.params)
     # default to id
     self.assertEqual(valid_name, 'test')
     name = 'aSDf'
     req = Request.blank('?name=%s' % name)
     valid_name = c._validate_name(req.params)
     self.assertEqual(valid_name, name)
     # only allow alphanumeric and dashes
     name = 'asdf_42'
     req = Request.blank('?name=%s' % name)
     self.assertRaises(HTTPPreconditionFailed, c._validate_name, req.params)
     # only allow alphanumeric and dashes
     name = 'asdf*+42'
     req = Request.blank('?name=%s' % name)
     self.assertRaises(HTTPPreconditionFailed, c._validate_name, req.params)
     name = 'asdf  2'
     req = Request.blank('?name=%s' % name)
     self.assertRaises(HTTPPreconditionFailed, c._validate_name, req.params)
Beispiel #16
0
    def test_validate_force_node(self):
        n = db.models.Node('mynode',
                           100,
                           volume_type=self.vtype,
                           hostname='10.127.0.72',
                           port=8152)
        self.db.add(n)
        self.db.commit()

        c = Controller({
            'account_id': self.account_id,
            'id': 'test'
        }, self.mock_app)
        req = Request.blank('?force_node=%s' % n.id)
        force_node = c._validate_force_node(req.params)
        self.assertEquals(force_node, n.id)
        req = Request.blank('?force_node=%s' % n.name)
        force_node = c._validate_force_node(req.params)
        self.assertEquals(force_node, n.name)
        req = Request.blank('?force_node=garbage')
        self.assertRaises(HTTPPreconditionFailed, c._validate_force_node,
                          req.params)
Beispiel #17
0
    def test_index_name_and_cinder_host(self):
        cinder_host = 'somehost'
        name = 'somehostvolume'
        n = db.models.Node('somehostnode', 1000, volume_type=self.vtype,
                           hostname='10.127.0.99', port=8099,
                           cinder_host=cinder_host)
        # Two volumes, same name
        v1 = db.models.Volume(node=self.node0, account=self.account,
                              status='ACTIVE', size=1, name=name)
        v2 = db.models.Volume(node=n, account=self.account,
                              status='ACTIVE', size=1, name=name)
        # One one the right node, different name
        v3 = db.models.Volume(node=n, account=self.account,
                              status='ACTIVE', size=1)
        self.db.add_all([n, v1, v2])
        self.db.commit()

        # Query by name
        c = Controller({'account_id':  self.account_id, 'id': 'test'},
                       self.mock_app)
        req = Request.blank('?name=%s' % name)
        res = c.index(req)
        self.assertEqual(len(res.body), 2)
        vol_ids = (v1.id, v2.id,)
        self.assertEqual((res.body[0]['id'], res.body[1]['id']), vol_ids)

        # Query by cinder_host
        req = Request.blank('?cinder_host=%s' % cinder_host)
        res = c.index(req)
        self.assertEqual(len(res.body), 2)
        vol_ids = (v2.id, v3.id,)
        self.assertEqual((res.body[0]['id'], res.body[1]['id']), vol_ids)

        # Query by both
        req = Request.blank('?cinder_host=%s&name=%s' % (cinder_host, name))
        res = c.index(req)
        self.assertEqual(len(res.body), 1)
        self.assertEqual(res.body[0]['id'], v2.id)
Beispiel #18
0
 def test_get_recommended_ignores_deleted_volumes(self):
     c = Controller({'account_id':  self.account_id}, self.mock_app)
     vtype = db.models.VolumeType('something_else')
     n = db.models.Node('newnode', 10, volume_type=vtype,
                        hostname='10.127.0.42', port=8242)
     self.db.add_all([vtype, n])
     self.db.commit()
     nodes = c.get_recommended_nodes('something_else', 1)
     self.assertNotEquals([], nodes)
     self.assertEquals(n.id, nodes[0].id)
     # Fill it to the gills.
     v = db.models.Volume(10, 'something_else', node=n,
                          account_id=self.account_id)
     self.db.add(v)
     self.db.commit()
     self.assertRaises(HTTPError, c.get_recommended_nodes,
                       'something_else', 1)
     v.status = 'DELETED'
     self.db.add(v)
     self.db.commit()
     nodes = c.get_recommended_nodes('something_else', 1)
     self.assertNotEquals([], nodes)
     self.assertEquals(n.id, nodes[0].id)
Beispiel #19
0
 def test_dont_recommend_full_nodes(self):
     c = Controller({'account_id': self.account_id}, self.mock_app)
     size = 2
     # (10 - (10 - 3 - 2)) / 10 == .5, IN!
     self.db.add(
         db.models.Volume(3,
                          'vtype',
                          node=self.node0,
                          account_id=self.account_id))
     # (11 - (11 - 4 - 2)) / 11 > .5, OUT!
     self.db.add(
         db.models.Volume(4,
                          'vtype',
                          node=self.node1,
                          account_id=self.account_id))
     # (12 - (12 - 20 - 2)) / 12 > .5, OUT!
     self.db.add(
         db.models.Volume(5,
                          'vtype',
                          node=self.node2,
                          account_id=self.account_id))
     nodes1 = c.get_recommended_nodes(self.vtype.name, size)
     self.assertEquals(nodes1, [self.node0])
Beispiel #20
0
    def test_create_from_image(self):
        image_id = 'my_image'
        base.urlopen = MockUrlopenWithImage
        c = Controller({'account_id': self.account_id, 'id': 'test1'},
                       self.mock_app)
        self.mock_called = False

        def mock_get_recommended_nodes(*args, **kwargs):
            self.assert_('imaging' in kwargs)
            self.assertTrue(kwargs['imaging'])
            self.mock_called = True
            return [self.node0]
        c.get_recommended_nodes = mock_get_recommended_nodes

        req = Request.blank('?size=2&volume_type_name=vtype&image_id=%s' %
                            image_id)
        res = c.create(req)

        self.assertTrue(self.mock_called)
        self.assertEqual(res.body['id'], 'test1')
        self.assertEqual(res.body['size'], 2)
        self.assertEqual(res.body['status'], 'IMAGING')
        self.assertEqual(res.body['image_id'], image_id)
Beispiel #21
0
 def test_transfer_404(self):
     c = Controller({'account_id': self.account_id, 'id': 'missing'},
                    self.mock_app)
     new_account_id = 'new_account_id'
     req = Request.blank('?account_id=%s' % new_account_id)
     self.assertRaises(HTTPNotFound, c.update, req)
Beispiel #22
0
 def test_index_empty(self):
     c = Controller({'account_id':  self.account_id}, self.mock_app)
     req = Request.blank('')
     res = c.index(req)
     self.assertEqual(res.body, [])