Skip to content

Commit 95b3aa7

Browse files
Alexandra Iordacheandreeaflorescu
authored andcommitted
api: integration tests for invalid request fields
Signed-off-by: Alexandra Iordache <[email protected]>
1 parent d38b59a commit 95b3aa7

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed

tests/functional/test_api.py

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,3 +448,141 @@ def test_rate_limiters_api_config(test_microvm_with_api):
448448
)
449449
""" Verify the request succeeded """
450450
assert(test_microvm.api_session.is_good_response(response.status_code))
451+
452+
453+
@pytest.mark.timeout(100)
454+
def test_api_unknown_fields(test_microvm_with_api):
455+
""" Tests that requests with unknown fields result in error 400 """
456+
457+
test_microvm = test_microvm_with_api
458+
459+
""" Test invalid field for APILoggerDescription """
460+
""" path -> pth """
461+
response = test_microvm.api_session.put(
462+
test_microvm.logger_url,
463+
json = {
464+
'pth': 'firecracker.log',
465+
'level': 'Info',
466+
'show_level': True,
467+
'show_log_origin': True
468+
}
469+
)
470+
assert response.status_code == 400
471+
472+
""" Test invalid field for BootSourceBody """
473+
""" source_type -> source-type """
474+
response = test_microvm.api_session.put(
475+
test_microvm.boot_cfg_url,
476+
json = {
477+
'boot_source_id': 'alinux_kernel',
478+
'source-type': 'LocalImage',
479+
'local_image': { 'kernel-image_path': test_microvm.slot.kernel_file },
480+
}
481+
)
482+
assert response.status_code == 400
483+
484+
""" Test invalid field for LocalImage """
485+
""" kernel_image_path -> kernel-image_path """
486+
response = test_microvm.api_session.put(
487+
test_microvm.boot_cfg_url,
488+
json = {
489+
'boot_source_id': 'alinux_kernel',
490+
'source_type': 'LocalImage',
491+
'local_image': { 'kernel-image_path': test_microvm.slot.kernel_file },
492+
}
493+
)
494+
assert response.status_code == 400
495+
496+
""" Test invalid field for DriveDescription """
497+
""" drive_id -> drive-id """
498+
response = test_microvm.api_session.put(
499+
test_microvm.blk_cfg_url,
500+
json = {
501+
'drive-id': 'root',
502+
'path_on_host': test_microvm.slot.rootfs_file,
503+
'is_root_device': True,
504+
'permissions': 'rw',
505+
'state': 'Attached'
506+
}
507+
)
508+
assert response.status_code == 400
509+
510+
""" Test invalid field for MachineConfiguration """
511+
""" vcpu_count -> vcpu-count """
512+
response = test_microvm.api_session.put(
513+
test_microvm.microvm_cfg_url,
514+
json = { 'vcpu-count': 4, 'mem_size_mib': 256 }
515+
)
516+
assert response.status_code == 400
517+
518+
""" Test invalid field for NetworkInterfaceBody """
519+
""" iface_id -> iface-id """
520+
response = test_microvm.api_session.put(
521+
test_microvm.net_cfg_url,
522+
json = {
523+
'iface-id': 1,
524+
'host_dev_name': 'vmtap33',
525+
'guest_mac': '06:00:00:00:00:01',
526+
'state': 'Attached'
527+
}
528+
)
529+
assert response.status_code == 400
530+
531+
""" Test invalid field for TokenBucketDescription """
532+
""" size -> siz """
533+
response = test_microvm.api_session.put(
534+
test_microvm.net_cfg_url,
535+
json = {
536+
'iface_id': 1,
537+
'host_dev_name': 'vmtap33',
538+
'guest_mac': '06:00:00:00:00:01',
539+
'state': 'Attached',
540+
'rx_rate_limiter': {
541+
'bandwidth': { 'siz': 1000000, 'refill_time': 1000 }
542+
}
543+
}
544+
)
545+
assert response.status_code == 400
546+
547+
""" Test invalid field for RateLimiterDescription """
548+
""" ops -> op """
549+
response = test_microvm.api_session.put(
550+
test_microvm.net_cfg_url,
551+
json = {
552+
'iface_id': 1,
553+
'host_dev_name': 'vmtap33',
554+
'guest_mac': '06:00:00:00:00:01',
555+
'state': 'Attached',
556+
'rx_rate_limiter': {
557+
'op': { 'size': 1000000, 'refill_time': 1000 }
558+
}
559+
}
560+
)
561+
assert response.status_code == 400
562+
563+
""" Test invalid field for AsyncRequestBody """
564+
""" action_id -> action-id """
565+
response = test_microvm.api_session.put(
566+
test_microvm.actions_url,
567+
json = {
568+
'action_di': 'start',
569+
'action_type': 'InstanceStart',
570+
}
571+
)
572+
assert response.status_code == 400
573+
574+
""" Test invalid field for InstanceDeviceDetachAction """
575+
""" device_resource_id -> device_resource_di """
576+
response = test_microvm.api_session.put(
577+
test_microvm.actions_url,
578+
json = {
579+
'action_id': 'start3',
580+
'action_type': 'InstanceStart',
581+
'instance_device_detach_action': {
582+
'device_type': 'Drive',
583+
'device_resource_di': 1,
584+
'force': True
585+
}
586+
}
587+
)
588+
assert response.status_code == 400

tests/microvm.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ class Microvm:
257257
blk_cfg_resource = 'drives'
258258
boot_cfg_resource = 'boot-source'
259259
actions_resource = 'actions'
260+
logger_resource = 'logger'
260261
# TODO: Get the API paths from the Firecracker API definition.
261262

262263
def __init__(
@@ -308,6 +309,7 @@ def spawn(self):
308309
self.blk_cfg_url = self.api_url + self.blk_cfg_resource
309310
self.boot_cfg_url = self.api_url + self.boot_cfg_resource
310311
self.actions_url = self.api_url + self.actions_resource
312+
self.logger_url = self.api_url + self.logger_resource
311313

312314
self.ensure_firecracker_binary()
313315

0 commit comments

Comments
 (0)