vm-tests: a running collector keeps its cursor when it switches to the static user
This commit is contained in:
parent
ba0c41048e
commit
0461d8add8
3 changed files with 148 additions and 1 deletions
|
|
@ -137,6 +137,7 @@ let
|
|||
swarm-builder-offline
|
||||
k8s-swarm-images
|
||||
k8s-node
|
||||
k8s-collector-switch
|
||||
;
|
||||
};
|
||||
in
|
||||
|
|
|
|||
|
|
@ -17,5 +17,6 @@
|
|||
full-setup = import ./vm-tests/full-setup.nix { inherit pkgs self; };
|
||||
k8s-swarm-images = import ./vm-tests/k8s-swarm-images.nix { inherit pkgs self; };
|
||||
k8s-node = import ./vm-tests/k8s-node.nix { inherit pkgs self; };
|
||||
k8s-collector-switch = import ./vm-tests/k8s-collector-switch.nix { inherit pkgs self; };
|
||||
semi-spread = import ./vm-tests/semi-spread.nix { inherit pkgs self; };
|
||||
}
|
||||
|
|
|
|||
134
nix/vm-tests/k8s-collector-switch.nix
Normal file
134
nix/vm-tests/k8s-collector-switch.nix
Normal file
|
|
@ -0,0 +1,134 @@
|
|||
# `vmTests.<system>.k8s-collector-switch`: a host whose collector runs as a
|
||||
# dynamic user switches to `services.hyperhive.deploy.k8s.enable`, which runs it
|
||||
# as the static user the pod logs ACL names (../host-modules/k8s/pod-logs.nix).
|
||||
# The collector keeps its state across the switch. The script asserts:
|
||||
#
|
||||
# - before the switch the collector runs as a dynamic user, its state directory
|
||||
# is a symlink into /var/lib/private, and its journald receiver has written a
|
||||
# cursor there;
|
||||
# - after `switch-to-configuration test` into the node's configuration the
|
||||
# collector's process runs as the static user, its state directory is a plain
|
||||
# directory, and the cursor file is still there;
|
||||
# - the receiver's `journalctl` starts `--after-cursor`, a journal entry the
|
||||
# collector shipped before the switch is in its output once, not twice, and
|
||||
# one logged after the switch arrives. The receiver starts at the journal's
|
||||
# beginning when it holds no cursor, so a lost cursor ships every entry
|
||||
# again, and a state file the new user couldn't write would stop the output.
|
||||
#
|
||||
# systemd moves the state directory out of /var/lib/private when the dynamic
|
||||
# user goes away. The files then belong to the nobody uid on disk, which the
|
||||
# unit sees as its own user's, so the test checks what the collector does, not
|
||||
# who owns the files from the host's side.
|
||||
#
|
||||
# The node's k3s is off in the switched configuration: the switch is about the
|
||||
# collector's unit, and k3s starting would only slow the test down.
|
||||
{ pkgs, self }:
|
||||
let
|
||||
collectorOut = "/var/lib/opentelemetry-collector/out.json";
|
||||
collectorUser = "opentelemetry-collector";
|
||||
in
|
||||
pkgs.testers.runNixOSTest {
|
||||
name = "k8s-collector-switch";
|
||||
|
||||
globalTimeout = 900;
|
||||
|
||||
nodes.machine =
|
||||
{ lib, ... }:
|
||||
{
|
||||
imports = [ self.nixosModules.default ];
|
||||
|
||||
services.hyperhive = {
|
||||
deploy.ci-metrics.enable = true;
|
||||
deploy.bao.ciMetricsClientCertFile = "/var/lib/ci-metrics/client.pem";
|
||||
deploy.bao.ciMetricsClientKeyFile = "/var/lib/ci-metrics/client-key.pem";
|
||||
deploy.hive-controller.hiveName = "h1";
|
||||
swarm.domain = "t.local";
|
||||
swarm.bao.domain = "bao.t.local";
|
||||
swarm.hives.h1.domain = "h1.t.local";
|
||||
};
|
||||
system.stateVersion = "25.11";
|
||||
|
||||
# A collector that reads the journal and keeps its cursor, as the hive
|
||||
# collector does, and writes to a file where the real one has the swarm's
|
||||
# collector. The cursor is the state the switch must not lose.
|
||||
services.opentelemetry-collector = {
|
||||
enable = true;
|
||||
package = pkgs.opentelemetry-collector-contrib;
|
||||
settings = {
|
||||
extensions.file_storage = {
|
||||
directory = "/var/lib/opentelemetry-collector";
|
||||
create_directory = true;
|
||||
};
|
||||
receivers.journald = {
|
||||
start_at = "beginning";
|
||||
storage = "file_storage";
|
||||
};
|
||||
exporters.file = {
|
||||
path = collectorOut;
|
||||
append = true;
|
||||
};
|
||||
service.extensions = [ "file_storage" ];
|
||||
service.pipelines.logs = {
|
||||
receivers = [ "journald" ];
|
||||
exporters = [ "file" ];
|
||||
};
|
||||
};
|
||||
};
|
||||
services.hyperhive.deploy.k8s.podLogsExporters = [ "file" ];
|
||||
|
||||
specialisation.node.configuration = {
|
||||
services.hyperhive.deploy.k8s.enable = true;
|
||||
services.k3s.enable = lib.mkForce false;
|
||||
};
|
||||
|
||||
virtualisation.memorySize = 2048;
|
||||
};
|
||||
|
||||
testScript = ''
|
||||
import shlex
|
||||
|
||||
machine.wait_for_unit("opentelemetry-collector.service")
|
||||
|
||||
def shipped(marker: str) -> int:
|
||||
# The entry's MESSAGE field, not its `_CMDLINE`, which holds the
|
||||
# marker too.
|
||||
pattern = f'"stringValue":"{marker}"'
|
||||
return int(machine.succeed(f"grep -o -F {shlex.quote(pattern)} ${collectorOut} | wc -l"))
|
||||
|
||||
def cursors() -> list[str]:
|
||||
out = machine.succeed("find /var/lib/opentelemetry-collector/ -type f ! -name out.json")
|
||||
return out.split()
|
||||
|
||||
with subtest("before the switch the collector is a dynamic user with a cursor in its private state"):
|
||||
show = machine.succeed("systemctl show -p DynamicUser opentelemetry-collector")
|
||||
assert "DynamicUser=yes" in show, show
|
||||
machine.succeed("test -L /var/lib/opentelemetry-collector")
|
||||
machine.succeed("logger -t switchmark marker-before-switch")
|
||||
machine.wait_until_succeeds("grep -F marker-before-switch ${collectorOut}", timeout=60)
|
||||
assert shipped("marker-before-switch") == 1, shipped("marker-before-switch")
|
||||
before = cursors()
|
||||
assert before, "the receiver wrote no cursor: the check below finds nothing"
|
||||
print(before)
|
||||
|
||||
with subtest("after the switch it is the static user and resumes from its cursor"):
|
||||
machine.succeed("/run/current-system/specialisation/node/bin/switch-to-configuration test")
|
||||
machine.wait_for_unit("opentelemetry-collector.service")
|
||||
show = machine.succeed("systemctl show -p User -p DynamicUser opentelemetry-collector")
|
||||
assert "User=${collectorUser}" in show and "DynamicUser=no" in show, show
|
||||
machine.succeed("test ! -L /var/lib/opentelemetry-collector")
|
||||
after = cursors()
|
||||
assert set(before) <= set(after), (before, after)
|
||||
pid = machine.succeed("systemctl show -p MainPID --value opentelemetry-collector").strip()
|
||||
assert machine.succeed(f"stat -c %U /proc/{pid}").strip() == "${collectorUser}"
|
||||
# The receiver's `journalctl` follows from the stored cursor: with none
|
||||
# it would start from the journal's beginning, which is `start_at` here.
|
||||
journal = machine.succeed("journalctl -u opentelemetry-collector -o cat --boot")
|
||||
assert "--after-cursor" in journal, journal
|
||||
|
||||
with subtest("the cursor survived: nothing shipped twice, and new entries arrive"):
|
||||
machine.succeed("logger -t switchmark marker-after-switch")
|
||||
machine.wait_until_succeeds("grep -F marker-after-switch ${collectorOut}", timeout=60)
|
||||
assert shipped("marker-after-switch") == 1
|
||||
assert shipped("marker-before-switch") == 1
|
||||
'';
|
||||
}
|
||||
Loading…
Reference in a new issue