Skip to content

Commit

Permalink
Add delete tests
Browse files Browse the repository at this point in the history
  • Loading branch information
micafer committed Oct 15, 2024
1 parent a640afd commit 18ab2de
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 2 deletions.
10 changes: 8 additions & 2 deletions pkg/backends/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ var errFake = errors.New("fake error")

// FakeBackend fake struct to mock the beahaviour of the ServerlessBackend interface
type FakeBackend struct {
errors map[string][]error
errors map[string][]error
Service *types.Service // service to be returned by the ReadService function
}

// MakeFakeBackend returns the pointer of a new FakeBackend struct
Expand Down Expand Up @@ -81,7 +82,12 @@ func (f *FakeBackend) CreateService(service types.Service) error {

// ReadService returns a Service (fake)
func (f *FakeBackend) ReadService(name string) (*types.Service, error) {
return &types.Service{Token: "AbCdEf123456"}, f.returnError(getCurrentFuncName())
// default service returned by the function
service := &types.Service{Token: "AbCdEf123456"}
if f.Service != nil {
service = f.Service
}
return service, f.returnError(getCurrentFuncName())
}

// UpdateService updates an existent service (fake)
Expand Down
102 changes: 102 additions & 0 deletions pkg/handlers/delete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package handlers

import (
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/gin-gonic/gin"
"github.com/grycap/oscar/v3/pkg/backends"
"github.com/grycap/oscar/v3/pkg/types"
k8serr "k8s.io/apimachinery/pkg/api/errors"
)

func TestMMakeDeleteHandler(t *testing.T) {
back := backends.MakeFakeBackend()

server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, hreq *http.Request) {
if hreq.URL.Path != "/input" && hreq.URL.Path != "/output" && !strings.HasPrefix(hreq.URL.Path, "/minio/admin/v3/") {
t.Errorf("Unexpected path in request, got: %s", hreq.URL.Path)
}
}))

// and set the MinIO endpoint to the fake server
cfg := types.Config{
MinIOProvider: &types.MinIOProvider{
Endpoint: server.URL,
Region: "us-east-1",
AccessKey: "minioadmin",
SecretKey: "minioadmin",
Verify: false,
},
}

svc := &types.Service{
Token: "AbCdEf123456",
Input: []types.StorageIOConfig{
{Provider: "minio." + types.DefaultProvider, Path: "/input"},
},
Output: []types.StorageIOConfig{
{Provider: "minio." + types.DefaultProvider, Path: "/output"},
},
StorageProviders: &types.StorageProviders{
MinIO: map[string]*types.MinIOProvider{types.DefaultProvider: {
Region: "us-east-1",
Endpoint: server.URL,
AccessKey: "ak",
SecretKey: "sk"}},
}}
back.Service = svc

r := gin.Default()
r.DELETE("/system/services/:serviceName", MakeDeleteHandler(&cfg, back))

scenarios := []struct {
name string
returnError bool
errType string
}{
{"valid", false, ""},
{"Service Not Found test", true, "404"},
{"Internal Server Error test", true, "500"},
}

for _, s := range scenarios {
t.Run(s.name, func(t *testing.T) {
w := httptest.NewRecorder()

if s.returnError {
switch s.errType {
case "404":
back.AddError("DeleteService", k8serr.NewGone("Not Found"))
case "500":
err := errors.New("Not found")
back.AddError("DeleteService", k8serr.NewInternalError(err))
}
}
serviceName := "testName"
req, _ := http.NewRequest("DELETE", "/system/services/"+serviceName, nil)

r.ServeHTTP(w, req)

if s.returnError {
if s.errType == "404" && w.Code != http.StatusNotFound {
t.Errorf("expecting code %d, got %d", http.StatusNotFound, w.Code)
}

if s.errType == "500" && w.Code != http.StatusInternalServerError {
t.Errorf("expecting code %d, got %d", http.StatusInternalServerError, w.Code)
}
} else {
if w.Code != http.StatusNoContent {
t.Errorf("expecting code %d, got %d", http.StatusNoContent, w.Code)
}
}
})
}

// Close the fake MinIO server
defer server.Close()
}

0 comments on commit 18ab2de

Please sign in to comment.