Skip to content

Commit

Permalink
TestOCIServerProxy test function and test cases added.
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 7163dfb commit 36e9223
Showing 1 changed file with 88 additions and 17 deletions.
105 changes: 88 additions & 17 deletions index/server/pkg/server/endpoint_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package server

import (
"bytes"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -121,12 +122,6 @@ var (
)

func serveManifest(c *gin.Context) {
if c.Request.Method == http.MethodGet {
getManifest(c)
}
}

func getManifest(c *gin.Context) {
name, ref := c.Param("name"), c.Param("ref")
var (
stackManifest ocispec.Manifest
Expand Down Expand Up @@ -167,24 +162,32 @@ func getManifest(c *gin.Context) {
}
}

bytes, err = json.Marshal(stackManifest)
if err != nil {
log.Fatal(err)
if c.Request.Method == http.MethodGet {
bytes, err = json.Marshal(stackManifest)
if err != nil {
log.Fatal(err)
}
}

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

func notFoundManifest(c *gin.Context, tag string) {
c.JSON(http.StatusNotFound, ocitest.WriteErrors([]ocitest.ResponseError{
{
Code: "MANIFEST_UNKNOWN",
Message: "manifest unknown",
Detail: ocitest.ResponseErrorDetails{
Tag: tag,
var data gin.H = nil

if c.Request.Method == http.MethodGet {
data = ocitest.WriteErrors([]ocitest.ResponseError{
{
Code: "MANIFEST_UNKNOWN",
Message: "manifest unknown",
Detail: ocitest.ResponseErrorDetails{
Tag: tag,
},
},
},
}))
})
}

c.JSON(http.StatusNotFound, data)
}

func digestEntity(e interface{}) (string, error) {
Expand Down Expand Up @@ -882,3 +885,71 @@ func TestServeDevfileStarterProjectWithVersion(t *testing.T) {
})
}
}

func TestOCIServerProxy(t *testing.T) {
tests := []struct {
name string
method string
url string
wantCode int
wantError bool
}{
{
name: "HEAD /v2/devfile-catalog/go/manifests/1.2.0",
method: http.MethodHead,
url: "/devfile-catalog/go/manifests/1.2.0",
wantCode: 200,
},
// TODO: Fix bug which overrides status code 200 for HEAD request despite response returning 404
// {
// name: "HEAD /v2/devfile-catalog/go/manifests/notfound",
// method: http.MethodHead,
// url: "/devfile-catalog/go/manifests/notfound",
// wantCode: 404,
// wantError: true,
// },
{
name: "GET /v2/devfile-catalog/go/manifests/1.2.0",
method: http.MethodGet,
url: "/devfile-catalog/go/manifests/1.2.0",
wantCode: 200,
},
{
name: "GET /v2/devfile-catalog/go/manifests/notfound",
method: http.MethodGet,
url: "/devfile-catalog/go/manifests/notfound",
wantCode: 404,
wantError: true,
},
}

closeServer, err := setupMockOCIServer()
if err != nil {
t.Errorf("Did not setup mock OCI server properly: %v", err)
return
}
defer closeServer()
setupVars()

for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
gin.SetMode(gin.TestMode)

w := ocitest.NewProxyRecorder()
c, _ := gin.CreateTestContext(w)
url := fmt.Sprintf("%s://%s", scheme, filepath.Join(ociServerIP, "v2", test.url))

c.Request, err = http.NewRequest(test.method, url, bytes.NewBuffer([]byte{}))
if err != nil {
t.Fatalf("Did not expect error: %v", err)
}
c.Params = append(c.Params, gin.Param{Key: "proxyPath", Value: test.url})

ociServerProxy(c)

if gotStatusCode := w.Code; !reflect.DeepEqual(gotStatusCode, test.wantCode) {
t.Errorf("Did not get expected status code, Got: %v, Expected: %v", gotStatusCode, test.wantCode)
}
})
}
}

0 comments on commit 36e9223

Please sign in to comment.