- Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathtest_cmd_line_start.py
491 lines (384 loc) · 16.4 KB
/
test_cmd_line_start.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""Tests microvm start with configuration file as command line parameter."""
importjson
importos
importplatform
importre
importshutil
frompathlibimportPath
importpytest
fromtenacityimportRetrying, retry_if_exception_type, stop_after_attempt, wait_fixed
fromframeworkimportutils
fromframework.utilsimportgenerate_mmds_get_request, generate_mmds_session_token
fromframework.utils_cpu_templatesimportSUPPORTED_CPU_TEMPLATES
# Directory with metadata JSON files
DIR=Path("./data")
def_configure_vm_from_json(test_microvm, vm_config_file):
"""
Configure a microvm using a file sent as command line parameter.
Create resources needed for the configuration of the microvm and
set as configuration file a copy of the file that was passed as
parameter to this helper function.
"""
# since we don't use basic-config, we do it by hand
test_microvm.create_jailed_resource(test_microvm.kernel_file)
test_microvm.create_jailed_resource(test_microvm.rootfs_file)
vm_config_file=Path(vm_config_file)
obj=json.load(vm_config_file.open(encoding="UTF-8"))
obj["boot-source"]["kernel_image_path"] =str(test_microvm.kernel_file.name)
obj["drives"][0]["path_on_host"] =str(test_microvm.rootfs_file.name)
obj["drives"][0]["is_read_only"] =True
vm_config=Path(test_microvm.chroot()) /vm_config_file.name
vm_config.write_text(json.dumps(obj))
test_microvm.jailer.extra_args= {"config-file": vm_config.name}
returnobj
def_add_metadata_file(test_microvm, metadata_file):
"""
Configure the microvm using a metadata file.
Given a test metadata file this creates a copy of the file and
uses the copy to configure the microvm.
"""
vm_metadata_path=os.path.join(test_microvm.path, os.path.basename(metadata_file))
shutil.copyfile(metadata_file, vm_metadata_path)
test_microvm.metadata_file=vm_metadata_path
def_configure_network_interface(test_microvm):
"""
Create tap interface before spawning the microVM.
The network namespace is already pre-created.
The tap interface has to be created beforehand when starting the microVM
from a config file.
"""
# Create tap device, and avoid creating it in the guest since it is already
# specified in the JSON
test_microvm.add_net_iface(api=False)
def_build_cmd_to_fetch_metadata(ssh_connection, version, ipv4_address):
"""
Build command to fetch metadata from the guest's side.
The request is built based on the MMDS version configured.
If MMDSv2 is used, a session token must be created before
the `GET` request.
"""
# Fetch data from MMDS from the guest's side.
ifversion=="V2":
# If MMDS is configured to version 2, so we need to create
# the session token first.
token=generate_mmds_session_token(ssh_connection, ipv4_address, token_ttl=60)
else:
token=None
returngenerate_mmds_get_request(ipv4_address, token)
def_get_optional_fields_from_file(vm_config_file):
"""
Retrieve optional `version` and `ipv4_address` fields from MMDS config.
Parse the vm config json file and retrieves optional fields from MMDS
config. Default values are used for the fields that are not specified.
:return: a pair of (version, ipv4_address) fields from mmds config.
"""
# Get MMDS version and IPv4 address configured from the file.
withopen(vm_config_file, encoding="utf-8") asjson_file:
mmds_config=json.load(json_file)["mmds-config"]
# Default to V1 if version is not specified.
version=mmds_config.get("version", "V1")
# Set to default if IPv4 is not specified .
ipv4_address=mmds_config.get("ipv4_address", "169.254.169.254")
returnversion, ipv4_address
@pytest.mark.parametrize("vm_config_file", ["framework/vm_config.json"])
deftest_config_start_with_api(uvm_plain, vm_config_file):
"""
Test if a microvm configured from file boots successfully.
"""
test_microvm=uvm_plain
vm_config=_configure_vm_from_json(test_microvm, vm_config_file)
test_microvm.spawn()
asserttest_microvm.state=="Running"
# Validate full vm configuration.
response=test_microvm.api.vm_config.get()
assertresponse.json() ==vm_config
@pytest.mark.parametrize("vm_config_file", ["framework/vm_config.json"])
deftest_config_start_no_api(uvm_plain, vm_config_file):
"""
Test microvm start when API server thread is disabled.
"""
test_microvm=uvm_plain
_configure_vm_from_json(test_microvm, vm_config_file)
test_microvm.jailer.extra_args.update({"no-api": None})
test_microvm.spawn()
# Get names of threads in Firecracker.
cmd=f"ps -T --no-headers -p {test_microvm.firecracker_pid} | awk '{{print $5}}'"
# Retry running 'ps' in case it failed to list the firecracker process
# The regex matches any expression that contains 'firecracker' and does
# not contain 'fc_api'
forattemptinRetrying(
retry=retry_if_exception_type(RuntimeError),
stop=stop_after_attempt(10),
wait=wait_fixed(1),
reraise=True,
):
withattempt:
utils.search_output_from_cmd(
cmd=cmd,
find_regex=re.compile("^(?!.*fc_api)(?:.*)?firecracker", re.DOTALL),
)
@pytest.mark.parametrize("vm_config_file", ["framework/vm_config_network.json"])
deftest_config_start_no_api_exit(uvm_plain, vm_config_file):
"""
Test microvm exit when API server is disabled.
"""
test_microvm=uvm_plain
_configure_vm_from_json(test_microvm, vm_config_file)
_configure_network_interface(test_microvm)
test_microvm.jailer.extra_args.update({"no-api": None})
test_microvm.spawn() # Start Firecracker and MicroVM
test_microvm.ssh.run("reboot") # Exit
test_microvm.mark_killed() # waits for process to terminate
# Check error log and exit code
test_microvm.check_log_message("Firecracker exiting successfully")
asserttest_microvm.get_exit_code() ==0
@pytest.mark.parametrize(
"vm_config_file",
[
"framework/vm_config_missing_vcpu_count.json",
"framework/vm_config_missing_mem_size_mib.json",
],
)
deftest_config_bad_machine_config(uvm_plain, vm_config_file):
"""
Test microvm start when the `machine_config` is invalid.
"""
test_microvm=uvm_plain
_configure_vm_from_json(test_microvm, vm_config_file)
test_microvm.jailer.extra_args.update({"no-api": None})
test_microvm.spawn()
test_microvm.check_log_message("Configuration for VMM from one single json failed")
test_microvm.mark_killed()
@pytest.mark.parametrize(
"test_config",
[
("framework/vm_config_cpu_template_C3.json", True, False),
("framework/vm_config_smt_true.json", False, True),
],
)
deftest_config_machine_config_params(uvm_plain, test_config):
"""
Test microvm start with optional `machine_config` parameters.
"""
test_microvm=uvm_plain
# Test configuration determines if the file is a valid config or not
# based on the CPU
(vm_config_file, cpu_template_used, smt_used) =test_config
_configure_vm_from_json(test_microvm, vm_config_file)
test_microvm.jailer.extra_args.update({"no-api": None})
test_microvm.spawn()
should_fail=False
ifcpu_template_usedand"C3"notinSUPPORTED_CPU_TEMPLATES:
should_fail=True
ifsmt_usedand (platform.machine() =="aarch64"):
should_fail=True
ifshould_fail:
test_microvm.check_any_log_message(
[
"Failed to build MicroVM from Json",
"Could not Start MicroVM from one single json",
]
)
test_microvm.mark_killed()
else:
test_microvm.check_log_message(
"Successfully started microvm that was configured from one single json"
)
@pytest.mark.parametrize("vm_config_file", ["framework/vm_config.json"])
deftest_config_start_with_limit(uvm_plain, vm_config_file):
"""
Negative test for customised request payload limit.
"""
test_microvm=uvm_plain
_configure_vm_from_json(test_microvm, vm_config_file)
test_microvm.jailer.extra_args.update({"http-api-max-payload-size": "250"})
test_microvm.spawn()
asserttest_microvm.state=="Running"
cmd="curl --unix-socket {} -i".format(test_microvm.api.socket)
cmd+=' -X PUT "http://localhost/mmds/config"'
cmd+=' -H "Content-Length: 260"'
cmd+=' -H "Accept: application/json"'
cmd+=' -d "some body"'
response="HTTP/1.1 400 \r\n"
response+="Server: Firecracker API\r\n"
response+="Connection: keep-alive\r\n"
response+="Content-Type: application/json\r\n"
response+="Content-Length: 145\r\n\r\n"
response+='{ "error": "Request payload with size 260 is larger than '
response+="the limit of 250 allowed by server.\n"
response+='All previous unanswered requests will be dropped." }'
_, stdout, _=utils.check_output(cmd)
assertstdout.encode("utf-8") ==response.encode("utf-8")
@pytest.mark.parametrize("vm_config_file", ["framework/vm_config.json"])
deftest_config_with_default_limit(uvm_plain, vm_config_file):
"""
Test for request payload limit.
"""
test_microvm=uvm_plain
_configure_vm_from_json(test_microvm, vm_config_file)
test_microvm.spawn()
asserttest_microvm.state=="Running"
data_store= {"latest": {"meta-data": {}}}
data_store["latest"]["meta-data"]["ami-id"] ="abc"
test_microvm.api.mmds.put(json=data_store)
cmd_err="curl --unix-socket {} -i".format(test_microvm.api.socket)
cmd_err+=' -X PUT "http://localhost/mmds/config"'
cmd_err+=' -H "Content-Length: 51201"'
cmd_err+=' -H "Accept: application/json"'
cmd_err+=' -d "some body"'
response_err="HTTP/1.1 400 \r\n"
response_err+="Server: Firecracker API\r\n"
response_err+="Connection: keep-alive\r\n"
response_err+="Content-Type: application/json\r\n"
response_err+="Content-Length: 149\r\n\r\n"
response_err+='{ "error": "Request payload with size 51201 is larger '
response_err+="than the limit of 51200 allowed by server.\n"
response_err+='All previous unanswered requests will be dropped." }'
_, stdout, _stderr=utils.check_output(cmd_err)
assertstdout.encode("utf-8") ==response_err.encode("utf-8")
deftest_start_with_metadata(uvm_plain):
"""
Test if metadata from file is available via MMDS.
"""
test_microvm=uvm_plain
metadata_file=DIR/"metadata.json"
_add_metadata_file(test_microvm, metadata_file)
test_microvm.spawn()
test_microvm.check_log_message("Successfully added metadata to mmds from file")
asserttest_microvm.state=="Not started"
response=test_microvm.api.mmds.get()
withopen(metadata_file, encoding="utf-8") asjson_file:
assertresponse.json() ==json.load(json_file)
deftest_start_with_metadata_limit(uvm_plain):
"""
Test that the metadata size limit is enforced when populating from a file.
"""
test_microvm=uvm_plain
test_microvm.jailer.extra_args.update({"mmds-size-limit": "30"})
metadata_file=DIR/"metadata.json"
_add_metadata_file(test_microvm, metadata_file)
test_microvm.spawn()
test_microvm.check_log_message(
"Populating MMDS from file failed: The MMDS patch request doesn't fit."
)
test_microvm.mark_killed()
deftest_start_with_metadata_default_limit(uvm_plain):
"""
Test that the metadata size limit defaults to the api payload limit.
"""
test_microvm=uvm_plain
test_microvm.jailer.extra_args.update({"http-api-max-payload-size": "30"})
metadata_file=DIR/"metadata.json"
_add_metadata_file(test_microvm, metadata_file)
test_microvm.spawn()
test_microvm.check_log_message(
"Populating MMDS from file failed: The MMDS patch request doesn't fit."
)
test_microvm.mark_killed()
deftest_start_with_missing_metadata(uvm_plain):
"""
Test if a microvm is configured with a missing metadata file.
"""
test_microvm=uvm_plain
metadata_file="../resources/tests/metadata_nonexisting.json"
vm_metadata_path=os.path.join(test_microvm.path, os.path.basename(metadata_file))
test_microvm.metadata_file=vm_metadata_path
try:
test_microvm.spawn()
exceptFileNotFoundError:
pass
finally:
test_microvm.check_log_message(
"Unable to open or read from the mmds content file"
)
test_microvm.check_log_message("No such file or directory")
test_microvm.mark_killed()
deftest_start_with_invalid_metadata(uvm_plain):
"""
Test if a microvm is configured with a invalid metadata file.
"""
test_microvm=uvm_plain
metadata_file=DIR/"metadata_invalid.json"
vm_metadata_path=os.path.join(test_microvm.path, os.path.basename(metadata_file))
shutil.copy(metadata_file, vm_metadata_path)
test_microvm.metadata_file=vm_metadata_path
try:
test_microvm.spawn()
exceptFileNotFoundError:
pass
finally:
test_microvm.check_log_message("MMDS error: metadata provided not valid json")
test_microvm.check_log_message("EOF while parsing an object")
test_microvm.mark_killed()
@pytest.mark.parametrize(
"vm_config_file",
["framework/vm_config_with_mmdsv1.json", "framework/vm_config_with_mmdsv2.json"],
)
deftest_config_start_and_mmds_with_api(uvm_plain, vm_config_file):
"""
Test MMDS behavior when the microvm is configured from file.
"""
test_microvm=uvm_plain
_configure_vm_from_json(test_microvm, vm_config_file)
_configure_network_interface(test_microvm)
# Network namespace has already been created.
test_microvm.spawn()
data_store= {
"latest": {
"meta-data": {"ami-id": "ami-12345678", "reservation-id": "r-fea54097"}
}
}
# MMDS should be empty by default.
response=test_microvm.api.mmds.get()
assertresponse.json() == {}
# Populate MMDS with data.
test_microvm.api.mmds.put(**data_store)
# Ensure the MMDS contents have been successfully updated.
response=test_microvm.api.mmds.get()
assertresponse.json() ==data_store
# Get MMDS version and IPv4 address configured from the file.
version, ipv4_address=_get_optional_fields_from_file(vm_config_file)
cmd="ip route add {} dev eth0".format(ipv4_address)
_, stdout, stderr=test_microvm.ssh.run(cmd)
assertstderr==stdout==""
# Fetch data from MMDS from the guest's side.
cmd=_build_cmd_to_fetch_metadata(test_microvm.ssh, version, ipv4_address)
cmd+="/latest/meta-data/"
_, stdout, _=test_microvm.ssh.run(cmd)
assertjson.loads(stdout) ==data_store["latest"]["meta-data"]
# Validate MMDS configuration.
response=test_microvm.api.vm_config.get()
assertresponse.json()["mmds-config"] == {
"network_interfaces": ["1"],
"ipv4_address": ipv4_address,
"version": version,
}
@pytest.mark.parametrize(
"vm_config_file",
["framework/vm_config_with_mmdsv1.json", "framework/vm_config_with_mmdsv2.json"],
)
@pytest.mark.parametrize("metadata_file", [DIR/"metadata.json"])
deftest_with_config_and_metadata_no_api(uvm_plain, vm_config_file, metadata_file):
"""
Test microvm start when config/mmds and API server thread is disabled.
Ensures the metadata is stored successfully inside the MMDS and
is available to reach from the guest's side.
"""
test_microvm=uvm_plain
_configure_vm_from_json(test_microvm, vm_config_file)
_add_metadata_file(test_microvm, metadata_file)
_configure_network_interface(test_microvm)
test_microvm.jailer.extra_args.update({"no-api": None})
test_microvm.spawn()
# Get MMDS version and IPv4 address configured from the file.
version, ipv4_address=_get_optional_fields_from_file(vm_config_file)
cmd="ip route add {} dev eth0".format(ipv4_address)
_, stdout, stderr=test_microvm.ssh.run(cmd)
assertstderr==stdout==""
# Fetch data from MMDS from the guest's side.
cmd=_build_cmd_to_fetch_metadata(test_microvm.ssh, version, ipv4_address)
_, stdout, _=test_microvm.ssh.run(cmd)
# Compare response against the expected MMDS contents.
assertjson.loads(stdout) ==json.load(Path(metadata_file).open(encoding="UTF-8"))