Skip to content

Commit

Permalink
use FakeClient as the backend for InformersService in unit tests (#466)
Browse files Browse the repository at this point in the history
  • Loading branch information
MatousJobanek authored Sep 25, 2024
1 parent e15d7cc commit c828088
Show file tree
Hide file tree
Showing 8 changed files with 428 additions and 732 deletions.
17 changes: 11 additions & 6 deletions pkg/controller/usernames_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
package controller_test

import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"

toolchainv1alpha1 "github.com/codeready-toolchain/api/api/v1alpha1"
"github.com/codeready-toolchain/registration-service/pkg/controller"
"github.com/codeready-toolchain/registration-service/pkg/informers/service"
"github.com/codeready-toolchain/registration-service/pkg/username"
"github.com/codeready-toolchain/registration-service/test"
"github.com/codeready-toolchain/registration-service/test/fake"
commontest "github.com/codeready-toolchain/toolchain-common/pkg/test"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"sigs.k8s.io/controller-runtime/pkg/client"
)

type TestUsernamesSuite struct {
Expand All @@ -31,13 +34,13 @@ func (s *TestUsernamesSuite) TestUsernamesGetHandler() {
req, err := http.NewRequest(http.MethodGet, "/api/v1/usernames", nil)
require.NoError(s.T(), err)

fakeClient := fake.InitClient(s.T(),
fakeClient := commontest.NewFakeClient(s.T(),
fake.NewMasterUserRecord("johnny"),
)

s.Run("success", func() {

fakeInformer := fake.GetInformerService(fakeClient)()
fakeInformer := service.NewInformerService(fakeClient, commontest.HostOperatorNs)
s.Application.MockInformerService(fakeInformer)

// Create Usernames controller instance.
Expand Down Expand Up @@ -97,9 +100,11 @@ func (s *TestUsernamesSuite) TestUsernamesGetHandler() {

s.Run("error", func() {
// force error while retrieving MUR
fakeInformer := fake.GetInformerService(fakeClient, fake.WithGetMurFunc(func(_ string) (*toolchainv1alpha1.MasterUserRecord, error) {
return nil, fmt.Errorf("mock error")
}))()
fakeClient := commontest.NewFakeClient(s.T())
fakeClient.MockGet = func(_ context.Context, _ client.ObjectKey, _ client.Object, _ ...client.GetOption) error {
return fmt.Errorf("mock error")
}
fakeInformer := service.NewInformerService(fakeClient, commontest.HostOperatorNs)
s.Application.MockInformerService(fakeInformer)

// Create Usernames controller instance.
Expand Down
Loading

0 comments on commit c828088

Please sign in to comment.