Skip to content

Commit

Permalink
use FakeClient as backend for RESTClient based unit tests (#469)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexey Kazakov <[email protected]>
Co-authored-by: Francisc Munteanu <[email protected]>
  • Loading branch information
3 people authored Oct 1, 2024
1 parent 503d232 commit 6d3e720
Show file tree
Hide file tree
Showing 22 changed files with 573 additions and 1,536 deletions.
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func main() {
}
}

app, err := server.NewInClusterApplication(cl)
app, err := server.NewInClusterApplication(cl, configuration.Namespace())
if err != nil {
panic(err.Error())
}
Expand Down
12 changes: 10 additions & 2 deletions pkg/application/service/factory/service_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ func (s *ServiceFactory) WithVerificationServiceOption(opt verificationservice.V
s.verificationServiceOptions = append(s.verificationServiceOptions, opt)
}

func (s *ServiceFactory) WithSignupService(signupService service.SignupService) {
s.signupServiceFunc = func(_ ...signupservice.SignupServiceOption) service.SignupService {
return signupService
}
}

// Option an option to configure the Service Factory
type Option func(f *ServiceFactory)

Expand Down Expand Up @@ -115,8 +121,10 @@ func NewServiceFactory(options ...Option) *ServiceFactory {
return verificationservice.NewVerificationService(f.getContext(), f.verificationServiceOptions...)
}

f.signupServiceFunc = func(_ ...signupservice.SignupServiceOption) service.SignupService {
return signupservice.NewSignupService(f.getContext(), f.signupServiceOptions...)
if f.signupServiceFunc == nil {
f.signupServiceFunc = func(_ ...signupservice.SignupServiceOption) service.SignupService {
return signupservice.NewSignupService(f.getContext(), f.signupServiceOptions...)
}
}

return f
Expand Down
181 changes: 75 additions & 106 deletions pkg/controller/signup_test.go

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions pkg/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -928,8 +928,7 @@ func (s *TestProxySuite) newMemberClusterServiceWithMembers(serverURL string) ap
fakeClient := commontest.NewFakeClient(s.T(), route)
return service.NewMemberClusterService(
fake.MemberClusterServiceContext{
CrtClient: s,
Svcs: s.Application,
Svcs: s.Application,
},
func(si *service.ServiceImpl) {
si.GetMembersFunc = func(_ ...commoncluster.Condition) []*commoncluster.CachedToolchainCluster {
Expand Down
15 changes: 5 additions & 10 deletions pkg/proxy/service/cluster_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ func (s *TestClusterServiceSuite) TestGetClusterAccess() {

svc := service.NewMemberClusterService(
fake.MemberClusterServiceContext{
CrtClient: s,
Svcs: s.Application,
Svcs: s.Application,
},
)

Expand Down Expand Up @@ -196,8 +195,7 @@ func (s *TestClusterServiceSuite) TestGetClusterAccess() {
s.Run("no member clusters", func() {
svc := service.NewMemberClusterService(
fake.MemberClusterServiceContext{
CrtClient: s,
Svcs: s.Application,
Svcs: s.Application,
},
func(si *service.ServiceImpl) {
si.GetMembersFunc = func(_ ...commoncluster.Condition) []*commoncluster.CachedToolchainCluster {
Expand Down Expand Up @@ -225,8 +223,7 @@ func (s *TestClusterServiceSuite) TestGetClusterAccess() {
s.Run("no member cluster with the given URL", func() {
svc := service.NewMemberClusterService(
fake.MemberClusterServiceContext{
CrtClient: s,
Svcs: s.Application,
Svcs: s.Application,
},
func(si *service.ServiceImpl) {
si.GetMembersFunc = func(_ ...commoncluster.Condition) []*commoncluster.CachedToolchainCluster {
Expand Down Expand Up @@ -287,8 +284,7 @@ func (s *TestClusterServiceSuite) TestGetClusterAccess() {

svc := service.NewMemberClusterService(
fake.MemberClusterServiceContext{
CrtClient: s,
Svcs: s.Application,
Svcs: s.Application,
},
func(si *service.ServiceImpl) {
si.GetMembersFunc = func(_ ...commoncluster.Condition) []*commoncluster.CachedToolchainCluster {
Expand Down Expand Up @@ -455,8 +451,7 @@ func (s *TestClusterServiceSuite) TestGetClusterAccess() {
s.Run("get workspace by name", func() {
svc := service.NewMemberClusterService(
fake.MemberClusterServiceContext{
CrtClient: s,
Svcs: s.Application,
Svcs: s.Application,
},
func(si *service.ServiceImpl) {
si.GetMembersFunc = func(_ ...commoncluster.Condition) []*commoncluster.CachedToolchainCluster {
Expand Down
9 changes: 4 additions & 5 deletions pkg/server/in_cluster_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/codeready-toolchain/registration-service/pkg/application"
"github.com/codeready-toolchain/registration-service/pkg/application/service"
"github.com/codeready-toolchain/registration-service/pkg/application/service/factory"
"github.com/codeready-toolchain/registration-service/pkg/configuration"
"github.com/codeready-toolchain/registration-service/pkg/kubeclient"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand All @@ -13,17 +12,17 @@ import (
// application type is intended to run inside a Kubernetes cluster, where it makes use of the rest.InClusterConfig()
// function to determine which Kubernetes configuration to use to create the REST client that interacts with the
// Kubernetes service endpoints.
func NewInClusterApplication(client client.Client) (application.Application, error) {
kubeClient, err := kubeclient.NewCRTRESTClient(client, configuration.Namespace())
func NewInClusterApplication(client client.Client, namespace string, options ...factory.Option) (application.Application, error) {
kubeClient, err := kubeclient.NewCRTRESTClient(client, namespace)
if err != nil {
return nil, err
}

return &InClusterApplication{
serviceFactory: factory.NewServiceFactory(
serviceFactory: factory.NewServiceFactory(append(options,
factory.WithServiceContextOptions(factory.CRTClientOption(kubeClient),
factory.InformerOption(client),
)),
))...),
}, nil
}

Expand Down
24 changes: 12 additions & 12 deletions pkg/signup/service/resource_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,26 @@ type ResourceProvider interface {
ListSpaceBindings(reqs ...labels.Requirement) ([]toolchainv1alpha1.SpaceBinding, error)
}

type crtClientProvider struct {
cl kubeclient.CRTClient
type CrtClientProvider struct {
Cl kubeclient.CRTClient
}

func (p crtClientProvider) GetMasterUserRecord(name string) (*toolchainv1alpha1.MasterUserRecord, error) {
return p.cl.V1Alpha1().MasterUserRecords().Get(name)
func (p CrtClientProvider) GetMasterUserRecord(name string) (*toolchainv1alpha1.MasterUserRecord, error) {
return p.Cl.V1Alpha1().MasterUserRecords().Get(name)
}

func (p crtClientProvider) GetToolchainStatus() (*toolchainv1alpha1.ToolchainStatus, error) {
return p.cl.V1Alpha1().ToolchainStatuses().Get()
func (p CrtClientProvider) GetToolchainStatus() (*toolchainv1alpha1.ToolchainStatus, error) {
return p.Cl.V1Alpha1().ToolchainStatuses().Get()
}

func (p crtClientProvider) GetUserSignup(name string) (*toolchainv1alpha1.UserSignup, error) {
return p.cl.V1Alpha1().UserSignups().Get(name)
func (p CrtClientProvider) GetUserSignup(name string) (*toolchainv1alpha1.UserSignup, error) {
return p.Cl.V1Alpha1().UserSignups().Get(name)
}

func (p crtClientProvider) GetSpace(name string) (*toolchainv1alpha1.Space, error) {
return p.cl.V1Alpha1().Spaces().Get(name)
func (p CrtClientProvider) GetSpace(name string) (*toolchainv1alpha1.Space, error) {
return p.Cl.V1Alpha1().Spaces().Get(name)
}

func (p crtClientProvider) ListSpaceBindings(reqs ...labels.Requirement) ([]toolchainv1alpha1.SpaceBinding, error) {
return p.cl.V1Alpha1().SpaceBindings().ListSpaceBindings(reqs...)
func (p CrtClientProvider) ListSpaceBindings(reqs ...labels.Requirement) ([]toolchainv1alpha1.SpaceBinding, error) {
return p.Cl.V1Alpha1().SpaceBindings().ListSpaceBindings(reqs...)
}
2 changes: 1 addition & 1 deletion pkg/signup/service/signup_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func NewSignupService(context servicecontext.ServiceContext, opts ...SignupServi

s := &ServiceImpl{
BaseService: base.NewBaseService(context),
defaultProvider: crtClientProvider{context.CRTClient()},
defaultProvider: CrtClientProvider{context.CRTClient()},
CaptchaChecker: captcha.Helper{},
}

Expand Down
Loading

0 comments on commit 6d3e720

Please sign in to comment.