test/api: example of x-make-view-request

This commit is contained in:
Dan Bungert 2023-01-24 14:52:37 -07:00
parent bf6f09efa8
commit ec38ae9120
2 changed files with 50 additions and 1 deletions

View File

@ -0,0 +1,5 @@
version: 1
identity:
hostname: ai-test
password: "$y$j9T$UdY22v4Kexn/AZcIzBSUc0$2DnuvWDSwoDCFPlbk1ghZeT2qFrEBKY1bpFQoexHdw7"
username: ubuntu

View File

@ -89,10 +89,28 @@ class Client:
return await self.request('POST', query, data, **kwargs) return await self.request('POST', query, data, **kwargs)
async def request(self, method, query, data=None, **kwargs): async def request(self, method, query, data=None, **kwargs):
"""send a GET or POST to the test instance
args:
method: 'GET' or 'POST'
query: endpoint, such as '/locale'
keyword arguments:
data: body of request
headers: dict of custom headers to include in request
all other keyword arguments are turned into query arguments
get('/meta/status', cur='WAITING') is equivalent to
get('/meta/status?cur="WAITING"')
returns:
python data of response content
"""
headers = kwargs.pop('headers', None)
params = {k: self.dumps(v) for k, v in kwargs.items()} params = {k: self.dumps(v) for k, v in kwargs.items()}
data = self.dumps(data) data = self.dumps(data)
async with self.session.request(method, f'http://a{query}', async with self.session.request(method, f'http://a{query}',
data=data, params=params) as resp: data=data, params=params,
headers=headers) as resp:
print(unquote(str(resp.url))) print(unquote(str(resp.url)))
content = await resp.content.read() content = await resp.content.read()
content = content.decode() content = content.decode()
@ -149,6 +167,9 @@ class Server(Client):
self.proc.kill() self.proc.kill()
except ProcessLookupError: except ProcessLookupError:
pass pass
# https://github.com/python/cpython/issues/88050
# fixed in python 3.11
self.proc._transport.close()
class SystemSetupServer(Server): class SystemSetupServer(Server):
@ -244,6 +265,8 @@ async def start_server_factory(factory, *args, **kwargs):
async def start_server(*args, **kwargs): async def start_server(*args, **kwargs):
async with start_server_factory(Server, *args, **kwargs) as instance: async with start_server_factory(Server, *args, **kwargs) as instance:
sources = await instance.get('/source') sources = await instance.get('/source')
if sources is None:
raise Exception('unexpected /source response')
await instance.post( await instance.post(
'/source', source_id=sources['sources'][0]['id']) '/source', source_id=sources['sources'][0]['id'])
while True: while True:
@ -1561,6 +1584,27 @@ class TestUbuntuProContractSelection(TestAPI):
await inst.get('/ubuntu_pro/contract_selection/wait') await inst.get('/ubuntu_pro/contract_selection/wait')
class TestAutoinstallServer(TestAPI):
@timeout(2)
async def test_make_view_requests(self):
cfg = 'examples/simple.json'
extra = [
'--autoinstall', 'examples/autoinstall-short.yaml',
'--source-catalog', 'examples/install-sources.yaml',
]
async with start_server(cfg, extra_args=extra) as inst:
view_request_unspecified = await inst.get('/locale')
self.assertEqual('en_US.UTF-8', view_request_unspecified)
view_request_no = await inst.get(
'/locale', headers={'x-make-view-request': 'no'})
self.assertEqual('en_US.UTF-8', view_request_no)
view_request_yes = await inst.get(
'/locale', headers={'x-make-view-request': 'yes'})
self.assertIsNone(view_request_yes)
class TestWSLSetupOptions(TestAPI): class TestWSLSetupOptions(TestAPI):
async def test_wslsetupoptions(self): async def test_wslsetupoptions(self):
async with start_system_setup_server('examples/simple.json') as inst: async with start_system_setup_server('examples/simple.json') as inst: