diff --git a/pkg/models/resources/v1alpha3/clusterdashboard/clusterdashboard.go b/pkg/models/resources/v1alpha3/clusterdashboard/clusterdashboard.go index 8bd3e729d..f5fb598fc 100644 --- a/pkg/models/resources/v1alpha3/clusterdashboard/clusterdashboard.go +++ b/pkg/models/resources/v1alpha3/clusterdashboard/clusterdashboard.go @@ -23,7 +23,6 @@ import ( "k8s.io/apimachinery/pkg/types" "k8s.io/klog" monitoringdashboardv1alpha1 "kubesphere.io/monitoring-dashboard/api/v1alpha1" - "sigs.k8s.io/controller-runtime/pkg/cache" "sigs.k8s.io/controller-runtime/pkg/client" "kubesphere.io/kubesphere/pkg/api" @@ -32,10 +31,10 @@ import ( ) type dashboardGetter struct { - c cache.Cache + c client.Reader } -func New(c cache.Cache) v1alpha3.Interface { +func New(c client.Reader) v1alpha3.Interface { return &dashboardGetter{c} } diff --git a/pkg/models/resources/v1alpha3/clusterdashboard/clusterdashboard_test.go b/pkg/models/resources/v1alpha3/clusterdashboard/clusterdashboard_test.go index 28440b6f8..33f6f653a 100644 --- a/pkg/models/resources/v1alpha3/clusterdashboard/clusterdashboard_test.go +++ b/pkg/models/resources/v1alpha3/clusterdashboard/clusterdashboard_test.go @@ -18,16 +18,14 @@ package clusterdashboard import ( "context" - "path/filepath" "reflect" "testing" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes/scheme" monitoringdashboardv1alpha1 "kubesphere.io/monitoring-dashboard/api/v1alpha1" - "sigs.k8s.io/controller-runtime/pkg/cache" "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/envtest" + "sigs.k8s.io/controller-runtime/pkg/client/fake" "kubesphere.io/kubesphere/pkg/apiserver/query" ) @@ -45,24 +43,12 @@ func compare(actual *monitoringdashboardv1alpha1.ClusterDashboard, } func TestGetListClusterDashboards(t *testing.T) { - e := &envtest.Environment{CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "..", "..", "config", "crds")}} - cfg, err := e.Start() - if err != nil { - t.Fatal(err) - } - sch := scheme.Scheme if err := monitoringdashboardv1alpha1.AddToScheme(sch); err != nil { t.Fatalf("unable add APIs to scheme: %v", err) } - stopCh := make(chan struct{}) - - ce, _ := cache.New(cfg, cache.Options{Scheme: sch}) - go ce.Start(stopCh) - ce.WaitForCacheSync(stopCh) - - c, _ = client.New(cfg, client.Options{Scheme: sch}) + c = fake.NewFakeClientWithScheme(sch) var labelSet1 = map[string]string{"foo-1": "bar-1"} var labelSet2 = map[string]string{"foo-2": "bar-2"} @@ -85,12 +71,12 @@ func TestGetListClusterDashboards(t *testing.T) { ctx := context.TODO() for _, board := range testCases { - if err = c.Create(ctx, board); err != nil { + if err := c.Create(ctx, board); err != nil { t.Fatal(err) } } - getter := New(ce) + getter := New(c) results, err := getter.List("", &query.Query{}) if err != nil { diff --git a/pkg/models/resources/v1alpha3/dashboard/dashboard.go b/pkg/models/resources/v1alpha3/dashboard/dashboard.go index 9f419c271..96d715443 100644 --- a/pkg/models/resources/v1alpha3/dashboard/dashboard.go +++ b/pkg/models/resources/v1alpha3/dashboard/dashboard.go @@ -23,7 +23,6 @@ import ( "k8s.io/apimachinery/pkg/types" "k8s.io/klog" monitoringdashboardv1alpha1 "kubesphere.io/monitoring-dashboard/api/v1alpha1" - "sigs.k8s.io/controller-runtime/pkg/cache" "sigs.k8s.io/controller-runtime/pkg/client" "kubesphere.io/kubesphere/pkg/api" @@ -32,10 +31,10 @@ import ( ) type dashboardGetter struct { - c cache.Cache + c client.Reader } -func New(c cache.Cache) v1alpha3.Interface { +func New(c client.Reader) v1alpha3.Interface { return &dashboardGetter{c} } diff --git a/pkg/models/resources/v1alpha3/dashboard/dashboard_test.go b/pkg/models/resources/v1alpha3/dashboard/dashboard_test.go index 57d860305..e05150f92 100644 --- a/pkg/models/resources/v1alpha3/dashboard/dashboard_test.go +++ b/pkg/models/resources/v1alpha3/dashboard/dashboard_test.go @@ -18,36 +18,20 @@ package dashboard import ( "context" - "path/filepath" "reflect" "testing" - core "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes/scheme" - "k8s.io/klog/v2" monitoringdashboardv1alpha1 "kubesphere.io/monitoring-dashboard/api/v1alpha1" - "sigs.k8s.io/controller-runtime/pkg/cache" "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/envtest" + "sigs.k8s.io/controller-runtime/pkg/client/fake" "kubesphere.io/kubesphere/pkg/apiserver/query" ) var c client.Client -func createNamespace(name string, ctx context.Context) { - namespace := &core.Namespace{ - ObjectMeta: metav1.ObjectMeta{ - Name: name, - }, - } - err := c.Create(ctx, namespace) - if err != nil { - klog.Error(err) - } -} - func compare(actual *monitoringdashboardv1alpha1.Dashboard, expects ...*monitoringdashboardv1alpha1.Dashboard) bool { for _, app := range expects { if actual.Name == app.Name && actual.Namespace == app.Namespace && reflect.DeepEqual(actual.Labels, app.Labels) { @@ -58,24 +42,12 @@ func compare(actual *monitoringdashboardv1alpha1.Dashboard, expects ...*monitori } func TestGetListDashboards(t *testing.T) { - e := &envtest.Environment{CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "..", "..", "config", "crds")}} - cfg, err := e.Start() - if err != nil { - t.Fatal(err) - } - sch := scheme.Scheme if err := monitoringdashboardv1alpha1.AddToScheme(sch); err != nil { t.Fatalf("unable add APIs to scheme: %v", err) } - stopCh := make(chan struct{}) - - ce, _ := cache.New(cfg, cache.Options{Scheme: sch}) - go ce.Start(stopCh) - ce.WaitForCacheSync(stopCh) - - c, _ = client.New(cfg, client.Options{Scheme: sch}) + c = fake.NewFakeClientWithScheme(sch) var labelSet1 = map[string]string{"foo-1": "bar-1"} var labelSet2 = map[string]string{"foo-2": "bar-2"} @@ -99,15 +71,14 @@ func TestGetListDashboards(t *testing.T) { } ctx := context.TODO() - createNamespace(ns, ctx) for _, board := range testCases { - if err = c.Create(ctx, board); err != nil { + if err := c.Create(ctx, board); err != nil { t.Fatal(err) } } - getter := New(ce) + getter := New(c) results, err := getter.List(ns, &query.Query{}) if err != nil {