Skip to content

Commit

Permalink
Refactor: use new function type instead of factory (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
int128 authored Jan 23, 2020
1 parent ae3156a commit 27020d0
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 125 deletions.
50 changes: 0 additions & 50 deletions pkg/adaptors/transport/mock_transport/mock_transport.go

This file was deleted.

13 changes: 3 additions & 10 deletions pkg/adaptors/transport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,13 @@ import (
)

var Set = wire.NewSet(
wire.Struct(new(Factory), "*"),
wire.Bind(new(FactoryInterface), new(*Factory)),
wire.Value(NewFunc(New)),
)

//go:generate mockgen -destination mock_transport/mock_transport.go github.com/int128/kauthproxy/pkg/adaptors/transport FactoryInterface

type FactoryInterface interface {
New(c *rest.Config) (http.RoundTripper, error)
}

type Factory struct{}
type NewFunc func(*rest.Config) (http.RoundTripper, error)

// New returns a RoundTripper with token support.
func (*Factory) New(c *rest.Config) (http.RoundTripper, error) {
func New(c *rest.Config) (http.RoundTripper, error) {
config := &transport.Config{
BearerToken: c.BearerToken,
BearerTokenFile: c.BearerTokenFile,
Expand Down
18 changes: 11 additions & 7 deletions pkg/di/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions pkg/usecases/authproxy/auth_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ var portForwarderConnectionLostError = xerrors.New("connection lost")

// AuthProxy provides a use-case of authentication proxy.
type AuthProxy struct {
ReverseProxy reverseproxy.Interface
PortForwarder portforwarder.Interface
ResolverFactory resolver.FactoryInterface
TransportFactory transport.FactoryInterface
Env env.Interface
Logger logger.Interface
ReverseProxy reverseproxy.Interface
PortForwarder portforwarder.Interface
ResolverFactory resolver.FactoryInterface
NewTransport transport.NewFunc
Env env.Interface
Logger logger.Interface
}

// Option represents an option of AuthProxy.
Expand Down Expand Up @@ -68,7 +68,7 @@ func (u *AuthProxy) Do(ctx context.Context, o Option) error {
if err != nil {
return xerrors.Errorf("could not allocate a local port: %w", err)
}
rpTransport, err := u.TransportFactory.New(o.Config)
rpTransport, err := u.NewTransport(o.Config)
if err != nil {
return xerrors.Errorf("could not create a transport for reverse proxy: %w", err)
}
Expand Down
102 changes: 51 additions & 51 deletions pkg/usecases/authproxy/auth_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/int128/kauthproxy/pkg/adaptors/resolver/mock_resolver"
"github.com/int128/kauthproxy/pkg/adaptors/reverseproxy"
"github.com/int128/kauthproxy/pkg/adaptors/reverseproxy/mock_reverseproxy"
"github.com/int128/kauthproxy/pkg/adaptors/transport/mock_transport"
"github.com/int128/kauthproxy/pkg/adaptors/transport"
"golang.org/x/xerrors"
v1 "k8s.io/api/core/v1"
v1meta "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -24,9 +24,19 @@ import (

var notNil = gomock.Not(gomock.Nil())

var restConfig rest.Config
var authProxyTransport http.Transport

func newTransport(t *testing.T) transport.NewFunc {
return func(got *rest.Config) (http.RoundTripper, error) {
if got != &restConfig {
t.Errorf("rest.Config mismatch, got %+v", got)
}
return &authProxyTransport, nil
}
}

func TestAuthProxy_Do(t *testing.T) {
var restConfig rest.Config
var authProxyTransport http.Transport
const containerPort = 18888
const transitPort = 28888
const podURL = "/api/v1/namespaces/kube-system/pods/kubernetes-dashboard-xxxxxxxx-xxxxxxxx"
Expand All @@ -38,19 +48,14 @@ func TestAuthProxy_Do(t *testing.T) {

t.Run("ToPod", func(t *testing.T) {
type mocks struct {
resolverFactory *mock_resolver.MockFactoryInterface
transportFactory *mock_transport.MockFactoryInterface
env *mock_env.MockInterface
resolverFactory *mock_resolver.MockFactoryInterface
env *mock_env.MockInterface
}
newMocks := func(ctrl *gomock.Controller) mocks {
m := mocks{
resolverFactory: mock_resolver.NewMockFactoryInterface(ctrl),
transportFactory: mock_transport.NewMockFactoryInterface(ctrl),
env: mock_env.NewMockInterface(ctrl),
resolverFactory: mock_resolver.NewMockFactoryInterface(ctrl),
env: mock_env.NewMockInterface(ctrl),
}
m.transportFactory.EXPECT().
New(&restConfig).
Return(&authProxyTransport, nil)
m.env.EXPECT().
AllocateLocalPort().
Return(transitPort, nil)
Expand Down Expand Up @@ -109,12 +114,12 @@ func TestAuthProxy_Do(t *testing.T) {
m.env.EXPECT().
OpenBrowser("http://localhost:8000")
u := &AuthProxy{
ReverseProxy: reverseProxy,
PortForwarder: portForwarder,
ResolverFactory: m.resolverFactory,
TransportFactory: m.transportFactory,
Env: m.env,
Logger: mock_logger.New(t),
ReverseProxy: reverseProxy,
PortForwarder: portForwarder,
ResolverFactory: m.resolverFactory,
NewTransport: newTransport(t),
Env: m.env,
Logger: mock_logger.New(t),
}
o := Option{
Config: &restConfig,
Expand Down Expand Up @@ -148,12 +153,12 @@ func TestAuthProxy_Do(t *testing.T) {
reverseProxy := mock_reverseproxy.NewMockInterface(ctrl)
m := newMocks(ctrl)
u := &AuthProxy{
ReverseProxy: reverseProxy,
PortForwarder: portForwarder,
ResolverFactory: m.resolverFactory,
TransportFactory: m.transportFactory,
Env: m.env,
Logger: mock_logger.New(t),
ReverseProxy: reverseProxy,
PortForwarder: portForwarder,
ResolverFactory: m.resolverFactory,
NewTransport: newTransport(t),
Env: m.env,
Logger: mock_logger.New(t),
}
o := Option{
Config: &restConfig,
Expand Down Expand Up @@ -201,12 +206,12 @@ func TestAuthProxy_Do(t *testing.T) {
})
m := newMocks(ctrl)
u := &AuthProxy{
ReverseProxy: reverseProxy,
PortForwarder: portForwarder,
ResolverFactory: m.resolverFactory,
TransportFactory: m.transportFactory,
Env: m.env,
Logger: mock_logger.New(t),
ReverseProxy: reverseProxy,
PortForwarder: portForwarder,
ResolverFactory: m.resolverFactory,
NewTransport: newTransport(t),
Env: m.env,
Logger: mock_logger.New(t),
}
o := Option{
Config: &restConfig,
Expand Down Expand Up @@ -275,12 +280,12 @@ func TestAuthProxy_Do(t *testing.T) {
m.env.EXPECT().
OpenBrowser("http://localhost:8000")
u := &AuthProxy{
ReverseProxy: reverseProxy,
PortForwarder: portForwarder,
ResolverFactory: m.resolverFactory,
TransportFactory: m.transportFactory,
Env: m.env,
Logger: mock_logger.New(t),
ReverseProxy: reverseProxy,
PortForwarder: portForwarder,
ResolverFactory: m.resolverFactory,
NewTransport: newTransport(t),
Env: m.env,
Logger: mock_logger.New(t),
}
o := Option{
Config: &restConfig,
Expand All @@ -297,19 +302,14 @@ func TestAuthProxy_Do(t *testing.T) {

t.Run("ToService", func(t *testing.T) {
type mocks struct {
resolverFactory *mock_resolver.MockFactoryInterface
transportFactory *mock_transport.MockFactoryInterface
env *mock_env.MockInterface
resolverFactory *mock_resolver.MockFactoryInterface
env *mock_env.MockInterface
}
newMocks := func(ctrl *gomock.Controller) mocks {
m := mocks{
resolverFactory: mock_resolver.NewMockFactoryInterface(ctrl),
transportFactory: mock_transport.NewMockFactoryInterface(ctrl),
env: mock_env.NewMockInterface(ctrl),
resolverFactory: mock_resolver.NewMockFactoryInterface(ctrl),
env: mock_env.NewMockInterface(ctrl),
}
m.transportFactory.EXPECT().
New(&restConfig).
Return(&authProxyTransport, nil)
m.env.EXPECT().
AllocateLocalPort().
Return(transitPort, nil)
Expand Down Expand Up @@ -367,12 +367,12 @@ func TestAuthProxy_Do(t *testing.T) {
m.env.EXPECT().
OpenBrowser("http://localhost:8000")
u := &AuthProxy{
ReverseProxy: reverseProxy,
PortForwarder: portForwarder,
ResolverFactory: m.resolverFactory,
TransportFactory: m.transportFactory,
Env: m.env,
Logger: mock_logger.New(t),
ReverseProxy: reverseProxy,
PortForwarder: portForwarder,
ResolverFactory: m.resolverFactory,
NewTransport: newTransport(t),
Env: m.env,
Logger: mock_logger.New(t),
}
o := Option{
Config: &restConfig,
Expand Down

0 comments on commit 27020d0

Please sign in to comment.