From d57ab7f4fafb78975aefb2642382e323ccf89041 Mon Sep 17 00:00:00 2001 From: David Trudgian Date: Fri, 21 Jun 2024 16:08:24 +0100 Subject: [PATCH] feat: Update SIF from ImageIndex Add `sif.Update` which accepts updates a SIF so that its content reflects the ImageIndex passed. Any blobs in the SIF that are not present in the new ImageIndex will be removed from the SIF. Any blobs that are present in the new ImageIndex, but not in the SIF, will be added to the SIF. Blobs that are present in both the SIF and the new ImageIndex are not re-written. They will remain at their current descriptor location in the SIF. Closes #48 --- pkg/sif/testdata/TestUpdate/AddImage.golden | Bin 0 -> 18089 bytes .../testdata/TestUpdate/AddImageIndex.golden | Bin 0 -> 18335 bytes pkg/sif/testdata/TestUpdate/AddLayer.golden | Bin 0 -> 18602 bytes .../testdata/TestUpdate/ReplaceLayers.golden | Bin 0 -> 61143 bytes pkg/sif/update.go | 341 ++++++++++++++++++ pkg/sif/update_test.go | 148 ++++++++ test/images.go | 4 +- 7 files changed, 491 insertions(+), 2 deletions(-) create mode 100644 pkg/sif/testdata/TestUpdate/AddImage.golden create mode 100644 pkg/sif/testdata/TestUpdate/AddImageIndex.golden create mode 100644 pkg/sif/testdata/TestUpdate/AddLayer.golden create mode 100644 pkg/sif/testdata/TestUpdate/ReplaceLayers.golden create mode 100644 pkg/sif/update.go create mode 100644 pkg/sif/update_test.go diff --git a/pkg/sif/testdata/TestUpdate/AddImage.golden b/pkg/sif/testdata/TestUpdate/AddImage.golden new file mode 100644 index 0000000000000000000000000000000000000000..59afbd8c98927023df30f2ae4db508f33390a755 GIT binary patch literal 18089 zcmeHO30M=?_6J2!5yh27kr)+fLCDM`lZ7HGRs~etP!SL(nHdO%ghBvW!cr)rB2=m@ zDkv%{xByxeM8O3?i-?E{Vil0eA|jg=S!(__B-r-v>wEU~d*Ao^z6@dR+&lN4d(ZFu z?zuNJCn_rc{8(eN*me1WB{tS7G)RR;`?&HbVRWZdDpgUWBNR2p4As<@UkIiuQWJS< zsmKpiQ>ID;ln5vhP$Hm2K#7170VM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_GELd;Wk6O=C!d18FwuXzU_!drf4fmn?3+}DV4zL3fxF@X@epHSVb>)J!OD)ZPeZ0*DA|56% z_u=w|oZsX>WPiOeSR~JaOR9@0)!Gx6$uf5*1Okz)z{V4Elj*C7|KjmzBuT;`l8fLG zD1zYx0|$YGbT&bUd2ATvk+Qes5X_^4C>}a^Q4ZoRm?VfRL`VjM#o_|k)`r8l`@w{Q9x>KztWLH@&yD3 zW3q7^a1(27(4?5H5?7qcdO_B4uH@FbFXKVY}c3Fkm^tLP;>+=oG$? zVosq-O#jvJey8!j7#`&#dz(@TPcLzRs{jj-%{sBSFChV*LIma{RlpbeZUs7fi$r1) z6a-Pl0bVlyF<9A01~8u_UHLe;=QQM02$IV{*f4}+2m&IqXeflzP&&kA!Walk#zEXD z24QRrfC}O&P$msyF)$h(!x3B-)J$ViU4V`KFmFC4a~f1X7F7~7nnIyaeR%GKC$?_L zmK4}@u-KEp`PkaQaZ#w4mzRLg!^FVa=6*ulOilnZ9N5~M&-Im+n|WeFK1uk9&HP{! z4@|_xy=D`Bxxqbyv^JK`Cf9Q28Dv5%M}AL z=By#bWIcSO+h2`VAJh#nem84>KZhhPlA&j_YSLHb zg_^3Eu11Id@%^SqQ&#mjyWB#oTmKU`FZ zZhrTpU?k1XXWEYN{0(j|qcWODI=ekSc;jO9Ij{B{dj{n;vvZQ@V9&nz+?9GaHoi0J zb@(<=pwV#D>i(skB_*43vz^Lkne1s8w_ou66UP*JCB)!=48mOpJKrS}`$ eLCAJdzgf;dS|mP^&GI#m{*P$*SK~xw#{ECQv>Pn| literal 0 HcmV?d00001 diff --git a/pkg/sif/testdata/TestUpdate/AddImageIndex.golden b/pkg/sif/testdata/TestUpdate/AddImageIndex.golden new file mode 100644 index 0000000000000000000000000000000000000000..fafc2ae0ae5e4b9ffe77b24066d527ac98657c24 GIT binary patch literal 18335 zcmeHO3tUXw{x4F8M4k~b1|`)rd+*uvN5>mGg5{eFa zM3iTVQYYn{Bnc%)4;>XrF9$uCyQV4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zRvwtET;KlqFFqga9gr}HeoQNmWV zz(?o~`lYyCj`0Zh{_4^`>qvkZ`k?2*7>Pm*h{NZB6zt3MIUF9F#RA)C96H!OgD{f7 z`49^Q+p>I|&V~sd%HpsIn1op@9nd4FV?&o(Y8B60sK;oQ?tiLL>-EqH|D)!MCD<;N~Uw|M2f@GjD43YA% zTo^>FfUsSN4Z`F=G#rxz{bN1lfZT|xUGcL1|L8;DDlBOz7j(Qg|r#ZjIKjsbnZp@_of z06CJRNiZ*(tH01wfmEMR&~=4kAjX_MpqRXc&vg5<(dvD=0m)LiI?ltS0sof>u<@_e z`iuf%zOdgmLJ>}E`^7@k0V2rR8}LtM{4pB!LxRGnd|)U{{K>+9GC~%NHBWa;LW;dS zaaz_)eQt=b}yov&U!zDHcLHk8mwlM?};k3T$=)E|}xr`Nd-cUqYwTA(Osy4V}*I zJLN9@*E0d;BN2$@ix)3Ugo{0BiWSo*xBvS>e&cu*mE~=VJv@j$KPCaKP{4^%6b~u= zty1U|F-d`_pPVkJ^+o;`!gxFo{(<5sh(jY`5+Oi{EXu^`z^lSwDgog|IKpPIc_2m> zEblM~{4)s20b$rIh{RD6>GQV(&JRqmUpl`2;Mn-o!XFuEzw1@|hZq7QpAWG=8}bRJ z+JTAf5Bh_D<|+Go3d5|=d-`XioB>(B*i8TLZjkYhZ1iWNDgP3h|CdexU?KUd70nmS TlZu%hM(Lk3`OgM5%I*1g8`+NJ literal 0 HcmV?d00001 diff --git a/pkg/sif/testdata/TestUpdate/AddLayer.golden b/pkg/sif/testdata/TestUpdate/AddLayer.golden new file mode 100644 index 0000000000000000000000000000000000000000..091662d91b06476984838fc58adb218230d3e841 GIT binary patch literal 18602 zcmeI33p^F;yT_5Jq@;^mqE(S-nVGd_-6TpkL`5pMG;1y_VacuB*=(uApDmY)QmM$b zwvciuQCmnV(k{x*CFEAgWm{*2{Xb{#bN1Q$?9cyv&OV`XRnH8c2XmTwKo6gK zf}bv@AMk%ECmR1BTHGBF2xf4Y01V(X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!Mw_wVp5`fq3c KS5p@zX8v!hR|(bt literal 0 HcmV?d00001 diff --git a/pkg/sif/testdata/TestUpdate/ReplaceLayers.golden b/pkg/sif/testdata/TestUpdate/ReplaceLayers.golden new file mode 100644 index 0000000000000000000000000000000000000000..a957a861901cd225f454e7e8f200e600c1cf93bf GIT binary patch literal 61143 zcmeI$U5J!b7zgmtEZZX3!iy-V7^;h0$MgNMQJ9qxr4&YpA$s2PvHP0tZZp%8Zkr`Y zr4&JhX+al76jAg^7gC#11QA6Sk`Y1WQUoe0bWv2>nVp@DP)dVjh5s-+&kXN*&-y%8|f|LNm<_ zV>4wrr(7PBg)}>--6^CU-3oNE`T5MC2wOj4FBPNd6)C5<@7H)S(yoijC^$wE3UXV)ntxxbz2i>*IX`O%4| zNFfC)tco!dTFV%ejf5+yeTdOHsS{Dj5=X6f+&V+86S<^}RMD$MX>_{1XuO`Vz*&oO z4b91?bqeX51MN9}otml>Pmw~3UV4*)(2AOZ8&7$Nff`DTq>)>bg!5kVB5G3v?o%N( zEi82fjZ{XTrdEvr0SG_<0uX=z1Rwwb2tWV=5P$##AOHafKmY;|fB*y_009U<00Izz z00bZa0SG_<0uX=z1Rwwb2tWV=5P$##AOHafKmY;|fB*y_009U<00Izz00bZa0SG_< z0uX=z1Rwwb2tWV=5P$##AOHafKmY;|fB*y_009U<00Izz00bZa0SG_<0uX=z1Rwwb z2tWV=5P$##AOHafKmY;|fB*y_009U<00Izz00bZa0SG_<0uX=z1Rwwb2tWV=5P$## zAOHafKmY;|fB*y_009U<00Izz00bZa0SG_<0uX=z1Rwwb2tWV=5P$##AOHafKmY;| zfB*y_009U<00Izz00bZa0SG_<0uX=z1R(HV1xBh3-yhez+he^^4h~d%SxV zIl`d7GuCUNY&7d-ad&I(TIcNAFzZa%C^zN|45Ol_8p^&rb>j#VG zo$>8gbNAl#`fFbwc;lnBm-T&r>Eq>f-SZF6`mj1@;rWMV)6JW^-|F7o`^qD0UcTgl zo=@km*tX@#E%Aywx;Edwm|VZ}mt}WtUH;pTU+&rX`mM`9S^4(!A3o5v;)#cEo^{>j z56|B6)EBCI@$NHTy=eXWL%TL~uc_YB_50%2R%}|i^x8e=&iU-f)enxGyXm{{p4<8J zkqZtiT=wj)Czmccxa-P^o#%Y~!~Nr9JKp`n4*&7Og>OE#X4z#wHSZc9+y2=C{?hN` z_wIdfG%T2R-|3I;GY7l2=M_y%jGg!HSS2rRr0VLvzidX|yu04o&Ad&%9$hnr{2>d{ z1(VO)2^})er%I`<2&33c1&TYtTP(BjMlvbCa)yi}_j-*AA9AnW(6LWh(@&_TgFoi0t zjv{bnXtI=3s*1pvw~RzmSSA>y(WmOF(R}kA97ttfzT)WUi8fxx*)&aG;dnZ*Jh?aFOqO0}k|*ZMY=LwT>-sWPUH&Qu!fO_4MUDjMcIpGC%r)y>=^xVu)?YsL!q^dLD@*S jlG=wDos&8dl`L`8ddICZ)H;!O%SaWyO1WB{Zms-JPzxm@ literal 0 HcmV?d00001 diff --git a/pkg/sif/update.go b/pkg/sif/update.go new file mode 100644 index 0000000..fedc198 --- /dev/null +++ b/pkg/sif/update.go @@ -0,0 +1,341 @@ +// Copyright 2024 Sylabs Inc. All rights reserved. +// +// SPDX-License-Identifier: Apache-2.0 + +package sif + +import ( + "bytes" + "io" + "os" + "path/filepath" + "slices" + + v1 "github.com/google/go-containerregistry/pkg/v1" + "github.com/google/go-containerregistry/pkg/v1/types" + "github.com/sylabs/sif/v2/pkg/sif" +) + +// updateOpts accumulates update options. +type updateOpts struct { + tempDir string +} + +// UpdateOpt are used to specify options to apply when updating a SIF. +type UpdateOpt func(*updateOpts) error + +// OptUpdateTempDir sets the directory to use for temporary files. If not set, the +// directory returned by os.TempDir is used. +func OptUpdateTempDir(d string) UpdateOpt { + return func(c *updateOpts) error { + c.tempDir = d + return nil + } +} + +// Update modifies the SIF file associated with fi so that it holds the content +// of ImageIndex ii. Any blobs in the SIF that are not referenced in ii are +// removed from the SIF. Any blobs that are referenced in ii but not present in +// the SIF are added to the SIF. The RootIndex of the SIF is replaced with ii. +// +// Update may create one or more temporary files during the update process. By +// default, the directory returned by os.TempDir is used. To override this, +// consider using OptUpdateTmpDir. +func Update(fi *sif.FileImage, ii v1.ImageIndex, opts ...UpdateOpt) error { + uo := updateOpts{ + tempDir: os.TempDir(), + } + for _, opt := range opts { + if err := opt(&uo); err != nil { + return err + } + } + + // If the existing OCI.RootIndex in the SIF matches ii, then there is nothing to do. + sifRootIndex, err := ImageIndexFromFileImage(fi) + if err != nil { + return err + } + sifRootDigest, err := sifRootIndex.Digest() + if err != nil { + return err + } + newRootDigest, err := ii.Digest() + if err != nil { + return err + } + if sifRootDigest == newRootDigest { + return nil + } + + // Get a list of all existing OCI.Blob digests in the SIF + sifBlobs, err := sifBlobs(fi) + if err != nil { + return err + } + + // Cache all new blobs referenced by the new ImageIndex and its child + // indices / images, which aren't already in the SIF. cachedblobs are new + // things to add. keepBlobs already exist in the SIF and should be kept. + blobCache, err := os.MkdirTemp(uo.tempDir, "") + if err != nil { + return err + } + defer os.RemoveAll(blobCache) + cachedBlobs, keepBlobs, err := cacheIndexBlobs(ii, sifBlobs, blobCache) + if err != nil { + return err + } + + // Compute the new RootIndex. + ri, err := ii.RawManifest() + if err != nil { + return err + } + + // Delete existing blobs from the SIF except those we want to keep. + if err := deleteBlobsExcept(fi, keepBlobs); err != nil { + return err + } + // Delete old RootIndex. + if err := deleteRootIndex(fi); err != nil { + return err + } + + // Write new (cached) blobs from ii into the SIF. + f := fileImage{fi} + for _, b := range cachedBlobs { + rc, err := readCacheBlob(b, blobCache) + if err != nil { + return err + } + if err := f.writeBlobToFileImage(rc, false); err != nil { + return err + } + if err := rc.Close(); err != nil { + return err + } + } + + // Write the new RootIndex into the SIF. + return f.writeBlobToFileImage(bytes.NewReader(ri), true) +} + +// sifBlobs will return a list of digests for all OCI.Blob descriptors in fi. +func sifBlobs(fi *sif.FileImage) ([]v1.Hash, error) { + descrs, err := fi.GetDescriptors(sif.WithDataType(sif.DataOCIBlob)) + if err != nil { + return nil, err + } + sifBlobs := make([]v1.Hash, len(descrs)) + for i, d := range descrs { + dDigest, err := d.OCIBlobDigest() + if err != nil { + return nil, err + } + sifBlobs[i] = dDigest + } + return sifBlobs, nil +} + +// cacheIndexBlobs will cache all blobs referenced by ii, except those with +// digests specified in skip. The blobs will be cached to files in cacheDir, +// with filenames equal to their digest. The function returns two lists of blobs +// - those that were cached (in ii but not skip), and those that were skipped +// (in ii and skip). +func cacheIndexBlobs(ii v1.ImageIndex, skip []v1.Hash, cacheDir string) ([]v1.Hash, []v1.Hash, error) { + index, err := ii.IndexManifest() + if err != nil { + return nil, nil, err + } + + cached := []v1.Hash{} + skipped := []v1.Hash{} + + for _, desc := range index.Manifests { + //nolint:exhaustive + switch desc.MediaType { + case types.DockerManifestList, types.OCIImageIndex: + childIndex, err := ii.ImageIndex(desc.Digest) + if err != nil { + return nil, nil, err + } + // Cache children of this ImageIndex + childCached, childSkipped, err := cacheIndexBlobs(childIndex, skip, cacheDir) + if err != nil { + return nil, nil, err + } + cached = append(cached, childCached...) + skipped = append(skipped, childSkipped...) + // Cache the ImageIndex itself. + if slices.Contains(skip, desc.Digest) { + skipped = append(skipped, desc.Digest) + continue + } + rm, err := childIndex.RawManifest() + if err != nil { + return nil, nil, err + } + rc := io.NopCloser(bytes.NewReader(rm)) + if err := writeCacheBlob(rc, desc.Digest, cacheDir); err != nil { + return nil, nil, err + } + cached = append(cached, desc.Digest) + + case types.DockerManifestSchema2, types.OCIManifestSchema1: + childImage, err := ii.Image(desc.Digest) + if err != nil { + return nil, nil, err + } + childCached, childSkipped, err := cacheImageBlobs(childImage, skip, cacheDir) + if err != nil { + return nil, nil, err + } + cached = append(cached, childCached...) + skipped = append(skipped, childSkipped...) + + default: + return nil, nil, errUnexpectedMediaType + } + } + return cached, skipped, nil +} + +// cacheImageBlobs will cache all blobs referenced by im, except those with +// digests specified in skip. The blobs will be cached to files in cacheDir, +// with filenames equal to their digest. The function returns lists of blobs +// that were cached (in ii but not skip), and those that were skipped (in ii and +// skipDigests). +func cacheImageBlobs(im v1.Image, skip []v1.Hash, cacheDir string) ([]v1.Hash, []v1.Hash, error) { + cached := []v1.Hash{} + skipped := []v1.Hash{} + + // Cache layers first. + layers, err := im.Layers() + if err != nil { + return nil, nil, err + } + for _, l := range layers { + ld, err := l.Digest() + if err != nil { + return nil, nil, err + } + + if slices.Contains(skip, ld) { + skipped = append(skipped, ld) + continue + } + + rc, err := l.Compressed() + if err != nil { + return nil, nil, err + } + if err := writeCacheBlob(rc, ld, cacheDir); err != nil { + return nil, nil, err + } + cached = append(cached, ld) + } + + // Cache image config. + mf, err := im.Manifest() + if err != nil { + return nil, nil, err + } + if slices.Contains(skip, mf.Config.Digest) { + skipped = append(skipped, mf.Config.Digest) + } else { + c, err := im.RawConfigFile() + if err != nil { + return nil, nil, err + } + rc := io.NopCloser(bytes.NewReader(c)) + if err := writeCacheBlob(rc, mf.Config.Digest, cacheDir); err != nil { + return nil, nil, err + } + cached = append(cached, mf.Config.Digest) + } + + // Cache image manifest itself. + id, err := im.Digest() + if err != nil { + return nil, nil, err + } + if slices.Contains(skip, id) { + skipped = append(skipped, id) + return cached, skipped, nil + } + rm, err := im.RawManifest() + if err != nil { + return nil, nil, err + } + rc := io.NopCloser(bytes.NewReader(rm)) + if err := writeCacheBlob(rc, id, cacheDir); err != nil { + return nil, nil, err + } + cached = append(cached, id) + + return cached, skipped, nil +} + +// writeCacheBlob writes blob content from rc into tmpDir with filename equal to +// specified digest. +func writeCacheBlob(rc io.ReadCloser, digest v1.Hash, cacheDir string) error { + path := filepath.Join(cacheDir, digest.String()) + f, err := os.Create(path) + if err != nil { + return err + } + defer f.Close() + + _, err = io.Copy(f, rc) + if err != nil { + return err + } + + if err := rc.Close(); err != nil { + return err + } + return nil +} + +// readCacheBlob returns a ReadCloser that will read blob content from cacheDir +// with filename equal to specified digest. +func readCacheBlob(digest v1.Hash, cacheDir string) (io.ReadCloser, error) { + path := filepath.Join(cacheDir, digest.String()) + f, err := os.Open(path) + if err != nil { + return nil, err + } + return f, nil +} + +// deleteBlobsExcept removes all OCI.Blob descriptors from fi, except those with +// digests listed in keep. +func deleteBlobsExcept(fi *sif.FileImage, keep []v1.Hash) error { + descs, err := fi.GetDescriptors(sif.WithDataType(sif.DataOCIBlob)) + if err != nil { + return err + } + for _, d := range descs { + dd, err := d.OCIBlobDigest() + if err != nil { + return err + } + if slices.Contains(keep, dd) { + continue + } + if err := fi.DeleteObject(d.ID(), sif.OptDeleteZero(true)); err != nil { + return err + } + } + return nil +} + +// deleteRootIndex removes the RootIndex from a the SIF fi. +func deleteRootIndex(fi *sif.FileImage) error { + desc, err := fi.GetDescriptor(sif.WithDataType(sif.DataOCIRootIndex)) + if err != nil { + return err + } + return fi.DeleteObject(desc.ID()) +} diff --git a/pkg/sif/update_test.go b/pkg/sif/update_test.go new file mode 100644 index 0000000..c5c7730 --- /dev/null +++ b/pkg/sif/update_test.go @@ -0,0 +1,148 @@ +// Copyright 2024 Sylabs Inc. All rights reserved. +// +// SPDX-License-Identifier: Apache-2.0 + +package sif_test + +import ( + "math/rand" + "os" + "testing" + + v1 "github.com/google/go-containerregistry/pkg/v1" + "github.com/google/go-containerregistry/pkg/v1/empty" + v1mutate "github.com/google/go-containerregistry/pkg/v1/mutate" + "github.com/google/go-containerregistry/pkg/v1/random" + "github.com/google/go-containerregistry/pkg/v1/types" + "github.com/sebdah/goldie/v2" + "github.com/sylabs/oci-tools/pkg/mutate" + "github.com/sylabs/oci-tools/pkg/sif" + ssif "github.com/sylabs/sif/v2/pkg/sif" +) + +const randomSeed = 1719306160 + +//nolint:gocognit +func TestUpdate(t *testing.T) { + r := rand.NewSource(randomSeed) + + tests := []struct { + name string + base string + updater func(*testing.T, v1.ImageIndex) v1.ImageIndex + opts []sif.UpdateOpt + }{ + { + name: "AddLayer", + base: "hello-world-docker-v2-manifest", + updater: func(t *testing.T, ii v1.ImageIndex) v1.ImageIndex { + t.Helper() + ih, err := v1.NewHash("sha256:432f982638b3aefab73cc58ab28f5c16e96fdb504e8c134fc58dff4bae8bf338") + if err != nil { + t.Fatal(err) + } + im, err := ii.Image(ih) + if err != nil { + t.Fatal(err) + } + l, err := random.Layer(64, types.DockerLayer, random.WithSource(r)) + if err != nil { + t.Fatal(err) + } + im, err = v1mutate.AppendLayers(im, l) + if err != nil { + t.Fatal(err) + } + return v1mutate.AppendManifests(empty.Index, v1mutate.IndexAddendum{Add: im}) + }, + }, + { + name: "ReplaceLayers", // Replaces many layers with a single layer + base: "many-layers", + updater: func(t *testing.T, ii v1.ImageIndex) v1.ImageIndex { + t.Helper() + ih, err := v1.NewHash("sha256:7c000de5bc837f29d1c9a5e76bba79922d860e5c0f448df3b6fc38431a067c9a") + if err != nil { + t.Fatal(err) + } + im, err := ii.Image(ih) + if err != nil { + t.Fatal(err) + } + l, err := random.Layer(64, types.DockerLayer, random.WithSource(r)) + if err != nil { + t.Fatal(err) + } + im, err = mutate.Apply(im, mutate.ReplaceLayers(l)) + if err != nil { + t.Fatal(err) + } + return v1mutate.AppendManifests(empty.Index, v1mutate.IndexAddendum{Add: im}) + }, + }, + { + name: "AddImage", + base: "hello-world-docker-v2-manifest", + updater: func(t *testing.T, ii v1.ImageIndex) v1.ImageIndex { + t.Helper() + im, err := random.Image(64, 1, random.WithSource(r)) + if err != nil { + t.Fatal(err) + } + if err != nil { + t.Fatal(err) + } + return v1mutate.AppendManifests(ii, v1mutate.IndexAddendum{Add: im}) + }, + }, + { + name: "AddImageIndex", + base: "hello-world-docker-v2-manifest", + updater: func(t *testing.T, ii v1.ImageIndex) v1.ImageIndex { + t.Helper() + addIdx, err := random.Index(64, 1, 1, random.WithSource(r)) + if err != nil { + t.Fatal(err) + } + if err != nil { + t.Fatal(err) + } + return v1mutate.AppendManifests(ii, v1mutate.IndexAddendum{Add: addIdx}) + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + sifPath := corpus.SIF(t, tt.base, sif.OptWriteWithSpareDescriptorCapacity(8)) + fi, err := ssif.LoadContainerFromPath(sifPath) + if err != nil { + t.Fatal(err) + } + ii, err := sif.ImageIndexFromFileImage(fi) + if err != nil { + t.Fatal(err) + } + + ii = tt.updater(t, ii) + + if err := sif.Update(fi, ii, tt.opts...); err != nil { + t.Fatal(err) + } + + if err := fi.UnloadContainer(); err != nil { + t.Fatal(err) + } + + b, err := os.ReadFile(sifPath) + if err != nil { + t.Fatal(err) + } + + g := goldie.New(t, + goldie.WithTestNameForDir(true), + ) + + g.Assert(t, tt.name, b) + }) + } +} diff --git a/test/images.go b/test/images.go index ddef73b..c815bcd 100644 --- a/test/images.go +++ b/test/images.go @@ -63,12 +63,12 @@ func (c *Corpus) OCILayout(tb testing.TB, name string) string { // SIF returns a temporary SIF for the test to use, populated from the OCI Image Layout with the // specified name in the corpus. The SIF is automatically removed when the test and all its // subtests complete. -func (c *Corpus) SIF(tb testing.TB, name string) string { +func (c *Corpus) SIF(tb testing.TB, name string, opt ...sif.WriteOpt) string { tb.Helper() path := filepath.Join(tb.TempDir(), "image.sif") - if err := sif.Write(path, c.ImageIndex(tb, name)); err != nil { + if err := sif.Write(path, c.ImageIndex(tb, name), opt...); err != nil { tb.Fatalf("failed to write SIF: %v", err) }