aetherscale

[unmaintained] code for a cloud provider tutorial
Log | Files | Refs | README | LICENSE

commit f7263b94dd7ebc1ccb1b6054b0b3f2b84f3b37a7
parent 5e8d8de6544f15a7886dd4ae24f3ab83e8c3def4
Author: Stefan Koch <programming@stefan-koch.name>
Date:   Sat,  6 Feb 2021 16:58:42 +0100

rename file for REST api to rest.py

Diffstat:
Raetherscale/api/flask.py -> aetherscale/api/rest.py | 0
Maetherscale/server.py | 4++--
Dtests/test_api_flask.py | 71-----------------------------------------------------------------------
Atests/test_api_rest.py | 71+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 73 insertions(+), 73 deletions(-)

diff --git a/aetherscale/api/flask.py b/aetherscale/api/rest.py diff --git a/aetherscale/server.py b/aetherscale/server.py @@ -3,7 +3,7 @@ import sys from aetherscale import __version__ from aetherscale import dependencies import aetherscale.computing -import aetherscale.api.flask +import aetherscale.api.rest def main(): @@ -16,6 +16,6 @@ def main(): help_text = dependencies.build_dependency_help_text(missing_deps) print(help_text, file=sys.stderr) elif len(sys.argv) >= 2 and sys.argv[1] == 'http': - aetherscale.api.flask.run() + aetherscale.api.rest.run() else: aetherscale.computing.run() diff --git a/tests/test_api_flask.py b/tests/test_api_flask.py @@ -1,71 +0,0 @@ -import json -from unittest import mock -import pytest - -import aetherscale.api.flask - - -@pytest.fixture -def client(): - with aetherscale.api.flask.app.test_client() as client: - return client - - -@mock.patch('aetherscale.api.flask.ComputingHandler') -def test_list_vms(handler, client): - handler.return_value.list_vms.return_value = [[]] - rv = client.get('/vm') - assert rv.json == [] - - handler.return_value.list_vms.return_value = [[{'vm-id': 'abc123'}]] - rv = client.get('/vm') - assert len(rv.json) == 1 - - -@mock.patch('aetherscale.api.flask.ComputingHandler') -def test_create_vm(handler, client): - client.post( - '/vm', data=json.dumps({'image': 'dummy-image'}), - content_type='application/json') - - handler.return_value.create_vm.assert_called_with({'image': 'dummy-image'}) - - -@mock.patch('aetherscale.api.flask.ComputingHandler') -def test_delete_vm(handler, client): - client.delete('/vm/my-vm-id') - handler.return_value.delete_vm.assert_called_with({'vm-id': 'my-vm-id'}) - - -@mock.patch('aetherscale.api.flask.ComputingHandler') -def test_start_vm(handler, client): - handler.return_value.start_vm.return_value = [[ - {'vm-id': 'my-vm-id', 'status': 'started'}, - ]] - - client.patch( - '/vm/my-vm-id', data=json.dumps({'status': 'started'}), - content_type='application/json') - - handler.return_value.start_vm.assert_called_with({'vm-id': 'my-vm-id'}) - - # missing message must lead to error - rv = client.patch('/vm/my-vm-id') - assert rv.status_code == 400 - - -@mock.patch('aetherscale.api.flask.ComputingHandler') -def test_stop_vm(handler, client): - handler.return_value.stop_vm.return_value = [[ - {'vm-id': 'my-vm-id', 'status': 'stopped'}, - ]] - - client.patch( - '/vm/my-vm-id', data=json.dumps({'status': 'stopped'}), - content_type='application/json') - - handler.return_value.stop_vm.assert_called_with({'vm-id': 'my-vm-id'}) - - # missing message must lead to error - rv = client.patch('/vm/my-vm-id') - assert rv.status_code == 400 diff --git a/tests/test_api_rest.py b/tests/test_api_rest.py @@ -0,0 +1,71 @@ +import json +from unittest import mock +import pytest + +import aetherscale.api.rest + + +@pytest.fixture +def client(): + with aetherscale.api.rest.app.test_client() as client: + return client + + +@mock.patch('aetherscale.api.rest.ComputingHandler') +def test_list_vms(handler, client): + handler.return_value.list_vms.return_value = [[]] + rv = client.get('/vm') + assert rv.json == [] + + handler.return_value.list_vms.return_value = [[{'vm-id': 'abc123'}]] + rv = client.get('/vm') + assert len(rv.json) == 1 + + +@mock.patch('aetherscale.api.rest.ComputingHandler') +def test_create_vm(handler, client): + client.post( + '/vm', data=json.dumps({'image': 'dummy-image'}), + content_type='application/json') + + handler.return_value.create_vm.assert_called_with({'image': 'dummy-image'}) + + +@mock.patch('aetherscale.api.rest.ComputingHandler') +def test_delete_vm(handler, client): + client.delete('/vm/my-vm-id') + handler.return_value.delete_vm.assert_called_with({'vm-id': 'my-vm-id'}) + + +@mock.patch('aetherscale.api.rest.ComputingHandler') +def test_start_vm(handler, client): + handler.return_value.start_vm.return_value = [[ + {'vm-id': 'my-vm-id', 'status': 'started'}, + ]] + + client.patch( + '/vm/my-vm-id', data=json.dumps({'status': 'started'}), + content_type='application/json') + + handler.return_value.start_vm.assert_called_with({'vm-id': 'my-vm-id'}) + + # missing message must lead to error + rv = client.patch('/vm/my-vm-id') + assert rv.status_code == 400 + + +@mock.patch('aetherscale.api.rest.ComputingHandler') +def test_stop_vm(handler, client): + handler.return_value.stop_vm.return_value = [[ + {'vm-id': 'my-vm-id', 'status': 'stopped'}, + ]] + + client.patch( + '/vm/my-vm-id', data=json.dumps({'status': 'stopped'}), + content_type='application/json') + + handler.return_value.stop_vm.assert_called_with({'vm-id': 'my-vm-id'}) + + # missing message must lead to error + rv = client.patch('/vm/my-vm-id') + assert rv.status_code == 400