Skip to content

Commit

Permalink
routing changed to use gin framework, ping test for mock OCI server.
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Valdron <mvaldron@redhat.com>
  • Loading branch information
michael-valdron committed Jul 29, 2022
1 parent e11d9fb commit 5fe00bf
Showing 1 changed file with 49 additions and 14 deletions.
63 changes: 49 additions & 14 deletions index/server/pkg/server/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,36 +63,51 @@ func validateMethod(handle http.HandlerFunc, allowedMethods ...string) http.Hand
})
}

func getManifest(w http.ResponseWriter, r *http.Request) {
bytes, err := json.Marshal(ocispec.Manifest{})
func servePing(c *gin.Context) {
data, err := json.Marshal(gin.H{
"message": "ok",
})
if err != nil {
log.Fatal(err)
}

w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", ocispec.MediaTypeImageManifest)
if _, err = w.Write(bytes); err != nil {
c.Data(http.StatusOK, "application/json", data)
}

func serveManifest(c *gin.Context) {
bytes, err := json.Marshal(ocispec.Manifest{})
if err != nil {
log.Fatal(err)
}

c.Data(http.StatusOK, ocispec.MediaTypeImageManifest, bytes)
}

func getBlob(w http.ResponseWriter, r *http.Request) {
func serveBlob(c *gin.Context) {
bytes, err := json.Marshal(ocispec.Descriptor{})
if err != nil {
log.Fatal(err)
}

w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", devfileMediaType)
if _, err = w.Write(bytes); err != nil {
log.Fatal(err)
}
c.Data(http.StatusOK, devfileMediaType, bytes)
}

func setupMockOCIServer() (func(), error) {
router := http.NewServeMux()
router.Handle("/v2/devfile-catalog/:name/manifests/:ref", validateMethod(getManifest, http.MethodGet, http.MethodHead))
router.Handle("/v2/devfile-catalog/:name/blob/:digest", validateMethod(getBlob, http.MethodGet, http.MethodHead))
gin.SetMode(gin.TestMode)

// Create router engine of mock OCI server
router := gin.Default()

// Testing Route for checking mock OCI server
router.GET("/v2/ping", servePing)

// Pull Routes
router.GET("/v2/devfile-catalog/:name/manifests/:ref", serveManifest)
router.GET("/v2/devfile-catalog/:name/blob/:digest", serveBlob)
router.HEAD("/v2/devfile-catalog/:name/manifests/:ref", serveManifest)
router.HEAD("/v2/devfile-catalog/:name/blob/:digest", serveBlob)

// Create mock OCI server using the router engine
testOCIServer := httptest.NewUnstartedServer(router)

l, err := net.Listen("tcp", ociServerIP)
Expand Down Expand Up @@ -134,6 +149,26 @@ func setupVars() {
}
}

func TestMockOCIServer(t *testing.T) {
closeServer, err := setupMockOCIServer()
if err != nil {
t.Errorf("Failed to setup mock OCI server: %v", err)
return
}
defer closeServer()
setupVars()

resp, err := http.Get(fmt.Sprintf("http://%s", filepath.Join(ociServerIP, "/v2/ping")))
if err != nil {
t.Errorf("Error in request: %v", err)
return
}

if !reflect.DeepEqual(resp.StatusCode, 200) {
t.Errorf("Did not get expected status code, Got: %v, Expected: %v", resp.StatusCode, 200)
}
}

func TestServeHealthCheck(t *testing.T) {
var got gin.H

Expand Down

0 comments on commit 5fe00bf

Please sign in to comment.