package configtemplate import ( "os" "path/filepath" "sort" "strings" "testing" ) func TestDeployWorkflowOnlyPublishesRuntimeImageForTaggedRelease(t *testing.T) { source := readDeploymentFile(t, ".github/workflows/deploy.yml") // The fallback remains necessary because Dockerfile.archied accepts a // runtime version, but it must not be used as the image-publish selector. for _, required := range []string{ `RUNTIME_TAG="$(git tag --points-at HEAD --list 'archie/v*' | sort -V | tail -1)"`, `RELEASE_TAG="$(git tag --points-at HEAD --list 'v*' | sort -V | tail -1)"`, `echo "runtime_tag=$RUNTIME_TAG" >> "$GITHUB_OUTPUT"`, `echo "release_tag=$RELEASE_TAG" >> "$GITHUB_OUTPUT"`, } { if !strings.Contains(source, required) { t.Errorf("deploy workflow is missing runtime-image guard %q", required) } } // The gateway build remains unconditional, while the runtime build is // guarded. Both selectors are required. A one-stream release creates only a // `v*` tag -- release.sh has no archie/v* arm any more -- so a // runtime_tag-only condition would mean the agent image is never rebuilt // again, and `docker compose pull agent` would fetch an ancient image // forever. The legacy archie/v* arm stays because history is not re-tagged. agentMarker := " - name: Build and push archie-agent\n" agentStart := strings.Index(source, agentMarker) if agentStart < 0 { t.Fatal("deploy workflow has no archie-agent build step") } agentIf := " if: steps.versions.outputs.runtime_tag != '' || steps.versions.outputs.release_tag != ''\n" if !strings.Contains(source[agentStart:], agentMarker+agentIf) { t.Fatal("archie-agent build step is not conditionally enabled") } gatewayMarker := " - name: Build and push archied\n" gatewayStart := strings.Index(source, gatewayMarker) if gatewayStart < 0 { t.Fatal("deploy workflow has no archied build step") } if strings.Contains(source[gatewayStart:agentStart], "\n if:") { t.Fatal("archied build step is unexpectedly conditional") } } // TestDeployWorkflowPublishesLatestOnlyFromARelease pins the rule that keeps a // release's :latest stamping from being overwritten by the next ordinary commit. // // build-and-push runs on any push to main and used to always push :latest, so a // release's own stamping survived only until the next commit — minutes, in an // active repo — and a host deploying :latest reported dev, leaving the update // path unable to tell current from stale (archie-core-i5nb). :latest now moves // only from a commit that IS a release; everything else publishes :edge and an // immutable sha-. func TestDeployWorkflowPublishesLatestOnlyFromARelease(t *testing.T) { source := readDeploymentFile(t, ".github/workflows/deploy.yml") for _, required := range []string{ `if [ -n "$RELEASE_TAG" ]; then`, `echo "ghcr.io/samcharles93/archied:latest"`, `echo "ghcr.io/samcharles93/archied:edge"`, `archied_tags< 0 { t.Errorf("deployments/systemd-user-service.md names no unit for: %s", strings.Join(undocumented, " ")) } } // installBuildCommands reads install.sh's build loop. The file has a second // `for cmd in` (the git/go preflight), so the loop is located by the go build it // drives rather than by the first marker -- and read from the real list, so a // missing command is named instead of the marker merely going absent. func installBuildCommands(t *testing.T, source string) []string { t.Helper() before, _, ok := strings.Cut(source, `go build -ldflags "${LDFLAGS}"`) if !ok { t.Fatal("install.sh has no go build loop to read") } prefix := before start := strings.LastIndex(prefix, "for cmd in ") if start < 0 { t.Fatal("install.sh build loop has no for cmd list") } rest := prefix[start+len("for cmd in "):] before0, _, ok0 := strings.Cut(rest, "; do") if !ok0 { t.Fatal("install.sh build loop is unterminated") } return strings.Fields(before0) } // hostRunCommands is every command under cmd/, minus the deliberately // unpackaged ones. Deriving it is the point: a list written out here would drift // from cmd/ exactly as the packaged lists did. func hostRunCommands(t *testing.T) []string { t.Helper() entries, err := os.ReadDir("cmd") if err != nil { t.Fatalf("read cmd/: %v", err) } var commands []string for _, entry := range entries { if !entry.IsDir() { continue } if _, skip := unpackagedCommands[entry.Name()]; skip { continue } commands = append(commands, entry.Name()) } if len(commands) == 0 { t.Fatal("cmd/ holds no commands: this guard would assert nothing") } sort.Strings(commands) return commands } func withoutCLIOnly(commands []string) []string { out := make([]string, 0, len(commands)) for _, command := range commands { if cliOnlyCommands[command] { continue } out = append(out, command) } return out } // wordsAfter returns the whitespace-separated words between two markers on the // same line. expand maps a word to the words it stands for, which is how the // installer's "$GATEWAY_SERVICES" reference is followed to what it names. func wordsAfter(t *testing.T, source, marker, terminator string, expand map[string][]string) []string { t.Helper() start := strings.Index(source, marker) if start < 0 { t.Fatalf("no %q in the file under test", marker) } rest := source[start+len(marker):] end := strings.Index(rest, terminator) if end < 0 { t.Fatalf("no %q after %q", terminator, marker) } var words []string for word := range strings.FieldsSeq(rest[:end]) { if replacement, ok := expand[word]; ok { words = append(words, replacement...) continue } words = append(words, word) } return words } // setDifference names what is missing from and extra in got, so a failure says // which side moved rather than that two lists differ. func setDifference(want, got []string) string { present := make(map[string]bool, len(got)) for _, item := range got { present[item] = true } var missing []string for _, item := range want { if !present[item] { missing = append(missing, item) } } expected := make(map[string]bool, len(want)) for _, item := range want { expected[item] = true } var extra []string for _, item := range got { if !expected[item] { extra = append(extra, item) } } sort.Strings(missing) sort.Strings(extra) var problems []string if len(missing) > 0 { problems = append(problems, "missing "+strings.Join(missing, " ")) } if len(extra) > 0 { problems = append(problems, "unexpected "+strings.Join(extra, " ")) } return strings.Join(problems, "; ") }