Skip to content

Commit

Permalink
refactor(pkg/test): split logic in pkg/test/common.go into multiple p…
Browse files Browse the repository at this point in the history
…ackages (#1861)

Which could be imported independently. See more details:
1. "zotregistry.io/zot/pkg/test/common" - currently used as
   tcommon "zotregistry.io/zot/pkg/test/common" - inside pkg/test
   test "zotregistry.io/zot/pkg/test/common" - in tests
   . "zotregistry.io/zot/pkg/test/common" - in tests
Decouple zb from code in test/pkg in order to keep the size small.

2. "zotregistry.io/zot/pkg/test/image-utils" - curently used as
   . "zotregistry.io/zot/pkg/test/image-utils"

3. "zotregistry.io/zot/pkg/test/deprecated" -  curently used as
   "zotregistry.io/zot/pkg/test/deprecated"
This one will bre replaced gradually by image-utils in the future.

4. "zotregistry.io/zot/pkg/test/signature" - (cosign + notation) use as
   "zotregistry.io/zot/pkg/test/signature"

5. "zotregistry.io/zot/pkg/test/auth" - (bearer + oidc)  curently used as
   authutils "zotregistry.io/zot/pkg/test/auth"

 6. "zotregistry.io/zot/pkg/test/oci-utils" -  curently used as
   ociutils "zotregistry.io/zot/pkg/test/oci-utils"

Some unused functions were removed, some were replaced, and in
a few cases specific funtions were moved to the files they were used in.

Added an interface for the StoreController, this reduces the number of imports
of the entire image store, decreasing binary size for tests.
If the zb code was still coupled with pkg/test, this would have reflected in zb size.

Signed-off-by: Andrei Aaron <[email protected]>
  • Loading branch information
andaaron authored Sep 27, 2023
1 parent c3801dc commit ba6f347
Show file tree
Hide file tree
Showing 82 changed files with 3,354 additions and 3,235 deletions.
61 changes: 45 additions & 16 deletions cmd/zb/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"math/rand"
"net/http"
"net/url"
"os"
"path"
"sync"
Expand All @@ -21,8 +22,6 @@ import (

zerr "zotregistry.io/zot/errors"
"zotregistry.io/zot/pkg/common"
testc "zotregistry.io/zot/pkg/test/common"
"zotregistry.io/zot/pkg/test/image-utils"
)

func makeHTTPGetRequest(url string, resultPtr interface{}, client *resty.Client) error {
Expand Down Expand Up @@ -344,7 +343,7 @@ func pushMonolithImage(workdir, url, trepo string, repos []string, config testCo
resp.StatusCode(), string(resp.Body())) //nolint: goerr113
}

loc := testc.Location(url, resp)
loc := getLocation(url, resp)

var size int

Expand Down Expand Up @@ -398,8 +397,8 @@ func pushMonolithImage(workdir, url, trepo string, repos []string, config testCo
resp.StatusCode(), string(resp.Body()))
}

loc = testc.Location(url, resp)
cblob, cdigest := getRandomImageConfig()
loc = getLocation(url, resp)
cblob, cdigest := getImageConfig()
resp, err = client.R().
SetContentLength(true).
SetHeader("Content-Length", fmt.Sprintf("%d", len(cblob))).
Expand Down Expand Up @@ -526,7 +525,7 @@ func pushMonolithAndCollect(workdir, url, trepo string, count int,
return
}

loc := testc.Location(url, resp)
loc := getLocation(url, resp)

var size int

Expand Down Expand Up @@ -594,8 +593,8 @@ func pushMonolithAndCollect(workdir, url, trepo string, count int,
return
}

loc = testc.Location(url, resp)
cblob, cdigest := getRandomImageConfig()
loc = getLocation(url, resp)
cblob, cdigest := getImageConfig()
resp, err = client.R().
SetContentLength(true).
SetHeader("Content-Length", fmt.Sprintf("%d", len(cblob))).
Expand Down Expand Up @@ -730,7 +729,7 @@ func pushChunkAndCollect(workdir, url, trepo string, count int,
return
}

loc := testc.Location(url, resp)
loc := getLocation(url, resp)

var size int

Expand Down Expand Up @@ -768,7 +767,7 @@ func pushChunkAndCollect(workdir, url, trepo string, count int,
return
}

loc = testc.Location(url, resp)
loc = getLocation(url, resp)

// request specific check
statusCode = resp.StatusCode()
Expand Down Expand Up @@ -822,8 +821,8 @@ func pushChunkAndCollect(workdir, url, trepo string, count int,
return
}

loc = testc.Location(url, resp)
cblob, cdigest := getRandomImageConfig()
loc = getLocation(url, resp)
cblob, cdigest := getImageConfig()
resp, err = client.R().
SetContentLength(true).
SetHeader("Content-Type", "application/octet-stream").
Expand Down Expand Up @@ -859,7 +858,7 @@ func pushChunkAndCollect(workdir, url, trepo string, count int,
return
}

loc = testc.Location(url, resp)
loc = getLocation(url, resp)

// request specific check
statusCode = resp.StatusCode()
Expand Down Expand Up @@ -1020,9 +1019,21 @@ func loadOrStore(statusRequests *sync.Map, key string, value int) int { //nolint
return intValue
}

// TO DO: replace with pkg/test/images when available.
func getRandomImageConfig() ([]byte, godigest.Digest) {
config := image.GetDefaultConfig()
func getImageConfig() ([]byte, godigest.Digest) {
createdTime := time.Date(2011, time.Month(1), 1, 1, 1, 1, 0, time.UTC)

config := ispec.Image{
Created: &createdTime,
Author: "ZotUser",
Platform: ispec.Platform{
OS: "linux",
Architecture: "amd64",
},
RootFS: ispec.RootFS{
Type: "layers",
DiffIDs: []godigest.Digest{},
},
}

configBlobContent, err := json.MarshalIndent(&config, "", "\t")
if err != nil {
Expand All @@ -1033,3 +1044,21 @@ func getRandomImageConfig() ([]byte, godigest.Digest) {

return configBlobContent, configBlobDigestRaw
}

func getLocation(baseURL string, resp *resty.Response) string {
// For some API responses, the Location header is set and is supposed to
// indicate an opaque value. However, it is not clear if this value is an
// absolute URL (https://server:port/v2/...) or just a path (/v2/...)
// zot implements the latter as per the spec, but some registries appear to
// return the former - this needs to be clarified
loc := resp.Header().Get("Location")

uloc, err := url.Parse(loc)
if err != nil {
return ""
}

path := uloc.Path

return baseURL + path
}
7 changes: 4 additions & 3 deletions pkg/api/authn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import (
"zotregistry.io/zot/pkg/log"
mTypes "zotregistry.io/zot/pkg/meta/types"
reqCtx "zotregistry.io/zot/pkg/requestcontext"
"zotregistry.io/zot/pkg/test"
authutils "zotregistry.io/zot/pkg/test/auth"
test "zotregistry.io/zot/pkg/test/common"
"zotregistry.io/zot/pkg/test/mocks"
)

Expand Down Expand Up @@ -81,7 +82,7 @@ func TestAPIKeys(t *testing.T) {
htpasswdPath := test.MakeHtpasswdFile()
defer os.Remove(htpasswdPath)

mockOIDCServer, err := test.MockOIDCRun()
mockOIDCServer, err := authutils.MockOIDCRun()
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -834,7 +835,7 @@ func TestAPIKeysOpenDBError(t *testing.T) {
htpasswdPath := test.MakeHtpasswdFile()
defer os.Remove(htpasswdPath)

mockOIDCServer, err := test.MockOIDCRun()
mockOIDCServer, err := authutils.MockOIDCRun()
if err != nil {
panic(err)
}
Expand Down
Loading

0 comments on commit ba6f347

Please sign in to comment.