From 73d9f4f6e94986be7ab94d5bf7fca86bb43de8aa Mon Sep 17 00:00:00 2001 From: Yuki Iwai Date: Wed, 31 May 2023 21:17:40 +0900 Subject: [PATCH] Replace dummy client with fake client Signed-off-by: Yuki Iwai --- docs/api/kubeflow.org_v1_generated.asciidoc | 2 - pkg/reconciler.v1/common/service_test.go | 23 ++++--- .../reconciler.v1/test_job/dummy_client.go | 60 ------------------- .../test_job/test_job_reconciler.go | 15 ++--- 4 files changed, 18 insertions(+), 82 deletions(-) delete mode 100644 test_job/reconciler.v1/test_job/dummy_client.go diff --git a/docs/api/kubeflow.org_v1_generated.asciidoc b/docs/api/kubeflow.org_v1_generated.asciidoc index 8d38d35361..6c4964f425 100644 --- a/docs/api/kubeflow.org_v1_generated.asciidoc +++ b/docs/api/kubeflow.org_v1_generated.asciidoc @@ -15,8 +15,6 @@ Package v1 is the v1 version of the API. Package v1 contains API Schema definitions for the kubeflow.org v1 API group -Package util provides various helper routines. - .Resource Types - xref:{anchor_prefix}-github-com-kubeflow-training-operator-pkg-apis-kubeflow-org-v1-mpijob[$$MPIJob$$] - xref:{anchor_prefix}-github-com-kubeflow-training-operator-pkg-apis-kubeflow-org-v1-mpijoblist[$$MPIJobList$$] diff --git a/pkg/reconciler.v1/common/service_test.go b/pkg/reconciler.v1/common/service_test.go index bca00fa2f7..6075fb8474 100644 --- a/pkg/reconciler.v1/common/service_test.go +++ b/pkg/reconciler.v1/common/service_test.go @@ -15,6 +15,7 @@ package common_test import ( + "context" "reflect" "strings" "testing" @@ -25,7 +26,9 @@ import ( test_utilv1 "github.com/kubeflow/training-operator/test_job/test_util/v1" corev1 "k8s.io/api/core/v1" + apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "sigs.k8s.io/controller-runtime/pkg/client" ) func TestCreateNewService(t *testing.T) { @@ -80,21 +83,15 @@ func TestCreateNewService(t *testing.T) { t.Errorf("Got error when CreateNewService: %v", err) continue } - - found := false - for _, obj := range testReconciler.DC.Cache { - if obj.GetName() == c.expectedService.GetName() && obj.GetNamespace() == c.expectedService.GetNamespace() { - found = true - svcCreated := obj.(*corev1.Service) - svcExpected := c.expectedService - if !reflect.DeepEqual(svcExpected.Spec, svcCreated.Spec) { - t.Errorf("Spec mismatch for service %s/%s", svcExpected.GetNamespace(), svcExpected.GetName()) - } + var got corev1.Service + if err = testReconciler.FC.Get(context.Background(), client.ObjectKeyFromObject(c.expectedService), &got, &client.GetOptions{}); err != nil { + if apierrors.IsNotFound(err) { + t.Errorf("Cannot find Service %s/%s created", c.expectedService.GetNamespace(), c.expectedService.GetName()) } + t.Errorf("Got error when Get service: %v", err) } - - if !found { - t.Errorf("Cannot find Service %s/%s created", c.expectedService.GetNamespace(), c.expectedService.GetName()) + if !reflect.DeepEqual(c.expectedService.Spec, got.Spec) { + t.Errorf("Spec mismatch for service %s/%s", c.expectedService.GetNamespace(), c.expectedService.GetName()) } } } diff --git a/test_job/reconciler.v1/test_job/dummy_client.go b/test_job/reconciler.v1/test_job/dummy_client.go deleted file mode 100644 index b1be597aef..0000000000 --- a/test_job/reconciler.v1/test_job/dummy_client.go +++ /dev/null @@ -1,60 +0,0 @@ -package test_job - -import ( - "context" - - "k8s.io/apimachinery/pkg/api/errors" - "k8s.io/apimachinery/pkg/runtime/schema" - - "k8s.io/apimachinery/pkg/api/meta" - "k8s.io/apimachinery/pkg/runtime" - "sigs.k8s.io/controller-runtime/pkg/client" -) - -type DummyClient struct { - scheme *runtime.Scheme - mapper meta.RESTMapper - client.Reader - client.Writer - client.StatusClient - Cache []client.Object -} - -func (c *DummyClient) Scheme() *runtime.Scheme { - return c.scheme -} - -func (c *DummyClient) RESTMapper() meta.RESTMapper { - return c.mapper -} - -func (c *DummyClient) Create(ctx context.Context, obj client.Object, opts ...client.CreateOption) error { - c.Cache = append(c.Cache, obj) - return nil -} - -func (c *DummyClient) Delete(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error { - for idx, o := range c.Cache { - if o.GetName() == obj.GetName() && o.GetNamespace() == obj.GetNamespace() && o.GetObjectKind() == obj.GetObjectKind() { - c.Cache = append(c.Cache[:idx], c.Cache[idx+1:]...) - return nil - } - } - return errors.NewNotFound(schema.GroupResource{ - Group: obj.GetObjectKind().GroupVersionKind().Group, - Resource: obj.GetSelfLink(), - }, obj.GetName()) -} - -func (c *DummyClient) Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error { - for idx, o := range c.Cache { - if o.GetName() == obj.GetName() && o.GetNamespace() == obj.GetNamespace() && o.GetObjectKind() == obj.GetObjectKind() { - c.Cache[idx] = obj - return nil - } - } - return errors.NewNotFound(schema.GroupResource{ - Group: obj.GetObjectKind().GroupVersionKind().Group, - Resource: obj.GetSelfLink(), - }, obj.GetName()) -} diff --git a/test_job/reconciler.v1/test_job/test_job_reconciler.go b/test_job/reconciler.v1/test_job/test_job_reconciler.go index d454f547cd..1cc2fce178 100644 --- a/test_job/reconciler.v1/test_job/test_job_reconciler.go +++ b/test_job/reconciler.v1/test_job/test_job_reconciler.go @@ -14,6 +14,7 @@ import ( clientgoscheme "k8s.io/client-go/kubernetes/scheme" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/client/fake" "sigs.k8s.io/controller-runtime/pkg/log" ) @@ -24,7 +25,7 @@ type TestReconciler struct { common_reconciler.VolcanoReconciler common_reconciler.JobReconciler - DC *DummyClient + FC client.Client Job *v1.TestJob Pods []*corev1.Pod Services []*corev1.Service @@ -36,23 +37,23 @@ func NewTestReconciler() *TestReconciler { utilruntime.Must(clientgoscheme.AddToScheme(scheme)) utilruntime.Must(v1.AddToScheme(scheme)) - dummy_client := &DummyClient{} + fakeClient := fake.NewClientBuilder().WithScheme(clientgoscheme.Scheme).Build() r := &TestReconciler{ - DC: dummy_client, + FC: fakeClient, } // Generate Bare Components - jobR := common_reconciler.BareJobReconciler(dummy_client) + jobR := common_reconciler.BareJobReconciler(fakeClient) jobR.OverrideForJobInterface(r, r, r, r) - podR := common_reconciler.BarePodReconciler(dummy_client) + podR := common_reconciler.BarePodReconciler(fakeClient) podR.OverrideForPodInterface(r, r, r) - svcR := common_reconciler.BareServiceReconciler(dummy_client) + svcR := common_reconciler.BareServiceReconciler(fakeClient) svcR.OverrideForServiceInterface(r, r, r) - gangR := common_reconciler.BareVolcanoReconciler(dummy_client, nil, false) + gangR := common_reconciler.BareVolcanoReconciler(fakeClient, nil, false) gangR.OverrideForGangSchedulingInterface(r) Log := log.Log