From 9b0e4745e77c5ad13b1cb3120b80a3782098fb5d Mon Sep 17 00:00:00 2001 From: Scott Leggett Date: Mon, 14 Oct 2024 14:13:32 +0800 Subject: [PATCH 1/2] chore: refactor mocks to remove internal/mock package This change simplifies mocks and coverage generation. --- .github/workflows/coverage.yaml | 4 +--- .gitignore | 1 - Makefile | 3 +-- .../{mock/permission.go => permission_mock_test.go} | 10 +++++----- internal/rbac/usercansshtoenvironment_test.go | 5 ++--- .../sessionhandler_mock_test.go} | 10 +++++----- internal/sshserver/sessionhandler_test.go | 13 ++++++------- .../{mock/ssh.go => sshserver/ssh_mock_test.go} | 8 ++++---- 8 files changed, 24 insertions(+), 30 deletions(-) rename internal/rbac/{mock/permission.go => permission_mock_test.go} (92%) rename internal/{mock/sshserver_sessionhandler.go => sshserver/sessionhandler_mock_test.go} (88%) rename internal/{mock/ssh.go => sshserver/ssh_mock_test.go} (97%) diff --git a/.github/workflows/coverage.yaml b/.github/workflows/coverage.yaml index 890d2b47..9459885b 100644 --- a/.github/workflows/coverage.yaml +++ b/.github/workflows/coverage.yaml @@ -16,9 +16,7 @@ jobs: go-version: stable - name: Calculate coverage run: | - go test -v -covermode=atomic -coverprofile=cover.out.raw -coverpkg=./... ./... - # remove generated code from coverage calculation - grep -Ev 'internal/mock|_enumer.go' cover.out.raw > cover.out + go test -v -covermode=atomic -coverprofile=cover.out -coverpkg=./... ./... - name: Generage coverage badge uses: vladopajic/go-test-coverage@c7fe52e0f48e0fbed8c1812824c5346218443c70 # v2.10.2 with: diff --git a/.gitignore b/.gitignore index 7e275b7b..6ee2014a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ /dist /cover.out -/cover.out.raw /sbom.spdx.json diff --git a/Makefile b/Makefile index 4655e5c2..aacc1ca7 100644 --- a/Makefile +++ b/Makefile @@ -27,6 +27,5 @@ fuzz: mod-tidy generate .PHONY: cover cover: mod-tidy generate - go test -v -covermode=atomic -coverprofile=cover.out.raw -coverpkg=./... ./... - grep -Ev 'internal/mock|_enumer.go' cover.out.raw > cover.out + go test -v -covermode=atomic -coverprofile=cover.out -coverpkg=./... ./... go tool cover -html=cover.out diff --git a/internal/rbac/mock/permission.go b/internal/rbac/permission_mock_test.go similarity index 92% rename from internal/rbac/mock/permission.go rename to internal/rbac/permission_mock_test.go index 4447dfe9..9e756800 100644 --- a/internal/rbac/mock/permission.go +++ b/internal/rbac/permission_mock_test.go @@ -1,13 +1,13 @@ // Code generated by MockGen. DO NOT EDIT. -// Source: ../permission.go +// Source: permission.go // // Generated by this command: // -// mockgen -source=../permission.go -package=mock -destination=permission.go -write_generate_directive +// mockgen -source=permission.go -package=rbac_test -destination=permission_mock_test.go -write_generate_directive // -// Package mock is a generated GoMock package. -package mock +// Package rbac_test is a generated GoMock package. +package rbac_test import ( context "context" @@ -18,7 +18,7 @@ import ( gomock "go.uber.org/mock/gomock" ) -//go:generate mockgen -source=../permission.go -package=mock -destination=permission.go -write_generate_directive +//go:generate mockgen -source=permission.go -package=rbac_test -destination=permission_mock_test.go -write_generate_directive // MockKeycloakService is a mock of KeycloakService interface. type MockKeycloakService struct { diff --git a/internal/rbac/usercansshtoenvironment_test.go b/internal/rbac/usercansshtoenvironment_test.go index d100ec6b..18d728ed 100644 --- a/internal/rbac/usercansshtoenvironment_test.go +++ b/internal/rbac/usercansshtoenvironment_test.go @@ -9,7 +9,6 @@ import ( "github.com/google/uuid" "github.com/uselagoon/ssh-portal/internal/lagoon" "github.com/uselagoon/ssh-portal/internal/rbac" - "github.com/uselagoon/ssh-portal/internal/rbac/mock" "go.uber.org/mock/gomock" ) @@ -336,12 +335,12 @@ func TestUserCanSSHDefaultRBAC(t *testing.T) { // set up mocks ctrl := gomock.NewController(tt) defer ctrl.Finish() - kcService := mock.NewMockKeycloakService(ctrl) + kcService := NewMockKeycloakService(ctrl) kcService.EXPECT(). UserRolesAndGroups(ctx, tc.userUUID). Return(tc.realmRoles, tc.userGroupPaths, nil). Times(2) - ldbService := mock.NewMockLagoonDBService(ctrl) + ldbService := NewMockLagoonDBService(ctrl) if !tc.realmRoleShortCircuit { kcService.EXPECT(). UserGroupIDRole(ctx, tc.userGroupPaths). diff --git a/internal/mock/sshserver_sessionhandler.go b/internal/sshserver/sessionhandler_mock_test.go similarity index 88% rename from internal/mock/sshserver_sessionhandler.go rename to internal/sshserver/sessionhandler_mock_test.go index e87a6b29..839f3f26 100644 --- a/internal/mock/sshserver_sessionhandler.go +++ b/internal/sshserver/sessionhandler_mock_test.go @@ -1,13 +1,13 @@ // Code generated by MockGen. DO NOT EDIT. -// Source: ../sshserver/sessionhandler.go +// Source: sessionhandler.go // // Generated by this command: // -// mockgen -source=../sshserver/sessionhandler.go -package=mock -destination=sshserver_sessionhandler.go -write_generate_directive +// mockgen -source=sessionhandler.go -package=sshserver_test -destination=sessionhandler_mock_test.go -write_generate_directive // -// Package mock is a generated GoMock package. -package mock +// Package sshserver_test is a generated GoMock package. +package sshserver_test import ( context "context" @@ -18,7 +18,7 @@ import ( gomock "go.uber.org/mock/gomock" ) -//go:generate mockgen -source=../sshserver/sessionhandler.go -package=mock -destination=sshserver_sessionhandler.go -write_generate_directive +//go:generate mockgen -source=sessionhandler.go -package=sshserver_test -destination=sessionhandler_mock_test.go -write_generate_directive // MockK8SAPIService is a mock of K8SAPIService interface. type MockK8SAPIService struct { diff --git a/internal/sshserver/sessionhandler_test.go b/internal/sshserver/sessionhandler_test.go index 940860fe..2185654e 100644 --- a/internal/sshserver/sessionhandler_test.go +++ b/internal/sshserver/sessionhandler_test.go @@ -6,7 +6,6 @@ import ( "testing" "github.com/gliderlabs/ssh" - "github.com/uselagoon/ssh-portal/internal/mock" "github.com/uselagoon/ssh-portal/internal/sshserver" "go.uber.org/mock/gomock" ) @@ -45,9 +44,9 @@ func TestExec(t *testing.T) { t.Run(name, func(tt *testing.T) { // set up mocks ctrl := gomock.NewController(tt) - k8sService := mock.NewMockK8SAPIService(ctrl) - sshSession := mock.NewMockSession(ctrl) - sshContext := mock.NewMockContext(ctrl) + k8sService := NewMockK8SAPIService(ctrl) + sshSession := NewMockSession(ctrl) + sshContext := NewMockContext(ctrl) // configure callback callback := sshserver.SessionHandler( log, @@ -120,9 +119,9 @@ func TestLogs(t *testing.T) { t.Run(name, func(tt *testing.T) { // set up mocks ctrl := gomock.NewController(tt) - k8sService := mock.NewMockK8SAPIService(ctrl) - sshSession := mock.NewMockSession(ctrl) - sshContext := mock.NewMockContext(ctrl) + k8sService := NewMockK8SAPIService(ctrl) + sshSession := NewMockSession(ctrl) + sshContext := NewMockContext(ctrl) // configure callback callback := sshserver.SessionHandler( log, diff --git a/internal/mock/ssh.go b/internal/sshserver/ssh_mock_test.go similarity index 97% rename from internal/mock/ssh.go rename to internal/sshserver/ssh_mock_test.go index 430cc30d..3f3a3b1d 100644 --- a/internal/mock/ssh.go +++ b/internal/sshserver/ssh_mock_test.go @@ -3,11 +3,11 @@ // // Generated by this command: // -// mockgen -package=mock -destination=ssh.go -write_generate_directive github.com/gliderlabs/ssh Session,Context +// mockgen -package=sshserver_test -destination=ssh_mock_test.go -write_generate_directive github.com/gliderlabs/ssh Session,Context // -// Package mock is a generated GoMock package. -package mock +// Package sshserver_test is a generated GoMock package. +package sshserver_test import ( io "io" @@ -19,7 +19,7 @@ import ( gomock "go.uber.org/mock/gomock" ) -//go:generate mockgen -package=mock -destination=ssh.go -write_generate_directive github.com/gliderlabs/ssh Session,Context +//go:generate mockgen -package=sshserver_test -destination=ssh_mock_test.go -write_generate_directive github.com/gliderlabs/ssh Session,Context // MockSession is a mock of Session interface. type MockSession struct { From 19ef890cd306a4c23480d43b47b4870924cda747 Mon Sep 17 00:00:00 2001 From: Scott Leggett Date: Mon, 14 Oct 2024 15:04:06 +0800 Subject: [PATCH 2/2] chore: avoid generating unnecessary enumer code --- internal/lagoon/userrole.go | 2 +- internal/lagoon/userrole_enumer.go | 33 +----------------------------- 2 files changed, 2 insertions(+), 33 deletions(-) diff --git a/internal/lagoon/userrole.go b/internal/lagoon/userrole.go index 6973aeaf..762209f8 100644 --- a/internal/lagoon/userrole.go +++ b/internal/lagoon/userrole.go @@ -1,6 +1,6 @@ package lagoon -//go:generate enumer -type=UserRole -sql -transform=lower +//go:generate enumer -type=UserRole -transform=lower // UserRole is an enum of valid User roles. type UserRole int diff --git a/internal/lagoon/userrole_enumer.go b/internal/lagoon/userrole_enumer.go index fcdf097d..4418f68f 100644 --- a/internal/lagoon/userrole_enumer.go +++ b/internal/lagoon/userrole_enumer.go @@ -1,9 +1,8 @@ -// Code generated by "enumer -type=UserRole -sql -transform=lower"; DO NOT EDIT. +// Code generated by "enumer -type=UserRole -transform=lower"; DO NOT EDIT. package lagoon import ( - "database/sql/driver" "fmt" "strings" ) @@ -93,33 +92,3 @@ func (i UserRole) IsAUserRole() bool { } return false } - -func (i UserRole) Value() (driver.Value, error) { - return i.String(), nil -} - -func (i *UserRole) Scan(value interface{}) error { - if value == nil { - return nil - } - - var str string - switch v := value.(type) { - case []byte: - str = string(v) - case string: - str = v - case fmt.Stringer: - str = v.String() - default: - return fmt.Errorf("invalid value of UserRole: %[1]T(%[1]v)", value) - } - - val, err := UserRoleString(str) - if err != nil { - return err - } - - *i = val - return nil -}