Skip to content

Commit

Permalink
api/v1: update mock Publish in tests to pass rollup bool parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
joelrebel committed Jul 3, 2024
1 parent d5825ef commit e2c2151
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 19 deletions.
30 changes: 25 additions & 5 deletions pkg/api/v1/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/metal-toolbox/conditionorc/internal/fleetdb"
"github.com/metal-toolbox/conditionorc/internal/model"
"github.com/metal-toolbox/conditionorc/internal/server"

"github.com/metal-toolbox/conditionorc/internal/store"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/mock"
Expand Down Expand Up @@ -287,7 +288,13 @@ func TestConditionCreate(t *testing.T) {
tester.fleetDB.On("GetServer", mock.Anything, mock.Anything).Return(&model.Server{FacilityCode: "facility"}, nil).Once()

if tc.expectPublish {
tester.stream.On("Publish", mock.Anything, mock.Anything, mock.Anything).Return(nil).Once()
tester.stream.On(
"Publish",
mock.Anything,
mock.Anything,
mock.Anything,
false,
).Return(nil).Once()
}

got, err := tester.client.ServerConditionCreate(context.TODO(), serverID, rctypes.FirmwareInstall, tc.payload)
Expand Down Expand Up @@ -385,7 +392,13 @@ func TestFirmwareInstall(t *testing.T) {

tester.fleetDB.On("GetServer", mock.Anything, mock.Anything).Return(&model.Server{FacilityCode: "facility"}, nil).Times(1)
if tc.expectPublish {
tester.stream.On("Publish", mock.Anything, mock.Anything, mock.Anything).Return(nil).Times(1)
tester.stream.On(
"Publish",
mock.Anything,
mock.Anything,
mock.Anything,
false,
).Return(nil).Times(1)
}

got, err := tester.client.ServerFirmwareInstall(context.TODO(), tc.payload)
Expand Down Expand Up @@ -535,7 +548,13 @@ func TestServerEnroll(t *testing.T) {
}

if tc.expectPublish {
tester.stream.On("Publish", mock.Anything, mock.Anything, mock.Anything).Return(nil).Times(1)
tester.stream.On(
"Publish",
mock.Anything,
mock.Anything,
mock.Anything,
false,
).Return(nil).Times(1)
}

defer func() {
Expand Down Expand Up @@ -610,8 +629,9 @@ func TestServerEnrollEmptyUUID(t *testing.T) {
tester.stream.On("Publish",
mock.Anything,
mock.Anything,
mock.Anything).
Return(nil)
mock.Anything,
false,
).Return(nil)

got, err := tester.client.ServerEnroll(context.TODO(), "", v1types.ConditionCreate{Parameters: validParams.MustJSON()})
if err != nil {
Expand Down
50 changes: 36 additions & 14 deletions pkg/api/v1/routes/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,13 @@ func TestAddServer(t *testing.T) {
Once()
},
mockStream: func(r *eventsm.MockStream) {
r.On("Publish", mock.Anything, fmt.Sprintf("%s.servers.%s", mockFacilityCode, rctypes.Inventory), mock.Anything).
Return(nil).
Once()
r.On(
"Publish",
mock.Anything,
fmt.Sprintf("%s.servers.%s", mockFacilityCode, rctypes.Inventory),
mock.Anything,
false,
).Return(nil).Once()
},
request: func(t *testing.T) *http.Request {
payload, err := json.Marshal(&v1types.ConditionCreate{Parameters: validParams.MustJSON()})
Expand Down Expand Up @@ -289,9 +293,13 @@ func TestAddServer(t *testing.T) {
Once()
},
mockStream: func(r *eventsm.MockStream) {
r.On("Publish", mock.Anything, fmt.Sprintf("%s.servers.%s", mockFacilityCode, rctypes.Inventory), mock.Anything).
Return(nil).
Once()
r.On(
"Publish",
mock.Anything,
fmt.Sprintf("%s.servers.%s", mockFacilityCode, rctypes.Inventory),
mock.Anything,
false,
).Return(nil).Once()
},
request: func(t *testing.T) *http.Request {
payload, err := json.Marshal(&v1types.ConditionCreate{Parameters: validParams.MustJSON()})
Expand Down Expand Up @@ -606,7 +614,7 @@ func TestAddServerRollback(t *testing.T) {
}

if tc.mockStreamErr.calledTime > 0 {
stream.On("Publish", mock.Anything, mock.Anything, mock.Anything).
stream.On("Publish", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).
Return(tc.mockStreamErr.err).
Times(tc.mockStreamErr.calledTime)
}
Expand Down Expand Up @@ -754,9 +762,13 @@ func TestServerConditionCreate(t *testing.T) {
Once()
},
mockStream: func(r *eventsm.MockStream) {
r.On("Publish", mock.Anything, fmt.Sprintf("%s.servers.%s", facilityCode, rctypes.FirmwareInstall), mock.Anything).
Return(nil).
Once()
r.On(
"Publish",
mock.Anything,
fmt.Sprintf("%s.servers.%s", facilityCode, rctypes.FirmwareInstall),
mock.Anything,
false,
).Return(nil).Once()
},
request: func(t *testing.T) *http.Request {
payload, err := json.Marshal(&v1types.ConditionCreate{Parameters: []byte(`{"some param": "1"}`)})
Expand Down Expand Up @@ -803,9 +815,13 @@ func TestServerConditionCreate(t *testing.T) {
Once()
},
mockStream: func(r *eventsm.MockStream) {
r.On("Publish", mock.Anything, fmt.Sprintf("%s.servers.%s", facilityCode, rctypes.FirmwareInstall), mock.Anything).
Return(nil).
Once()
r.On(
"Publish",
mock.Anything,
fmt.Sprintf("%s.servers.%s", facilityCode, rctypes.FirmwareInstall),
mock.Anything,
false,
).Return(nil).Once()
},
request: func(t *testing.T) *http.Request {
fault := rctypes.Fault{Panic: true, DelayDuration: "10s", FailAt: "foobar"}
Expand Down Expand Up @@ -890,7 +906,13 @@ func TestServerConditionCreate(t *testing.T) {
Once()
},
mockStream: func(r *eventsm.MockStream) {
r.On("Publish", mock.Anything, fmt.Sprintf("%s.servers.%s", facilityCode, rctypes.FirmwareInstall), mock.Anything).
r.On(
"Publish",
mock.Anything,
fmt.Sprintf("%s.servers.%s", facilityCode, rctypes.FirmwareInstall),
mock.Anything,
false,
).
Return(errors.New("gremlins in the pipes")).
Once()
},
Expand Down

0 comments on commit e2c2151

Please sign in to comment.