From a6fc4f6d1c9dfeebe638650bbea7dfc3ab0a7606 Mon Sep 17 00:00:00 2001 From: Scott Leggett Date: Tue, 20 Feb 2024 15:22:26 +0800 Subject: [PATCH 1/2] chore: add lint target to Makefile --- Makefile | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 37854662..54a1e239 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # local development targets .PHONY: test -test: mod-tidy generate +test: mod-tidy generate lint go test -v ./... .PHONY: mod-tidy @@ -17,6 +17,10 @@ build: GOVERSION=$$(go version) \ goreleaser build --clean --debug --single-target --snapshot +.PHONY: lint +lint: + golangci-lint run --enable gocritic + .PHONY: fuzz fuzz: mod-tidy generate go test -fuzz='^Fuzz' -fuzztime=10s -v ./internal/server From 683d965cc42579c1dcbf3cb7fbe388e3c18e43a2 Mon Sep 17 00:00:00 2001 From: Scott Leggett Date: Tue, 20 Feb 2024 15:22:41 +0800 Subject: [PATCH 2/2] fix: avoid flaky test race Occasionally the mock matcher will complain about the fake childCtx, so ignore that since it is an implementation detail. --- internal/sshserver/helper_test.go | 10 ++++++-- internal/sshserver/sessionhandler_test.go | 29 ++++++++++------------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/internal/sshserver/helper_test.go b/internal/sshserver/helper_test.go index d92afc5c..17ee91fb 100644 --- a/internal/sshserver/helper_test.go +++ b/internal/sshserver/helper_test.go @@ -10,5 +10,11 @@ var ParseLogsArg = parseLogsArg // SessionHandler exposes the private sessionHandler for testing only. var SessionHandler = sessionHandler -// CtxKey exposes the private ctxKey for testing only. -type CtxKey = ctxKey +// Exposes the private ctxKey constants for testing only. +const ( + EnvironmentIDKey = environmentIDKey + EnvironmentNameKey = environmentNameKey + ProjectIDKey = projectIDKey + ProjectNameKey = projectNameKey + SSHFingerprint = sshFingerprint +) diff --git a/internal/sshserver/sessionhandler_test.go b/internal/sshserver/sessionhandler_test.go index 36cdbe61..940860fe 100644 --- a/internal/sshserver/sessionhandler_test.go +++ b/internal/sshserver/sessionhandler_test.go @@ -1,7 +1,6 @@ package sshserver_test import ( - "context" "log/slog" "os" "testing" @@ -67,11 +66,11 @@ func TestExec(t *testing.T) { tc.user, tc.deployment, ).Return(tc.deployment, nil) - sshContext.EXPECT().Value(sshserver.CtxKey(0)).Return(0) - sshContext.EXPECT().Value(sshserver.CtxKey(1)).Return("test") - sshContext.EXPECT().Value(sshserver.CtxKey(2)).Return(0) - sshContext.EXPECT().Value(sshserver.CtxKey(3)).Return("project") - sshContext.EXPECT().Value(sshserver.CtxKey(4)).Return("fingerprint") + sshContext.EXPECT().Value(sshserver.EnvironmentIDKey).Return(0) + sshContext.EXPECT().Value(sshserver.EnvironmentNameKey).Return("test") + sshContext.EXPECT().Value(sshserver.ProjectIDKey).Return(0) + sshContext.EXPECT().Value(sshserver.ProjectNameKey).Return("project") + sshContext.EXPECT().Value(sshserver.SSHFingerprint).Return("fingerprint") winch := make(<-chan ssh.Window) sshSession.EXPECT().Pty().Return(ssh.Pty{}, winch, tc.pty) sshSession.EXPECT().Stderr().Return(os.Stderr) @@ -142,20 +141,18 @@ func TestLogs(t *testing.T) { tc.user, tc.deployment, ).Return(tc.deployment, nil) - sshContext.EXPECT().Value(sshserver.CtxKey(0)).Return(0) - sshContext.EXPECT().Value(sshserver.CtxKey(1)).Return("test") - sshContext.EXPECT().Value(sshserver.CtxKey(2)).Return(0) - sshContext.EXPECT().Value(sshserver.CtxKey(3)).Return("project") - sshContext.EXPECT().Value(sshserver.CtxKey(4)).Return("fingerprint") + sshContext.EXPECT().Value(sshserver.EnvironmentIDKey).Return(0) + sshContext.EXPECT().Value(sshserver.EnvironmentNameKey).Return("test") + sshContext.EXPECT().Value(sshserver.ProjectIDKey).Return(0) + sshContext.EXPECT().Value(sshserver.ProjectNameKey).Return("project") + sshContext.EXPECT().Value(sshserver.SSHFingerprint).Return("fingerprint") - // this call is executed by context.WithCancel() - sshContext.EXPECT().Value(gomock.Any()).Return(nil).Times(4) + // called by context.WithCancel() + sshContext.EXPECT().Value(gomock.Any()).Return(nil).AnyTimes() sshContext.EXPECT().Done().Return(make(<-chan struct{})).AnyTimes() - childCtx, cancel := context.WithCancel(sshContext) - defer cancel() k8sService.EXPECT().Logs( - childCtx, + gomock.Any(), // private childCtx tc.user, tc.deployment, "",