"""Exercise image startup settings through the private provisioning boundary.""" from __future__ import annotations import time import pytest from test_app import CLOUD_CREATE_BODY from test_engine_boundary import ( SUPER, USER, boundary, call, import_cloud_template_for_test, ) def set_script(client, template, script): path = f"/internal/v1/vm-templates/{template['id']}/startup-script" current = call(client, "GET", path, SUPER) assert current.status_code == 200, current.text saved = call(client, "PATCH", path, SUPER, { "script": script, "expected_revision": current.json()["revision"], "created_at": current.json()["created_at"], }) assert saved.status_code == 200, saved.text return saved.json() def create(client, template, *, host="hk", suffix="first", ip="8.8.8.18/24"): actor = { **USER, "can_provision": True, "operation_id": f"startup-script-create-{suffix}", } response = call(client, "POST", f"/internal/v1/hosts/{host}/vms", actor, { **CLOUD_CREATE_BODY, "template_id": template["id"], "name": f"startup-{suffix}", "public_ip_cidr": ip, }) assert response.status_code == 201, response.text return response, actor def created_specs(service): return [row[1] for row in service.calls if row[0] == "create_cloud_vm"] def test_provisioning_uses_selected_image_script_and_keeps_legacy_images_empty( boundary, tmp_path): client, engine, _store, _logs, services = boundary first, _ = import_cloud_template_for_test(client, tmp_path, "First Image") second, _ = import_cloud_template_for_test(client, tmp_path, "Second Image") legacy, _ = import_cloud_template_for_test(client, tmp_path, "Legacy Image") first_script = "#!/bin/sh\necho image-one > /root/image-one\n" second_script = "#!/bin/sh\necho image-two > /root/image-two\n" set_script(client, first, first_script) set_script(client, second, second_script) # Imported images predating the feature have no private settings fields. legacy_record = engine.registry.cloud_templates.catalog.get(legacy["id"]) assert "startup_script" not in legacy_record assert "startup_script_revision" not in legacy_record create(client, first, suffix="one") create(client, second, host="jp", suffix="two") create(client, legacy, suffix="legacy", ip="8.8.8.20/24") assert created_specs(services["hk"])[0]["startup_script"] == first_script assert created_specs(services["jp"])[0]["startup_script"] == second_script assert "startup_script" not in created_specs(services["hk"])[1] # Explicitly clearing a script has the same create behavior as an old image. set_script(client, first, "") create(client, first, suffix="cleared", ip="8.8.8.21/24") assert "startup_script" not in created_specs(services["hk"])[2] def test_script_is_snapshotted_for_create_and_hidden_from_tenant_results( boundary, tmp_path): client, _engine, _store, _logs, services = boundary template, _ = import_cloud_template_for_test(client, tmp_path, "Script Image") first_script = "#!/bin/sh\necho admin-only-script-v1 > /root/first\n" next_script = "#!/bin/sh\necho admin-only-script-v2 > /root/second\n" set_script(client, template, first_script) first, actor = create(client, template, suffix="before-edit") set_script(client, template, next_script) second, _ = create(client, template, suffix="after-edit", ip="8.8.8.19/24") assert [spec["startup_script"] for spec in created_specs(services["hk"])] == [ first_script, next_script, ] public_responses = [first, second] for path in ( "/internal/v1/vm-templates", "/internal/v1/hosts/hk/vm-templates", "/internal/v1/hosts/hk/vms", f"/internal/v1/hosts/hk/vms/{second.json()['uuid']}", f"/internal/v1/operations/{actor['operation_id']}", ): response = call(client, "GET", path, USER) assert response.status_code == 200, response.text public_responses.append(response) for response in public_responses: assert "startup_script" not in response.text assert "admin-only-script" not in response.text @pytest.mark.parametrize("extra", [ {"startup_script": "#!/bin/sh\necho tenant-script\n"}, {"startup_script_revision": 500}, ]) def test_tenant_cannot_override_image_script_in_provisioning( boundary, tmp_path, extra): client, _engine, _store, _logs, services = boundary template, _ = import_cloud_template_for_test(client, tmp_path, "Trusted Image") script = "#!/bin/sh\necho trusted-image-script\n" set_script(client, template, script) rejected = call(client, "POST", "/internal/v1/hosts/hk/vms", { **USER, "can_provision": True, "operation_id": "startup-script-tenant-override", }, {**CLOUD_CREATE_BODY, "template_id": template["id"], **extra}) assert rejected.status_code == 422, rejected.text assert not any(created_specs(service) for service in services.values()) # Validation failure must not poison the VM admission path either. create(client, template, suffix="after-rejection") assert created_specs(services["hk"])[0]["startup_script"] == script def test_reinstall_does_not_dispatch_initial_install_script( boundary, tmp_path, monkeypatch): client, _engine, _store, _logs, services = boundary original, _ = import_cloud_template_for_test(client, tmp_path, "Original Image") target, _ = import_cloud_template_for_test(client, tmp_path, "Reinstall Image") initial_script = "#!/bin/sh\necho initial-install-only\n" target_script = "#!/bin/sh\necho target-initial-install-only\n" set_script(client, original, initial_script) set_script(client, target, target_script) created, _ = create(client, original, suffix="reinstall") vm_id = created.json()["uuid"] service = services["hk"] dispatched = [] reinstall = service.reinstall_cloud_vm def capture_reinstall(*args, **kwargs): dispatched.append((args, kwargs.copy())) return reinstall(*args, **kwargs) monkeypatch.setattr(service, "reinstall_cloud_vm", capture_reinstall) queued = call(client, "POST", f"/internal/v1/hosts/hk/vms/{vm_id}/reinstall", { **USER, "operation_id": "startup-script-reinstall-excluded", }, { "template_id": target["id"], "confirmation_name": created.json()["name"], "ssh_public_key": CLOUD_CREATE_BODY["guest_ssh_public_key"], }) assert queued.status_code == 202, queued.text path = f"/internal/v1/hosts/hk/reinstall-jobs/{queued.json()['job_id']}" for _ in range(100): job = call(client, "GET", path, USER) assert job.status_code == 200, job.text if job.json()["status"] in {"succeeded", "failed", "uncertain"}: break time.sleep(0.01) assert job.json()["status"] == "succeeded", job.text assert len(dispatched) == 1 assert "startup_script" not in dispatched[0][1] assert target_script not in repr(dispatched) assert created_specs(service)[0]["startup_script"] == initial_script