def test_delete(self): c = Controller({}, self.mock_app) req = Request.blank('?name=test&size=1&volume_type_name=vtype&' 'hostname=127.0.0.1&port=8080') id = c.create(req).body['id'] c = Controller({'id': id}, self.mock_app) req = Request.blank('') res = c.delete(req) self.assertEqual(res.body['id'], id) self.assertEqual(res.body['status'], 'DELETED') c = Controller({'id': '00000000-0000-0000-0000-000000000000'}, self.mock_app) req = Request.blank('') self.assertRaises(HTTPNotFound, c.delete, req)
def test_show_with_storage_used(self): # create a new node n = db.models.Node(size=100) self.db.add(n) self.db.commit() # create controller for resource with node's id c = Controller({'id': n.id}, self.mock_app) # verify node is empty expected = { 'size': 100, 'storage_used': 0, 'storage_free': 100, } details = make_request(c.show).body for k, v in expected.items(): self.assertEquals(details[k], v) # add some volumes to the node a = db.models.Account() v1 = db.models.Volume(node=n, size=10, account=a) v2 = db.models.Volume(node=n, size=20, account=a) self.db.add_all([v1, v2, a]) self.db.commit() # very available storage expected = { 'size': 100, 'storage_used': 30, 'storage_free': 70, } details = make_request(c.show).body for k, v in expected.items(): self.assertEquals(details[k], v)
def test_show_defaults(self): # make node named 'test' n = db.models.Node() self.db.add(n) self.db.commit() # create controller for resource with node's id c = Controller({'id': n.id}, self.mock_app) # make show request with no params details = make_request(c.show).body # assert defaults expected = dict(n) for k, v in expected.items(): self.assertEquals(details[k], v)
def test_create(self): c = Controller({}, self.mock_app) req = Request.blank('') self.assertRaises(HTTPPreconditionFailed, c.create, req) req = Request.blank('?name=test&size=a&volume_type_name=vtype&' 'hostname=127.0.0.1&port=8080') self.assertRaises(HTTPPreconditionFailed, c.create, req) req = Request.blank('?name=test&size=1') self.assertRaises(HTTPBadRequest, c.create, req) req = Request.blank('?name=test&size=1&volume_type_name=notvtype') self.assertRaises(HTTPPreconditionFailed, c.create, req) req = Request.blank('?name=test&size=1&volume_type_name=vtype&' 'hostname=127.0.0.1&port=8080') res = c.create(req) self.assertEqual(res.body['name'], 'test') req = Request.blank('?name=test&size=1&volume_type_name=vtype&' 'hostname=127.0.0.1&port=8080') self.assertRaises(HTTPConflict, c.create, req)
def test_index(self): c = Controller({}, self.mock_app) req = Request.blank('') res = c.index(req) self.assertEqual(res.body, []) req = Request.blank('?name=test&size=1&volume_type_name=vtype&' 'hostname=127.0.0.1&port=8080') res = c.create(req) req = Request.blank('?name=test2&size=1&volume_type_name=vtype&' 'hostname=127.0.0.1&port=8080') res = c.create(req) req = Request.blank('') res = c.index(req) self.assertEqual(len(res.body), 2) req = Request.blank('?name=test2') res = c.index(req) self.assertEqual(len(res.body), 1) self.assertEqual(res.body[0]['name'], 'test2')
def test_show_for_missing_id(self): # make controller for id not in database c = Controller({'id': str(uuid4())}, self.mock_app) res = get_error_response(c.show) self.assertEquals(res.status_int, 404)