Skip to content

Commit 7833b2b

Browse files
committed
refactor(test): Delete a helper function used only once
`_validate_mmds_snapshot()` is used only once. Signed-off-by: Takahiro Itazuri <[email protected]>
1 parent 8126df4 commit 7833b2b

File tree

1 file changed

+95
-112
lines changed

1 file changed

+95
-112
lines changed

tests/integration_tests/functional/test_mmds.py

Lines changed: 95 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -30,109 +30,6 @@
3030
MMDS_VERSIONS = ["V2", "V1"]
3131

3232

33-
def _validate_mmds_snapshot(
34-
basevm,
35-
microvm_factory,
36-
version,
37-
imds_compat,
38-
fc_binary_path=None,
39-
jailer_binary_path=None,
40-
):
41-
"""Test MMDS behaviour across snap-restore."""
42-
ipv4_address = "169.254.169.250"
43-
44-
# Configure MMDS version with custom IPv4 address.
45-
configure_mmds(
46-
basevm,
47-
version=version,
48-
iface_ids=["eth0"],
49-
ipv4_address=ipv4_address,
50-
imds_compat=imds_compat,
51-
)
52-
53-
expected_mmds_config = {
54-
"version": version,
55-
"ipv4_address": ipv4_address,
56-
"network_interfaces": ["eth0"],
57-
"imds_compat": False if imds_compat is None else imds_compat,
58-
}
59-
response = basevm.api.vm_config.get()
60-
assert response.json()["mmds-config"] == expected_mmds_config
61-
62-
data_store = {"latest": {"meta-data": {"ami-id": "ami-12345678"}}}
63-
populate_data_store(basevm, data_store)
64-
expected_response = "latest/" if imds_compat else data_store
65-
66-
basevm.start()
67-
ssh_connection = basevm.ssh
68-
run_guest_cmd(ssh_connection, f"ip route add {ipv4_address} dev eth0", "")
69-
70-
# Both V1 and V2 support token generation.
71-
token = generate_mmds_session_token(ssh_connection, ipv4_address, token_ttl=60)
72-
73-
# Fetch metadata.
74-
cmd = generate_mmds_get_request(
75-
ipv4_address,
76-
token=token,
77-
)
78-
run_guest_cmd(ssh_connection, cmd, expected_response, use_json=not imds_compat)
79-
80-
# Create snapshot.
81-
snapshot = basevm.snapshot_full()
82-
83-
# Resume microVM and ensure session token is still valid on the base.
84-
response = basevm.resume()
85-
86-
# Fetch metadata again using the same token.
87-
run_guest_cmd(ssh_connection, cmd, expected_response, use_json=not imds_compat)
88-
89-
# Kill base microVM.
90-
basevm.kill()
91-
92-
# Load microVM clone from snapshot.
93-
kwargs = {}
94-
if fc_binary_path:
95-
kwargs["fc_binary_path"] = fc_binary_path
96-
if jailer_binary_path:
97-
kwargs["jailer_binary_path"] = jailer_binary_path
98-
microvm = microvm_factory.build(**kwargs)
99-
microvm.spawn()
100-
microvm.restore_from_snapshot(snapshot, resume=True)
101-
102-
ssh_connection = microvm.ssh
103-
104-
# Check the reported MMDS config.
105-
response = microvm.api.vm_config.get()
106-
assert response.json()["mmds-config"] == expected_mmds_config
107-
108-
# Since V1 should accept GET request even with invalid token, don't regenerate a token for V1.
109-
if version == "V2":
110-
# Attempting to reuse the token across a restore must fail in V2.
111-
cmd = generate_mmds_get_request(ipv4_address, token=token)
112-
run_guest_cmd(ssh_connection, cmd, "MMDS token not valid.")
113-
114-
# Re-generate token.
115-
token = generate_mmds_session_token(ssh_connection, ipv4_address, token_ttl=60)
116-
117-
# Data store is empty after a restore.
118-
cmd = generate_mmds_get_request(ipv4_address, token=token)
119-
run_guest_cmd(
120-
ssh_connection,
121-
cmd,
122-
(
123-
"Cannot retrieve value. The value has an unsupported type."
124-
if imds_compat
125-
else "null"
126-
),
127-
)
128-
129-
# Now populate the store.
130-
populate_data_store(microvm, data_store)
131-
132-
# Fetch metadata.
133-
run_guest_cmd(ssh_connection, cmd, expected_response, use_json=not imds_compat)
134-
135-
13633
@pytest.mark.parametrize("version", MMDS_VERSIONS)
13734
@pytest.mark.parametrize("imds_compat", [True, False])
13835
def test_mmds_token(uvm_plain, version, imds_compat):
@@ -627,18 +524,104 @@ def test_mmds_snapshot(uvm_nano, microvm_factory, version, imds_compat):
627524
Ensures that the version is persisted or initialised with the default if
628525
the firecracker version does not support it.
629526
"""
630-
631527
current_release = working_version_as_artifact()
632-
uvm_nano.add_net_iface()
633-
_validate_mmds_snapshot(
634-
uvm_nano,
635-
microvm_factory,
636-
version,
637-
imds_compat,
638-
fc_binary_path=current_release.path,
639-
jailer_binary_path=current_release.jailer,
528+
fc_binary_path = current_release.path
529+
jailer_binary_path = current_release.jailer
530+
531+
basevm = uvm_nano
532+
basevm.add_net_iface()
533+
ipv4_address = "169.254.169.250"
534+
535+
# Configure MMDS version with custom IPv4 address.
536+
configure_mmds(
537+
basevm,
538+
version=version,
539+
iface_ids=["eth0"],
540+
ipv4_address=ipv4_address,
541+
imds_compat=imds_compat,
542+
)
543+
544+
expected_mmds_config = {
545+
"version": version,
546+
"ipv4_address": ipv4_address,
547+
"network_interfaces": ["eth0"],
548+
"imds_compat": False if imds_compat is None else imds_compat,
549+
}
550+
response = basevm.api.vm_config.get()
551+
assert response.json()["mmds-config"] == expected_mmds_config
552+
553+
data_store = {"latest": {"meta-data": {"ami-id": "ami-12345678"}}}
554+
populate_data_store(basevm, data_store)
555+
expected_response = "latest/" if imds_compat else data_store
556+
557+
basevm.start()
558+
ssh_connection = basevm.ssh
559+
run_guest_cmd(ssh_connection, f"ip route add {ipv4_address} dev eth0", "")
560+
561+
# Both V1 and V2 support token generation.
562+
token = generate_mmds_session_token(ssh_connection, ipv4_address, token_ttl=60)
563+
564+
# Fetch metadata.
565+
cmd = generate_mmds_get_request(
566+
ipv4_address,
567+
token=token,
568+
)
569+
run_guest_cmd(ssh_connection, cmd, expected_response, use_json=not imds_compat)
570+
571+
# Create snapshot.
572+
snapshot = basevm.snapshot_full()
573+
574+
# Resume microVM and ensure session token is still valid on the base.
575+
response = basevm.resume()
576+
577+
# Fetch metadata again using the same token.
578+
run_guest_cmd(ssh_connection, cmd, expected_response, use_json=not imds_compat)
579+
580+
# Kill base microVM.
581+
basevm.kill()
582+
583+
# Load microVM clone from snapshot.
584+
kwargs = {}
585+
if fc_binary_path:
586+
kwargs["fc_binary_path"] = fc_binary_path
587+
if jailer_binary_path:
588+
kwargs["jailer_binary_path"] = jailer_binary_path
589+
microvm = microvm_factory.build(**kwargs)
590+
microvm.spawn()
591+
microvm.restore_from_snapshot(snapshot, resume=True)
592+
593+
ssh_connection = microvm.ssh
594+
595+
# Check the reported MMDS config.
596+
response = microvm.api.vm_config.get()
597+
assert response.json()["mmds-config"] == expected_mmds_config
598+
599+
if version == "V2":
600+
# Attempting to reuse the token across a restore must fail in V2.
601+
cmd = generate_mmds_get_request(ipv4_address, token=token)
602+
run_guest_cmd(ssh_connection, cmd, "MMDS token not valid.")
603+
604+
# Re-generate token.
605+
token = generate_mmds_session_token(ssh_connection, ipv4_address, token_ttl=60)
606+
607+
# Data store is empty after a restore.
608+
cmd = generate_mmds_get_request(ipv4_address, token=token)
609+
run_guest_cmd(
610+
ssh_connection,
611+
cmd,
612+
(
613+
"Cannot retrieve value. The value has an unsupported type."
614+
if imds_compat
615+
else "null"
616+
),
640617
)
641618

619+
# Now populate the store.
620+
populate_data_store(microvm, data_store)
621+
622+
# Fetch metadata.
623+
run_guest_cmd(ssh_connection, cmd, expected_response, use_json=not imds_compat)
624+
642625

643626
def test_mmds_v2_negative(uvm_plain):
644627
"""

0 commit comments

Comments
 (0)