diff options
author | David Kim <dkim@gitlab.com> | 2020-04-03 12:46:40 +1030 |
---|---|---|
committer | David Kim <dkim@gitlab.com> | 2020-04-03 12:46:40 +1030 |
commit | 1af1245ec4a9fc330a7d4979a55a5fee1aca82cb (patch) | |
tree | 4c7d0ebe013c516f197bf117b4bcce8f53915eef | |
parent | 3f3a405c181840e2de13ffb22f7a50fd78054dc0 (diff) | |
download | gitlab-shell-1af1245ec4a9fc330a7d4979a55a5fee1aca82cb.tar.gz |
Add test for command logging
-rw-r--r-- | internal/command/receivepack/gitalycall.go | 13 | ||||
-rw-r--r-- | internal/command/receivepack/gitalycall_test.go | 5 | ||||
-rw-r--r-- | internal/command/shared/commandlogger/commandlogger.go | 20 | ||||
-rw-r--r-- | internal/command/uploadarchive/gitalycall.go | 12 | ||||
-rw-r--r-- | internal/command/uploadarchive/gitalycall_test.go | 5 | ||||
-rw-r--r-- | internal/command/uploadpack/gitalycall.go | 13 | ||||
-rw-r--r-- | internal/command/uploadpack/gitalycall_test.go | 5 |
7 files changed, 35 insertions, 38 deletions
diff --git a/internal/command/receivepack/gitalycall.go b/internal/command/receivepack/gitalycall.go index 8a9b4ba..9de3b8e 100644 --- a/internal/command/receivepack/gitalycall.go +++ b/internal/command/receivepack/gitalycall.go @@ -6,11 +6,11 @@ import ( "google.golang.org/grpc" - log "github.com/sirupsen/logrus" "gitlab.com/gitlab-org/gitaly/client" pb "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "gitlab.com/gitlab-org/gitlab-shell/internal/command/commandargs" "gitlab.com/gitlab-org/gitlab-shell/internal/command/shared/accessverifier" + "gitlab.com/gitlab-org/gitlab-shell/internal/command/shared/commandlogger" "gitlab.com/gitlab-org/gitlab-shell/internal/handler" ) @@ -32,16 +32,7 @@ func (c *Command) performGitalyCall(response *accessverifier.Response) error { GitConfigOptions: response.GitConfigOptions, } - fields := log.Fields{ - "command": "git-receive-pack", - "glProjectPath": request.Repository.GlProjectPath, - "glRepository": request.Repository.GlRepository, - "userId": response.UserId, - "userName": response.Username, - "gitProtocol": request.GitProtocol, - } - - log.WithFields(fields).Info("executing git command") + commandlogger.Log("git-receive-pack", request.Repository, response, "") return gc.RunGitalyCommand(func(ctx context.Context, conn *grpc.ClientConn) (int32, error) { ctx, cancel := context.WithCancel(ctx) diff --git a/internal/command/receivepack/gitalycall_test.go b/internal/command/receivepack/gitalycall_test.go index a20e858..ece9f1e 100644 --- a/internal/command/receivepack/gitalycall_test.go +++ b/internal/command/receivepack/gitalycall_test.go @@ -2,8 +2,8 @@ package receivepack import ( "bytes" - "testing" "strings" + "testing" "github.com/sirupsen/logrus" @@ -13,8 +13,8 @@ import ( "gitlab.com/gitlab-org/gitlab-shell/internal/command/readwriter" "gitlab.com/gitlab-org/gitlab-shell/internal/config" "gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/testserver" - "gitlab.com/gitlab-org/gitlab-shell/internal/testhelper/requesthandlers" "gitlab.com/gitlab-org/gitlab-shell/internal/testhelper" + "gitlab.com/gitlab-org/gitlab-shell/internal/testhelper/requesthandlers" ) func TestReceivePack(t *testing.T) { @@ -45,4 +45,5 @@ func TestReceivePack(t *testing.T) { require.Equal(t, "ReceivePack: "+userId+" "+repo, output.String()) require.Equal(t, logrus.InfoLevel, hook.LastEntry().Level) require.True(t, strings.Contains(hook.LastEntry().Message, "executing git command")) + require.True(t, strings.Contains(hook.LastEntry().Message, "command=git-receive-pack")) } diff --git a/internal/command/shared/commandlogger/commandlogger.go b/internal/command/shared/commandlogger/commandlogger.go new file mode 100644 index 0000000..017eb11 --- /dev/null +++ b/internal/command/shared/commandlogger/commandlogger.go @@ -0,0 +1,20 @@ +package commandlogger + +import ( + log "github.com/sirupsen/logrus" + pb "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" + "gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/accessverifier" +) + +func Log(command string, repository *pb.Repository, response *accessverifier.Response, protocol string) { + fields := log.Fields{ + "command": command, + "glProjectPath": repository.GlProjectPath, + "glRepository": repository.GlRepository, + "userId": response.UserId, + "userName": response.Username, + "gitProtocol": protocol, + } + + log.WithFields(fields).Info("executing git command") +} diff --git a/internal/command/uploadarchive/gitalycall.go b/internal/command/uploadarchive/gitalycall.go index e07e6d1..edc3bb1 100644 --- a/internal/command/uploadarchive/gitalycall.go +++ b/internal/command/uploadarchive/gitalycall.go @@ -5,10 +5,10 @@ import ( "google.golang.org/grpc" - log "github.com/sirupsen/logrus" "gitlab.com/gitlab-org/gitaly/client" pb "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "gitlab.com/gitlab-org/gitlab-shell/internal/command/commandargs" + "gitlab.com/gitlab-org/gitlab-shell/internal/command/shared/commandlogger" "gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/accessverifier" "gitlab.com/gitlab-org/gitlab-shell/internal/handler" ) @@ -24,15 +24,7 @@ func (c *Command) performGitalyCall(response *accessverifier.Response) error { request := &pb.SSHUploadArchiveRequest{Repository: &response.Gitaly.Repo} - fields := log.Fields{ - "command": "git-upload-archive", - "glProjectPath": request.Repository.GlProjectPath, - "glRepository": request.Repository.GlRepository, - "userId": response.UserId, - "userName": response.Username, - } - - log.WithFields(fields).Info("executing git command") + commandlogger.Log("git-upload-archive", request.Repository, response, "") return gc.RunGitalyCommand(func(ctx context.Context, conn *grpc.ClientConn) (int32, error) { ctx, cancel := context.WithCancel(ctx) diff --git a/internal/command/uploadarchive/gitalycall_test.go b/internal/command/uploadarchive/gitalycall_test.go index 15122c7..6e67571 100644 --- a/internal/command/uploadarchive/gitalycall_test.go +++ b/internal/command/uploadarchive/gitalycall_test.go @@ -2,8 +2,8 @@ package uploadarchive import ( "bytes" - "testing" "strings" + "testing" "github.com/sirupsen/logrus" @@ -13,8 +13,8 @@ import ( "gitlab.com/gitlab-org/gitlab-shell/internal/command/readwriter" "gitlab.com/gitlab-org/gitlab-shell/internal/config" "gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/testserver" - "gitlab.com/gitlab-org/gitlab-shell/internal/testhelper/requesthandlers" "gitlab.com/gitlab-org/gitlab-shell/internal/testhelper" + "gitlab.com/gitlab-org/gitlab-shell/internal/testhelper/requesthandlers" ) func TestUploadPack(t *testing.T) { @@ -45,4 +45,5 @@ func TestUploadPack(t *testing.T) { require.Equal(t, "UploadArchive: "+repo, output.String()) require.Equal(t, logrus.InfoLevel, hook.LastEntry().Level) require.True(t, strings.Contains(hook.LastEntry().Message, "executing git command")) + require.True(t, strings.Contains(hook.LastEntry().Message, "command=git-upload-archive")) } diff --git a/internal/command/uploadpack/gitalycall.go b/internal/command/uploadpack/gitalycall.go index a9a05e4..8ab163d 100644 --- a/internal/command/uploadpack/gitalycall.go +++ b/internal/command/uploadpack/gitalycall.go @@ -6,10 +6,10 @@ import ( "google.golang.org/grpc" - log "github.com/sirupsen/logrus" "gitlab.com/gitlab-org/gitaly/client" pb "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "gitlab.com/gitlab-org/gitlab-shell/internal/command/commandargs" + "gitlab.com/gitlab-org/gitlab-shell/internal/command/shared/commandlogger" "gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/accessverifier" "gitlab.com/gitlab-org/gitlab-shell/internal/handler" ) @@ -29,16 +29,7 @@ func (c *Command) performGitalyCall(response *accessverifier.Response) error { GitConfigOptions: response.GitConfigOptions, } - fields := log.Fields{ - "command": "git-upload-pack", - "glProjectPath": request.Repository.GlProjectPath, - "glRepository": request.Repository.GlRepository, - "userId": response.UserId, - "userName": response.Username, - "gitProtocol": request.GitProtocol, - } - - log.WithFields(fields).Info("executing git command") + commandlogger.Log("git-upload-pack", request.Repository, response, request.GitProtocol) return gc.RunGitalyCommand(func(ctx context.Context, conn *grpc.ClientConn) (int32, error) { ctx, cancel := context.WithCancel(ctx) diff --git a/internal/command/uploadpack/gitalycall_test.go b/internal/command/uploadpack/gitalycall_test.go index 86dfbf1..e5fe8b2 100644 --- a/internal/command/uploadpack/gitalycall_test.go +++ b/internal/command/uploadpack/gitalycall_test.go @@ -2,8 +2,8 @@ package uploadpack import ( "bytes" - "testing" "strings" + "testing" "github.com/sirupsen/logrus" @@ -14,8 +14,8 @@ import ( "gitlab.com/gitlab-org/gitlab-shell/internal/command/readwriter" "gitlab.com/gitlab-org/gitlab-shell/internal/config" "gitlab.com/gitlab-org/gitlab-shell/internal/gitlabnet/testserver" - "gitlab.com/gitlab-org/gitlab-shell/internal/testhelper/requesthandlers" "gitlab.com/gitlab-org/gitlab-shell/internal/testhelper" + "gitlab.com/gitlab-org/gitlab-shell/internal/testhelper/requesthandlers" ) func TestUploadPack(t *testing.T) { @@ -46,6 +46,7 @@ func TestUploadPack(t *testing.T) { require.Equal(t, "UploadPack: "+repo, output.String()) require.Equal(t, logrus.InfoLevel, hook.LastEntry().Level) require.True(t, strings.Contains(hook.LastEntry().Message, "executing git command")) + require.True(t, strings.Contains(hook.LastEntry().Message, "command=git-upload-pack")) for k, v := range map[string]string{ "gitaly-feature-cache_invalidator": "true", |