def test_kill_process(): ''' Ensure the killing a process by pid + release resources ''' processes.kill_process(1) expected_cpus = { 'steal': [0.0, 0.0], 'idle': [94.0, 94.0], 'user': [2.0, 2.0], 'irq': [0.0, 0.0], 'iowait': [2.0, 2.0], 'soft': [0.0, 0.0], 'system': [2.0, 2.0], 'guest': [0.0, 0.0], 'nice': [0.0, 0.0] } expected_memory = { 'used': 2048, 'cached': 0, 'free': 4192256, 'shared': 0, 'total': 4194304, 'buffers': 0 } assert len(processes.processes) == 2 assert jsonify(expected_cpus) == jsonify(cpus.dump()) assert jsonify(expected_memory) == jsonify(memory.dump())
def get(self, uid, tid): if self.req_user and self.req_user['myself'] is True: data = jsonify(self.temp) wio = Wio(self.req_token) try: thing_list = yield wio.get_all_thing() except Exception as e: gen_log.error(e) raise HTTPError( 400, "Get thing is failure from built-in Wio server, {}".format( str(e))) for thing in thing_list: if thing["id"] == data["id"]: data["online"] = thing["online"] self.finish(data) else: if self.temp['private'] is True: raise HTTPError(400, "The device is private") data = jsonify(self.temp) # value = {} # TODO, filter output value # for key in ['temperature', 'temperature_f', 'temperature_updated_at', 'updated_at', 'created_at']: # value[key] = data[key] # self.finish(value) self.finish(data)
def test_killall_process(): ''' Ensure the killing a process by name + release resources ''' processes.killall_process('bash') expected_cpus = { 'steal': [0.0, 0.0], 'idle': [97.0, 97.0], 'user': [1.0, 1.0], 'irq': [0.0, 0.0], 'iowait': [1.0, 1.0], 'soft': [0.0, 0.0], 'system': [1.0, 1.0], 'guest': [0.0, 0.0], 'nice': [0.0, 0.0] } expected_memory = { 'used': 1024, 'cached': 0, 'free': 4193280, 'shared': 0, 'total': 4194304, 'buffers': 0 } assert len(processes.processes) == 1 assert jsonify(expected_cpus) == jsonify(cpus.dump()) assert jsonify(expected_memory) == jsonify(memory.dump())
def get(self, uid): """get all temp-io devices of specific user""" if self.req_user and self.req_user['myself'] is True: wio = Wio(self.req_token) try: thing_list = yield wio.get_all_thing() except Exception as e: gen_log.error(e) raise HTTPError(400, "Get temp is failure, {}".format(e.message)) if thing_list is None: self.set_status(200) self.finish({"temps": []}) return docs = yield self.db_temp.get_all_temp_by_uid(uid) temp_list = [jsonify(doc) for doc in docs] for temp in temp_list[:]: # FIXME, need more efficiency for thing in thing_list: if thing['id'] == temp['id']: temp['online'] = thing['online'] self.finish({"temps": temp_list}) else: docs = yield self.db_temp.get_all_temp_by_uid(uid) temp_list = [] for doc in docs: if doc['private'] is False: temp_list.append(jsonify(doc)) self.finish({"temps": temp_list})
def test_process_counts(): ''' Ensure the default processes are created ''' expected_cpus = { 'steal': [0.0, 0.0], 'idle': [91.0, 91.0], 'user': [3.0, 3.0], 'irq': [0.0, 0.0], 'iowait': [3.0, 3.0], 'soft': [0.0, 0.0], 'system': [3.0, 3.0], 'guest': [0.0, 0.0], 'nice': [0.0, 0.0] } expected_memory = { 'used': 3072, 'cached': 0, 'free': 4191232, 'shared': 0, 'total': 4194304, 'buffers': 0 } assert len(processes.processes) == 3 assert jsonify(expected_cpus) == jsonify(cpus.dump()) assert jsonify(expected_memory) == jsonify(memory.dump())
def test_get_details_of_bad_file(): ''' Test we can fetch the details of a bad file ''' details = disks.get_details('/some/random/path') expected = {} assert jsonify(details) == jsonify(expected)
def get(self, uid): if self.user and self.user['myself']: self.target_user.pop('tokens') self.finish(jsonify(self.target_user)) else: self.target_user.pop('tokens') self.target_user.pop('email') self.target_user.pop('sign_in_provider') self.finish(jsonify(self.target_user))
def test_get_details_of_mount_point(): ''' Test we can fetch the details of a mount point folder ''' details = disks.get_details('/') expected = { 'mount': True, 'size': 1000000, 'owner': 'root', 'group': 'root', 'mode': 755 } assert jsonify(details) == jsonify(expected)
def test_init_bare(): ''' Test class instantiation without params ''' dump = memory.dump() expected = { 'free': 4194304, 'used': 0, 'buffers': 0, 'cached': 0, 'shared': 0, 'total': 4194304 } assert jsonify(dump) == jsonify(expected)
def test_set_cached(): ''' Test set method to update cached ''' memory.set('cached', 113664) dump = memory.dump() expected = { 'free': 3853312, 'used': 340992, 'buffers': 113664, 'cached': 113664, 'shared': 113664, 'total': 4194304 } assert jsonify(dump) == jsonify(expected)
def test_set_buffers(): ''' Test set method to update buffers ''' memory.set('buffers', 113664) dump = memory.dump() expected = { 'free': 3966976, 'used': 227328, 'buffers': 113664, 'cached': 0, 'shared': 113664, 'total': 4194304 } assert jsonify(dump) == jsonify(expected)
def test_set_free(): ''' Test set_used method to update used and free ''' memory.set('free', 0) dump = memory.dump() expected = { 'free': 0, 'used': 4194304, 'buffers': 0, 'cached': 0, 'shared': 0, 'total': 4194304 } assert jsonify(dump) == jsonify(expected)
def test_decrease_total(): ''' Test decrease total memory ''' memory.set('total', 3194304) dump = memory.dump() expected = { 'free': 2853312, 'used': 340992, 'buffers': 113664, 'cached': 113664, 'shared': 113664, 'total': 3194304 } assert jsonify(dump) == jsonify(expected)
def get(self, uid, tid): ota = yield self.db_temp.get_ota(tid) if ota is None: gen_log.error("Not ota field") self.set_status(400) self.finish({"error", "Can't found ota status."}) self.finish(jsonify(ota))
def customization_list(user): data = { "aliases": list(current_app.mdb.aliases.find({"owner": user.id})), "snippets": list(current_app.mdb.snippets.find({"owner": user.id})), "uvars": list(current_app.mdb.uvars.find({"owner": user.id})) } return jsonify(data)
def user_packs(): user = get_user_info() data = list(mdb.packs.find({"$or": [{"owner.id": user.id}, {"editors.id": user.id}]})) for pack in data: pack['numItems'] = len(pack['items']) del pack['items'] return jsonify(data)
def get_specific_gvar(key): get_user_info() # endpoint requires auth gvar = current_app.mdb.gvars.find_one({"key": key}) if gvar is None: return "Gvar not found", 404 return jsonify(gvar)
def user_tomes(): user = get_user_info() data = list(mdb.tomes.find({"$or": [{"owner.id": user.id}, {"editors.id": user.id}]})) for tome in data: tome['numSpells'] = len(tome['spells']) del tome['spells'] return jsonify(data)
def test_update_buffers_increase(): ''' Test update buffers memory (+) ''' success = memory.update('buffers', 1) dump = memory.dump() expected = { 'free': 2853312, 'used': 340992, 'buffers': 113664, 'cached': 113664, 'shared': 113664, 'total': 3194304 } assert success == True assert jsonify(dump) == jsonify(expected)
def user_tomes(user): data = list(_editable(user)) for tome in data: tome['numSpells'] = len(tome['spells']) tome['owner'] = str(tome['owner']) del tome['spells'] return jsonify(data)
def user_packs(user): data = list(_editable(user)) for pack in data: pack['numItems'] = len(pack['items']) pack['owner'] = str(pack['owner']) del pack['items'] return jsonify(data)
def test_set_used(): ''' Test set method to update used ''' memory.set('used', 0) dump = memory.dump() # Cached / buffers / shared "should" be decreased (later) expected = { 'free': 4194304, 'used': 0, 'buffers': 0, 'cached': 0, 'shared': 0, 'total': 4194304 } assert jsonify(dump) == jsonify(expected)
def meta(user): data = list( current_app.mdb.characters.find({"owner": user.id}, [ "upstream", "active", "name", "description", "image", "levels", "import_version", "overrides" ])) return jsonify(data)
def gvar_list(): user = get_user_info() data = { "owned": list(mdb.gvars.find({"owner": user.id})), "editable": list(mdb.gvars.find({"editors": user.id})) } return jsonify(data)
def test_decrease_total_too_much(): ''' Test decrease total memory by too much ''' success = memory.set('total', 0) dump = memory.dump() expected = { 'free': 2853312, 'used': 340992, 'buffers': 113664, 'cached': 113664, 'shared': 113664, 'total': 3194304 } assert success == False assert jsonify(dump) == jsonify(expected)
def test_update_free_reduce(): ''' Test update free memory (-) ''' success = memory.update('free', -1) dump = memory.dump() expected = { 'free': 2853311, 'used': 340993, 'buffers': 113664, 'cached': 113664, 'shared': 113664, 'total': 3194304 } assert success == True assert jsonify(dump) == jsonify(expected)
def add_todo(request): form = TodoForm(request.POST, user=request.user) if form.is_valid(): todo = form.save() response = {'id': todo.id, 'title': todo.title} else: response = form.errors_as_json() return JsonResponse(jsonify(response))
def get_pack(user, pack): data = current_app.mdb.packs.find_one({"_id": ObjectId(pack)}) if data is None: return "Pack not found", 404 if not _can_view(user, ObjectId(pack)): return "You do not have permission to view this pack", 403 data['owner'] = str(data['owner']) return jsonify(data)
def user_char(user, _id): char = current_app.mdb.characters.find_one({ "owner": user, "upstream": _id }) if char is None: return "Character not found", 404 return jsonify(char)
def get_tome(user, tome): data = current_app.mdb.tomes.find_one({"_id": ObjectId(tome)}) if data is None: return "Tome not found", 404 if not _can_view(user, ObjectId(tome)): return "You do not have permission to view this tome", 403 data['owner'] = str(data['owner']) return jsonify(data)
def get_user(user_id): """ GET /discord/users/:id Returns: (UserInfo) """ return jsonify(fetch_user_info(user_id).to_dict())
def get(self, uid, tid): if self.req_user and self.req_user['myself'] is False: raise HTTPError(400, "No operation permission") ota = yield self.db_temp.get_ota(tid) if ota is None: gen_log.error("Not ota field") raise HTTPError(400, "Can't found ota status.") self.finish(jsonify(ota))
def test_dump(): ''' Test that we receive the default CPUs dump ''' dump = cpus.dump() expected = { "user": [0.0, 0.0], "nice": [0.0, 0.0], "system": [0.0, 0.0], "iowait": [0.0, 0.0], "irq": [0.0, 0.0], "soft": [0.0, 0.0], "steal": [0.0, 0.0], "guest": [0.0, 0.0], "idle": [100.0, 100.0] } assert jsonify(dump) == jsonify(expected)
def test_set_cpu_by_array(): ''' Test that we can set a value by a dedicated array of value per core ''' success = cpus.set('iowait', [10, 50]) assert success == True dump = cpus.dump() expected = { "user": [10.0, 10.0], "nice": [0.0, 0.0], "system": [0.0, 0.0], "iowait": [10.0, 50.0], "irq": [0.0, 0.0], "soft": [0.0, 0.0], "steal": [0.0, 0.0], "guest": [0.0, 0.0], "idle": [80.0, 40.0] } assert jsonify(dump) == jsonify(expected)
def test_update_cpu_by_array(): ''' Test that we can set a value by a dedicated array of value per core ''' success = cpus.update('iowait', [-5, +10]) assert success == True dump = cpus.dump() expected = { "user": [20.0, 20.0], "nice": [0.0, 0.0], "system": [0.0, 0.0], "iowait": [5.0, 60.0], "irq": [0.0, 0.0], "soft": [0.0, 0.0], "steal": [0.0, 0.0], "guest": [0.0, 0.0], "idle": [75.0, 20.0] } assert jsonify(dump) == jsonify(expected)
def test_update_cpu_by_avg(): ''' Test that we can set a value by its average ''' success = cpus.update('user', 10) assert success == True dump = cpus.dump() expected = { "user": [20.0, 20.0], "nice": [0.0, 0.0], "system": [0.0, 0.0], "iowait": [10.0, 50.0], "irq": [0.0, 0.0], "soft": [0.0, 0.0], "steal": [0.0, 0.0], "guest": [0.0, 0.0], "idle": [70.0, 30.0] } assert jsonify(dump) == jsonify(expected)
def complete_todo(request): todo = Todo.objects.get(pk=request.POST.get('id'), user=request.user) if todo.completed_on: todo.completed_on = None completed = False else: todo.completed_on = datetime.now() completed = True todo.save() return JsonResponse(jsonify({'completed': completed}))
def test_update_file(): ''' Test we can update an existing file and the size get propagated ''' disks.update_file('/etc/new_file', size=5000, mode=644, owner='foo', group='bar') details = disks.get_details('/etc/new_file') expected = { 'size': 5000, 'owner': 'foo', 'group': 'bar', 'filetype': 'file', 'mode': 644 } assert jsonify(details) == jsonify(expected) details = disks.get_details('/etc') expected = { 'filetype': 'folder', 'size': 6111, 'owner': 'root', 'group': 'root', 'mode': 755 } assert jsonify(details) == jsonify(expected) details = disks.get_details('/') expected = { 'mount': True, 'size': 1005000, 'owner': 'root', 'group': 'root', 'mode': 755 } assert jsonify(details) == jsonify(expected)
def test_remove_file(): ''' Test we can remove a file and get the size propagated ''' disks.remove_file('/etc/new_file') details = disks.get_details('/etc/new_file') expected = {} assert jsonify(details) == jsonify(expected) details = disks.get_details('/etc') expected = { 'filetype': 'folder', 'size': 1111, 'owner': 'root', 'group': 'root', 'mode': 755 } assert jsonify(details) == jsonify(expected) details = disks.get_details('/') expected = { 'mount': True, 'size': 1000000, 'owner': 'root', 'group': 'root', 'mode': 755 } assert jsonify(details) == jsonify(expected)
def test_remove_folder(): ''' Test folder removal - fail on non recursive ''' disks.remove_file('/etc') details = disks.get_details('/etc') # Fail - not deleted expected = { 'filetype': 'folder', 'size': 1111, 'owner': 'root', 'group': 'root', 'mode': 755 } assert jsonify(details) == jsonify(expected) disks.remove_file('/etc', recursive=True) details = disks.get_details('/etc') # Fail - not deleted expected = {} assert jsonify(details) == jsonify(expected) details = disks.get_details('/') expected = { 'mount': True, 'size': 998889, 'owner': 'root', 'group': 'root', 'mode': 755 } assert jsonify(details) == jsonify(expected)
def test_add_new_file(): ''' Test we can add a new file and the size get propagated ''' disks.add_file('/etc/new_file', size=1000) details = disks.get_details('/etc/new_file') expected = { 'size': 1000, 'owner': 'root', 'group': 'root', 'filetype': 'file', 'mode': 755 } assert jsonify(details) == jsonify(expected) details = disks.get_details('/etc') expected = { 'filetype': 'folder', 'size': 2111, 'owner': 'root', 'group': 'root', 'mode': 755 } assert jsonify(details) == jsonify(expected) details = disks.get_details('/') expected = { 'mount': True, 'size': 1001000, 'owner': 'root', 'group': 'root', 'mode': 755 } assert jsonify(details) == jsonify(expected)
def test_get_details_of_good_file(): ''' Test we can fetch the details of a good file ''' details = disks.get_details('/etc/hosts') expected = { 'filetype': 'file', 'size': 11, 'owner': 'root', 'group': 'root', 'mode': 644 } assert jsonify(details) == jsonify(expected) details = disks.get_details('/etc') expected = { 'filetype': 'folder', 'size': 1111, 'owner': 'root', 'group': 'root', 'mode': 755 } assert jsonify(details) == jsonify(expected)
def patch(self, uid, tid): # TODO: (ten) authenticated uid is correct? data = json_decode(self.request.body) temp = yield self.db_temp.get_temp(tid) # TODO: limit input field if data.get('open'): if temp.get('activated'): self.temp_task.add_in_tasks(tid) else: self.set_status(400) self.finish({"error": "The temp-io is not activated!"}) return result = yield self.db_temp.update_temp(tid, data) if result is None: self.set_status(400) self.finish({"error": "Not found this temp"}) return data = jsonify(result) self.finish(data)
def get(self, uid): wio = Wio(self.current_user['token']) try: thing_list = yield wio.get_all_thing() except Exception as e: gen_log.error(e) self.set_status(400) self.finish({"error": "Get temp is failure, {}".format(e.message)}) return if thing_list is None: self.set_status(400) self.finish({"temps": []}) return docs = yield self.db_temp.get_all_temp_by_uid(uid) temp_list = [jsonify(doc) for doc in docs] for temp in temp_list[:]: # FIXME, need more efficiency for thing in thing_list: if thing['id'] == temp['id']: temp['online'] = thing['online'] self.finish({"temps": temp_list})
def get(self, uid, tid): # TODO: (ten) authenticated uid is correct? result = yield self.db_temp.get_temp(tid) if result is None: gen_log.error("Not this temp") self.set_status(400) self.finish({"error": "Not this temp"}) return wio = Wio(self.current_user['token']) try: thing_list = yield wio.get_all_thing() except Exception as e: gen_log.error(e) self.set_status(400) self.finish({"error": "Get thing is failure on Wio, {}".format(e.message)}) return data = jsonify(result) for thing in thing_list: if thing["id"] == data["id"]: data["online"] = thing["online"] self.finish(data)
def post(self, uid): wio = Wio(self.current_user['token']) try: thing = yield wio.add_thing() except Exception as e: gen_log.error(e) self.set_status(400) self.finish({"error": "Create thing is failure on Wio, {}".format(e.message)}) return cur_time = datetime.utcnow() document = { "uid": uid, "id": thing['id'], "key": thing['key'], # "online": False, "board_type": 1, "temperature": None, "temperature_f": None, "temperature_updated_at": None, "read_period": 60, "has_sleep": True, "status": "", "status_text": "", "open": False, "activated": False, "name": "", "description": "", "private": False, "gps": "", "picture_url": "", "updated_at": cur_time, "created_at": cur_time } result = yield self.db_temp.add_temp(document) data = jsonify(result) self.set_status(201) self.finish(data)
def post(self, uid, tid): temp = yield self.db_temp.get_temp(tid) if temp is None: gen_log.error("Not this temp") self.set_status(400) self.finish({"error": "Not this temp"}) return thing_key = temp['key'] wio = Wio(thing_key) try: result = yield wio.add_ota() except Exception as e: gen_log.error(e) self.set_status(400) self.finish({"error": "Trigger OTA is failure."}) return ota = yield self.db_temp.update_ota(tid, {"status": result['status'], "status_text": result['status_text']}) self.set_status(202) self.add_header("Location", "") # TODO, get status url self.finish(jsonify(ota)) IOLoop.current().add_callback(self.get_ota, tid, thing_key)
def test_remove_mount(): ''' Test mount point folder removal - fail ''' disks.remove_file('/boot') details = disks.get_details('/boot') # Fail - not deleted expected = { 'mount': True, 'size': 1111, 'owner': 'root', 'group': 'root', 'mode': 755 } assert jsonify(details) == jsonify(expected) disks.remove_file('/boot', recursive=True) details = disks.get_details('/boot') # Fail - not deleted expected = { 'mount': True, 'size': 1111, 'owner': 'root', 'group': 'root', 'mode': 755 } assert jsonify(details) == jsonify(expected) details = disks.get_details('/') # Unchanged from previous test and not propagated since other mount point expected = { 'mount': True, 'size': 998889, 'owner': 'root', 'group': 'root', 'mode': 755 } assert jsonify(details) == jsonify(expected)
def clear_completed(request): completed_todos = Todo.completed.filter(user=request.user) response = {'ids': [c.pk for c in completed_todos]} completed_todos.delete() return JsonResponse(jsonify(response))
def get(self, uid, tid): result = yield self.db_temperature.get_all_temperature_by_tid(tid) self.finish({"temperatures": jsonify(result)})
def get(self): docs = yield self.db_temp.get_all_public_temp() temp_list = [jsonify(doc) for doc in docs] self.finish({"temps": temp_list})
def get(self): uid = self.current_user['id'] user = yield self.db_user.get_user_by_uid(uid) user.pop('tokens') self.finish(jsonify(user))
def get(self, uid): user = yield self.db_user.get_user_by_uid(uid) user.pop('tokens') self.finish(jsonify(user))