Skip to content

Commit

Permalink
Return fake shoot client for tests with dependency injection and Glob…
Browse files Browse the repository at this point in the history
…al package method
  • Loading branch information
koala7659 committed Aug 5, 2024
1 parent 9f89a79 commit 98cb471
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"

"k8s.io/client-go/tools/clientcmd"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -23,9 +22,9 @@ var (
}
)

func getShootClientWithAdmin(ctx context.Context,
adminKubeconfigClient client.SubResourceClient,
shoot *gardener_api.Shoot) (client.Client, error) {
//nolint:gochecknoglobals
var GetShootClient = func(ctx context.Context,
adminKubeconfigClient client.SubResourceClient, shoot *gardener_api.Shoot) (client.Client, error) {
// request for admin kubeconfig with low expiration timeout
var req authenticationv1alpha1.AdminKubeconfigRequest
if err := adminKubeconfigClient.Create(ctx, shoot, &req); err != nil {
Expand Down Expand Up @@ -155,7 +154,7 @@ func updateCRBApplyFailed(rt *imv1.Runtime) {
func sFnApplyClusterRoleBindings(ctx context.Context, m *fsm, s *systemState) (stateFn, *ctrl.Result, error) {
// prepare subresource client to request admin kubeconfig
srscClient := m.ShootClient.SubResource("adminkubeconfig")
shootAdminClient, err := getShootClientWithAdmin(ctx, srscClient, s.shoot)
shootAdminClient, err := GetShootClient(ctx, srscClient, s.shoot)
if err != nil {
updateCRBApplyFailed(&s.instance)
return updateStatusAndStopWithError(err)
Expand Down
6 changes: 5 additions & 1 deletion internal/controller/runtime/runtime_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
)

var _ = Describe("Runtime Controller", func() {

Context("When reconciling a resource", func() {
const ResourceName = "test-resource"

Expand Down Expand Up @@ -212,7 +213,10 @@ func CreateRuntimeStub(resourceName string) *imv1.Runtime {
},
},
Security: imv1.Security{
Administrators: []string{},
Administrators: []string{
"test-admin1@sap.com",
"test-admin2@sap.com",
},
},
},
}
Expand Down
63 changes: 55 additions & 8 deletions internal/controller/runtime/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
gardener_shoot "github.com/kyma-project/infrastructure-manager/internal/gardener/shoot"
. "github.com/onsi/ginkgo/v2" //nolint:revive
. "github.com/onsi/gomega" //nolint:revive
rbacv1 "k8s.io/api/rbac/v1"
//nolint:revive
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
Expand All @@ -47,14 +48,15 @@ import (
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.

var (
cfg *rest.Config //nolint:gochecknoglobals
k8sClient client.Client //nolint:gochecknoglobals
gardenerTestClient client.Client //nolint:gochecknoglobals
testEnv *envtest.Environment //nolint:gochecknoglobals
suiteCtx context.Context //nolint:gochecknoglobals
cancelSuiteCtx context.CancelFunc //nolint:gochecknoglobals
runtimeReconciler *RuntimeReconciler //nolint:gochecknoglobals
customTracker *CustomTracker //nolint:gochecknoglobals
cfg *rest.Config //nolint:gochecknoglobals
k8sClient client.Client //nolint:gochecknoglobals
k8sFakeClientRoleBindings client.Client //nolint:gochecknoglobals
gardenerTestClient client.Client //nolint:gochecknoglobals
testEnv *envtest.Environment //nolint:gochecknoglobals
suiteCtx context.Context //nolint:gochecknoglobals
cancelSuiteCtx context.CancelFunc //nolint:gochecknoglobals
runtimeReconciler *RuntimeReconciler //nolint:gochecknoglobals
customTracker *CustomTracker //nolint:gochecknoglobals
)

func TestControllers(t *testing.T) {
Expand Down Expand Up @@ -109,6 +111,14 @@ var _ = BeforeSuite(func() {
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())

shootClientScheme := runtime.NewScheme()
_ = rbacv1.AddToScheme(shootClientScheme)
k8sFakeClientRoleBindings = fake.NewClientBuilder().WithScheme(shootClientScheme).Build()

fsm.GetShootClient = func(_ context.Context, _ client.SubResourceClient, _ *gardener_api.Shoot) (client.Client, error) {
return k8sFakeClientRoleBindings, nil
}

go func() {
defer GinkgoRecover()
suiteCtx, cancelSuiteCtx = context.WithCancel(context.Background())
Expand Down Expand Up @@ -137,13 +147,50 @@ func setupGardenerTestClientForUpdate() {
setupShootClientWithSequence(shoots)
}

// FakeClient is a mock implementation of the client.Client interface.
// type FakeClient struct {
// client.Client
//}
//
//func NewFakeClientWithMockedSubresource(mockedClient client.Client) client.Client {
// return &FakeClient{
// Client: mockedClient,
// }
//}
//
//// SubResource returns a mock SubResourceClient for the "adminkubeconfig" subresource.
//func (f *FakeClient) SubResource(subResource string) client.SubResourceClient {
// if subResource == "adminkubeconfig" {
// return &FakeSubResourceClient{}
// }
// return nil
//}
//
//// FakeSubResourceClient is a mock implementation of the client.SubResourceClient interface.
//type FakeSubResourceClient struct {
// client.SubResourceClient
//}

// Create handles the creation of the "adminkubeconfig" subresource.
// func (f *FakeSubResourceClient) Create(_ context.Context, obj client.Object, subResource client.Object, opts ...client.SubResourceCreateOption) error {
// if _, ok := obj.(*gardener_api.Shoot); ok {
// if req, ok := subResource.(*authenticationv1alpha1.AdminKubeconfigRequest); ok {
// // Simulate setting the Kubeconfig in the request status
// req.Status.Kubeconfig = []byte("fake-kubeconfig")
// return nil
// }
// }
// return errors.New("unsupported subresource")
//}

func setupShootClientWithSequence(shoots []*gardener_api.Shoot) {
clientScheme := runtime.NewScheme()
_ = gardener_api.AddToScheme(clientScheme)

tracker := clienttesting.NewObjectTracker(clientScheme, serializer.NewCodecFactory(clientScheme).UniversalDecoder())
customTracker = NewCustomTracker(tracker, shoots)
gardenerTestClient = fake.NewClientBuilder().WithScheme(clientScheme).WithObjectTracker(customTracker).Build()
// gardenerTestClient = NewFakeClientWithMockedSubresource(fake.NewClientBuilder().WithScheme(clientScheme).WithObjectTracker(customTracker).Build())

runtimeReconciler.UpdateShootClient(gardenerTestClient)
}
Expand Down

0 comments on commit 98cb471

Please sign in to comment.