ceph_brag/tests/__init__.py
ceph_brag/tests/config.py
ceph_brag/tests/test_functional.py
-ceph_brag/tests/test_units.py
+ceph_brag/tests/test_units.py
\ No newline at end of file
from pecan import expose, request, abort, response
-from webob.exc import status_map
+from webob import exc
from pecan.rest import RestController
from ceph_brag.model import db
import sys, traceback
class RootController(RestController):
+ def fail(self, status_code=200, msg="OK"):
+ response.status = status_code
+ return msg
+
@expose('json')
def get(self, *args, **kwargs):
if len(args) is 0:
try:
result = db.get_uuids()
except Exception as e:
- traceback.print_exc()
- abort(500, comment="Internal Server Error")
+ return self.fail(500, msg="Internal Server Error")
elif len(args) is 1 or len(args) is 2 and args[1] == '':
#/uuid
try:
result = db.get_versions(args[0])
except Exception as e:
- traceback.print_exc()
- abort(status_code=500, comment="Internal Server Error")
+ return self.fail(status_code=500, msg="Internal Server Error")
if result is None:
- abort(400, comment="Invalid UUID")
+ return self.fail(400, msg="Invalid UUID")
elif len(args) is 2 or len(args) is 3 and args[2] == '':
#/uuid/version_number
try:
result = db.get_brag(args[0], args[1])
except Exception as e:
- traceback.print_exc()
- abort(status_code=500, comment="Internal Server Error")
+ return self.fail(status_code=500, msg="Internal Server Error")
if result is None:
- abort(status_code=400, comment="Invalid UUID,version combination")
- else:
- abort(status_code=400, comment="Invalid args")
-
+ return self.fail(status_code=400, msg="Invalid UUID,version combination")
+ else:
+ return self.fail(status_code=400, msg="Invalid args")
+
return result
@expose(content_type='application/json')
def put(self, *args, **kwargs):
try:
db.put_new_version(request.body)
+ except ValueError as ve:
+ return self.fail(status_code=422, msg="Improper payload")
+ except KeyError as ke:
+ msg = "Payload not as expected, some keys are missing"
+ return self.fail(status_code=422, msg=msg)
except Exception as e:
- traceback.print_exc()
- abort(status_code=500, comment="Internal Server Error")
+ return self.fail(status_code=500, msg="Internal Server Error")
response.status = 201
return "CREATED"
@expose()
def delete(self, *args, **kwargs):
+ if 'uuid' not in kwargs:
+ return self.fail(status_code=400, msg="Required uuid parameter")
+
uuid = kwargs['uuid']
- if uuid is None:
- abort(status_code=400, comment="Required uuid parameter")
-
try:
status = db.delete_uuid(uuid)
except Exception as e:
- traceback.print_exc()
- abort(status_code=500, comment="Internal Server Error")
+ return self.fail(status_code=500, msg="Internal Server Error")
if status is not None:
- abort(status_code=status['status'], comment=status['comment'])
+ return self.fail(status_code=status['status'], msg=status['msg'])
response.status=200
return "DELETED"
email=ci.contact_email,
cluster_name=ci.cluster_name,
cluster_creation_date=str(ci.cluster_creation_date),
+ num_versions=ci.num_versions
)
@jsonify.register(db.components_info)
def delete_uuid(uuid):
ci = Session.query(cluster_info).filter_by(uuid=uuid).first()
if ci is None:
- return {'status':400, 'comment':'No information for this UUID'}
+ return {'status':400, 'msg':'No information for this UUID'}
for v in Session.query(version_info).filter_by(cluster_id=ci.index).all():
Session.query(components_info).filter_by(vid=v.index).delete()
from unittest import TestCase
from webtest import TestApp
from ceph_brag.tests import FunctionalTest
-import sys
+import json, sys
from pecan import request
class TestRootController(FunctionalTest):
+ def test_1_get_invalid_url_format(self):
+ response = self.app.get('/1/2/3', expect_errors=True)
+ assert response.status_int == 400
- def test_get(self):
- response = self.app.get('/?k1=v1&k2=v2')
- assert response.status_int == 200
-
- def test_put(self):
- #response = self.app.put('/', upload_files=[("uploadfield", "/tmp/sample.json")])
- with open ("/tmp/sample.json", "r") as myfile:
+ def test_2_put(self):
+ with open ("sample.json", "r") as myfile:
data=myfile.read().replace('\n', '')
response = self.app.request('/', method='PUT', body=data)
- assert response.status_int == 302
+ assert response.status_int == 201
+
+ def test_3_put_invalid_json(self):
+ response = self.app.request('/', method='PUT', body='{asdfg', expect_errors=True)
+ assert response.status_int == 422
+
+ def test_4_put_invalid_entries_1(self):
+ response = self.app.request('/', method='PUT', body='{}', expect_errors=True)
+ assert response.status_int == 422
+
+ def test_5_put_incomplete_json(self):
+ response = self.app.request('/', method='PUT', body='{\"uuid\":\"adfs-12312ad\"}',
+ expect_errors=True)
+ assert response.status_int == 422
+
+ def test_6_get(self):
+ response = self.app.get('/')
+ js = json.loads(response.body)
+ for entry in js:
+ ci = entry
+ break
+
+ response = self.app.get('/'+ci['uuid']+'/'+str(ci['num_versions']))
+ assert response.status_int == 200
+
+ def test_7_get_invalid_uuid(self):
+ response = self.app.get('/xxxxxx', expect_errors=True)
+ assert response.status_int == 400
+
+ def test_8_get_invalid_version(self):
+ response = self.app.get('/')
+ js = json.loads(response.body)
+ for entry in js:
+ ci = entry
+ break
+
+ response = self.app.get('/'+ci['uuid']+'/'+str(0), expect_errors=True)
+ assert response.status_int == 400
+
+ def test_9_delete_invalid_parameters(self):
+ response = self.app.delete('/', expect_errors=True)
+ assert response.status_int == 400
+
+ def test_91_delete_wrong_uuid(self):
+ response = self.app.delete('/?uuid=xxxx', expect_errors=True)
+ assert response.status_int == 400
- def test_get_not_found(self):
- response = self.app.get('/a/bogus/url', expect_errors=True)
- assert response.status_int == 404
+ def test_92_delete(self):
+ response = self.app.get('/')
+ js = json.loads(response.body)
+ for entry in js:
+ response = self.app.delete('/?uuid='+entry['uuid'])
+ assert response.status_int == 200
--- /dev/null
+{
+ "cluster_creation_date": "2014-01-16 13:38:41.928551",
+ "uuid": "20679d0e-04b1-4004-8ee9-45ac271510e9",
+ "components_count": {
+ "bytes": {
+ "count": 0,
+ "scale": "bytes"
+ },
+ "osds": 1,
+ "objects": 0,
+ "pgs": 192,
+ "pools": 3,
+ "mdss": 1,
+ "mons": 1
+ },
+ "crush_types": [
+ "osd",
+ "host",
+ "chassis",
+ "rack",
+ "row",
+ "pdu",
+ "pod",
+ "room",
+ "datacenter",
+ "region",
+ "root"
+ ],
+ "ownership": {
+ "organization": "eNovance",
+ "description": "Use case1",
+ "email": "mail@enovance.com",
+ "name": "Cluster1"
+ },
+ "pool_metadata": [
+ {
+ "rep_size": 3,
+ "id": "0",
+ "name": "data"
+ },
+ {
+ "rep_size": 3,
+ "id": "1",
+ "name": "metadata"
+ },
+ {
+ "rep_size": 3,
+ "id": "2",
+ "name": "rbd"
+ }
+ ],
+ "sysinfo": [
+ {
+ "nw_info": {
+ "hostname": "ceph-brag",
+ "address": "127.0.0.1"
+ },
+ "hw_info": {
+ "swap_kb": 0,
+ "arch": "x86_64",
+ "cpu": "Intel Xeon E312xx (Sandy Bridge)",
+ "mem_kb": 2051648
+ },
+ "id": 0,
+ "os_info": {
+ "version": "3.2.0-23-virtual",
+ "os": "Linux",
+ "description": "#36-Ubuntu SMP Tue Apr 10 22:29:03 UTC 2012",
+ "distro": "Ubuntu 12.04 precise (Ubuntu 12.04 LTS)"
+ },
+ "ceph_version": "ceph version 0.75-229-g4050eae (4050eae32cd77a1c210ca11d0f12c74daecb1bd3)"
+ }
+ ]
+ }