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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
package kasapp
import (
"context"
"errors"
"net/http"
"strconv"
"testing"
"github.com/getsentry/sentry-go"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/api"
gapi "gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/gitlab/api"
"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/tool/cache"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/tool/errz"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/tool/testing/mock_cache"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/tool/testing/mock_gitlab"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/tool/testing/testhelpers"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
"go.uber.org/zap/zaptest"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
var (
_ modserver.RpcApi = (*serverRpcApi)(nil)
_ modserver.RpcApiFactory = (*serverRpcApiFactory)(nil).New
_ modserver.AgentRpcApi = (*serverAgentRpcApi)(nil)
_ modserver.AgentRpcApiFactory = (*serverAgentRpcApiFactory)(nil).New
)
func TestGetAgentInfo_Errors(t *testing.T) {
tests := []struct {
httpStatus int
code codes.Code
captureErr string
}{
{
httpStatus: http.StatusForbidden,
code: codes.PermissionDenied,
},
{
httpStatus: http.StatusUnauthorized,
code: codes.Unauthenticated,
},
{
httpStatus: http.StatusNotFound,
code: codes.NotFound,
},
{
httpStatus: http.StatusInternalServerError,
captureErr: "HTTP status code: 500 for path /api/v4/internal/kubernetes/agent_info",
code: codes.Unavailable,
},
{
httpStatus: http.StatusBadGateway,
captureErr: "HTTP status code: 502 for path /api/v4/internal/kubernetes/agent_info",
code: codes.Unavailable,
},
{
httpStatus: http.StatusServiceUnavailable,
captureErr: "HTTP status code: 503 for path /api/v4/internal/kubernetes/agent_info",
code: codes.Unavailable,
},
}
for _, tc := range tests {
t.Run(strconv.Itoa(tc.httpStatus), func(t *testing.T) {
ctx, log, hub, rpcApi, traceId := setupAgentRpcApi(t, tc.httpStatus)
if tc.captureErr != "" {
hub.EXPECT().
CaptureEvent(gomock.Any()).
Do(func(event *sentry.Event) {
assert.Equal(t, traceId.String(), event.Tags[modserver.SentryFieldTraceId])
assert.Empty(t, event.User.ID)
assert.Equal(t, sentry.LevelError, event.Level)
assert.Equal(t, "*gitlab.ClientError", event.Exception[0].Type)
assert.Equal(t, "AgentInfo(): "+tc.captureErr, event.Exception[0].Value)
})
}
info, err := rpcApi.AgentInfo(ctx, log)
assert.Equal(t, tc.code, status.Code(err))
assert.Nil(t, info)
})
}
}
func TestRpcHandleProcessingError_UserError(t *testing.T) {
_, log, _, rpcApi, _ := setupAgentRpcApi(t, http.StatusInternalServerError)
err := errz.NewUserError("boom")
rpcApi.HandleProcessingError(log, testhelpers.AgentId, "Bla", err)
}
func TestRpcHandleProcessingError_NonUserError_AgentId(t *testing.T) {
_, log, hub, rpcApi, traceId := setupAgentRpcApi(t, http.StatusInternalServerError)
err := errors.New("boom")
hub.EXPECT().
CaptureEvent(gomock.Any()).
Do(func(event *sentry.Event) {
assert.Equal(t, traceId.String(), event.Tags[modserver.SentryFieldTraceId])
assert.Equal(t, strconv.FormatInt(testhelpers.AgentId, 10), event.User.ID)
assert.Equal(t, sentry.LevelError, event.Level)
assert.Equal(t, "*errors.errorString", event.Exception[0].Type)
assert.Equal(t, "Bla: boom", event.Exception[0].Value)
})
rpcApi.HandleProcessingError(log, testhelpers.AgentId, "Bla", err)
}
func TestRpcHandleProcessingError_NonUserError_NoAgentId(t *testing.T) {
_, log, hub, rpcApi, traceId := setupAgentRpcApi(t, http.StatusInternalServerError)
err := errors.New("boom")
hub.EXPECT().
CaptureEvent(gomock.Any()).
Do(func(event *sentry.Event) {
assert.Equal(t, traceId.String(), event.Tags[modserver.SentryFieldTraceId])
assert.Empty(t, event.User.ID)
assert.Equal(t, sentry.LevelError, event.Level)
assert.Equal(t, "*errors.errorString", event.Exception[0].Type)
assert.Equal(t, "Bla: boom", event.Exception[0].Value)
})
rpcApi.HandleProcessingError(log, modshared.NoAgentId, "Bla", err)
}
func setupAgentRpcApi(t *testing.T, statusCode int) (context.Context, *zap.Logger, *MockSentryHub, *serverAgentRpcApi, trace.TraceID) {
log := zaptest.NewLogger(t)
ctrl := gomock.NewController(t)
hub := NewMockSentryHub(ctrl)
errCacher := mock_cache.NewMockErrCacher[api.AgentToken](ctrl)
ctx, traceId := testhelpers.CtxWithSpanContext(t)
gitLabClient := mock_gitlab.SetupClient(t, gapi.AgentInfoApiPath, func(w http.ResponseWriter, r *http.Request) {
testhelpers.AssertGetJsonRequestIsCorrect(t, r, traceId)
w.WriteHeader(statusCode)
})
sra := &serverRpcApi{
RpcApiStub: modshared.RpcApiStub{
Logger: log,
StreamCtx: ctx,
},
sentryHubRoot: sentry.NewHub(nil, sentry.NewScope()),
service: "svc",
method: "method",
}
sra.hub() // so that Once fires here and doesn't overwrite our mock later
sra.sentryHub = hub
rpcApi := &serverAgentRpcApi{
RpcApi: sra,
Token: testhelpers.AgentkToken,
GitLabClient: gitLabClient,
AgentInfoCache: cache.NewWithError[api.AgentToken, *api.AgentInfo](0, 0, errCacher,
trace.NewNoopTracerProvider().Tracer(kasTracerName),
func(err error) bool { return false }), // no cache!
}
return ctx, log, hub, rpcApi, traceId
}
|