From c909777b6391107d42fb012a48c70fdffc26ed80 Mon Sep 17 00:00:00 2001 From: maia Date: Mon, 23 Nov 2026 20:18:50 +0100 Subject: [PATCH] k8s VM tests: wait for the store's active node after the unseal The tests waited on `bao status`, which answers once the node is unsealed and before raft has elected it, so the first policy write raced the election and failed with "local node not active but active cluster node not found". `unseal_bao` unseals and then waits for sys/leader to say the node is the leader; the four tests that bootstrap the store use it at both unseal sites. Closes #6807 --- nix/vm-tests/k8s-bao-login.nix | 4 ++-- nix/vm-tests/k8s-controller-switch.nix | 6 ++---- nix/vm-tests/k8s-nats-switch.nix | 6 ++---- nix/vm-tests/k8s-runtime-switch.nix | 7 ++----- nix/vm-tests/lib/k3s.py | 15 +++++++++++++++ 5 files changed, 23 insertions(+), 15 deletions(-) diff --git a/nix/vm-tests/k8s-bao-login.nix b/nix/vm-tests/k8s-bao-login.nix index 5302d9d68..133b7fa12 100644 --- a/nix/vm-tests/k8s-bao-login.nix +++ b/nix/vm-tests/k8s-bao-login.nix @@ -293,7 +293,7 @@ pkgs.testers.runNixOSTest { with subtest("the store is initialised, unsealed and bootstrapped"): machine.wait_until_succeeds('[ "$(bao status >/dev/null 2>&1; echo $?)" = 2 ]', timeout=900) machine.succeed(f"umask 077 && bao operator init -key-shares=1 -key-threshold=1 -format=json > {INIT}") - machine.succeed(f"bao operator unseal \"$(jq -r '.unseal_keys_b64[0]' {INIT})\"") + unseal_bao(machine, INIT) machine.succeed( "set -o pipefail; " f'export BAO_TOKEN="$(jq -r .root_token {INIT})"; ' @@ -317,7 +317,7 @@ pkgs.testers.runNixOSTest { if machine.execute("journalctl -u swarm-bao-agent-pki --no-pager | grep -F 'restart openbao.service'")[0] == 0: machine.succeed("systemctl --machine=swarm-bao restart openbao.service") machine.wait_until_succeeds('[ "$(bao status >/dev/null 2>&1; echo $?)" = 2 ]', timeout=900) - machine.succeed(f"bao operator unseal \"$(jq -r '.unseal_keys_b64[0]' {INIT})\"") + unseal_bao(machine, INIT) units = [ "swarm-bao-controller-policy", "swarm-bao-ci-metrics-policy", diff --git a/nix/vm-tests/k8s-controller-switch.nix b/nix/vm-tests/k8s-controller-switch.nix index ed16fb38a..8b48ba1d0 100644 --- a/nix/vm-tests/k8s-controller-switch.nix +++ b/nix/vm-tests/k8s-controller-switch.nix @@ -458,8 +458,7 @@ pkgs.testers.runNixOSTest { with subtest("the store is initialised, unsealed and bootstrapped"): machine.wait_until_succeeds('[ "$(bao status >/dev/null 2>&1; echo $?)" = 2 ]', timeout=900) machine.succeed(f"umask 077 && bao operator init -key-shares=1 -key-threshold=1 -format=json > {INIT}") - machine.succeed(f"bao operator unseal \"$(jq -r '.unseal_keys_b64[0]' {INIT})\"") - machine.wait_until_succeeds("bao status", timeout=120) + unseal_bao(machine, INIT) machine.succeed( "set -o pipefail; " f'export BAO_TOKEN="$(jq -r .root_token {INIT})"; ' @@ -481,8 +480,7 @@ pkgs.testers.runNixOSTest { if machine.execute("journalctl -u swarm-bao-agent-pki --no-pager | grep -F 'restart openbao.service'")[0] == 0: machine.succeed("systemctl --machine=swarm-bao restart openbao.service") machine.wait_until_succeeds('[ "$(bao status >/dev/null 2>&1; echo $?)" = 2 ]', timeout=900) - machine.succeed(f"bao operator unseal \"$(jq -r '.unseal_keys_b64[0]' {INIT})\"") - machine.wait_until_succeeds("bao status", timeout=120) + unseal_bao(machine, INIT) for unit in ["swarm-bao-controller-policy", "swarm-bao-services-issuer-policy", "swarm-bao-k8s-jwt-policy"]: settle(f'[ "$(systemctl is-active {unit}.service)" = active ]', 900, [f"{unit}.service"]) machine.wait_until_succeeds( diff --git a/nix/vm-tests/k8s-nats-switch.nix b/nix/vm-tests/k8s-nats-switch.nix index 321219382..02267fb82 100644 --- a/nix/vm-tests/k8s-nats-switch.nix +++ b/nix/vm-tests/k8s-nats-switch.nix @@ -510,8 +510,7 @@ pkgs.testers.runNixOSTest { with subtest("the store is initialised, unsealed and bootstrapped"): machine.wait_until_succeeds('[ "$(bao status >/dev/null 2>&1; echo $?)" = 2 ]', timeout=900) machine.succeed(f"umask 077 && bao operator init -key-shares=1 -key-threshold=1 -format=json > {INIT}") - machine.succeed(f"bao operator unseal \"$(jq -r '.unseal_keys_b64[0]' {INIT})\"") - machine.wait_until_succeeds("bao status", timeout=120) + unseal_bao(machine, INIT) machine.succeed( "set -o pipefail; " f'export BAO_TOKEN="$(jq -r .root_token {INIT})"; ' @@ -533,8 +532,7 @@ pkgs.testers.runNixOSTest { if machine.execute("journalctl -u swarm-bao-agent-pki --no-pager | grep -F 'restart openbao.service'")[0] == 0: machine.succeed("systemctl --machine=swarm-bao restart openbao.service") machine.wait_until_succeeds('[ "$(bao status >/dev/null 2>&1; echo $?)" = 2 ]', timeout=900) - machine.succeed(f"bao operator unseal \"$(jq -r '.unseal_keys_b64[0]' {INIT})\"") - machine.wait_until_succeeds("bao status", timeout=120) + unseal_bao(machine, INIT) for unit in [ "swarm-bao-nats-tls-policy", "swarm-bao-nats-auth-policy", diff --git a/nix/vm-tests/k8s-runtime-switch.nix b/nix/vm-tests/k8s-runtime-switch.nix index 25a11df80..fdc0729d6 100644 --- a/nix/vm-tests/k8s-runtime-switch.nix +++ b/nix/vm-tests/k8s-runtime-switch.nix @@ -547,9 +547,7 @@ pkgs.testers.runNixOSTest { with subtest("the store is initialised, unsealed and bootstrapped"): machine.wait_until_succeeds('[ "$(bao status >/dev/null 2>&1; echo $?)" = 2 ]', timeout=900) machine.succeed(f"umask 077 && bao operator init -key-shares=1 -key-threshold=1 -format=json > {INIT}") - machine.succeed(f"bao operator unseal \"$(jq -r '.unseal_keys_b64[0]' {INIT})\"") - # Right after the unseal the store has no active node for a moment. - machine.wait_until_succeeds("bao status", timeout=120) + unseal_bao(machine, INIT) machine.succeed( "set -o pipefail; " f'export BAO_TOKEN="$(jq -r .root_token {INIT})"; ' @@ -573,8 +571,7 @@ pkgs.testers.runNixOSTest { if machine.execute("journalctl -u swarm-bao-agent-pki --no-pager | grep -F 'restart openbao.service'")[0] == 0: machine.succeed("systemctl --machine=swarm-bao restart openbao.service") machine.wait_until_succeeds('[ "$(bao status >/dev/null 2>&1; echo $?)" = 2 ]', timeout=900) - machine.succeed(f"bao operator unseal \"$(jq -r '.unseal_keys_b64[0]' {INIT})\"") - machine.wait_until_succeeds("bao status", timeout=120) + unseal_bao(machine, INIT) for unit in [ "swarm-bao-notify-router-policy", "swarm-bao-stt-policy", diff --git a/nix/vm-tests/lib/k3s.py b/nix/vm-tests/lib/k3s.py index 59da2305f..3ad765749 100644 --- a/nix/vm-tests/lib/k3s.py +++ b/nix/vm-tests/lib/k3s.py @@ -60,6 +60,21 @@ def wait_images(node: QemuMachine, timeout: int = 120) -> None: ) +def unseal_bao(node: QemuMachine, init_file: str, timeout: int = 120) -> None: + """Unseals the store with the key `bao operator init` saved in `init_file`, + then waits until the store's node is the active one. `bao status` answers + as soon as the node is unsealed, before raft has elected it, and a write + in that gap fails with "local node not active but active cluster node not + found".""" + node.succeed(f"bao operator unseal \"$(jq -r '.unseal_keys_b64[0]' {init_file})\"") + # `sys/leader` needs no token. The answer is the bare object, or under + # `.data` where the client wraps it. + node.wait_until_succeeds( + "bao read -format=json sys/leader | jq -e '(.data // .).is_self == true'", + timeout=timeout, + ) + + def pod(node: QemuMachine, name: str) -> dict: return json.loads(kube(node, f"get pod {name} -o json"))