Skip to content

Commit

Permalink
refactor(test): make sure cli tests are not internal unless they need…
Browse files Browse the repository at this point in the history
… to be (#1878)

As part of this change searchConfig needed to be exported,
as it was passed as a parameter to exported functions

At this moment most of the tests remaining internal depend on the mock service.
The interface it implements has unexported methods.

Signed-off-by: Andrei Aaron <[email protected]>
  • Loading branch information
andaaron authored Oct 3, 2023
1 parent 99e29c0 commit ca1c328
Show file tree
Hide file tree
Showing 18 changed files with 3,227 additions and 3,118 deletions.
40 changes: 20 additions & 20 deletions pkg/cli/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ type httpJob struct {
password string
imageName string
tagName string
config searchConfig
config SearchConfig
}

const rateLimiterBuffer = 5000
Expand Down Expand Up @@ -218,16 +218,16 @@ func (p *requestsPool) doJob(ctx context.Context, job *httpJob) {
defer p.wtgrp.Done()

// Check manifest media type
header, err := makeHEADRequest(ctx, job.url, job.username, job.password, job.config.verifyTLS,
job.config.debug)
header, err := makeHEADRequest(ctx, job.url, job.username, job.password, job.config.VerifyTLS,
job.config.Debug)
if err != nil {
if isContextDone(ctx) {
return
}
p.outputCh <- stringResult{"", err}
}

verbose := job.config.verbose
verbose := job.config.Verbose

switch header.Get("Content-Type") {
case ispec.MediaTypeImageManifest:
Expand All @@ -242,7 +242,7 @@ func (p *requestsPool) doJob(ctx context.Context, job *httpJob) {
}
platformStr := getPlatformStr(image.Manifests[0].Platform)

str, err := image.string(job.config.outputFormat, len(job.imageName), len(job.tagName), len(platformStr), verbose)
str, err := image.string(job.config.OutputFormat, len(job.imageName), len(job.tagName), len(platformStr), verbose)
if err != nil {
if isContextDone(ctx) {
return
Expand Down Expand Up @@ -270,7 +270,7 @@ func (p *requestsPool) doJob(ctx context.Context, job *httpJob) {

platformStr := getPlatformStr(image.Manifests[0].Platform)

str, err := image.string(job.config.outputFormat, len(job.imageName), len(job.tagName), len(platformStr), verbose)
str, err := image.string(job.config.OutputFormat, len(job.imageName), len(job.tagName), len(platformStr), verbose)
if err != nil {
if isContextDone(ctx) {
return
Expand All @@ -294,7 +294,7 @@ func fetchImageIndexStruct(ctx context.Context, job *httpJob) (*imageStruct, err
var indexContent ispec.Index

header, err := makeGETRequest(ctx, job.url, job.username, job.password,
job.config.verifyTLS, job.config.debug, &indexContent, job.config.resultWriter)
job.config.VerifyTLS, job.config.Debug, &indexContent, job.config.ResultWriter)
if err != nil {
if isContextDone(ctx) {
return nil, context.Canceled
Expand Down Expand Up @@ -376,16 +376,16 @@ func fetchImageManifestStruct(ctx context.Context, job *httpJob) (*imageStruct,
}, nil
}

func fetchManifestStruct(ctx context.Context, repo, manifestReference string, searchConf searchConfig,
func fetchManifestStruct(ctx context.Context, repo, manifestReference string, searchConf SearchConfig,
username, password string,
) (common.ManifestSummary, error) {
manifestResp := ispec.Manifest{}

URL := fmt.Sprintf("%s/v2/%s/manifests/%s",
searchConf.servURL, repo, manifestReference)
searchConf.ServURL, repo, manifestReference)

header, err := makeGETRequest(ctx, URL, username, password,
searchConf.verifyTLS, searchConf.debug, &manifestResp, searchConf.resultWriter)
searchConf.VerifyTLS, searchConf.Debug, &manifestResp, searchConf.ResultWriter)
if err != nil {
if isContextDone(ctx) {
return common.ManifestSummary{}, context.Canceled
Expand Down Expand Up @@ -465,16 +465,16 @@ func fetchManifestStruct(ctx context.Context, repo, manifestReference string, se
}, nil
}

func fetchConfig(ctx context.Context, repo, configDigest string, searchConf searchConfig,
func fetchConfig(ctx context.Context, repo, configDigest string, searchConf SearchConfig,
username, password string,
) (ispec.Image, error) {
configContent := ispec.Image{}

URL := fmt.Sprintf("%s/v2/%s/blobs/%s",
searchConf.servURL, repo, configDigest)
searchConf.ServURL, repo, configDigest)

_, err := makeGETRequest(ctx, URL, username, password,
searchConf.verifyTLS, searchConf.debug, &configContent, searchConf.resultWriter)
searchConf.VerifyTLS, searchConf.Debug, &configContent, searchConf.ResultWriter)
if err != nil {
if isContextDone(ctx) {
return ispec.Image{}, context.Canceled
Expand All @@ -486,16 +486,16 @@ func fetchConfig(ctx context.Context, repo, configDigest string, searchConf sear
return configContent, nil
}

func isNotationSigned(ctx context.Context, repo, digestStr string, searchConf searchConfig,
func isNotationSigned(ctx context.Context, repo, digestStr string, searchConf SearchConfig,
username, password string,
) bool {
var referrers ispec.Index

URL := fmt.Sprintf("%s/v2/%s/referrers/%s?artifactType=%s",
searchConf.servURL, repo, digestStr, common.ArtifactTypeNotation)
searchConf.ServURL, repo, digestStr, common.ArtifactTypeNotation)

_, err := makeGETRequest(ctx, URL, username, password,
searchConf.verifyTLS, searchConf.debug, &referrers, searchConf.resultWriter)
searchConf.VerifyTLS, searchConf.Debug, &referrers, searchConf.ResultWriter)
if err != nil {
return false
}
Expand All @@ -507,16 +507,16 @@ func isNotationSigned(ctx context.Context, repo, digestStr string, searchConf se
return false
}

func isCosignSigned(ctx context.Context, repo, digestStr string, searchConf searchConfig,
func isCosignSigned(ctx context.Context, repo, digestStr string, searchConf SearchConfig,
username, password string,
) bool {
var result interface{}
cosignTag := strings.Replace(digestStr, ":", "-", 1) + "." + remote.SignatureTagSuffix

URL := fmt.Sprintf("%s/v2/%s/manifests/%s", searchConf.servURL, repo, cosignTag)
URL := fmt.Sprintf("%s/v2/%s/manifests/%s", searchConf.ServURL, repo, cosignTag)

_, err := makeGETRequest(ctx, URL, username, password, searchConf.verifyTLS,
searchConf.debug, &result, searchConf.resultWriter)
_, err := makeGETRequest(ctx, URL, username, password, searchConf.VerifyTLS,
searchConf.Debug, &result, searchConf.ResultWriter)

return err == nil
}
Expand Down
31 changes: 25 additions & 6 deletions pkg/cli/client/internal_test.go → pkg/cli/client/client_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
//go:build search
// +build search

package client
package client_test

import (
"bytes"
"crypto/tls"
"crypto/x509"
"fmt"
"os"
"path"
"path/filepath"
"testing"

Expand All @@ -18,6 +19,7 @@ import (
"zotregistry.io/zot/pkg/api"
"zotregistry.io/zot/pkg/api/config"
"zotregistry.io/zot/pkg/api/constants"
"zotregistry.io/zot/pkg/cli/client"
extConf "zotregistry.io/zot/pkg/extensions/config"
test "zotregistry.io/zot/pkg/test/common"
)
Expand Down Expand Up @@ -91,7 +93,7 @@ func TestTLSWithAuth(t *testing.T) {
defer os.RemoveAll(destCertsDir)

args := []string{"name", "dummyImageName", "--url", HOST1}
imageCmd := NewImageCommand(new(searchService))
imageCmd := client.NewImageCommand(client.NewSearchService())
imageBuff := bytes.NewBufferString("")
imageCmd.SetOut(imageBuff)
imageCmd.SetErr(imageBuff)
Expand All @@ -105,7 +107,7 @@ func TestTLSWithAuth(t *testing.T) {
fmt.Sprintf(`{"configs":[{"_name":"imagetest","url":"%s%s%s","showspinner":false}]}`,
BaseSecureURL1, constants.RoutePrefix, constants.ExtCatalogPrefix))
defer os.Remove(configPath)
imageCmd = NewImageCommand(new(searchService))
imageCmd = client.NewImageCommand(client.NewSearchService())
imageBuff = bytes.NewBufferString("")
imageCmd.SetOut(imageBuff)
imageCmd.SetErr(imageBuff)
Expand All @@ -120,7 +122,7 @@ func TestTLSWithAuth(t *testing.T) {
fmt.Sprintf(`{"configs":[{"_name":"imagetest","url":"%s%s%s","showspinner":false}]}`,
BaseSecureURL1, constants.RoutePrefix, constants.ExtCatalogPrefix))
defer os.Remove(configPath)
imageCmd = NewImageCommand(new(searchService))
imageCmd = client.NewImageCommand(client.NewSearchService())
imageBuff = bytes.NewBufferString("")
imageCmd.SetOut(imageBuff)
imageCmd.SetErr(imageBuff)
Expand Down Expand Up @@ -174,7 +176,7 @@ func TestTLSWithoutAuth(t *testing.T) {
defer os.RemoveAll(destCertsDir)

args := []string{"list", "--config", "imagetest"}
imageCmd := NewImageCommand(new(searchService))
imageCmd := client.NewImageCommand(client.NewSearchService())
imageBuff := bytes.NewBufferString("")
imageCmd.SetOut(imageBuff)
imageCmd.SetErr(imageBuff)
Expand Down Expand Up @@ -215,7 +217,7 @@ func TestTLSBadCerts(t *testing.T) {
defer os.Remove(configPath)

args := []string{"list", "--config", "imagetest"}
imageCmd := NewImageCommand(new(searchService))
imageCmd := client.NewImageCommand(client.NewSearchService())
imageBuff := bytes.NewBufferString("")
imageCmd.SetOut(imageBuff)
imageCmd.SetErr(imageBuff)
Expand All @@ -226,3 +228,20 @@ func TestTLSBadCerts(t *testing.T) {
})
})
}

func makeConfigFile(content string) string {
os.Setenv("HOME", os.TempDir())

home, err := os.UserHomeDir()
if err != nil {
panic(err)
}

configPath := path.Join(home, "/.zot")

if err := os.WriteFile(configPath, []byte(content), 0o600); err != nil {
panic(err)
}

return configPath
}
Loading

0 comments on commit ca1c328

Please sign in to comment.