diff --git a/Justfile b/Justfile index c7a5a4c8..2c4a9335 100644 --- a/Justfile +++ b/Justfile @@ -39,6 +39,10 @@ check: test -f system_files/shared/usr/lib/systemd/system/bootc-unified-storage.service.d/10-utah-local-test.conf grep -q 'enable gdm.service' system_files/shared/usr/lib/systemd/system-preset/85-utah-desktop.preset grep -q 'enable ublue-system-setup.service' system_files/shared/usr/lib/systemd/system-preset/85-utah-desktop.preset + grep -q 'enable utah-countme.timer' system_files/shared/usr/lib/systemd/system-preset/85-utah-desktop.preset + test -f system_files/shared/usr/lib/systemd/system/utah-countme.service + test -f system_files/shared/usr/lib/systemd/system/utah-countme.timer + test -f system_files/shared/usr/libexec/utah-countme grep -q 'disable bootc-fetch-apply-updates.timer' system_files/shared/usr/lib/systemd/system-preset/85-utah-desktop.preset grep -q 'disable bootc-fetch-apply-updates.service' system_files/shared/usr/lib/systemd/system-preset/85-utah-desktop.preset grep -q 'bootc-fetch-apply-updates.timer' scripts/configure-services.sh diff --git a/README.md b/README.md index e94fdd29..b2115c96 100644 --- a/README.md +++ b/README.md @@ -104,6 +104,10 @@ This is the honest list, and it is why the label above says pre-alpha. repository is consumed and the local QEMU image reaches GDM and GNOME Shell. - **CUDA is deliberately excluded** — 7.68 GB installed. Use the NVIDIA container toolkit, which is included, and run CUDA in a container. +- **Telemetry (countme).** Utah includes a weekly countme client reporting + to `countme.projectbluefin.io` following Bluefin ADR 0006. Users can opt out + by running `sudo touch /etc/projectbluefin/countme/disabled` or by masking + the timer: `sudo systemctl mask utah-countme.timer`. See the [open issues](https://github.com/projectbluefin/utah/issues) for where things stand. diff --git a/contracts/bluefin-desktop.toml b/contracts/bluefin-desktop.toml index b4b9603a..145b52a4 100644 --- a/contracts/bluefin-desktop.toml +++ b/contracts/bluefin-desktop.toml @@ -138,6 +138,7 @@ enabled = [ "flatpak-preinstall.service", "gdm.service", "ublue-system-setup.service", + "utah-countme.timer", "uupd.timer", ] masked = [ diff --git a/docs/skills/desktop-contract.md b/docs/skills/desktop-contract.md index 073a3c1f..398a598d 100644 --- a/docs/skills/desktop-contract.md +++ b/docs/skills/desktop-contract.md @@ -1,20 +1,20 @@ --- name: desktop-contract -version: "1.0" +version: "1.1" last_updated: "2026-09-18" id: desktop-contract -one_line_purpose: Maintain Utah identity, Bluefin desktop defaults, and first-boot Flatpak policy. +one_line_purpose: Maintain Utah identity, Bluefin desktop defaults, telemetry, and first-boot Flatpak policy. entry_point: docs/skills/desktop-contract.md category: contracts mcp_compliance_level: partial optimization_status: draft status: active dependencies: [] -tags: [desktop, branding, gnome, flatpak] +tags: [desktop, branding, gnome, flatpak, telemetry, countme] description: >- The runtime desktop contract in contracts/bluefin-desktop.toml and its in-image verifiers. Use when changing branding, os-release, service - presets, GNOME extensions, or first-boot Flatpak behavior. + presets, GNOME extensions, countme telemetry, or first-boot Flatpak behavior. metadata: type: policy --- @@ -57,7 +57,7 @@ The TOML's sections are the contract's table of contents: - **`[services]`** — systemd units the preset must enable: `gdm.service`, `bluetooth.service`, `ublue-system-setup.service`, `flatpak-preinstall.service`, `flatpak-nuke-fedora.service`, `brew-setup.service`, `dconf-update.service`, - `bootc-unified-storage.service`, `uupd.timer`. Update policy delegates + `bootc-unified-storage.service`, `utah-countme.timer`, `uupd.timer`. Update policy delegates background updates to `uupd.timer`; `bootc-fetch-apply-updates.timer` and `bootc-fetch-apply-updates.service` are masked in `/etc` and `/usr/lib` (and disabled in `85-utah-desktop.preset`) so cross-vendor `/etc` 3-way merges @@ -100,16 +100,38 @@ on `GjsPrivate.DBusImplementation`, gracefully degrading to an inert portal on G Hummingbird defaults to a server preset and disables unlisted services, so the desktop policy is applied explicitly. `scripts/configure-services.sh` mirrors bluefin-lts's `40-services.sh`: it applies the desktop presets, -enables GDM, firmware updates, Tailscale, uupd, user setup and resolved, -configures authselect, and removes the extension build toolchain before -cleanup (Containerfile RUN comment; originated in `docs/building.md`'s former -design section and now lives in this skill). +enables GDM, firmware updates, Tailscale, uupd, user setup, resolved, and +the weekly `utah-countme.timer` client reporting, configures authselect, and +removes the extension build toolchain before cleanup (Containerfile RUN comment; +originated in `docs/building.md`'s former design section and now lives in this +skill). Hummingbird's base does not include `systemd-resolved` by default; it is listed under `[services]` in `packages/utah.toml` and configured in `scripts/configure-services.sh`, which also disables `PrivateTmp` on `systemd-resolved.service` for bootc early-boot DNS resolution. +## Countme telemetry + +Utah reports weekly anonymous telemetry to `countme.projectbluefin.io` matching +Project Bluefin ADR 0006: +- **Client implementation:** `/usr/libexec/utah-countme` executed via + `utah-countme.service` and scheduled by `utah-countme.timer`. +- **Systemd isolation:** Runs as `DynamicUser=yes` with + `StateDirectory=utah-countme` storing local installation `epoch` and + `lastrun` timestamps. +- **Reporting parameters & query string:** An empty HTTP GET request is sent to + `https://countme.projectbluefin.io/metalink?repo=${REPO}&tag=${IMAGE_TAG}&flavor=${IMAGE_FLAVOR}&arch=${ARCH}&countme=${BUCKET}` + - `repo`: image name (`utah` or custom variant) + - `tag`: OS image version / release tag + - `flavor`: desktop flavor (`main`, `nvidia`, `gaming`, etc.) + - `arch`: system architecture (`x86_64`) + - `countme`: installation age cohort bucket (1: first week, 2: 2–4 weeks, 3: 5–24 weeks, 4: >24 weeks) +- **Data & logging policy:** No personal information or hardware identifiers are transmitted; requests contain no HTTP body. As with all HTTP requests, standard server connection metadata (including client IP address) is received by the endpoint for aggregate deduplication and cohort counting. +- **Network & failure resilience:** Requests use an explicit `--max-time 10` timeout. `lastrun` is updated unconditionally before the request so network or endpoint outages do not trigger daily retry storms on reboot; failed reports are simply dropped. +- **Persistent state integrity:** The install epoch is stored in `/var/lib/utah-countme/epoch`. If this file exists but cannot be read or is corrupted, execution degrades to a no-op (no ping sent and no epoch reset) to avoid resetting systems into bucket 1. +- **Opt-out:** Honored whenever `/etc/projectbluefin/countme/disabled` exists, or by masking the timer (`systemctl mask utah-countme.timer`). + ## The verifiers run twice The same verifier runs in the Containerfile and on demand, so a local image diff --git a/docs/skills/index.json b/docs/skills/index.json index 93d6db3e..152b6a63 100644 --- a/docs/skills/index.json +++ b/docs/skills/index.json @@ -41,7 +41,7 @@ { "id": "desktop-contract", "name": "desktop-contract", - "one_line_purpose": "Maintain Utah identity, Bluefin desktop defaults, and first-boot Flatpak policy.", + "one_line_purpose": "Maintain Utah identity, Bluefin desktop defaults, telemetry, and first-boot Flatpak policy.", "entry_point": "docs/skills/desktop-contract.md", "category": "contracts", "status": "active", @@ -49,10 +49,12 @@ "desktop", "branding", "gnome", - "flatpak" + "flatpak", + "telemetry", + "countme" ], - "description": "The runtime desktop contract in contracts/bluefin-desktop.toml and its in-image verifiers. Use when changing branding, os-release, service presets, GNOME extensions, or first-boot Flatpak behavior.", - "version": "1.0", + "description": "The runtime desktop contract in contracts/bluefin-desktop.toml and its in-image verifiers. Use when changing branding, os-release, service presets, GNOME extensions, countme telemetry, or first-boot Flatpak behavior.", + "version": "1.1", "last_updated": "2026-09-18", "doc_type": "policy" }, diff --git a/docs/skills/index.md b/docs/skills/index.md index d3800c2c..fb6fdd98 100644 --- a/docs/skills/index.md +++ b/docs/skills/index.md @@ -9,7 +9,7 @@ Generated: 2026-09-19 · schema 1.0 · 8 skills |---|---|---|---| | [ci-workflows](ci-workflows.md) | ci-ops | active | Navigate Utah's build, promote, and sync workflow topology. | | [containerfile](containerfile.md) | image-build | active | Edit the Containerfile without regressing layer count or cache hits. | -| [desktop-contract](desktop-contract.md) | contracts | active | Maintain Utah identity, Bluefin desktop defaults, and first-boot Flatpak policy. | +| [desktop-contract](desktop-contract.md) | contracts | active | Maintain Utah identity, Bluefin desktop defaults, telemetry, and first-boot Flatpak policy. | | [flavors](flavors.md) | image-build | active | Add, remove, or retire an image flavor safely. | | [kernel-cache](kernel-cache.md) | image-build | active | Understand and rebuild the OGC kernel and NVIDIA module cache image. | | [local-testing](local-testing.md) | testing | active | Build, install, and boot Utah locally in a VM or live ISO. | diff --git a/scripts/configure-services.sh b/scripts/configure-services.sh index 53f684b4..52a7b13e 100644 --- a/scripts/configure-services.sh +++ b/scripts/configure-services.sh @@ -69,6 +69,7 @@ enable_unit fwupd.service enable_unit fwupd-refresh.timer enable_unit dconf-update.service enable_unit tailscaled.service +enable_unit utah-countme.timer enable_unit uupd.timer enable_unit ublue-system-setup.service enable_unit systemd-resolved.service diff --git a/system_files/shared/usr/lib/systemd/system-preset/85-utah-desktop.preset b/system_files/shared/usr/lib/systemd/system-preset/85-utah-desktop.preset index ce3eabb8..0b138c2f 100644 --- a/system_files/shared/usr/lib/systemd/system-preset/85-utah-desktop.preset +++ b/system_files/shared/usr/lib/systemd/system-preset/85-utah-desktop.preset @@ -6,6 +6,7 @@ enable firewalld.service enable fwupd.service enable fwupd-refresh.timer enable tailscaled.service +enable utah-countme.timer enable uupd.timer enable ublue-system-setup.service enable systemd-resolved.service diff --git a/system_files/shared/usr/lib/systemd/system/utah-countme.service b/system_files/shared/usr/lib/systemd/system/utah-countme.service new file mode 100644 index 00000000..be9db5f3 --- /dev/null +++ b/system_files/shared/usr/lib/systemd/system/utah-countme.service @@ -0,0 +1,16 @@ +[Unit] +Description=Weekly Utah Count Me telemetry reporting +Documentation=https://docs.projectbluefin.io +Wants=network-online.target +After=network-online.target +ConditionPathExists=/run/ostree-booted +ConditionPathExists=/usr/share/ublue-os/image-info.json +ConditionPathExists=!/etc/projectbluefin/countme/disabled + +[Service] +Type=oneshot +DynamicUser=yes +StateDirectory=utah-countme +ExecStart=/usr/libexec/utah-countme +StandardOutput=journal +StandardError=journal diff --git a/system_files/shared/usr/lib/systemd/system/utah-countme.timer b/system_files/shared/usr/lib/systemd/system/utah-countme.timer new file mode 100644 index 00000000..660dd7bf --- /dev/null +++ b/system_files/shared/usr/lib/systemd/system/utah-countme.timer @@ -0,0 +1,16 @@ +[Unit] +Description=Weekly Utah Count Me telemetry timer +Documentation=https://docs.projectbluefin.io +ConditionPathExists=/run/ostree-booted +ConditionPathExists=/usr/share/ublue-os/image-info.json +ConditionPathExists=!/etc/projectbluefin/countme/disabled + +[Timer] +OnBootSec=10m +OnCalendar=weekly +Persistent=true +AccuracySec=1h +RandomizedDelaySec=12h + +[Install] +WantedBy=timers.target diff --git a/system_files/shared/usr/libexec/utah-countme b/system_files/shared/usr/libexec/utah-countme new file mode 100755 index 00000000..a4856bf3 --- /dev/null +++ b/system_files/shared/usr/libexec/utah-countme @@ -0,0 +1,136 @@ +#!/usr/bin/bash +# /usr/libexec/utah-countme +# Weekly user-counting client for Utah (bootc) systems +set -euo pipefail + +DISABLED_FILE="${DISABLED_FILE:-/etc/projectbluefin/countme/disabled}" +IMAGE_INFO="${IMAGE_INFO:-/usr/share/ublue-os/image-info.json}" +STATE_DIR="${STATE_DIRECTORY:-/var/lib/utah-countme}" +OS_RELEASE_FILE="${OS_RELEASE_FILE:-/etc/os-release}" +WEEK=604800 + +# Allow users to opt out without disabling the timer. +[ -e "$DISABLED_FILE" ] && exit 0 + +if ! command -v curl >/dev/null 2>&1; then + echo "utah-countme: curl is unavailable; skipping telemetry" >&2 + exit 0 +fi + +NOW=$(date +%s) + +# Ensure state directory exists +if [ ! -d "$STATE_DIR" ]; then + mkdir -p "$STATE_DIR" +fi + +EPOCH_FILE="${STATE_DIR}/epoch" +LASTRUN_FILE="${STATE_DIR}/lastrun" + +# 1. Read or initialize epoch file +# Note: The epoch is recorded the first time this script runs (typically ~10m +# after first boot via OnBootSec=10m) and acts as the baseline for cohort age. +if [ ! -f "$EPOCH_FILE" ]; then + echo "$NOW" > "$EPOCH_FILE" || { + echo "utah-countme: cannot initialize epoch file; skipping telemetry" >&2 + exit 0 + } +fi + +if ! EPOCH=$(cat "$EPOCH_FILE" 2>/dev/null) || [ -z "$EPOCH" ] || ! [[ "$EPOCH" =~ ^[0-9]+$ ]]; then + echo "utah-countme: cannot read epoch file; skipping telemetry" >&2 + exit 0 +fi + +# 2. Throttling: only run once per 7-day window +if [ -f "$LASTRUN_FILE" ]; then + LASTRUN=$(cat "$LASTRUN_FILE" 2>/dev/null || echo 0) + if [[ "$LASTRUN" =~ ^[0-9]+$ ]] && (( NOW - LASTRUN < WEEK )); then + exit 0 + fi +fi + +# 3. Compute Fedora-style week bucket +# 1: first week, 2: 2–4w, 3: 5–24w, 4: >24w +if (( NOW < EPOCH )); then + WEEKS=1 +else + WEEKS=$(( (NOW - EPOCH) / WEEK + 1 )) +fi + +if (( WEEKS <= 1 )); then + BUCKET=1 +elif (( WEEKS <= 4 )); then + BUCKET=2 +elif (( WEEKS <= 24 )); then + BUCKET=3 +else + BUCKET=4 +fi + +# 4. Extract image metadata +REPO="utah" +IMAGE_TAG="unknown" +IMAGE_FLAVOR="unknown" + +if [ -f "$IMAGE_INFO" ]; then + read -r INFO_NAME INFO_TAG INFO_FLAVOR < <(python3 -c ' +import json, sys +try: + with open(sys.argv[1]) as f: + d = json.load(f) + print(d.get("image-name", "") or "", d.get("image-tag", "") or "", d.get("image-flavor", "") or "") +except Exception: + print("", "", "") +' "$IMAGE_INFO") + if [ -n "$INFO_NAME" ]; then + REPO="$INFO_NAME" + fi + if [ -n "$INFO_TAG" ]; then + IMAGE_TAG="$INFO_TAG" + fi + if [ -n "$INFO_FLAVOR" ]; then + IMAGE_FLAVOR="$INFO_FLAVOR" + fi +fi + +# Fallback to os-release if tag or flavor are still unknown +if [ -f "$OS_RELEASE_FILE" ]; then + if [ "$IMAGE_TAG" = "unknown" ]; then + OS_VERSION=$(grep -E '^OSTREE_VERSION=' "$OS_RELEASE_FILE" 2>/dev/null | head -n1 | cut -d= -f2 | tr -d '"' || true) + if [ -z "$OS_VERSION" ]; then + OS_VERSION=$(grep -E '^IMAGE_VERSION=' "$OS_RELEASE_FILE" 2>/dev/null | head -n1 | cut -d= -f2 | tr -d '"' || true) + fi + if [ -z "$OS_VERSION" ]; then + OS_VERSION=$(grep -E '^VERSION_ID=' "$OS_RELEASE_FILE" 2>/dev/null | head -n1 | cut -d= -f2 | tr -d '"' || true) + fi + if [ -n "$OS_VERSION" ]; then + IMAGE_TAG="$OS_VERSION" + fi + fi + if [ "$IMAGE_FLAVOR" = "unknown" ]; then + OS_FLAVOR=$(grep -E '^IMAGE_FLAVOR=' "$OS_RELEASE_FILE" 2>/dev/null | cut -d= -f2 | tr -d '"' || true) + if [ -n "$OS_FLAVOR" ]; then + IMAGE_FLAVOR="$OS_FLAVOR" + fi + fi +fi + +# Default flavor to "main" if still unspecified +if [ "$IMAGE_FLAVOR" = "unknown" ]; then + IMAGE_FLAVOR="main" +fi + +ARCH=$(uname -m) +OS_VERSION_ID="unknown" +if [ -f "$OS_RELEASE_FILE" ]; then + OS_VERSION_ID=$(grep -E '^VERSION_ID=' "$OS_RELEASE_FILE" 2>/dev/null | cut -d= -f2 | tr -d '"' || echo "unknown") +fi + +# 5. Formulate request +USER_AGENT="utah-countme (${REPO} ${OS_VERSION_ID}; ${IMAGE_FLAVOR}; Linux.${ARCH})" +URL="https://countme.projectbluefin.io/metalink?repo=${REPO}&tag=${IMAGE_TAG}&flavor=${IMAGE_FLAVOR}&arch=${ARCH}&countme=${BUCKET}" + +# 6. Send ping (update lastrun unconditionally to prevent outage retry storms) +echo "$NOW" > "$LASTRUN_FILE" +curl -sf --retry 3 --max-time 10 -H "User-Agent: ${USER_AGENT}" "$URL" >/dev/null || true diff --git a/tests/test_countme.py b/tests/test_countme.py new file mode 100644 index 00000000..d9e60ef7 --- /dev/null +++ b/tests/test_countme.py @@ -0,0 +1,275 @@ +"""Unit tests for the utah-countme client script and systemd units.""" + +from __future__ import annotations + +import json +import os +import subprocess +import tempfile +import time +import unittest +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +SCRIPT_PATH = ROOT / "system_files/shared/usr/libexec/utah-countme" +SERVICE_PATH = ROOT / "system_files/shared/usr/lib/systemd/system/utah-countme.service" +TIMER_PATH = ROOT / "system_files/shared/usr/lib/systemd/system/utah-countme.timer" +PRESET_PATH = ROOT / "system_files/shared/usr/lib/systemd/system-preset/85-utah-desktop.preset" +CONTRACT_PATH = ROOT / "contracts/bluefin-desktop.toml" +SERVICES_SCRIPT = ROOT / "scripts/configure-services.sh" + + +class CountmeUnitTest(unittest.TestCase): + def test_units_exist_and_match_contract(self): + self.assertTrue(SCRIPT_PATH.is_file()) + self.assertTrue(os.access(SCRIPT_PATH, os.X_OK)) + self.assertTrue(SERVICE_PATH.is_file()) + self.assertTrue(TIMER_PATH.is_file()) + + service_text = SERVICE_PATH.read_text() + self.assertIn("ConditionPathExists=!/etc/projectbluefin/countme/disabled", service_text) + self.assertIn("ConditionPathExists=/run/ostree-booted", service_text) + self.assertIn("ConditionPathExists=/usr/share/ublue-os/image-info.json", service_text) + self.assertIn("DynamicUser=yes", service_text) + self.assertIn("StateDirectory=utah-countme", service_text) + self.assertIn("ExecStart=/usr/libexec/utah-countme", service_text) + + timer_text = TIMER_PATH.read_text() + self.assertIn("ConditionPathExists=!/etc/projectbluefin/countme/disabled", timer_text) + self.assertIn("ConditionPathExists=/run/ostree-booted", timer_text) + self.assertIn("ConditionPathExists=/usr/share/ublue-os/image-info.json", timer_text) + self.assertIn("OnCalendar=weekly", timer_text) + self.assertIn("Persistent=true", timer_text) + + preset_text = PRESET_PATH.read_text() + self.assertIn("enable utah-countme.timer", preset_text) + + contract_text = CONTRACT_PATH.read_text() + self.assertIn('"utah-countme.timer"', contract_text) + + services_script_text = SERVICES_SCRIPT.read_text() + self.assertIn("enable_unit utah-countme.timer", services_script_text) + + def test_opt_out_file_skips_execution(self): + with tempfile.TemporaryDirectory() as tmp: + tmp_path = Path(tmp) + disabled_file = tmp_path / "disabled" + disabled_file.touch() + + mock_bin = tmp_path / "bin" + mock_bin.mkdir() + curl_mock = mock_bin / "curl" + curl_mock.write_text("#!/bin/sh\nexit 1\n") + curl_mock.chmod(0o755) + + state_dir = tmp_path / "state" + + env = os.environ.copy() + env["PATH"] = f"{mock_bin}:{env['PATH']}" + env["DISABLED_FILE"] = str(disabled_file) + env["STATE_DIRECTORY"] = str(state_dir) + + res = subprocess.run([str(SCRIPT_PATH)], env=env, capture_output=True, text=True) + self.assertEqual(res.returncode, 0) + self.assertFalse((state_dir / "lastrun").exists()) + + def test_first_run_creates_epoch_and_pings(self): + with tempfile.TemporaryDirectory() as tmp: + tmp_path = Path(tmp) + state_dir = tmp_path / "state" + mock_bin = tmp_path / "bin" + mock_bin.mkdir() + + curl_args_log = tmp_path / "curl_args.txt" + curl_mock = mock_bin / "curl" + curl_mock.write_text(f'#!/bin/sh\necho "$@" > "{curl_args_log}"\nexit 0\n') + curl_mock.chmod(0o755) + + image_info = tmp_path / "image-info.json" + image_info.write_text( + json.dumps( + { + "image-name": "utah", + "image-flavor": "nvidia", + "image-tag": "testing-20260911", + } + ) + ) + + os_release = tmp_path / "os-release" + os_release.write_text('VERSION_ID="43"\n') + + env = os.environ.copy() + env["PATH"] = f"{mock_bin}:{env['PATH']}" + env["STATE_DIRECTORY"] = str(state_dir) + env["IMAGE_INFO"] = str(image_info) + env["OS_RELEASE_FILE"] = str(os_release) + env["DISABLED_FILE"] = str(tmp_path / "nonexistent-disabled") + + res = subprocess.run([str(SCRIPT_PATH)], env=env, capture_output=True, text=True) + self.assertEqual(res.returncode, 0) + self.assertTrue((state_dir / "epoch").exists()) + self.assertTrue((state_dir / "lastrun").exists()) + + args = curl_args_log.read_text() + self.assertIn("https://countme.projectbluefin.io/metalink", args) + self.assertIn("repo=utah", args) + self.assertIn("tag=testing-20260911", args) + self.assertIn("flavor=nvidia", args) + self.assertIn("countme=1", args) + self.assertIn("utah-countme", args) + + def test_throttling_skips_within_7_days(self): + with tempfile.TemporaryDirectory() as tmp: + tmp_path = Path(tmp) + state_dir = tmp_path / "state" + state_dir.mkdir() + mock_bin = tmp_path / "bin" + mock_bin.mkdir() + + curl_invoked = tmp_path / "invoked.txt" + curl_mock = mock_bin / "curl" + curl_mock.write_text(f'#!/bin/sh\ntouch "{curl_invoked}"\nexit 0\n') + curl_mock.chmod(0o755) + + now = int(time.time()) + (state_dir / "epoch").write_text(str(now - 100)) + (state_dir / "lastrun").write_text(str(now - 86400)) + + env = os.environ.copy() + env["PATH"] = f"{mock_bin}:{env['PATH']}" + env["STATE_DIRECTORY"] = str(state_dir) + env["DISABLED_FILE"] = str(tmp_path / "nonexistent-disabled") + + res = subprocess.run([str(SCRIPT_PATH)], env=env, capture_output=True, text=True) + self.assertEqual(res.returncode, 0) + self.assertFalse(curl_invoked.exists()) + + def test_age_cohort_buckets(self): + with tempfile.TemporaryDirectory() as tmp: + tmp_path = Path(tmp) + mock_bin = tmp_path / "bin" + mock_bin.mkdir() + + curl_args_log = tmp_path / "curl_args.txt" + curl_mock = mock_bin / "curl" + curl_mock.write_text(f'#!/bin/sh\necho "$@" > "{curl_args_log}"\nexit 0\n') + curl_mock.chmod(0o755) + + image_info = tmp_path / "image-info.json" + image_info.write_text(json.dumps({"image-name": "utah", "image-flavor": "main", "image-tag": "latest"})) + + week_secs = 604800 + now = int(time.time()) + + test_cases = [ + (0, 1), # 0 weeks -> bucket 1 + (week_secs * 2, 2), # 2 weeks -> bucket 2 (2-4w) + (week_secs * 6, 3), # 6 weeks -> bucket 3 (5-24w) + (week_secs * 30, 4), # 30 weeks -> bucket 4 (>24w) + ] + + for offset_secs, expected_bucket in test_cases: + with self.subTest(bucket=expected_bucket): + state_dir = tmp_path / f"state_{expected_bucket}" + state_dir.mkdir() + (state_dir / "epoch").write_text(str(now - offset_secs)) + + env = os.environ.copy() + env["PATH"] = f"{mock_bin}:{env['PATH']}" + env["STATE_DIRECTORY"] = str(state_dir) + env["IMAGE_INFO"] = str(image_info) + env["DISABLED_FILE"] = str(tmp_path / "nonexistent-disabled") + + res = subprocess.run([str(SCRIPT_PATH)], env=env, capture_output=True, text=True) + self.assertEqual(res.returncode, 0) + args = curl_args_log.read_text() + self.assertIn(f"countme={expected_bucket}", args) + + def test_corrupt_epoch_skips_telemetry_without_resetting(self): + with tempfile.TemporaryDirectory() as tmp: + tmp_path = Path(tmp) + state_dir = tmp_path / "state" + state_dir.mkdir() + (state_dir / "epoch").write_text("not-a-number") + + mock_bin = tmp_path / "bin" + mock_bin.mkdir() + curl_invoked = tmp_path / "invoked.txt" + curl_mock = mock_bin / "curl" + curl_mock.write_text(f'#!/bin/sh\ntouch "{curl_invoked}"\nexit 0\n') + curl_mock.chmod(0o755) + + env = os.environ.copy() + env["PATH"] = f"{mock_bin}:{env['PATH']}" + env["STATE_DIRECTORY"] = str(state_dir) + env["DISABLED_FILE"] = str(tmp_path / "nonexistent-disabled") + + res = subprocess.run([str(SCRIPT_PATH)], env=env, capture_output=True, text=True) + self.assertEqual(res.returncode, 0) + self.assertFalse(curl_invoked.exists()) + self.assertEqual((state_dir / "epoch").read_text(), "not-a-number") + self.assertFalse((state_dir / "lastrun").exists()) + + def test_curl_failure_still_updates_lastrun_to_prevent_retry_storm(self): + with tempfile.TemporaryDirectory() as tmp: + tmp_path = Path(tmp) + state_dir = tmp_path / "state" + mock_bin = tmp_path / "bin" + mock_bin.mkdir() + + curl_mock = mock_bin / "curl" + curl_mock.write_text("#!/bin/sh\nexit 1\n") + curl_mock.chmod(0o755) + + image_info = tmp_path / "image-info.json" + image_info.write_text(json.dumps({"image-name": "utah", "image-flavor": "main", "image-tag": "latest"})) + + env = os.environ.copy() + env["PATH"] = f"{mock_bin}:{env['PATH']}" + env["STATE_DIRECTORY"] = str(state_dir) + env["IMAGE_INFO"] = str(image_info) + env["DISABLED_FILE"] = str(tmp_path / "nonexistent-disabled") + + res = subprocess.run([str(SCRIPT_PATH)], env=env, capture_output=True, text=True) + self.assertEqual(res.returncode, 0) + self.assertTrue((state_dir / "epoch").exists()) + self.assertTrue((state_dir / "lastrun").exists()) + + def test_os_release_fallback_when_image_info_missing(self): + with tempfile.TemporaryDirectory() as tmp: + tmp_path = Path(tmp) + state_dir = tmp_path / "state" + mock_bin = tmp_path / "bin" + mock_bin.mkdir() + + curl_args_log = tmp_path / "curl_args.txt" + curl_mock = mock_bin / "curl" + curl_mock.write_text(f'#!/bin/sh\necho "$@" > "{curl_args_log}"\nexit 0\n') + curl_mock.chmod(0o755) + + os_release = tmp_path / "os-release" + os_release.write_text( + 'VERSION_ID="43"\n' + 'IMAGE_VERSION="testing-osrelease"\n' + 'IMAGE_FLAVOR="gaming"\n' + ) + + env = os.environ.copy() + env["PATH"] = f"{mock_bin}:{env['PATH']}" + env["STATE_DIRECTORY"] = str(state_dir) + env["IMAGE_INFO"] = str(tmp_path / "nonexistent-image-info") + env["OS_RELEASE_FILE"] = str(os_release) + env["DISABLED_FILE"] = str(tmp_path / "nonexistent-disabled") + + res = subprocess.run([str(SCRIPT_PATH)], env=env, capture_output=True, text=True) + self.assertEqual(res.returncode, 0) + args = curl_args_log.read_text() + self.assertIn("repo=utah", args) + self.assertIn("tag=testing-osrelease", args) + self.assertIn("flavor=gaming", args) + self.assertIn("countme=1", args) + + +if __name__ == "__main__": + unittest.main()