Skip to content

Commit

Permalink
✅ Change distribution test case (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
tosone authored Nov 27, 2023
1 parent 5732830 commit 7dea547
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 68 deletions.
2 changes: 1 addition & 1 deletion pkg/configs/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ type ConfigurationStorage struct {

// ConfigurationProxy ...
type ConfigurationProxy struct {
Enabled string `yaml:"enabled"`
Enabled bool `yaml:"enabled"`
Endpoint string `yaml:"endpoint"`
TlsVerify bool `yaml:"tlsVerify"`
Username string `yaml:"username"`
Expand Down
36 changes: 18 additions & 18 deletions pkg/handlers/distribution/clients/blobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (

func TestGetBlob(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/v2/", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/v2/", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})

Expand All @@ -42,7 +42,7 @@ func TestGetBlob(t *testing.T) {
assert.NoError(t, err)
dgest, err := digest.Parse("sha256:" + hashStr)
assert.NoError(t, err)
mux.HandleFunc("/v2/library/busybox/blobs/"+dgest.String(), func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/v2/library/busybox/blobs/"+dgest.String(), func(w http.ResponseWriter, _ *http.Request) {
w.Header().Add(consts.ContentDigest, dgest.String())
w.Header().Add(echo.HeaderContentType, "application/vnd.oci.image.layer.v1.tar+gzip")
w.Write([]byte(body)) // nolint: errcheck
Expand Down Expand Up @@ -72,7 +72,7 @@ func TestGetBlob(t *testing.T) {

func TestGetBlob1(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/v2/", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/v2/", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})

Expand All @@ -81,7 +81,7 @@ func TestGetBlob1(t *testing.T) {
assert.NoError(t, err)
dgest, err := digest.Parse("sha256:" + hashStr)
assert.NoError(t, err)
mux.HandleFunc("/v2/library/busybox/blobs/"+dgest.String(), func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/v2/library/busybox/blobs/"+dgest.String(), func(w http.ResponseWriter, _ *http.Request) {
w.Header().Add(consts.ContentDigest, dgest.String())
w.Header().Add(echo.HeaderContentType, "application/vnd.oci.image.layer.v1.tar+gzip")
w.Header().Add(echo.HeaderContentLength, "aaa")
Expand All @@ -106,7 +106,7 @@ func TestGetBlob1(t *testing.T) {

func TestGetBlob2(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/v2/", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/v2/", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})

Expand All @@ -115,7 +115,7 @@ func TestGetBlob2(t *testing.T) {
assert.NoError(t, err)
dgest, err := digest.Parse("sha256:" + hashStr)
assert.NoError(t, err)
mux.HandleFunc("/v2/library/busybox/blobs/"+dgest.String(), func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/v2/library/busybox/blobs/"+dgest.String(), func(w http.ResponseWriter, _ *http.Request) {
w.Header().Add(consts.ContentDigest, "m"+dgest.String())
w.Header().Add(echo.HeaderContentType, "application/vnd.oci.image.layer.v1.tar+gzip")
w.Write([]byte(body)) // nolint: errcheck
Expand All @@ -139,7 +139,7 @@ func TestGetBlob2(t *testing.T) {

func TestGetBlob3(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/v2/", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/v2/", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})

Expand All @@ -148,7 +148,7 @@ func TestGetBlob3(t *testing.T) {
assert.NoError(t, err)
dgest, err := digest.Parse("sha256:" + hashStr)
assert.NoError(t, err)
mux.HandleFunc("/v2/library/busybox/blobs/"+dgest.String(), func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/v2/library/busybox/blobs/"+dgest.String(), func(w http.ResponseWriter, _ *http.Request) {
w.Header().Add(consts.ContentDigest, "m"+dgest.String())
w.Header().Add(echo.HeaderContentType, "application/vnd.oci.image.layer.v1.tar+gzip")
w.Write([]byte(body)) // nolint: errcheck
Expand All @@ -174,7 +174,7 @@ func TestGetBlob3(t *testing.T) {

func TestGetBlob4(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/v2/", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/v2/", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})

Expand All @@ -183,7 +183,7 @@ func TestGetBlob4(t *testing.T) {
assert.NoError(t, err)
dgest, err := digest.Parse("sha256:" + hashStr)
assert.NoError(t, err)
mux.HandleFunc("/v2/library/busybox/blobs/"+dgest.String(), func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/v2/library/busybox/blobs/"+dgest.String(), func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
})
s := httptest.NewServer(mux)
Expand All @@ -205,7 +205,7 @@ func TestGetBlob4(t *testing.T) {

func TestHeadBlob(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/v2/", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/v2/", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})

Expand All @@ -214,7 +214,7 @@ func TestHeadBlob(t *testing.T) {
assert.NoError(t, err)
dgest, err := digest.Parse("sha256:" + hashStr)
assert.NoError(t, err)
mux.HandleFunc("/v2/library/busybox/blobs/"+dgest.String(), func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/v2/library/busybox/blobs/"+dgest.String(), func(w http.ResponseWriter, _ *http.Request) {
w.Header().Add(consts.ContentDigest, dgest.String())
w.Header().Add(echo.HeaderContentType, "application/vnd.oci.image.layer.v1.tar+gzip")
w.Write([]byte(body)) // nolint: errcheck
Expand All @@ -241,7 +241,7 @@ func TestHeadBlob(t *testing.T) {

func TestHeadBlob2(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/v2/", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/v2/", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})

Expand All @@ -250,7 +250,7 @@ func TestHeadBlob2(t *testing.T) {
assert.NoError(t, err)
dgest, err := digest.Parse("sha256:" + hashStr)
assert.NoError(t, err)
mux.HandleFunc("/v2/library/busybox/blobs/"+dgest.String(), func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/v2/library/busybox/blobs/"+dgest.String(), func(w http.ResponseWriter, _ *http.Request) {
w.Header().Add(consts.ContentDigest, "m"+dgest.String())
w.Header().Add(echo.HeaderContentType, "application/vnd.oci.image.layer.v1.tar+gzip")
w.Write([]byte(body)) // nolint: errcheck
Expand All @@ -274,7 +274,7 @@ func TestHeadBlob2(t *testing.T) {

func TestHeadBlob3(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/v2/", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/v2/", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})

Expand All @@ -283,7 +283,7 @@ func TestHeadBlob3(t *testing.T) {
assert.NoError(t, err)
dgest, err := digest.Parse("sha256:" + hashStr)
assert.NoError(t, err)
mux.HandleFunc("/v2/library/busybox/blobs/"+dgest.String(), func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/v2/library/busybox/blobs/"+dgest.String(), func(w http.ResponseWriter, _ *http.Request) {
w.Header().Add(consts.ContentDigest, "m"+dgest.String())
w.Header().Add(echo.HeaderContentType, "application/vnd.oci.image.layer.v1.tar+gzip")
w.Write([]byte(body)) // nolint: errcheck
Expand All @@ -309,7 +309,7 @@ func TestHeadBlob3(t *testing.T) {

func TestHeadBlob4(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/v2/", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/v2/", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})

Expand All @@ -318,7 +318,7 @@ func TestHeadBlob4(t *testing.T) {
assert.NoError(t, err)
dgest, err := digest.Parse("sha256:" + hashStr)
assert.NoError(t, err)
mux.HandleFunc("/v2/library/busybox/blobs/"+dgest.String(), func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/v2/library/busybox/blobs/"+dgest.String(), func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
})
s := httptest.NewServer(mux)
Expand Down
16 changes: 5 additions & 11 deletions pkg/handlers/distribution/clients/clients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,18 +245,12 @@ func TestDoRequestPing2(t *testing.T) {
cPassword := "sigma"

mux := http.NewServeMux()
mux.HandleFunc("/v2/", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/v2/", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
})
s := httptest.NewServer(mux)
defer s.Close()

// viper.SetDefault("log.level", "info")
// viper.SetDefault("proxy.endpoint", s.URL)
// viper.SetDefault("proxy.tlsVerify", true)
// viper.SetDefault("proxy.username", cUsername)
// viper.SetDefault("proxy.password", cPassword)

f := NewClientsFactory()
_, err := f.New(configs.Configuration{
Log: configs.ConfigurationLog{
Expand All @@ -276,7 +270,7 @@ func TestDoRequestPing3(t *testing.T) {
cPassword := "sigma"

mux := http.NewServeMux()
mux.HandleFunc("/v2/", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/v2/", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusUnauthorized)
})
s := httptest.NewServer(mux)
Expand Down Expand Up @@ -304,7 +298,7 @@ func TestDoRequestPing4(t *testing.T) {
wwwAuthenticate2 := fmt.Sprintf(`Bearer realm="%s",service="%s",scope="%s"`, "http://127.0.0.1:3001/user/token", "registry.docker.io", "repository:library/alpine:pull")

mux := http.NewServeMux()
mux.HandleFunc("/v2/", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/v2/", func(w http.ResponseWriter, _ *http.Request) {
w.Header().Add(echo.HeaderWWWAuthenticate, wwwAuthenticate1)
w.Header().Add(echo.HeaderWWWAuthenticate, wwwAuthenticate2)
w.WriteHeader(http.StatusUnauthorized)
Expand Down Expand Up @@ -332,7 +326,7 @@ func TestDoRequestNoUsername(t *testing.T) {
wwwAuthenticate := fmt.Sprintf(`Basic realm="%s",service="%s",scope="%s"`, "http://127.0.0.1:3000/user/token", "registry.docker.io", "repository:library/alpine:pull")

mux := http.NewServeMux()
mux.HandleFunc("/v2/", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/v2/", func(w http.ResponseWriter, _ *http.Request) {
w.Header().Add(echo.HeaderWWWAuthenticate, wwwAuthenticate)
w.WriteHeader(http.StatusUnauthorized)
})
Expand Down Expand Up @@ -362,7 +356,7 @@ func TestDoRequestPingBasicAuth(t *testing.T) {
authTimes := 0

mux := http.NewServeMux()
mux.HandleFunc("/v2/", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/v2/", func(w http.ResponseWriter, _ *http.Request) {
authTimes++
if authTimes == 1 {
w.Header().Add(echo.HeaderWWWAuthenticate, wwwAuthenticate)
Expand Down
9 changes: 7 additions & 2 deletions pkg/handlers/distribution/manifest/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ type Handler interface {
var _ Handler = &handler{}

type handler struct {
config configs.Configuration

config *configs.Configuration
repositoryServiceFactory dao.RepositoryServiceFactory
tagServiceFactory dao.TagServiceFactory
artifactServiceFactory dao.ArtifactServiceFactory
Expand All @@ -53,6 +52,7 @@ type handler struct {
}

type inject struct {
config *configs.Configuration
repositoryServiceFactory dao.RepositoryServiceFactory
tagServiceFactory dao.TagServiceFactory
artifactServiceFactory dao.ArtifactServiceFactory
Expand All @@ -67,6 +67,7 @@ func handlerNew(injects ...inject) Handler {
artifactServiceFactory := dao.NewArtifactServiceFactory()
blobServiceFactory := dao.NewBlobServiceFactory()
auditServiceFactory := dao.NewAuditServiceFactory()
config := configs.GetConfiguration()
if len(injects) > 0 {
ij := injects[0]
if ij.artifactServiceFactory != nil {
Expand All @@ -84,8 +85,12 @@ func handlerNew(injects ...inject) Handler {
if ij.auditServiceFactory != nil {
auditServiceFactory = ij.auditServiceFactory
}
if ij.config != nil {
config = ij.config
}
}
return &handler{
config: config,
repositoryServiceFactory: repositoryServiceFactory,
tagServiceFactory: tagServiceFactory,
artifactServiceFactory: artifactServiceFactory,
Expand Down
2 changes: 0 additions & 2 deletions pkg/handlers/distribution/manifest/manifest_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

"github.com/labstack/echo/v4"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"

"github.com/go-sigma/sigma/pkg/dal"
Expand All @@ -37,7 +36,6 @@ import (

func TestDeleteManifest(t *testing.T) {
logger.SetLevel("debug")
viper.SetDefault("log.level", "debug")
err := tests.Initialize(t)
assert.NoError(t, err)
err = tests.DB.Init()
Expand Down
3 changes: 2 additions & 1 deletion pkg/handlers/distribution/manifest/manifest_fallback.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/rs/zerolog/log"

"github.com/go-sigma/sigma/pkg/handlers/distribution/clients"
"github.com/go-sigma/sigma/pkg/utils/ptr"
)

// fallbackProxy cannot found the manifest, proxy to the origin registry
Expand All @@ -33,7 +34,7 @@ func (h *handler) fallbackProxy(c echo.Context) (int, http.Header, []byte, error
headers.Add(echo.HeaderAccept, "application/vnd.oci.image.index.v1+json")

f := clients.NewClientsFactory()
cli, err := f.New(h.config) // TODO: config param
cli, err := f.New(ptr.To(h.config)) // TODO: config param
if err != nil {
return 0, nil, nil, err
}
Expand Down
9 changes: 1 addition & 8 deletions pkg/handlers/distribution/manifest/manifest_fallback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (

"github.com/labstack/echo/v4"
dtspecv1 "github.com/opencontainers/distribution-spec/specs-go/v1"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"

"github.com/go-sigma/sigma/pkg/configs"
Expand Down Expand Up @@ -79,12 +78,6 @@ func TestFallbackProxy(t *testing.T) {
}))
defer srv.Close()

viper.SetDefault("log.level", "debug")
viper.SetDefault("proxy.endpoint", srv.URL)
viper.SetDefault("proxy.tlsVerify", true)
viper.SetDefault("proxy.username", cUsername)
viper.SetDefault("proxy.password", cPassword)

wwwAuthenticate = fmt.Sprintf(`Bearer realm="%s",service="%s",scope="%s"`, srv.URL+"/user/token", "registry.docker.io", "repository:library/alpine:pull")

req := httptest.NewRequest(http.MethodGet, "/v2/_catalog", nil)
Expand All @@ -93,7 +86,7 @@ func TestFallbackProxy(t *testing.T) {
c := e.NewContext(req, rec)

h := handler{
config: configs.Configuration{
config: &configs.Configuration{
Log: configs.ConfigurationLog{
ProxyLevel: enums.LogLevelDebug,
},
Expand Down
5 changes: 2 additions & 3 deletions pkg/handlers/distribution/manifest/manifest_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/labstack/echo/v4"
"github.com/opencontainers/go-digest"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"gorm.io/gorm"

"github.com/go-sigma/sigma/pkg/consts"
Expand Down Expand Up @@ -60,7 +59,7 @@ func (h *handler) GetManifest(c echo.Context) error {
tagService := h.tagServiceFactory.New()
tag, err := tagService.GetByName(ctx, repositoryObj.ID, ref)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) && viper.GetBool("proxy.enabled") {
if errors.Is(err, gorm.ErrRecordNotFound) && h.config.Proxy.Enabled {
return h.getManifestFallbackProxy(c, refs)
}
log.Error().Err(err).Str("ref", ref).Msg("Get artifact failed")
Expand All @@ -76,7 +75,7 @@ func (h *handler) GetManifest(c echo.Context) error {
artifactService := h.artifactServiceFactory.New()
artifact, err := artifactService.GetByDigest(ctx, repositoryObj.ID, refs.Digest.String())
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) && viper.GetBool("proxy.enabled") {
if errors.Is(err, gorm.ErrRecordNotFound) && h.config.Proxy.Enabled {
return h.getManifestFallbackProxy(c, refs)
}
log.Error().Err(err).Str("ref", ref).Msg("Get artifact failed")
Expand Down
2 changes: 1 addition & 1 deletion pkg/handlers/distribution/manifest/manifest_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
func TestGetManifestFallbackProxyAuthError(t *testing.T) {
mux := http.NewServeMux()

mux.HandleFunc("/v2/", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/v2/", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
})

Expand Down
5 changes: 2 additions & 3 deletions pkg/handlers/distribution/manifest/manifest_head.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/labstack/echo/v4"
"github.com/opencontainers/go-digest"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"gorm.io/gorm"

"github.com/go-sigma/sigma/pkg/consts"
Expand Down Expand Up @@ -60,7 +59,7 @@ func (h *handler) HeadManifest(c echo.Context) error {
tagService := h.tagServiceFactory.New()
tag, err := tagService.GetByName(ctx, repositoryObj.ID, ref)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) && viper.GetBool("proxy.enabled") {
if errors.Is(err, gorm.ErrRecordNotFound) && h.config.Proxy.Enabled {
return h.headManifestFallbackProxy(c)
}
log.Error().Err(err).Str("ref", ref).Msg("Get artifact failed")
Expand All @@ -77,7 +76,7 @@ func (h *handler) HeadManifest(c echo.Context) error {
artifact, err := artifactService.GetByDigest(ctx, repositoryObj.ID, refs.Digest.String())
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
if viper.GetBool("proxy.enabled") {
if h.config.Proxy.Enabled {
return h.headManifestFallbackProxy(c)
} else {
log.Error().Err(err).Str("ref", ref).Msg("Artifact not found")
Expand Down
Loading

0 comments on commit 7dea547

Please sign in to comment.