Skip to content

Commit

Permalink
fix: address route registration issue
Browse files Browse the repository at this point in the history
  • Loading branch information
avtakkar committed Apr 9, 2024
1 parent 6c3e2e1 commit c561070
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 13 deletions.
4 changes: 2 additions & 2 deletions build/ci/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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) )
Expand All @@ -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) )
Expand Down
22 changes: 12 additions & 10 deletions internal/handlers/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check warning on line 34 in internal/handlers/root.go

View check run for this annotation

Codecov / codecov/patch

internal/handlers/root.go#L34

Added line #L34 was not covered by tests

return engine, nil
}
Expand Down Expand Up @@ -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"
Expand Down
78 changes: 78 additions & 0 deletions internal/handlers/root_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
2 changes: 1 addition & 1 deletion internal/handlers/v2/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Check warning on line 30 in internal/handlers/v2/handler.go

View check run for this annotation

Codecov / codecov/patch

internal/handlers/v2/handler.go#L30

Added line #L30 was not covered by tests
l.Debug().Msg("v2 handler start")
s := time.Now()
defer func() {
Expand Down

0 comments on commit c561070

Please sign in to comment.