Skip to content

Commit

Permalink
mock OCI server package created.
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 d4aed3d commit 044de92
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 50 deletions.
90 changes: 90 additions & 0 deletions index/server/pkg/ocitest/ocitest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package ocitest

import (
"encoding/json"
"fmt"
"log"
"net"
"net/http"
"net/http/httptest"

"github.com/gin-gonic/gin"
)

type ResponseError struct {
Code string `json:"code"`
Message string `json:"message"`
Detail string `json:"detail"`
}

type MockOCIServer struct {
httpserver *httptest.Server
router *gin.Engine
ServeManifest func(c *gin.Context)
ServeBlob func(c *gin.Context)
}

func servePing(c *gin.Context) {
data, err := json.Marshal(gin.H{
"message": "ok",
})
if err != nil {
log.Fatal(err)
}

c.JSON(http.StatusOK, data)
}

func WriteErrors(errors []ResponseError) ([]byte, error) {
return json.Marshal(gin.H{
"errors": errors,
})
}

func NewMockOCIServer() *MockOCIServer {
gin.SetMode(gin.TestMode)

mockOCIServer := &MockOCIServer{
// Create router engine of mock OCI server
router: gin.Default(),
}

// Create mock OCI server using the router engine
mockOCIServer.httpserver = httptest.NewUnstartedServer(mockOCIServer.router)

return mockOCIServer
}

func (server *MockOCIServer) Start(listenAddr string) error {
// Testing Route for checking mock OCI server
server.router.GET("/v2/ping", servePing)

// Pull Routes
// Fetch manifest routes
if server.ServeManifest != nil {
server.router.GET("/v2/devfile-catalog/:name/manifests/:ref", server.ServeManifest)
server.router.HEAD("/v2/devfile-catalog/:name/manifests/:ref", server.ServeManifest)
}

// Fetch blob routes
if server.ServeBlob != nil {
server.router.GET("/v2/devfile-catalog/:name/blob/:digest", server.ServeBlob)
server.router.HEAD("/v2/devfile-catalog/:name/blob/:digest", server.ServeBlob)
}

l, err := net.Listen("tcp", listenAddr)
if err != nil {
return fmt.Errorf("unexpected error while creating listener: %v", err)
}

server.httpserver.Listener.Close()
server.httpserver.Listener = l

server.httpserver.Start()

return nil
}

func (server *MockOCIServer) Close() {
server.httpserver.Close()
}
60 changes: 10 additions & 50 deletions index/server/pkg/server/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"net/http/httptest"
"os"
Expand All @@ -14,6 +13,7 @@ import (
"strings"
"testing"

"github.com/devfile/registry-support/index/server/pkg/ocitest"
"github.com/gin-gonic/gin"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)
Expand All @@ -22,29 +22,6 @@ const (
ociServerIP = "127.0.0.1:5000"
)

type responseError struct {
code string `json:"code"`
message string `json:"message"`
detail string `json:"detail"`
}

func writeErrors(errors []responseError) ([]byte, error) {
return json.Marshal(gin.H{
"errors": errors,
})
}

func servePing(c *gin.Context) {
data, err := json.Marshal(gin.H{
"message": "ok",
})
if err != nil {
log.Fatal(err)
}

c.Data(http.StatusOK, "application/json", data)
}

func serveManifest(c *gin.Context) {
bytes, err := json.Marshal(ocispec.Manifest{})
if err != nil {
Expand All @@ -64,34 +41,17 @@ func serveBlob(c *gin.Context) {
}

func setupMockOCIServer() (func(), error) {
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)
mockOCIServer := ocitest.NewMockOCIServer()

// 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)
mockOCIServer.ServeManifest = serveManifest
mockOCIServer.ServeBlob = serveBlob

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

l, err := net.Listen("tcp", ociServerIP)
if err != nil {
return testOCIServer.Close, fmt.Errorf("Unexpected error while creating listener: %v", err)
if err := mockOCIServer.Start(ociServerIP); err != nil {
return nil, err
}

testOCIServer.Listener.Close()
testOCIServer.Listener = l

testOCIServer.Start()

return testOCIServer.Close, nil
return mockOCIServer.Close, nil
}

func setupVars() {
Expand Down Expand Up @@ -121,12 +81,12 @@ func setupVars() {
}

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

resp, err := http.Get(fmt.Sprintf("http://%s", filepath.Join(ociServerIP, "/v2/ping")))
Expand Down

0 comments on commit 044de92

Please sign in to comment.