Skip to content

Commit

Permalink
Merge pull request #398 from uselagoon/fix-mock-race
Browse files Browse the repository at this point in the history
fix mock race
  • Loading branch information
smlx authored Feb 20, 2024
2 parents 992ad4e + 683d965 commit a51ebb8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# local development targets

.PHONY: test
test: mod-tidy generate
test: mod-tidy generate lint
go test -v ./...

.PHONY: mod-tidy
Expand All @@ -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
Expand Down
10 changes: 8 additions & 2 deletions internal/sshserver/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
29 changes: 13 additions & 16 deletions internal/sshserver/sessionhandler_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package sshserver_test

import (
"context"
"log/slog"
"os"
"testing"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
"",
Expand Down

0 comments on commit a51ebb8

Please sign in to comment.