|
| 1 | +#!/usr/bin/env python3 |
| 2 | +r"""Host Command Execution from Container |
| 3 | +
|
| 4 | +This test verifies that a container running on Infix can execute commands |
| 5 | +that affect the host system. Specifically, it confirms that the container |
| 6 | +can change the hostname of the host. |
| 7 | +""" |
| 8 | + |
| 9 | +import infamy |
| 10 | +from infamy.util import until, to_binary |
| 11 | + |
| 12 | +with infamy.Test() as test: |
| 13 | + cont_image = f"oci-archive:{infamy.Container.NFTABLES_IMAGE}" |
| 14 | + cont_name = "cont0" |
| 15 | + hostname_init = "container-host" |
| 16 | + hostname_new = "coffee" |
| 17 | + |
| 18 | + with test.step("Set up topology and attach to target DUT"): |
| 19 | + env = infamy.Env() |
| 20 | + target = env.attach("target", "mgmt") |
| 21 | + |
| 22 | + if not target.has_model("infix-containers"): |
| 23 | + test.skip() |
| 24 | + |
| 25 | + with test.step("Set initial hostname"): |
| 26 | + target.put_config_dict("ietf-system", { |
| 27 | + "system": { |
| 28 | + "hostname": hostname_init |
| 29 | + } |
| 30 | + }) |
| 31 | + |
| 32 | + with test.step("Verify initial hostname in operational"): |
| 33 | + oper = target.get_data("/ietf-system:system") |
| 34 | + name = oper["system"]["hostname"] |
| 35 | + |
| 36 | + if name != hostname_init: |
| 37 | + print(f"Expected hostname: {hostname_init}, actual hostname: {name}") |
| 38 | + test.fail() |
| 39 | + |
| 40 | + with test.step("Include script in OCI image to modify host hostname"): |
| 41 | + commands = to_binary(f"""#!/bin/sh |
| 42 | +nsenter -m/1/ns/mnt -u/1/ns/uts -i/1/ns/ipc -n/1/ns/net hostname {hostname_new} |
| 43 | +""") |
| 44 | + |
| 45 | + target.put_config_dict("infix-containers", { |
| 46 | + "containers": { |
| 47 | + "container": [ |
| 48 | + { |
| 49 | + "name": cont_name, |
| 50 | + "image": cont_image, |
| 51 | + "network": { |
| 52 | + "host": True |
| 53 | + }, |
| 54 | + "mount": [ |
| 55 | + { |
| 56 | + "name": "rc.local", |
| 57 | + "content": commands, |
| 58 | + "target": "/etc/rc.local", |
| 59 | + "mode": "0755" |
| 60 | + }, |
| 61 | + { |
| 62 | + "name": "proc1ns", |
| 63 | + "source": "/proc/1/ns", |
| 64 | + "target": "/1/ns", |
| 65 | + } |
| 66 | + ], |
| 67 | + "privileged": True |
| 68 | + } |
| 69 | + ] |
| 70 | + } |
| 71 | + }) |
| 72 | + |
| 73 | + with test.step("Verify container has started"): |
| 74 | + c = infamy.Container(target) |
| 75 | + until(lambda: c.running(cont_name), attempts=10) |
| 76 | + |
| 77 | + with test.step("Verify the new hostname set by the container"): |
| 78 | + oper = target.get_data("/ietf-system:system") |
| 79 | + name = oper["system"]["hostname"] |
| 80 | + |
| 81 | + if name != hostname_new: |
| 82 | + print(f"Expected hostname: {hostname_new}, actual hostname: {name}") |
| 83 | + test.fail() |
| 84 | + |
| 85 | + test.succeed() |
0 commit comments