diff --git a/cmd/github-actions-runner-job/output_readiness_test.go b/cmd/github-actions-runner-job/output_readiness_test.go new file mode 100644 index 0000000..c17c931 --- /dev/null +++ b/cmd/github-actions-runner-job/output_readiness_test.go @@ -0,0 +1,96 @@ +package main + +import ( + "archive/tar" + "compress/gzip" + "errors" + "io" + "os" + "path/filepath" + "slices" + "strings" + "testing" + + "github.com/GoCodeAlone/workflow-plugin-github/internal" +) + +// This exercises explicit collector calls, not automatic GitHub job retry. +func TestCopyWorkloadOutputsMissingRequestedFileDoesNotPublishArchive(t *testing.T) { + t.Chdir(t.TempDir()) + runnerDir := t.TempDir() + workspace := filepath.Join(runnerDir, "_work", "sample", "sample") + if err := os.MkdirAll(filepath.Join(workspace, "build"), 0o700); err != nil { + t.Fatal(err) + } + const summary = "ordinary build summary\n" + if err := os.WriteFile(filepath.Join(workspace, "build", "summary.txt"), []byte(summary), 0o600); err != nil { + t.Fatal(err) + } + + // A different runner has the same ordinary filename. It must not satisfy + // this driver's missing requested file; this is workspace, not run-ID, binding. + other := filepath.Join(t.TempDir(), "_work", "sample", "sample", "build") + if err := os.MkdirAll(other, 0o700); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(other, "result.json"), []byte("{\"result\":8}\n"), 0o600); err != nil { + t.Fatal(err) + } + driver := &runnerDriver{ + req: internal.EphemeralRunnerJobRequest{Repository: "fixture/sample"}, + runnerDir: runnerDir, + } + paths := []string{"build/summary.txt", "build/result.json"} + artifacts, err := driver.copyWorkloadOutputs(paths) + if err == nil || !strings.Contains(err.Error(), "build/result.json") { + t.Fatalf("missing requested file error = %v", err) + } + if len(artifacts) != 0 { + t.Fatalf("failed collection published artifact refs: %v", artifacts) + } + if _, err := os.Stat(workloadArtifactArchiveName); !os.IsNotExist(err) { + t.Fatalf("incomplete archive remains: %v", err) + } + if got := readFile(t, filepath.Join(workspace, "build", "summary.txt")); got != summary { + t.Fatalf("collection changed source file: %q", got) + } + + const result = "{\"result\":7}\n" + if err := os.WriteFile(filepath.Join(workspace, "build", "result.json"), []byte(result), 0o600); err != nil { + t.Fatal(err) + } + artifacts, err = driver.copyWorkloadOutputs(paths) + if err != nil { + t.Fatalf("explicit collection after file creation: %v", err) + } + if !slices.Equal(artifacts, []string{workloadArtifactArchiveName}) { + t.Fatalf("artifact refs = %v", artifacts) + } + archive, err := os.Open(workloadArtifactArchiveName) + if err != nil { + t.Fatal(err) + } + defer archive.Close() + compressed, err := gzip.NewReader(archive) + if err != nil { + t.Fatal(err) + } + defer compressed.Close() + reader := tar.NewReader(compressed) + for i, want := range []string{summary, result} { + header, err := reader.Next() + if err != nil { + t.Fatal(err) + } + if header.Name != paths[i] || header.Typeflag != tar.TypeReg || header.Size != int64(len(want)) { + t.Fatalf("unexpected archive member: %+v", header) + } + body, err := io.ReadAll(reader) + if err != nil || string(body) != want { + t.Fatalf("archive member %q bytes = %q, error = %v", header.Name, body, err) + } + } + if header, err := reader.Next(); !errors.Is(err, io.EOF) { + t.Fatalf("unexpected additional archive member: %+v, error = %v", header, err) + } +}