1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
package server
import (
"context"
"errors"
"testing"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/module/modserver"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/module/modshared"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/module/notifications/rpc"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/tool/testing/mock_modserver"
"go.uber.org/zap"
)
var (
_ modserver.Module = &module{}
_ modserver.Factory = &Factory{}
_ rpc.NotificationsServer = &server{}
)
func TestServer_GitPushEvent_SuccessfulPublish(t *testing.T) {
// GIVEN
// setup test fixtures
ctrl := gomock.NewController(t)
rpcApi := mock_modserver.NewMockRpcApi(ctrl)
ctx := modserver.InjectRpcApi(context.Background(), rpcApi)
var proj *modserver.Project
// setup server under test
s := newServer(func(ctx context.Context, e *modserver.Project) error {
proj = e
return nil
})
// WHEN
_, err := s.GitPushEvent(ctx, &rpc.GitPushEventRequest{
Project: &rpc.Project{Id: 42, FullPath: "foo/bar"},
})
// THEN
require.NoError(t, err)
assert.EqualValues(t, 42, proj.Id)
assert.EqualValues(t, "foo/bar", proj.FullPath)
}
func TestServer_GitPushEvent_FailedPublish(t *testing.T) {
// GIVEN
// setup test fixtures
ctrl := gomock.NewController(t)
rpcApi := mock_modserver.NewMockRpcApi(ctrl)
ctx := modserver.InjectRpcApi(context.Background(), rpcApi)
rpcApi.EXPECT().Log().Return(zap.NewNop())
givenErr := errors.New("some error")
rpcApi.EXPECT().
HandleProcessingError(gomock.Any(), modshared.NoAgentId, gomock.Any(), givenErr)
// setup server under test
s := newServer(func(ctx context.Context, e *modserver.Project) error {
return givenErr
})
// WHEN
_, err := s.GitPushEvent(ctx, &rpc.GitPushEventRequest{
Project: &rpc.Project{Id: 42, FullPath: "foo/bar"},
})
// THEN
assert.EqualError(t, err, "rpc error: code = Unavailable desc = Failed to publish received git push event: some error")
}
|