From 6fa0d98547c56ce32edcc980337450561f77b244 Mon Sep 17 00:00:00 2001 From: zhaohuiweixiao Date: Mon, 6 Mar 2023 19:36:59 +0800 Subject: [PATCH] Fix: render addon application checkDeployClusters invalid (#5555) * Fix: render addon application checkDeployClusters invalid Signed-off-by: zhaohuihui * Feat: add getClusters test logic Signed-off-by: zhaohuihui --------- Signed-off-by: zhaohuihui --- pkg/addon/addon.go | 12 +++++++++++- pkg/addon/addon_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/pkg/addon/addon.go b/pkg/addon/addon.go index 0592880b6..adf166287 100644 --- a/pkg/addon/addon.go +++ b/pkg/addon/addon.go @@ -599,10 +599,20 @@ func getClusters(args map[string]interface{}) []string { return nil } cc, ok := ccr.([]string) + if ok { + return cc + } + ccrslice, ok := ccr.([]interface{}) if !ok { return nil } - return cc + var ccstring []string + for _, c := range ccrslice { + if cstring, ok := c.(string); ok { + ccstring = append(ccstring, cstring) + } + } + return ccstring } // renderNeededNamespaceAsComps will convert namespace as app components to create namespace for managed clusters diff --git a/pkg/addon/addon_test.go b/pkg/addon/addon_test.go index d6b0306e8..900ee4299 100644 --- a/pkg/addon/addon_test.go +++ b/pkg/addon/addon_test.go @@ -298,6 +298,41 @@ func TestRenderK8sObjects(t *testing.T) { assert.Equal(t, comp.Type, "k8s-objects") } +func TestGetClusters(t *testing.T) { + // string array test + args := map[string]interface{}{ + types.ClustersArg: []string{ + "cluster1", "cluster2", + }, + } + clusters := getClusters(args) + assert.Equal(t, clusters, []string{ + "cluster1", "cluster2", + }) + // interface array test + args1 := map[string]interface{}{ + types.ClustersArg: []interface{}{ + "cluster3", "cluster4", + }, + } + clusters1 := getClusters(args1) + assert.Equal(t, clusters1, []string{ + "cluster3", "cluster4", + }) + // no cluster arg test + args2 := map[string]interface{}{ + "anyargkey": "anyargvalue", + } + clusters2 := getClusters(args2) + assert.Nil(t, clusters2) + // other type test + args3 := map[string]interface{}{ + types.ClustersArg: "cluster5", + } + clusters3 := getClusters(args3) + assert.Nil(t, clusters3) +} + func TestGetAddonStatus(t *testing.T) { getFunc := test.MockGetFn(func(ctx context.Context, key client.ObjectKey, obj client.Object) error { switch key.Name {