|
| 1 | +package leeway_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/gitpod-io/leeway/pkg/leeway" |
| 10 | +) |
| 11 | + |
| 12 | +func TestAccessAttestationBundleInCachedArchive(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + setupFunc func(t *testing.T, dir string) string |
| 16 | + expectError bool |
| 17 | + expectContent string |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "provenance exists outside tar.gz", |
| 21 | + setupFunc: func(t *testing.T, dir string) string { |
| 22 | + artifactPath := filepath.Join(dir, "test.tar.gz") |
| 23 | + provenancePath := artifactPath + ".provenance.jsonl" |
| 24 | + |
| 25 | + // Create empty artifact |
| 26 | + if err := os.WriteFile(artifactPath, []byte("fake tar.gz"), 0644); err != nil { |
| 27 | + t.Fatal(err) |
| 28 | + } |
| 29 | + |
| 30 | + // Create provenance file |
| 31 | + content := `{"test": "provenance"}` |
| 32 | + if err := os.WriteFile(provenancePath, []byte(content), 0644); err != nil { |
| 33 | + t.Fatal(err) |
| 34 | + } |
| 35 | + |
| 36 | + return artifactPath |
| 37 | + }, |
| 38 | + expectError: false, |
| 39 | + expectContent: `{"test": "provenance"}`, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "provenance does not exist", |
| 43 | + setupFunc: func(t *testing.T, dir string) string { |
| 44 | + artifactPath := filepath.Join(dir, "test.tar.gz") |
| 45 | + |
| 46 | + // Create only artifact, no provenance |
| 47 | + if err := os.WriteFile(artifactPath, []byte("fake tar.gz"), 0644); err != nil { |
| 48 | + t.Fatal(err) |
| 49 | + } |
| 50 | + |
| 51 | + return artifactPath |
| 52 | + }, |
| 53 | + expectError: true, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "artifact does not exist", |
| 57 | + setupFunc: func(t *testing.T, dir string) string { |
| 58 | + return filepath.Join(dir, "nonexistent.tar.gz") |
| 59 | + }, |
| 60 | + expectError: true, |
| 61 | + }, |
| 62 | + } |
| 63 | + |
| 64 | + for _, tt := range tests { |
| 65 | + t.Run(tt.name, func(t *testing.T) { |
| 66 | + tmpDir := t.TempDir() |
| 67 | + artifactPath := tt.setupFunc(t, tmpDir) |
| 68 | + |
| 69 | + var content []byte |
| 70 | + err := leeway.AccessAttestationBundleInCachedArchive(artifactPath, func(bundle io.Reader) error { |
| 71 | + var readErr error |
| 72 | + content, readErr = io.ReadAll(bundle) |
| 73 | + return readErr |
| 74 | + }) |
| 75 | + |
| 76 | + if tt.expectError { |
| 77 | + if err == nil { |
| 78 | + t.Errorf("expected error but got none") |
| 79 | + } |
| 80 | + } else { |
| 81 | + if err != nil { |
| 82 | + t.Errorf("unexpected error: %v", err) |
| 83 | + } |
| 84 | + if string(content) != tt.expectContent { |
| 85 | + t.Errorf("expected content %q, got %q", tt.expectContent, string(content)) |
| 86 | + } |
| 87 | + } |
| 88 | + }) |
| 89 | + } |
| 90 | +} |
0 commit comments