diff --git a/build/ci/Makefile b/build/ci/Makefile index a6cd1f0..9e609e7 100644 --- a/build/ci/Makefile +++ b/build/ci/Makefile @@ -52,7 +52,7 @@ ifndef NODEPOOL $(eval NODEPOOL := $(shell date +"prel%y%m%d")) endif ifndef PEERD_IMAGE_TAG - $(eval PEERD_IMAGE_TAG := "dev") + $(eval PEERD_IMAGE_TAG := "stable") endif @echo "\033[92mRunning CI NODEPOOL: $(NODEPOOL)\033[0m" @( PEERD_IMAGE_TAG=$(PEERD_IMAGE_TAG) PEERD_CONFIGURE_MIRRORS=true $(SCRIPTS_DIR)/azure.sh nodepool up -y $(NODEPOOL) ) @@ -66,7 +66,7 @@ ifndef NODEPOOL $(eval NODEPOOL := $(shell date +"prels%y%m%d")) endif ifndef PEERD_IMAGE_TAG - $(eval PEERD_IMAGE_TAG := "dev") + $(eval PEERD_IMAGE_TAG := "stable") endif @echo "\033[92mRunning CI NODEPOOL: $(NODEPOOL)\033[0m" @( PEERD_IMAGE_TAG=$(PEERD_IMAGE_TAG) PEERD_CONFIGURE_MIRRORS=false $(SCRIPTS_DIR)/azure.sh nodepool up -y $(NODEPOOL) ) diff --git a/internal/handlers/root.go b/internal/handlers/root.go index e1308f1..543b74c 100644 --- a/internal/handlers/root.go +++ b/internal/handlers/root.go @@ -31,16 +31,7 @@ func Handler(ctx context.Context, r routing.Router, containerdStore containerd.S } engine := newEngine(ctx) - - engine.HEAD("/blobs/*url", fileHandler) - engine.GET("/blobs/*url", fileHandler) - - engine.HEAD("/v2", v2Handler) - engine.GET("/v2", v2Handler) - engine.HEAD("/v2/:repo/manifests/:reference", v2Handler) - engine.GET("/v2/:repo/manifests/:reference", v2Handler) - engine.HEAD("/v2/:repo/blobs/:digest", v2Handler) - engine.GET("/v2/:repo/blobs/:digest", v2Handler) + registerRoutes(engine, fileHandler, v2Handler) return engine, nil } @@ -85,6 +76,17 @@ func newEngine(ctx context.Context) *gin.Engine { return engine } +// registerRoutes registers the routes for the HTTP server. +func registerRoutes(engine *gin.Engine, f, v gin.HandlerFunc) { + engine.HEAD("/blobs/*url", f) + engine.GET("/blobs/*url", f) + + engine.HEAD("/v2", v) + engine.GET("/v2", v) + engine.HEAD("/v2/*ref", v) + engine.GET("/v2/*ref", v) +} + // fileHandler is a handler function for the /blob API // @Summary Get a blob by URL // @Param url path string true "The URL of the blob" diff --git a/internal/handlers/root_test.go b/internal/handlers/root_test.go new file mode 100644 index 0000000..3a4c8ea --- /dev/null +++ b/internal/handlers/root_test.go @@ -0,0 +1,78 @@ +package handlers + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/gin-gonic/gin" +) + +var simpleOKHandler = gin.HandlerFunc(func(c *gin.Context) { + c.Status(http.StatusOK) +}) + +func TestV2RoutesRegistrations(t *testing.T) { + recorder := httptest.NewRecorder() + mc, me := gin.CreateTestContext(recorder) + registerRoutes(me, nil, simpleOKHandler) + + tests := []struct { + name string + method string + path string + expectedStatus int + }{ + { + name: "root", + method: http.MethodGet, + path: "/v2", + expectedStatus: http.StatusOK, + }, + { + name: "root head", + method: http.MethodHead, + path: "/v2", + expectedStatus: http.StatusOK, + }, + { + name: "manifests", + method: http.MethodGet, + path: "/v2/azure-cli/manifests/latest?ns=registry.k8s.io", + expectedStatus: http.StatusOK, + }, + { + name: "manifests nested", + method: http.MethodGet, + path: "/v2/azure-cli/with/a/nested/component/manifests/latest?ns=registry.k8s.io", + expectedStatus: http.StatusOK, + }, + { + name: "blobs", + method: http.MethodGet, + path: "/v2/azure-cli/blobs/sha256:1234?ns=registry.k8s.io", + expectedStatus: http.StatusOK, + }, + { + name: "blobs nested", + method: http.MethodGet, + path: "/v2/azure-cli/with/a/nested/component/blobs/sha256:1234?ns=registry.k8s.io", + expectedStatus: http.StatusOK, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + req, err := http.NewRequest(tt.method, tt.path, nil) + if err != nil { + t.Fatal(err) + } + + me.ServeHTTP(mc.Writer, req) + + if recorder.Code != http.StatusOK { + t.Errorf("%s: expected status code %d, got %d", tt.name, http.StatusOK, recorder.Code) + } + }) + } +} diff --git a/internal/handlers/v2/handler.go b/internal/handlers/v2/handler.go index b10bd18..29dea07 100644 --- a/internal/handlers/v2/handler.go +++ b/internal/handlers/v2/handler.go @@ -27,7 +27,7 @@ var _ gin.HandlerFunc = (&V2Handler{}).Handle // Handle handles a request for a file. func (h *V2Handler) Handle(c *gin.Context) { - l := p2pcontext.Logger(c).With().Str("blob", c.GetString(p2pcontext.BlobUrlCtxKey)).Bool("p2p", p2pcontext.IsRequestFromAPeer(c)).Logger() + l := p2pcontext.Logger(c).With().Bool("p2p", p2pcontext.IsRequestFromAPeer(c)).Logger() l.Debug().Msg("v2 handler start") s := time.Now() defer func() {