Skip to content

Commit

Permalink
Refactor NewUnstructured method
Browse files Browse the repository at this point in the history
Signed-off-by: Jian Qiu <jqiu@redhat.com>
  • Loading branch information
qiujian16 committed Apr 11, 2024
1 parent 1ea7512 commit fc8f639
Show file tree
Hide file tree
Showing 16 changed files with 186 additions and 191 deletions.
46 changes: 23 additions & 23 deletions pkg/addon/controllers/addonprogressing/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ func TestReconcile(t *testing.T) {
clusterManagementAddon: []runtime.Object{},
managedClusteraddon: []runtime.Object{},
work: []runtime.Object{},
validateAddonActions: addontesting.AssertNoActions,
validateAddonActions: testingcommon.AssertNoActions,
},
{
name: "no managedClusteraddon",
syncKey: "test/test",
managedClusteraddon: []runtime.Object{},
clusterManagementAddon: []runtime.Object{addontesting.NewClusterManagementAddon("test", "testcrd", "testcr").Build()},
work: []runtime.Object{},
validateAddonActions: addontesting.AssertNoActions,
validateAddonActions: testingcommon.AssertNoActions,
},
{
name: "no work applied condition",
Expand All @@ -58,7 +58,7 @@ func TestReconcile(t *testing.T) {
},
clusterManagementAddon: []runtime.Object{addontesting.NewClusterManagementAddon("test", "testcrd", "testcr").Build()},
work: []runtime.Object{},
validateAddonActions: addontesting.AssertNoActions,
validateAddonActions: testingcommon.AssertNoActions,
},
{
name: "update managedclusteraddon to installing when no work",
Expand All @@ -76,7 +76,7 @@ func TestReconcile(t *testing.T) {
clusterManagementAddon: []runtime.Object{addontesting.NewClusterManagementAddon("test", "testcrd", "testcr").Build()},
work: []runtime.Object{},
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) {
addontesting.AssertActions(t, actions, "patch")
testingcommon.AssertActions(t, actions, "patch")
actual := actions[0].(clienttesting.PatchActionImpl).Patch

addOn := &addonapiv1alpha1.ManagedClusterAddOn{}
Expand Down Expand Up @@ -121,8 +121,8 @@ func TestReconcile(t *testing.T) {
work := addontesting.NewManifestWork(
"addon-test-deploy",
"cluster1",
addontesting.NewUnstructured("v1", "ConfigMap", "default", "test1"),
addontesting.NewUnstructured("v1", "Deployment", "default", "test1"),
testingcommon.NewUnstructured("v1", "ConfigMap", "default", "test1"),
testingcommon.NewUnstructured("v1", "Deployment", "default", "test1"),
)
work.SetLabels(map[string]string{
addonapiv1alpha1.AddonLabelKey: "test",
Expand All @@ -143,7 +143,7 @@ func TestReconcile(t *testing.T) {
return work
}()},
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) {
addontesting.AssertActions(t, actions, "patch")
testingcommon.AssertActions(t, actions, "patch")
actual := actions[0].(clienttesting.PatchActionImpl).Patch

addOn := &addonapiv1alpha1.ManagedClusterAddOn{}
Expand Down Expand Up @@ -191,8 +191,8 @@ func TestReconcile(t *testing.T) {
work := addontesting.NewManifestWork(
"addon-test-deploy",
"cluster1",
addontesting.NewUnstructured("v1", "ConfigMap", "default", "test1"),
addontesting.NewUnstructured("v1", "Deployment", "default", "test1"),
testingcommon.NewUnstructured("v1", "ConfigMap", "default", "test1"),
testingcommon.NewUnstructured("v1", "Deployment", "default", "test1"),
)
work.SetLabels(map[string]string{
addonapiv1alpha1.AddonLabelKey: "test",
Expand All @@ -213,7 +213,7 @@ func TestReconcile(t *testing.T) {
return work
}()},
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) {
addontesting.AssertActions(t, actions, "patch")
testingcommon.AssertActions(t, actions, "patch")
actual := actions[0].(clienttesting.PatchActionImpl).Patch

addOn := &addonapiv1alpha1.ManagedClusterAddOn{}
Expand Down Expand Up @@ -261,8 +261,8 @@ func TestReconcile(t *testing.T) {
work := addontesting.NewManifestWork(
"addon-test-deploy",
"cluster1",
addontesting.NewUnstructured("v1", "ConfigMap", "default", "test1"),
addontesting.NewUnstructured("v1", "Deployment", "default", "test1"),
testingcommon.NewUnstructured("v1", "ConfigMap", "default", "test1"),
testingcommon.NewUnstructured("v1", "Deployment", "default", "test1"),
)
work.SetLabels(map[string]string{
addonapiv1alpha1.AddonLabelKey: "test",
Expand All @@ -283,7 +283,7 @@ func TestReconcile(t *testing.T) {
return work
}()},
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) {
addontesting.AssertActions(t, actions, "patch")
testingcommon.AssertActions(t, actions, "patch")
actual := actions[0].(clienttesting.PatchActionImpl).Patch

addOn := &addonapiv1alpha1.ManagedClusterAddOn{}
Expand Down Expand Up @@ -331,8 +331,8 @@ func TestReconcile(t *testing.T) {
work := addontesting.NewManifestWork(
"addon-test-deploy",
"cluster1",
addontesting.NewUnstructured("v1", "ConfigMap", "default", "test1"),
addontesting.NewUnstructured("v1", "Deployment", "default", "test1"),
testingcommon.NewUnstructured("v1", "ConfigMap", "default", "test1"),
testingcommon.NewUnstructured("v1", "Deployment", "default", "test1"),
)
work.SetLabels(map[string]string{
addonapiv1alpha1.AddonLabelKey: "test",
Expand All @@ -353,7 +353,7 @@ func TestReconcile(t *testing.T) {
return work
}()},
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) {
addontesting.AssertActions(t, actions, "patch")
testingcommon.AssertActions(t, actions, "patch")
actual := actions[0].(clienttesting.PatchActionImpl).Patch

addOn := &addonapiv1alpha1.ManagedClusterAddOn{}
Expand Down Expand Up @@ -401,8 +401,8 @@ func TestReconcile(t *testing.T) {
work := addontesting.NewManifestWork(
"addon-test-deploy",
"cluster1",
addontesting.NewUnstructured("v1", "ConfigMap", "default", "test1"),
addontesting.NewUnstructured("v1", "Deployment", "default", "test1"),
testingcommon.NewUnstructured("v1", "ConfigMap", "default", "test1"),
testingcommon.NewUnstructured("v1", "Deployment", "default", "test1"),
)
work.SetLabels(map[string]string{
addonapiv1alpha1.AddonLabelKey: "test",
Expand All @@ -423,7 +423,7 @@ func TestReconcile(t *testing.T) {
return work
}()},
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) {
addontesting.AssertActions(t, actions, "patch")
testingcommon.AssertActions(t, actions, "patch")
actual := actions[0].(clienttesting.PatchActionImpl).Patch

addOn := &addonapiv1alpha1.ManagedClusterAddOn{}
Expand Down Expand Up @@ -474,8 +474,8 @@ func TestReconcile(t *testing.T) {
work := addontesting.NewManifestWork(
"addon-test-deploy",
"cluster1",
addontesting.NewUnstructured("v1", "ConfigMap", "default", "test1"),
addontesting.NewUnstructured("v1", "Deployment", "default", "test1"),
testingcommon.NewUnstructured("v1", "ConfigMap", "default", "test1"),
testingcommon.NewUnstructured("v1", "Deployment", "default", "test1"),
)
work.SetLabels(map[string]string{
addonapiv1alpha1.AddonLabelKey: "test",
Expand All @@ -496,7 +496,7 @@ func TestReconcile(t *testing.T) {
return work
}()},
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) {
addontesting.AssertActions(t, actions, "patch")
testingcommon.AssertActions(t, actions, "patch")
actual := actions[0].(clienttesting.PatchActionImpl).Patch

addOn := &addonapiv1alpha1.ManagedClusterAddOn{}
Expand Down Expand Up @@ -552,7 +552,7 @@ func TestReconcile(t *testing.T) {
clusterManagementAddon: []runtime.Object{addontesting.NewClusterManagementAddon("test", "testcrd", "testcr").Build()},
work: []runtime.Object{},
validateAddonActions: func(t *testing.T, actions []clienttesting.Action) {
addontesting.AssertActions(t, actions, "patch")
testingcommon.AssertActions(t, actions, "patch")
patch := actions[0].(clienttesting.PatchAction).GetPatch()
addOn := &addonapiv1alpha1.ManagedClusterAddOn{}
err := json.Unmarshal(patch, addOn)
Expand Down
46 changes: 46 additions & 0 deletions pkg/common/testing/unstructured.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package testing

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
)

func NewUnstructured(apiVersion, kind, namespace, name string, owners ...metav1.OwnerReference) *unstructured.Unstructured {
u := &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": apiVersion,
"kind": kind,
"metadata": map[string]interface{}{
"namespace": namespace,
"name": name,
},
},
}

u.SetOwnerReferences(owners)

return u
}

func NewUnstructuredWithContent(
apiVersion, kind, namespace, name string, content map[string]interface{}) *unstructured.Unstructured {
object := NewUnstructured(apiVersion, kind, namespace, name)
for key, val := range content {
object.Object[key] = val
}

return object
}

func NewUnstructuredSecret(namespace, name string, terminated bool, uid string, owners ...metav1.OwnerReference) *unstructured.Unstructured {
u := NewUnstructured("v1", "Secret", namespace, name, owners...)
if terminated {
now := metav1.Now()
u.SetDeletionTimestamp(&now)
}
if uid != "" {
u.SetUID(types.UID(uid))
}
return u
}
14 changes: 0 additions & 14 deletions pkg/registration/helpers/testing/testinghelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
kubeversion "k8s.io/client-go/pkg/version"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
Expand Down Expand Up @@ -264,19 +263,6 @@ func NewNode(name string, capacity, allocatable corev1.ResourceList) *corev1.Nod
}
}

func NewUnstructuredObj(apiVersion, kind, namespace, name string) *unstructured.Unstructured {
return &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": apiVersion,
"kind": kind,
"metadata": map[string]interface{}{
"namespace": namespace,
"name": name,
},
},
}
}

type CSRHolder struct {
Name string
Labels map[string]string
Expand Down
5 changes: 3 additions & 2 deletions pkg/work/helper/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

workapiv1 "open-cluster-management.io/api/work/v1"

testingcommon "open-cluster-management.io/ocm/pkg/common/testing"
"open-cluster-management.io/ocm/pkg/work/spoke/spoketesting"
)

Expand Down Expand Up @@ -611,7 +612,7 @@ func TestBuildResourceMeta(t *testing.T) {
{
name: "secret success",
index: 1,
obj: spoketesting.NewUnstructured("v1", "Secret", "ns1", "test"),
obj: testingcommon.NewUnstructured("v1", "Secret", "ns1", "test"),
expectedErr: nil,
expectedGVR: schema.GroupVersionResource{
Group: "",
Expand All @@ -631,7 +632,7 @@ func TestBuildResourceMeta(t *testing.T) {
{
name: "unknow object type",
index: 1,
obj: spoketesting.NewUnstructured("test/v1", "NewObject", "ns1", "test"),
obj: testingcommon.NewUnstructured("test/v1", "NewObject", "ns1", "test"),
expectedErr: fmt.Errorf("the server doesn't have a resource type %q", "NewObject"),
expectedGVR: schema.GroupVersionResource{},
expectedMeta: workapiv1.ManifestResourceMeta{
Expand Down
21 changes: 11 additions & 10 deletions pkg/work/hub/test/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
workapiv1 "open-cluster-management.io/api/work/v1"
workapiv1alpha1 "open-cluster-management.io/api/work/v1alpha1"

testingcommon "open-cluster-management.io/ocm/pkg/common/testing"
"open-cluster-management.io/ocm/pkg/work/spoke/spoketesting"
)

Expand All @@ -36,7 +37,7 @@ func CreateTestManifestWorkReplicaSet(name string, ns string, placementNames ...

func CreateTestManifestWorkReplicaSetWithRollOutStrategy(name string, ns string,
placements map[string]clusterv1alpha1.RolloutStrategy) *workapiv1alpha1.ManifestWorkReplicaSet {
obj := spoketesting.NewUnstructured("v1", "kind", "test-ns", "test-name")
obj := testingcommon.NewUnstructured("v1", "kind", "test-ns", "test-name")
mw, _ := spoketesting.NewManifestWork(0, obj)
var placementRefs []workapiv1alpha1.LocalPlacementReference

Expand All @@ -63,8 +64,8 @@ func CreateTestManifestWorkReplicaSetWithRollOutStrategy(name string, ns string,
}

func CreateTestManifestWorks(name, namespace string, placementName string, clusters ...string) []runtime.Object {
obj := spoketesting.NewUnstructured("v1", "kind", "test-ns", "test-name")
works := []runtime.Object{}
obj := testingcommon.NewUnstructured("v1", "kind", "test-ns", "test-name")
var works []runtime.Object
for _, c := range clusters {
mw, _ := spoketesting.NewManifestWork(0, obj)
mw.Name = name
Expand All @@ -87,7 +88,7 @@ func CreateTestManifestWorks(name, namespace string, placementName string, clust
}

func CreateTestManifestWork(name, namespace string, placementName string, clusterName string) *workapiv1.ManifestWork {
obj := spoketesting.NewUnstructured("v1", "kind", "test-ns", "test-name")
obj := testingcommon.NewUnstructured("v1", "kind", "test-ns", "test-name")
mw, _ := spoketesting.NewManifestWork(0, obj)
mw.Name = name
mw.Namespace = clusterName
Expand All @@ -108,14 +109,14 @@ func CreateTestManifestWork(name, namespace string, placementName string, cluste
}

func CreateTestManifestWorkSpecWithSecret(mApiVersion string, mKind string, mNS string, mName string) workapiv1.ManifestWorkSpec {
secret := spoketesting.NewUnstructuredSecret(mName, mNS, true, "0b1441ec-717f-4877-a165-27e5b59245f6")
obj := spoketesting.NewUnstructuredWithContent(mApiVersion, mKind, mNS, mName, secret.Object)
secret := testingcommon.NewUnstructuredSecret(mName, mNS, true, "0b1441ec-717f-4877-a165-27e5b59245f6")
obj := testingcommon.NewUnstructuredWithContent(mApiVersion, mKind, mNS, mName, secret.Object)
mw, _ := spoketesting.NewManifestWork(0, obj)

return mw.Spec
}

// Return placement with predicate of label cluster name
// CreateTestPlacement Return placement with predicate of label cluster name
func CreateTestPlacement(name string, ns string, clusters ...string) (*clusterv1beta1.Placement, *clusterv1beta1.PlacementDecision) {
placement, placementDesicions := CreateTestPlacementWithDecisionStrategy(name, ns, len(clusters), clusters...)

Expand Down Expand Up @@ -151,7 +152,7 @@ func CreateTestPlacementWithDecisionStrategy(name string, ns string, clsPerDecis
}

groupStrategy := clusterv1beta1.GroupStrategy{
ClustersPerDecisionGroup: intstr.FromInt(clsPerDecisionGroup),
ClustersPerDecisionGroup: intstr.FromInt32(int32(clsPerDecisionGroup)),
}
decisionStrategy := clusterv1beta1.DecisionStrategy{
GroupStrategy: groupStrategy,
Expand Down Expand Up @@ -182,7 +183,7 @@ func CreateTestPlacementWithDecisionStrategy(name string, ns string, clsPerDecis
},
}

decisions := []clusterv1beta1.ClusterDecision{}
var decisions []clusterv1beta1.ClusterDecision
for _, cls := range clsGroup {
decisions = append(decisions, clusterv1beta1.ClusterDecision{
ClusterName: cls,
Expand Down Expand Up @@ -214,7 +215,7 @@ func getClusterGroups(clusters []string, clsPerDecisionGroup int) [][]string {
return clusterGroups
}

decisionGroupCount := (len(clusters) / clsPerDecisionGroup)
decisionGroupCount := len(clusters) / clsPerDecisionGroup
if len(clusters)%clsPerDecisionGroup > 0 {
decisionGroupCount = decisionGroupCount + 1
}
Expand Down
7 changes: 3 additions & 4 deletions pkg/work/spoke/apply/create_only_apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
clienttesting "k8s.io/client-go/testing"

testingcommon "open-cluster-management.io/ocm/pkg/common/testing"
"open-cluster-management.io/ocm/pkg/work/spoke/spoketesting"
)

func TestCreateOnlyApply(t *testing.T) {
Expand All @@ -29,7 +28,7 @@ func TestCreateOnlyApply(t *testing.T) {
name: "create a non exist object",
owner: metav1.OwnerReference{APIVersion: "v1", Name: "test", UID: defaultOwner},
existing: nil,
required: spoketesting.NewUnstructured("v1", "Secret", "ns1", "test"),
required: testingcommon.NewUnstructured("v1", "Secret", "ns1", "test"),
gvr: schema.GroupVersionResource{Version: "v1", Resource: "secrets"},
validateActions: func(t *testing.T, actions []clienttesting.Action) {
testingcommon.AssertActions(t, actions, "get", "create")
Expand All @@ -48,8 +47,8 @@ func TestCreateOnlyApply(t *testing.T) {
{
name: "create an already existing object",
owner: metav1.OwnerReference{APIVersion: "v1", Name: "test", UID: defaultOwner},
existing: spoketesting.NewUnstructured("v1", "Secret", "ns1", "test"),
required: spoketesting.NewUnstructured("v1", "Secret", "ns1", "test"),
existing: testingcommon.NewUnstructured("v1", "Secret", "ns1", "test"),
required: testingcommon.NewUnstructured("v1", "Secret", "ns1", "test"),
gvr: schema.GroupVersionResource{Version: "v1", Resource: "secrets"},
validateActions: func(t *testing.T, actions []clienttesting.Action) {
testingcommon.AssertActions(t, actions, "get")
Expand Down
Loading

0 comments on commit fc8f639

Please sign in to comment.