|
| 1 | +package imgutil_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/google/go-cmp/cmp" |
| 10 | + v1 "github.com/google/go-containerregistry/pkg/v1" |
| 11 | + "github.com/google/go-containerregistry/pkg/v1/random" |
| 12 | + "github.com/google/go-containerregistry/pkg/v1/types" |
| 13 | + |
| 14 | + "github.com/buildpacks/imgutil" |
| 15 | +) |
| 16 | + |
| 17 | +func TestCNBIndexSetIndexAnnotations(t *testing.T) { |
| 18 | + for _, test := range []struct { |
| 19 | + name string |
| 20 | + mediaType types.MediaType |
| 21 | + expectedMediaType types.MediaType |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: "oci index", |
| 25 | + expectedMediaType: types.OCIImageIndex, |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "docker index", |
| 29 | + mediaType: types.DockerManifestList, |
| 30 | + expectedMediaType: types.DockerManifestList, |
| 31 | + }, |
| 32 | + } { |
| 33 | + t.Run(test.name, func(t *testing.T) { |
| 34 | + tmpDir := t.TempDir() |
| 35 | + repoName := "some/index" |
| 36 | + |
| 37 | + index, err := imgutil.NewCNBIndex(repoName, imgutil.IndexOptions{ |
| 38 | + MediaType: test.mediaType, |
| 39 | + LayoutIndexOptions: imgutil.LayoutIndexOptions{ |
| 40 | + XdgPath: tmpDir, |
| 41 | + }, |
| 42 | + }) |
| 43 | + if err != nil { |
| 44 | + t.Fatal(err) |
| 45 | + } |
| 46 | + |
| 47 | + image, err := random.Image(1024, 1) |
| 48 | + if err != nil { |
| 49 | + t.Fatal(err) |
| 50 | + } |
| 51 | + index.AddManifest(image) |
| 52 | + |
| 53 | + existingAnnotations := map[string]string{ |
| 54 | + "existing-key": "existing-value", |
| 55 | + "shared-key": "old-value", |
| 56 | + } |
| 57 | + if err := index.SetIndexAnnotations(existingAnnotations); err != nil { |
| 58 | + t.Fatal(err) |
| 59 | + } |
| 60 | + |
| 61 | + newAnnotations := map[string]string{ |
| 62 | + "some-index-key": "some-index-value", |
| 63 | + "shared-key": "new-value", |
| 64 | + } |
| 65 | + if err := index.SetIndexAnnotations(newAnnotations); err != nil { |
| 66 | + t.Fatal(err) |
| 67 | + } |
| 68 | + if err := index.SaveDir(); err != nil { |
| 69 | + t.Fatal(err) |
| 70 | + } |
| 71 | + |
| 72 | + expectedAnnotations := map[string]string{ |
| 73 | + "existing-key": "existing-value", |
| 74 | + "some-index-key": "some-index-value", |
| 75 | + "shared-key": "new-value", |
| 76 | + } |
| 77 | + indexManifest := readIndexManifest(t, tmpDir, repoName) |
| 78 | + if diff := cmp.Diff(expectedAnnotations, indexManifest.Annotations); diff != "" { |
| 79 | + t.Fatalf("unexpected index annotations (-want +got):\n%s", diff) |
| 80 | + } |
| 81 | + if diff := cmp.Diff(test.expectedMediaType, indexManifest.MediaType); diff != "" { |
| 82 | + t.Fatalf("unexpected index media type (-want +got):\n%s", diff) |
| 83 | + } |
| 84 | + if diff := cmp.Diff(1, len(indexManifest.Manifests)); diff != "" { |
| 85 | + t.Fatalf("unexpected manifest count (-want +got):\n%s", diff) |
| 86 | + } |
| 87 | + }) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func TestCNBIndexSaveDirWithoutIndexAnnotations(t *testing.T) { |
| 92 | + tmpDir := t.TempDir() |
| 93 | + repoName := "some/index" |
| 94 | + |
| 95 | + index, err := imgutil.NewCNBIndex(repoName, imgutil.IndexOptions{ |
| 96 | + LayoutIndexOptions: imgutil.LayoutIndexOptions{ |
| 97 | + XdgPath: tmpDir, |
| 98 | + }, |
| 99 | + }) |
| 100 | + if err != nil { |
| 101 | + t.Fatal(err) |
| 102 | + } |
| 103 | + |
| 104 | + image, err := random.Image(1024, 1) |
| 105 | + if err != nil { |
| 106 | + t.Fatal(err) |
| 107 | + } |
| 108 | + index.AddManifest(image) |
| 109 | + |
| 110 | + if err := index.SaveDir(); err != nil { |
| 111 | + t.Fatal(err) |
| 112 | + } |
| 113 | + |
| 114 | + indexManifest := readIndexManifest(t, tmpDir, repoName) |
| 115 | + if diff := cmp.Diff(map[string]string(nil), indexManifest.Annotations); diff != "" { |
| 116 | + t.Fatalf("unexpected index annotations (-want +got):\n%s", diff) |
| 117 | + } |
| 118 | + if diff := cmp.Diff(1, len(indexManifest.Manifests)); diff != "" { |
| 119 | + t.Fatalf("unexpected manifest count (-want +got):\n%s", diff) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func readIndexManifest(t *testing.T, tmpDir, repoName string) v1.IndexManifest { |
| 124 | + t.Helper() |
| 125 | + |
| 126 | + rawIndex, err := os.ReadFile(filepath.Join(tmpDir, imgutil.MakeFileSafeName(repoName), "index.json")) |
| 127 | + if err != nil { |
| 128 | + t.Fatal(err) |
| 129 | + } |
| 130 | + |
| 131 | + var indexManifest v1.IndexManifest |
| 132 | + if err := json.Unmarshal(rawIndex, &indexManifest); err != nil { |
| 133 | + t.Fatal(err) |
| 134 | + } |
| 135 | + |
| 136 | + return indexManifest |
| 137 | +} |
0 commit comments