@@ -3,6 +3,7 @@ package main
33import (
44 "bytes"
55 "encoding/json"
6+ "errors"
67 "fmt"
78 "io"
89 "log"
@@ -18,21 +19,83 @@ import (
1819 ispec "github.com/opencontainers/image-spec/specs-go/v1"
1920 "gopkg.in/resty.v1"
2021
21- "zotregistry.io/zot/errors "
22+ "zotregistry.io/zot/pkg/api "
2223 "zotregistry.io/zot/pkg/test"
2324)
2425
26+ func makeHTTPGetRequest (url string , resultPtr interface {}, client * resty.Client ) error {
27+ resp , err := client .R ().Get (url )
28+ if err != nil {
29+ return err
30+ }
31+
32+ if resp .StatusCode () != http .StatusOK {
33+ log .Printf ("unable to make GET request on %s, response status code: %d" , url , resp .StatusCode ())
34+
35+ return errors .New (string (resp .Body ())) //nolint: goerr113
36+ }
37+
38+ err = json .Unmarshal (resp .Body (), resultPtr )
39+ if err != nil {
40+ return err
41+ }
42+
43+ return nil
44+ }
45+
46+ func makeHTTPDeleteRequest (url string , client * resty.Client ) error {
47+ resp , err := client .R ().Delete (url )
48+ if err != nil {
49+ return err
50+ }
51+
52+ if resp .StatusCode () != http .StatusAccepted {
53+ log .Printf ("unable to make DELETE request on %s, response status code: %d" , url , resp .StatusCode ())
54+
55+ return errors .New (string (resp .Body ())) //nolint: goerr113
56+ }
57+
58+ return nil
59+ }
60+
2561func deleteTestRepo (repos []string , url string , client * resty.Client ) error {
2662 for _ , repo := range repos {
27- resp , err := client .R ().Delete ((fmt .Sprintf ("%s/v2/%s/" , url , repo )))
63+ var tags api.ImageTags
64+
65+ // get tags
66+ err := makeHTTPGetRequest (fmt .Sprintf ("%s/v2/%s/tags/list" , url , repo ), & tags , client )
2867 if err != nil {
2968 return err
3069 }
3170
32- // request specific check
33- statusCode := resp .StatusCode ()
34- if statusCode != http .StatusAccepted {
35- return errors .ErrUnknownCode
71+ for _ , tag := range tags .Tags {
72+ var manifest ispec.Manifest
73+
74+ // first get tag manifest to get containing blobs
75+ err := makeHTTPGetRequest (fmt .Sprintf ("%s/v2/%s/manifests/%s" , url , repo , tag ), & manifest , client )
76+ if err != nil {
77+ return err
78+ }
79+
80+ // delete blobs
81+ for _ , blob := range manifest .Layers {
82+ err := makeHTTPDeleteRequest (fmt .Sprintf ("%s/v2/%s/blobs/%s" , url , repo , blob .Digest .String ()), client )
83+ if err != nil {
84+ return err
85+ }
86+ }
87+
88+ // delete config blob
89+ err = makeHTTPDeleteRequest (fmt .Sprintf ("%s/v2/%s/blobs/%s" , url , repo , manifest .Config .Digest .String ()), client )
90+ if err != nil {
91+ return err
92+ }
93+
94+ // delete manifest
95+ err = makeHTTPDeleteRequest (fmt .Sprintf ("%s/v2/%s/manifests/%s" , url , repo , tag ), client )
96+ if err != nil {
97+ return err
98+ }
3699 }
37100 }
38101
@@ -273,7 +336,7 @@ func pushMonolithImage(workdir, url, trepo string, repos []string, config testCo
273336 // request specific check
274337 statusCode = resp .StatusCode ()
275338 if statusCode != http .StatusAccepted {
276- return nil , repos , errors .ErrUnknownCode
339+ return nil , repos , errors .New ( string ( resp . Body ())) //nolint: goerr113
277340 }
278341
279342 loc := test .Location (url , resp )
@@ -311,7 +374,7 @@ func pushMonolithImage(workdir, url, trepo string, repos []string, config testCo
311374 // request specific check
312375 statusCode = resp .StatusCode ()
313376 if statusCode != http .StatusCreated {
314- return nil , repos , errors .ErrUnknownCode
377+ return nil , repos , errors .New ( string ( resp . Body ())) //nolint: goerr113
315378 }
316379
317380 // upload image config blob
@@ -325,7 +388,7 @@ func pushMonolithImage(workdir, url, trepo string, repos []string, config testCo
325388 // request specific check
326389 statusCode = resp .StatusCode ()
327390 if statusCode != http .StatusAccepted {
328- return nil , repos , errors .ErrUnknownCode
391+ return nil , repos , errors .New ( string ( resp . Body ())) //nolint: goerr113
329392 }
330393
331394 loc = test .Location (url , resp )
@@ -345,7 +408,7 @@ func pushMonolithImage(workdir, url, trepo string, repos []string, config testCo
345408 // request specific check
346409 statusCode = resp .StatusCode ()
347410 if statusCode != http .StatusCreated {
348- return nil , repos , errors .ErrUnknownCode
411+ return nil , repos , errors .New ( string ( resp . Body ())) //nolint: goerr113
349412 }
350413
351414 // create a manifest
@@ -388,7 +451,7 @@ func pushMonolithImage(workdir, url, trepo string, repos []string, config testCo
388451 // request specific check
389452 statusCode = resp .StatusCode ()
390453 if statusCode != http .StatusCreated {
391- return nil , repos , errors .ErrUnknownCode
454+ return nil , repos , errors .New ( string ( resp . Body ())) //nolint: goerr113
392455 }
393456
394457 manifestHash [repo ] = manifestTag
0 commit comments