From d506595e4304165d6bfe5d461ad794fc3239df99 Mon Sep 17 00:00:00 2001 From: Robert Rose Date: Thu, 15 Aug 2024 17:11:28 +0200 Subject: [PATCH] nixos/k3s: use jq instead of JSONpath to process test data Although kubectl has builtin JSONpath support, it is only supported partially and the support varies between different versions. While using JSONpath in tests worked for some versions, it failed for others. This contribution replaces the problematic JSONpath usages with the jq JSON processor. --- nixos/tests/k3s/containerd-config.nix | 10 ++++++++-- nixos/tests/k3s/kubelet-config.nix | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/nixos/tests/k3s/containerd-config.nix b/nixos/tests/k3s/containerd-config.nix index db4b3320411f..ffc449b03aba 100644 --- a/nixos/tests/k3s/containerd-config.nix +++ b/nixos/tests/k3s/containerd-config.nix @@ -1,6 +1,11 @@ # A test that containerdConfigTemplate settings get written to containerd/config.toml import ../make-test-python.nix ( - { lib, k3s, ... }: + { + pkgs, + lib, + k3s, + ... + }: let nodeName = "test"; in @@ -9,6 +14,7 @@ import ../make-test-python.nix ( nodes.machine = { ... }: { + environment.systemPackages = [ pkgs.jq ]; # k3s uses enough resources the default vm fails. virtualisation.memorySize = 1536; virtualisation.diskSize = 4096; @@ -38,7 +44,7 @@ import ../make-test-python.nix ( start_all() machine.wait_for_unit("k3s") # wait until the node is ready - machine.wait_until_succeeds(r"""kubectl wait --for='jsonpath={.status.conditions[?(@.type=="Ready")].status}=True' nodes/${nodeName}""") + machine.wait_until_succeeds(r"""kubectl get node ${nodeName} -ojson | jq -e '.status.conditions[] | select(.type == "Ready") | .status == "True"'""") # test whether the config template file contains the magic comment out=machine.succeed("cat /var/lib/rancher/k3s/agent/etc/containerd/config.toml.tmpl") assert "MAGIC COMMENT" in out, "the containerd config template does not contain the magic comment" diff --git a/nixos/tests/k3s/kubelet-config.nix b/nixos/tests/k3s/kubelet-config.nix index e2c8e0434d62..031c9f823a63 100644 --- a/nixos/tests/k3s/kubelet-config.nix +++ b/nixos/tests/k3s/kubelet-config.nix @@ -53,7 +53,7 @@ import ../make-test-python.nix ( start_all() machine.wait_for_unit("k3s") # wait until the node is ready - machine.wait_until_succeeds(r"""kubectl wait --for='jsonpath={.status.conditions[?(@.type=="Ready")].status}=True' nodes/${nodeName}""") + machine.wait_until_succeeds(r"""kubectl get node ${nodeName} -ojson | jq -e '.status.conditions[] | select(.type == "Ready") | .status == "True"'""") # test whether the kubelet registered an inhibitor lock machine.succeed("systemd-inhibit --list --no-legend | grep \"kubelet.*k3s-server.*shutdown\"") # run kubectl proxy in the background, close stdout through redirection to not wait for the command to finish