@@ -2237,6 +2237,103 @@ func TestStore_PredecessorsAndDelete(t *testing.T) {
22372237 }
22382238}
22392239
2240+ func TestStore_PredecessorsAndDeleteTree (t * testing.T ) {
2241+ tempDir := t .TempDir ()
2242+ s , err := New (tempDir )
2243+ if err != nil {
2244+ t .Fatal ("New() error =" , err )
2245+ }
2246+ ctx := context .Background ()
2247+
2248+ // generate test content
2249+ var blobs [][]byte
2250+ var descs []ocispec.Descriptor
2251+ appendBlob := func (mediaType string , blob []byte ) {
2252+ blobs = append (blobs , blob )
2253+ descs = append (descs , ocispec.Descriptor {
2254+ MediaType : mediaType ,
2255+ Digest : digest .FromBytes (blob ),
2256+ Size : int64 (len (blob )),
2257+ })
2258+ }
2259+ generateManifest := func (config ocispec.Descriptor , layers ... ocispec.Descriptor ) {
2260+ manifest := ocispec.Manifest {
2261+ Config : config ,
2262+ Layers : layers ,
2263+ }
2264+ manifestJSON , err := json .Marshal (manifest )
2265+ if err != nil {
2266+ t .Fatal (err )
2267+ }
2268+ appendBlob (ocispec .MediaTypeImageManifest , manifestJSON )
2269+ }
2270+ generateIndex := func (manifests ... ocispec.Descriptor ) {
2271+ index := ocispec.Index {
2272+ Manifests : manifests ,
2273+ }
2274+ indexJSON , err := json .Marshal (index )
2275+ if err != nil {
2276+ t .Fatal (err )
2277+ }
2278+ appendBlob (ocispec .MediaTypeImageIndex , indexJSON )
2279+ }
2280+
2281+ appendBlob (ocispec .MediaTypeImageLayer , []byte ("foo" )) // Blob 0
2282+ appendBlob (ocispec .MediaTypeImageLayer , []byte ("bar" )) // Blob 1
2283+ generateManifest (ocispec .DescriptorEmptyJSON , descs [0 ]) // Blob 2
2284+ generateManifest (ocispec .DescriptorEmptyJSON , descs [1 ]) // Blob 3
2285+ generateIndex (descs [2 :4 ]... ) // Blob 4
2286+
2287+ eg , egCtx := errgroup .WithContext (ctx )
2288+ for i := range blobs {
2289+ eg .Go (func (i int ) func () error {
2290+ return func () error {
2291+ err := s .Push (egCtx , descs [i ], bytes .NewReader (blobs [i ]))
2292+ if err != nil {
2293+ return fmt .Errorf ("failed to push test content to src: %d: %v" , i , err )
2294+ }
2295+ return nil
2296+ }
2297+ }(i ))
2298+ }
2299+ if err := eg .Wait (); err != nil {
2300+ t .Fatal (err )
2301+ }
2302+
2303+ // verify predecessors
2304+ wants := [][]ocispec.Descriptor {
2305+ {descs [2 ]}, // Blob 0
2306+ {descs [3 ]}, // Blob 1
2307+ {descs [4 ]}, // Blob 2
2308+ {descs [4 ]}, // Blob 3
2309+ nil , // Blob 4
2310+ }
2311+ for i , want := range wants {
2312+ predecessors , err := s .Predecessors (ctx , descs [i ])
2313+ if err != nil {
2314+ t .Errorf ("Store.Predecessors(%d) error = %v" , i , err )
2315+ }
2316+ if ! equalDescriptorSet (predecessors , want ) {
2317+ t .Errorf ("Store.Predecessors(%d) = %v, want %v" , i , predecessors , want )
2318+ }
2319+ }
2320+
2321+ // delete the tree and verify the result
2322+ err = s .DeleteTree (egCtx , descs [4 ])
2323+ if err != nil {
2324+ t .Errorf ("failed deleting tree: %v" , err )
2325+ }
2326+ for i , desc := range descs {
2327+ ok , err := s .Exists (ctx , desc )
2328+ if err != nil {
2329+ t .Errorf ("failed testing Store.Exists(%d): %v" , i , err )
2330+ }
2331+ if ok {
2332+ t .Errorf ("Store.Exists(%d) should have been deleted" , i )
2333+ }
2334+ }
2335+ }
2336+
22402337func equalDescriptorSet (actual []ocispec.Descriptor , expected []ocispec.Descriptor ) bool {
22412338 if len (actual ) != len (expected ) {
22422339 return false
0 commit comments