Switch to stretchr/testify / mockery for mocks
testify is used throughout the codebase; this switches mocks from gomock to testify with the help of mockery for code generation. Handlers and mocks in test/utils/oidc are moved to a new package: mockery operates package by package, and requires packages to build correctly; test/utils/oidc/testserver.go relies on the mocks and fails to build when they are removed. Moving the interface and mocks to a different package allows mockery to process that package without having to build testserver.go. Signed-off-by: Stephen Kitt <skitt@redhat.com>
This commit is contained in:
@@ -1,108 +0,0 @@
|
||||
/*
|
||||
Copyright The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
// Code generated by MockGen. DO NOT EDIT.
|
||||
// Source: handlers.go
|
||||
//
|
||||
// Generated by this command:
|
||||
//
|
||||
// mockgen -source=handlers.go -destination=handlers.mock.go -package=oidc TokenHandler JWKsHandler
|
||||
//
|
||||
|
||||
// Package oidc is a generated GoMock package.
|
||||
package oidc
|
||||
|
||||
import (
|
||||
reflect "reflect"
|
||||
|
||||
gomock "go.uber.org/mock/gomock"
|
||||
jose "gopkg.in/square/go-jose.v2"
|
||||
)
|
||||
|
||||
// MockTokenHandler is a mock of TokenHandler interface.
|
||||
type MockTokenHandler struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *MockTokenHandlerMockRecorder
|
||||
}
|
||||
|
||||
// MockTokenHandlerMockRecorder is the mock recorder for MockTokenHandler.
|
||||
type MockTokenHandlerMockRecorder struct {
|
||||
mock *MockTokenHandler
|
||||
}
|
||||
|
||||
// NewMockTokenHandler creates a new mock instance.
|
||||
func NewMockTokenHandler(ctrl *gomock.Controller) *MockTokenHandler {
|
||||
mock := &MockTokenHandler{ctrl: ctrl}
|
||||
mock.recorder = &MockTokenHandlerMockRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
// EXPECT returns an object that allows the caller to indicate expected use.
|
||||
func (m *MockTokenHandler) EXPECT() *MockTokenHandlerMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// Token mocks base method.
|
||||
func (m *MockTokenHandler) Token() (Token, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "Token")
|
||||
ret0, _ := ret[0].(Token)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// Token indicates an expected call of Token.
|
||||
func (mr *MockTokenHandlerMockRecorder) Token() *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Token", reflect.TypeOf((*MockTokenHandler)(nil).Token))
|
||||
}
|
||||
|
||||
// MockJWKsHandler is a mock of JWKsHandler interface.
|
||||
type MockJWKsHandler struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *MockJWKsHandlerMockRecorder
|
||||
}
|
||||
|
||||
// MockJWKsHandlerMockRecorder is the mock recorder for MockJWKsHandler.
|
||||
type MockJWKsHandlerMockRecorder struct {
|
||||
mock *MockJWKsHandler
|
||||
}
|
||||
|
||||
// NewMockJWKsHandler creates a new mock instance.
|
||||
func NewMockJWKsHandler(ctrl *gomock.Controller) *MockJWKsHandler {
|
||||
mock := &MockJWKsHandler{ctrl: ctrl}
|
||||
mock.recorder = &MockJWKsHandlerMockRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
// EXPECT returns an object that allows the caller to indicate expected use.
|
||||
func (m *MockJWKsHandler) EXPECT() *MockJWKsHandlerMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// KeySet mocks base method.
|
||||
func (m *MockJWKsHandler) KeySet() jose.JSONWebKeySet {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "KeySet")
|
||||
ret0, _ := ret[0].(jose.JSONWebKeySet)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// KeySet indicates an expected call of KeySet.
|
||||
func (mr *MockJWKsHandlerMockRecorder) KeySet() *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "KeySet", reflect.TypeOf((*MockJWKsHandler)(nil).KeySet))
|
||||
}
|
||||
13
test/utils/oidc/handlers/.mockery.yaml
Normal file
13
test/utils/oidc/handlers/.mockery.yaml
Normal file
@@ -0,0 +1,13 @@
|
||||
---
|
||||
dir: .
|
||||
filename: mock_{{.InterfaceName | snakecase}}.go
|
||||
boilerplate-file: ../../../../hack/boilerplate/boilerplate.generatego.txt
|
||||
inpackage: true
|
||||
with-expecter: true
|
||||
packages:
|
||||
k8s.io/kubernetes/test/utils/oidc/handlers:
|
||||
interfaces:
|
||||
JWKsHandler:
|
||||
config:
|
||||
filename: mock_jwks_handler.go
|
||||
TokenHandler:
|
||||
@@ -14,9 +14,9 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
//go:generate mockgen -source=handlers.go -destination=handlers.mock.go -package=oidc TokenHandler JWKsHandler
|
||||
//go:generate mockery
|
||||
|
||||
package oidc
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"gopkg.in/square/go-jose.v2"
|
||||
112
test/utils/oidc/handlers/mock_jwks_handler.go
Normal file
112
test/utils/oidc/handlers/mock_jwks_handler.go
Normal file
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
Copyright The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
/*
|
||||
Copyright The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
// Code generated by mockery v2.40.3. DO NOT EDIT.
|
||||
|
||||
package handlers
|
||||
|
||||
import (
|
||||
mock "github.com/stretchr/testify/mock"
|
||||
jose "gopkg.in/square/go-jose.v2"
|
||||
)
|
||||
|
||||
// MockJWKsHandler is an autogenerated mock type for the JWKsHandler type
|
||||
type MockJWKsHandler struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
type MockJWKsHandler_Expecter struct {
|
||||
mock *mock.Mock
|
||||
}
|
||||
|
||||
func (_m *MockJWKsHandler) EXPECT() *MockJWKsHandler_Expecter {
|
||||
return &MockJWKsHandler_Expecter{mock: &_m.Mock}
|
||||
}
|
||||
|
||||
// KeySet provides a mock function with given fields:
|
||||
func (_m *MockJWKsHandler) KeySet() jose.JSONWebKeySet {
|
||||
ret := _m.Called()
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for KeySet")
|
||||
}
|
||||
|
||||
var r0 jose.JSONWebKeySet
|
||||
if rf, ok := ret.Get(0).(func() jose.JSONWebKeySet); ok {
|
||||
r0 = rf()
|
||||
} else {
|
||||
r0 = ret.Get(0).(jose.JSONWebKeySet)
|
||||
}
|
||||
|
||||
return r0
|
||||
}
|
||||
|
||||
// MockJWKsHandler_KeySet_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'KeySet'
|
||||
type MockJWKsHandler_KeySet_Call struct {
|
||||
*mock.Call
|
||||
}
|
||||
|
||||
// KeySet is a helper method to define mock.On call
|
||||
func (_e *MockJWKsHandler_Expecter) KeySet() *MockJWKsHandler_KeySet_Call {
|
||||
return &MockJWKsHandler_KeySet_Call{Call: _e.mock.On("KeySet")}
|
||||
}
|
||||
|
||||
func (_c *MockJWKsHandler_KeySet_Call) Run(run func()) *MockJWKsHandler_KeySet_Call {
|
||||
_c.Call.Run(func(args mock.Arguments) {
|
||||
run()
|
||||
})
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockJWKsHandler_KeySet_Call) Return(_a0 jose.JSONWebKeySet) *MockJWKsHandler_KeySet_Call {
|
||||
_c.Call.Return(_a0)
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockJWKsHandler_KeySet_Call) RunAndReturn(run func() jose.JSONWebKeySet) *MockJWKsHandler_KeySet_Call {
|
||||
_c.Call.Return(run)
|
||||
return _c
|
||||
}
|
||||
|
||||
// NewMockJWKsHandler creates a new instance of MockJWKsHandler. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
|
||||
// The first argument is typically a *testing.T value.
|
||||
func NewMockJWKsHandler(t interface {
|
||||
mock.TestingT
|
||||
Cleanup(func())
|
||||
}) *MockJWKsHandler {
|
||||
mock := &MockJWKsHandler{}
|
||||
mock.Mock.Test(t)
|
||||
|
||||
t.Cleanup(func() { mock.AssertExpectations(t) })
|
||||
|
||||
return mock
|
||||
}
|
||||
119
test/utils/oidc/handlers/mock_token_handler.go
Normal file
119
test/utils/oidc/handlers/mock_token_handler.go
Normal file
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
Copyright The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
/*
|
||||
Copyright The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
// Code generated by mockery v2.40.3. DO NOT EDIT.
|
||||
|
||||
package handlers
|
||||
|
||||
import mock "github.com/stretchr/testify/mock"
|
||||
|
||||
// MockTokenHandler is an autogenerated mock type for the TokenHandler type
|
||||
type MockTokenHandler struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
type MockTokenHandler_Expecter struct {
|
||||
mock *mock.Mock
|
||||
}
|
||||
|
||||
func (_m *MockTokenHandler) EXPECT() *MockTokenHandler_Expecter {
|
||||
return &MockTokenHandler_Expecter{mock: &_m.Mock}
|
||||
}
|
||||
|
||||
// Token provides a mock function with given fields:
|
||||
func (_m *MockTokenHandler) Token() (Token, error) {
|
||||
ret := _m.Called()
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for Token")
|
||||
}
|
||||
|
||||
var r0 Token
|
||||
var r1 error
|
||||
if rf, ok := ret.Get(0).(func() (Token, error)); ok {
|
||||
return rf()
|
||||
}
|
||||
if rf, ok := ret.Get(0).(func() Token); ok {
|
||||
r0 = rf()
|
||||
} else {
|
||||
r0 = ret.Get(0).(Token)
|
||||
}
|
||||
|
||||
if rf, ok := ret.Get(1).(func() error); ok {
|
||||
r1 = rf()
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// MockTokenHandler_Token_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Token'
|
||||
type MockTokenHandler_Token_Call struct {
|
||||
*mock.Call
|
||||
}
|
||||
|
||||
// Token is a helper method to define mock.On call
|
||||
func (_e *MockTokenHandler_Expecter) Token() *MockTokenHandler_Token_Call {
|
||||
return &MockTokenHandler_Token_Call{Call: _e.mock.On("Token")}
|
||||
}
|
||||
|
||||
func (_c *MockTokenHandler_Token_Call) Run(run func()) *MockTokenHandler_Token_Call {
|
||||
_c.Call.Run(func(args mock.Arguments) {
|
||||
run()
|
||||
})
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockTokenHandler_Token_Call) Return(_a0 Token, _a1 error) *MockTokenHandler_Token_Call {
|
||||
_c.Call.Return(_a0, _a1)
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockTokenHandler_Token_Call) RunAndReturn(run func() (Token, error)) *MockTokenHandler_Token_Call {
|
||||
_c.Call.Return(run)
|
||||
return _c
|
||||
}
|
||||
|
||||
// NewMockTokenHandler creates a new instance of MockTokenHandler. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
|
||||
// The first argument is typically a *testing.T value.
|
||||
func NewMockTokenHandler(t interface {
|
||||
mock.TestingT
|
||||
Cleanup(func())
|
||||
}) *MockTokenHandler {
|
||||
mock := &MockTokenHandler{}
|
||||
mock.Mock.Test(t)
|
||||
|
||||
t.Cleanup(func() { mock.AssertExpectations(t) })
|
||||
|
||||
return mock
|
||||
}
|
||||
@@ -32,8 +32,8 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.uber.org/mock/gomock"
|
||||
"gopkg.in/square/go-jose.v2"
|
||||
"k8s.io/kubernetes/test/utils/oidc/handlers"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -50,17 +50,17 @@ var (
|
||||
|
||||
type TestServer struct {
|
||||
httpServer *httptest.Server
|
||||
tokenHandler *MockTokenHandler
|
||||
jwksHandler *MockJWKsHandler
|
||||
tokenHandler *handlers.MockTokenHandler
|
||||
jwksHandler *handlers.MockJWKsHandler
|
||||
}
|
||||
|
||||
// JwksHandler is getter of JSON Web Key Sets handler
|
||||
func (ts *TestServer) JwksHandler() *MockJWKsHandler {
|
||||
func (ts *TestServer) JwksHandler() *handlers.MockJWKsHandler {
|
||||
return ts.jwksHandler
|
||||
}
|
||||
|
||||
// TokenHandler is getter of JWT token handler
|
||||
func (ts *TestServer) TokenHandler() *MockTokenHandler {
|
||||
func (ts *TestServer) TokenHandler() *handlers.MockTokenHandler {
|
||||
return ts.tokenHandler
|
||||
}
|
||||
|
||||
@@ -98,17 +98,14 @@ func BuildAndRunTestServer(t *testing.T, caPath, caKeyPath, issuerOverride strin
|
||||
}
|
||||
httpServer.StartTLS()
|
||||
|
||||
mockCtrl := gomock.NewController(t)
|
||||
|
||||
t.Cleanup(func() {
|
||||
mockCtrl.Finish()
|
||||
httpServer.Close()
|
||||
})
|
||||
|
||||
oidcServer := &TestServer{
|
||||
httpServer: httpServer,
|
||||
tokenHandler: NewMockTokenHandler(mockCtrl),
|
||||
jwksHandler: NewMockJWKsHandler(mockCtrl),
|
||||
tokenHandler: handlers.NewMockTokenHandler(t),
|
||||
jwksHandler: handlers.NewMockJWKsHandler(t),
|
||||
}
|
||||
|
||||
issuer := httpServer.URL
|
||||
@@ -197,10 +194,10 @@ func TokenHandlerBehaviorReturningPredefinedJWT[K JosePrivateKey](
|
||||
t *testing.T,
|
||||
privateKey K,
|
||||
claims map[string]interface{}, accessToken, refreshToken string,
|
||||
) func() (Token, error) {
|
||||
) func() (handlers.Token, error) {
|
||||
t.Helper()
|
||||
|
||||
return func() (Token, error) {
|
||||
return func() (handlers.Token, error) {
|
||||
signer, err := jose.NewSigner(jose.SigningKey{Algorithm: GetSignatureAlgorithm(privateKey), Key: privateKey}, nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -212,7 +209,7 @@ func TokenHandlerBehaviorReturningPredefinedJWT[K JosePrivateKey](
|
||||
idToken, err := idTokenSignature.CompactSerialize()
|
||||
require.NoError(t, err)
|
||||
|
||||
return Token{
|
||||
return handlers.Token{
|
||||
IDToken: idToken,
|
||||
AccessToken: accessToken,
|
||||
RefreshToken: refreshToken,
|
||||
|
||||
Reference in New Issue
Block a user