aetherscale

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

test_api_rest.py (2281B)


      1 import json
      2 from unittest import mock
      3 import pytest
      4 
      5 import aetherscale.api.rest
      6 
      7 
      8 @pytest.fixture
      9 def client():
     10     with aetherscale.api.rest.app.test_client() as client:
     11         return client
     12 
     13 
     14 @mock.patch('aetherscale.api.rest.ComputingHandler')
     15 def test_list_vms(handler, client):
     16     handler.return_value.list_vms.return_value = [[]]
     17     rv = client.get('/vm')
     18     assert rv.json == []
     19 
     20     handler.return_value.list_vms.return_value = [[{'vm-id': 'abc123'}]]
     21     rv = client.get('/vm')
     22     assert len(rv.json) == 1
     23 
     24 
     25 @mock.patch('aetherscale.api.rest.ComputingHandler')
     26 def test_show_vm_info(handler, client):
     27     handler.return_value.vm_info.return_value = [{'vm-id': 'abc123'}]
     28     rv = client.get('/vm/abc123')
     29     assert rv.json['vm-id'] == 'abc123'
     30 
     31 
     32 @mock.patch('aetherscale.api.rest.ComputingHandler')
     33 def test_create_vm(handler, client):
     34     client.post(
     35         '/vm', data=json.dumps({'image': 'dummy-image'}),
     36         content_type='application/json')
     37 
     38     handler.return_value.create_vm.assert_called_with({'image': 'dummy-image'})
     39 
     40 
     41 @mock.patch('aetherscale.api.rest.ComputingHandler')
     42 def test_delete_vm(handler, client):
     43     client.delete('/vm/my-vm-id')
     44     handler.return_value.delete_vm.assert_called_with({'vm-id': 'my-vm-id'})
     45 
     46 
     47 @mock.patch('aetherscale.api.rest.ComputingHandler')
     48 def test_start_vm(handler, client):
     49     handler.return_value.start_vm.return_value = [[
     50         {'vm-id': 'my-vm-id', 'status': 'started'},
     51     ]]
     52 
     53     client.patch(
     54         '/vm/my-vm-id', data=json.dumps({'status': 'started'}),
     55         content_type='application/json')
     56 
     57     handler.return_value.start_vm.assert_called_with({'vm-id': 'my-vm-id'})
     58 
     59     # missing message must lead to error
     60     rv = client.patch('/vm/my-vm-id')
     61     assert rv.status_code == 400
     62 
     63 
     64 @mock.patch('aetherscale.api.rest.ComputingHandler')
     65 def test_stop_vm(handler, client):
     66     handler.return_value.stop_vm.return_value = [[
     67         {'vm-id': 'my-vm-id', 'status': 'stopped'},
     68     ]]
     69 
     70     client.patch(
     71         '/vm/my-vm-id', data=json.dumps({'status': 'stopped'}),
     72         content_type='application/json')
     73 
     74     handler.return_value.stop_vm.assert_called_with({'vm-id': 'my-vm-id'})
     75 
     76     # missing message must lead to error
     77     rv = client.patch('/vm/my-vm-id')
     78     assert rv.status_code == 400