Skip to content

Commit

Permalink
Another test.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit91 committed Jan 9, 2024
1 parent 59575aa commit 7a2a145
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cmd/metal-api/internal/service/size-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ func (r *sizeResource) webService() *restful.WebService {
Returns(http.StatusOK, "OK", []v1.SizeResponse{}).
DefaultReturns("Error", httperrors.HTTPErrorResponse{}))

ws.Route(ws.GET("/reservations").
ws.Route(ws.POST("/reservations").
To(r.listSizeReservations).
Operation("listSizeReservations").
Doc("get all size reservations").
Metadata(restfulspec.KeyOpenAPITags, tags).
Metadata(auditing.Exclude, true).
Writes([]v1.SizeReservationResponse{}).
Returns(http.StatusOK, "OK", []v1.SizeReservationResponse{}).
DefaultReturns("Error", httperrors.HTTPErrorResponse{}))
Expand Down
44 changes: 44 additions & 0 deletions cmd/metal-api/internal/service/size-service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http/httptest"
"testing"

"github.com/google/go-cmp/cmp"
mdmv1 "github.com/metal-stack/masterdata-api/api/v1"
mdmv1mock "github.com/metal-stack/masterdata-api/api/v1/mocks"
mdm "github.com/metal-stack/masterdata-api/pkg/client"
Expand Down Expand Up @@ -290,3 +291,46 @@ func TestUpdateSize(t *testing.T) {
require.Equal(t, minCores, result.SizeConstraints[0].Min)
require.Equal(t, maxCores, result.SizeConstraints[0].Max)
}

func TestListSizeReservations(t *testing.T) {
ds, mock := datastore.InitMockDB(t)
testdata.InitMockDBData(mock)
log := zaptest.NewLogger(t).Sugar()

psc := &mdmv1mock.ProjectServiceClient{}
psc.On("Find", testifymock.Anything, &mdmv1.ProjectFindRequest{}).Return(&mdmv1.ProjectListResponse{Projects: []*mdmv1.Project{
{Meta: &mdmv1.Meta{Id: "p1"}},
}}, nil)
mdc := mdm.NewMock(psc, &mdmv1mock.TenantServiceClient{})

sizeservice := NewSize(log, ds, mdc)
container := restful.NewContainer().Add(sizeservice)

req := httptest.NewRequest("POST", "/v1/size/reservations", nil)
req.Header.Add("Content-Type", "application/json")
container = injectAdmin(log, container, req)
w := httptest.NewRecorder()
container.ServeHTTP(w, req)

resp := w.Result()
defer resp.Body.Close()
require.Equal(t, http.StatusOK, resp.StatusCode, w.Body.String())
var result []*v1.SizeReservationResponse
err := json.NewDecoder(resp.Body).Decode(&result)
require.NoError(t, err)

want := []*v1.SizeReservationResponse{
{
SizeID: testdata.Sz1.ID,
PartitionID: "1",
ProjectID: "p1",
Reservations: 3,
UsedReservations: 1,
ProjectAllocations: 1,
},
}

if diff := cmp.Diff(result, want); diff != "" {
t.Errorf("diff (-want +got):\n%s", diff)
}
}

0 comments on commit 7a2a145

Please sign in to comment.