diff --git a/Justfile b/Justfile index 6706129f..e386578f 100644 --- a/Justfile +++ b/Justfile @@ -19,6 +19,12 @@ 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 + bash -n system_files/shared/usr/libexec/utah-countme + pytest tests/unit/test_countme.py test -f scripts/configure-services.sh bash -n scripts/configure-services.sh test -f scripts/configure-branding.sh diff --git a/contracts/bluefin-desktop.toml b/contracts/bluefin-desktop.toml index 5fbada3a..02236435 100644 --- a/contracts/bluefin-desktop.toml +++ b/contracts/bluefin-desktop.toml @@ -138,5 +138,6 @@ enabled = [ "flatpak-preinstall.service", "gdm.service", "ublue-system-setup.service", + "utah-countme.timer", "uupd.timer", ] diff --git a/docs/skills/desktop-contract.md b/docs/skills/desktop-contract.md index 5e6c1e4f..2f7c909a 100644 --- a/docs/skills/desktop-contract.md +++ b/docs/skills/desktop-contract.md @@ -1,20 +1,20 @@ --- name: desktop-contract -version: "1.0" -last_updated: "2026-09-05" +version: "1.1" +last_updated: "2026-09-11" 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`, `ublue-system-setup.service`, `flatpak-preinstall.service`, `flatpak-nuke-fedora.service`, `brew-setup.service`, `dconf-update.service`, - `bootc-unified-storage.service`, `uupd.timer`. + `bootc-unified-storage.service`, `utah-countme.timer`, `uupd.timer`. ## GNOME extensions are pinned submodules @@ -88,16 +88,38 @@ system and glib-compile-schemas. 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. If the server is unreachable or offline, the script exits cleanly without updating `lastrun`, allowing the ping to be retried on the next schedule without skipping a weekly reporting window. +- **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 9a380aa0..7a235db4 100644 --- a/docs/skills/index.json +++ b/docs/skills/index.json @@ -1,5 +1,5 @@ { - "generated_at": "2026-09-05", + "generated_at": "2026-09-11", "schema_version": "1.0", "skills": [ { @@ -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,11 +49,13 @@ "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", - "last_updated": "2026-09-05", + "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-11", "doc_type": "policy" }, { diff --git a/docs/skills/index.md b/docs/skills/index.md index 2864a0b1..2526bc75 100644 --- a/docs/skills/index.md +++ b/docs/skills/index.md @@ -3,13 +3,13 @@ This file is a human-readable mirror of `index.json`. Both are generated by `scripts/generate_skill_index.py` — do not hand-edit either file. -Generated: 2026-09-05 · schema 1.0 · 8 skills +Generated: 2026-09-11 · schema 1.0 · 8 skills | id | category | status | one-line purpose | |---|---|---|---| | [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 49e783dc..1bb41d25 100644 --- a/scripts/configure-services.sh +++ b/scripts/configure-services.sh @@ -46,6 +46,7 @@ enable_unit fwupd-refresh.timer enable_unit dconf-update.service enable_unit tailscaled.service enable_unit uupd.timer +enable_unit utah-countme.timer enable_unit ublue-system-setup.service enable_unit systemd-resolved.service enable_unit bootc-unified-storage.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 ad4db080..e3e1648c 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 fwupd.service enable fwupd-refresh.timer enable tailscaled.service enable uupd.timer +enable utah-countme.timer enable ublue-system-setup.service enable systemd-resolved.service enable bootc-unified-storage.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..4b81c040 --- /dev/null +++ b/system_files/shared/usr/libexec/utah-countme @@ -0,0 +1,123 @@ +#!/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 jq >/dev/null 2>&1; then + echo "utah-countme: jq is unavailable; skipping telemetry" >&2 + exit 0 +fi + +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 +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 (( 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 +WEEKS=$(( (NOW - EPOCH) / WEEK + 1 )) +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 + INFO_NAME=$(jq -r '."image-name" // empty' "$IMAGE_INFO" 2>/dev/null || true) + if [ -n "$INFO_NAME" ]; then + REPO="$INFO_NAME" + fi + INFO_TAG=$(jq -r '."image-tag" // empty' "$IMAGE_INFO" 2>/dev/null || true) + if [ -n "$INFO_TAG" ]; then + IMAGE_TAG="$INFO_TAG" + fi + INFO_FLAVOR=$(jq -r '."image-flavor" // empty' "$IMAGE_INFO" 2>/dev/null || true) + 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|IMAGE_VERSION|VERSION_ID)=' "$OS_RELEASE_FILE" 2>/dev/null | head -n1 | cut -d= -f2 | tr -d '"' || true) + 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 +if curl -sf --retry 3 --max-time 10 -H "User-Agent: ${USER_AGENT}" "$URL" >/dev/null; then + echo "$NOW" > "$LASTRUN_FILE" +fi diff --git a/tests/unit/test_countme.py b/tests/unit/test_countme.py new file mode 100644 index 00000000..9d221149 --- /dev/null +++ b/tests/unit/test_countme.py @@ -0,0 +1,222 @@ +"""Unit tests for the utah-countme client script and systemd units.""" + +from __future__ import annotations + +import json +import os +import subprocess +from pathlib import Path + +REPO_ROOT = Path(__file__).resolve().parent.parent.parent +SCRIPT_PATH = REPO_ROOT / "system_files/shared/usr/libexec/utah-countme" +SERVICE_PATH = REPO_ROOT / "system_files/shared/usr/lib/systemd/system/utah-countme.service" +TIMER_PATH = REPO_ROOT / "system_files/shared/usr/lib/systemd/system/utah-countme.timer" +PRESET_PATH = REPO_ROOT / "system_files/shared/usr/lib/systemd/system-preset/85-utah-desktop.preset" + + +def test_units_exist_and_match_contract(): + assert SCRIPT_PATH.is_file() + assert os.access(SCRIPT_PATH, os.X_OK) + assert SERVICE_PATH.is_file() + assert TIMER_PATH.is_file() + + service_text = SERVICE_PATH.read_text() + assert "ConditionPathExists=!/etc/projectbluefin/countme/disabled" in service_text + assert "ConditionPathExists=/run/ostree-booted" in service_text + assert "ConditionPathExists=/usr/share/ublue-os/image-info.json" in service_text + assert "DynamicUser=yes" in service_text + assert "StateDirectory=utah-countme" in service_text + assert "ExecStart=/usr/libexec/utah-countme" in service_text + + timer_text = TIMER_PATH.read_text() + assert "ConditionPathExists=!/etc/projectbluefin/countme/disabled" in timer_text + assert "ConditionPathExists=/run/ostree-booted" in timer_text + assert "ConditionPathExists=/usr/share/ublue-os/image-info.json" in timer_text + assert "OnCalendar=weekly" in timer_text + assert "Persistent=true" in timer_text + + preset_text = PRESET_PATH.read_text() + assert "enable utah-countme.timer" in preset_text + + +def test_opt_out_file_skips_execution(tmp_path): + disabled_file = tmp_path / "disabled" + disabled_file.touch() + + mock_bin = tmp_path / "bin" + mock_bin.mkdir() + # Mock curl that fails if called + curl_mock = mock_bin / "curl" + curl_mock.write_text("#!/bin/sh\nexit 1\n") + curl_mock.chmod(0o755) + + env = os.environ.copy() + env["PATH"] = f"{mock_bin}:{env['PATH']}" + env["DISABLED_FILE"] = str(disabled_file) + env["STATE_DIRECTORY"] = str(tmp_path / "state") + + res = subprocess.run([str(SCRIPT_PATH)], env=env, capture_output=True, text=True) + assert res.returncode == 0 + assert not (tmp_path / "state" / "lastrun").exists() + + +def test_first_run_creates_epoch_and_pings(tmp_path): + 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) + assert res.returncode == 0 + assert (state_dir / "epoch").exists() + assert (state_dir / "lastrun").exists() + + args = curl_args_log.read_text() + assert "https://countme.projectbluefin.io/metalink" in args + assert "repo=utah" in args + assert "tag=testing-20260911" in args + assert "flavor=nvidia" in args + assert "countme=1" in args + assert "utah-countme" in args + + +def test_throttling_skips_within_7_days(tmp_path): + state_dir = tmp_path / "state" + state_dir.mkdir() + mock_bin = tmp_path / "bin" + mock_bin.mkdir() + + # If curl is called, it will write to this file + 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) + + import time + now = int(time.time()) + (state_dir / "epoch").write_text(str(now - 100)) + # Last run was 1 day ago (less than 7 days) + (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) + assert res.returncode == 0 + assert not curl_invoked.exists() + + +def test_age_cohort_buckets(tmp_path): + 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 + import time + 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: + 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) + assert res.returncode == 0 + args = curl_args_log.read_text() + assert f"countme={expected_bucket}" in args + + +def test_corrupt_epoch_skips_telemetry_without_resetting(tmp_path): + 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) + assert res.returncode == 0 + assert not curl_invoked.exists() + assert (state_dir / "epoch").read_text() == "not-a-number" + assert not (state_dir / "lastrun").exists() + + +def test_curl_failure_does_not_update_lastrun(tmp_path): + 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) + assert res.returncode == 0 + assert (state_dir / "epoch").exists() + assert not (state_dir / "lastrun").exists() +