forked from devfile/registry-support
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Michael Valdron <mvaldron@redhat.com>
- Loading branch information
1 parent
d4aed3d
commit 044de92
Showing
2 changed files
with
100 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters