|
| 1 | +import io |
| 2 | +import os |
| 3 | +import re |
| 4 | +import subprocess |
| 5 | +import tarfile |
| 6 | +import tempfile |
| 7 | + |
| 8 | +from .incus_test_vm import IncusTestVM, IncusOSException |
| 9 | + |
| 10 | +def TestFlasherToolStableIMG(_): |
| 11 | + test_name = "flasher-tool-stable-img" |
| 12 | + |
| 13 | + test_image = _flasher_download_image("stable", "img") |
| 14 | + |
| 15 | + with IncusTestVM(test_name, test_image) as vm: |
| 16 | + vm.StartVM() |
| 17 | + vm.WaitAgentRunning() |
| 18 | + vm.WaitExpectedLog("incus-osd", "Installing IncusOS source=/dev/sdb target=/dev/sda") |
| 19 | + vm.WaitExpectedLog("incus-osd", "IncusOS was successfully installed") |
| 20 | + |
| 21 | +def TestFlasherToolTestingISO(_): |
| 22 | + test_name = "flasher-tool-testing-iso" |
| 23 | + |
| 24 | + test_image = _flasher_download_image("testing", "iso") |
| 25 | + |
| 26 | + with IncusTestVM(test_name, test_image) as vm: |
| 27 | + vm.StartVM() |
| 28 | + vm.WaitAgentRunning() |
| 29 | + vm.WaitExpectedLog("incus-osd", "Installing IncusOS source=/dev/mapper/sr0 target=/dev/sda") |
| 30 | + vm.WaitExpectedLog("incus-osd", "IncusOS was successfully installed") |
| 31 | + |
| 32 | +def _flasher_download_image(channel, image_format): |
| 33 | + if not os.path.exists("./incus-osd/flasher-tool"): |
| 34 | + subprocess.run(["go", "build", "./cmd/flasher-tool/"], cwd=os.path.join(os.getcwd(), "incus-osd"), capture_output=True, check=True) |
| 35 | + |
| 36 | + with tempfile.TemporaryDirectory(dir=os.getcwd()) as tmp_dir: |
| 37 | + with tarfile.open(os.path.join(tmp_dir, "seed.tar"), mode="w") as tar: |
| 38 | + raw = "force_reboot: true".encode("utf-8") |
| 39 | + buf = io.BytesIO(raw) |
| 40 | + ti = tarfile.TarInfo(name="install.yaml") |
| 41 | + ti.size = len(raw) |
| 42 | + tar.addfile(ti, buf) |
| 43 | + |
| 44 | + result = subprocess.run(["../incus-osd/flasher-tool", "--channel", channel, "--format", image_format, "--seed", "seed.tar"], cwd=tmp_dir, capture_output=True, check=True) |
| 45 | + |
| 46 | + match = re.search("Downloading and decompressing IncusOS image \\(" + image_format + "\\) version (\\d+) from Linux Containers CDN", str(result.stderr)) |
| 47 | + if not match: |
| 48 | + raise IncusOSException("Failed to download image") |
| 49 | + |
| 50 | + os.rename(os.path.join(tmp_dir, "IncusOS_" + match.group(1) + "." + image_format), "flasher-install-image." + image_format) |
| 51 | + |
| 52 | + return os.path.join(os.getcwd(), "flasher-install-image." + image_format) |
0 commit comments