Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 Randomly generated namespace in util pkg tests #4963

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions util/patch/patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,31 @@ import (
)

func TestPatchHelper(t *testing.T) {
ns, err := env.CreateNamespace(ctx, "test-patch-helper")
if err != nil {
t.Fatal(err)
}
defer func() {
if err := env.Delete(ctx, ns); err != nil {
t.Fatal(err)
}
}()

t.Run("should patch an unstructured object", func(t *testing.T) {
obj := &unstructured.Unstructured{
Object: map[string]interface{}{
"kind": "BootstrapMachine",
"apiVersion": "bootstrap.cluster.x-k8s.io/v1alpha4",
"metadata": map[string]interface{}{
"generateName": "test-bootstrap-",
"namespace": "default",
"namespace": ns.Name,
},
},
}

t.Run("adding an owner reference, preserving its status", func(t *testing.T) {
g := NewWithT(t)

obj := obj.DeepCopy()

t.Log("Creating the unstructured object")
g.Expect(env.Create(ctx, obj)).To(Succeed())
defer func() {
Expand Down Expand Up @@ -110,10 +118,10 @@ func TestPatchHelper(t *testing.T) {
g := NewWithT(t)

conditionTime := metav1.Date(2015, 1, 1, 12, 0, 0, 0, metav1.Now().Location())

obj := &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "node-patch-test-",
Namespace: ns.Name,
Annotations: map[string]string{
"test": "1",
},
Expand Down Expand Up @@ -165,7 +173,7 @@ func TestPatchHelper(t *testing.T) {
obj := &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-",
Namespace: "default",
Namespace: ns.Name,
},
}

Expand Down Expand Up @@ -478,7 +486,7 @@ func TestPatchHelper(t *testing.T) {
obj := &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-",
Namespace: "default",
Namespace: ns.Name,
},
}

Expand Down Expand Up @@ -565,7 +573,6 @@ func TestPatchHelper(t *testing.T) {
g := NewWithT(t)

obj := obj.DeepCopy()
obj.ObjectMeta.Namespace = "default"

t.Log("Creating the object")
g.Expect(env.Create(ctx, obj)).To(Succeed())
Expand All @@ -589,7 +596,7 @@ func TestPatchHelper(t *testing.T) {
obj.Spec.InfrastructureRef = &corev1.ObjectReference{
Kind: "test-kind",
Name: "test-ref",
Namespace: "default",
Namespace: ns.Name,
}

t.Log("Patching the object")
Expand Down Expand Up @@ -649,7 +656,6 @@ func TestPatchHelper(t *testing.T) {
g := NewWithT(t)

obj := obj.DeepCopy()
obj.ObjectMeta.Namespace = "default"

t.Log("Creating the object")
g.Expect(env.Create(ctx, obj)).To(Succeed())
Expand All @@ -673,7 +679,7 @@ func TestPatchHelper(t *testing.T) {
obj.Spec.InfrastructureRef = &corev1.ObjectReference{
Kind: "test-kind",
Name: "test-ref",
Namespace: "default",
Namespace: ns.Name,
}

t.Log("Updating the object status")
Expand Down Expand Up @@ -703,7 +709,7 @@ func TestPatchHelper(t *testing.T) {
obj := &clusterv1.MachineSet{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-ms",
Namespace: "default",
Namespace: ns.Name,
},
Spec: clusterv1.MachineSetSpec{
ClusterName: "test1",
Expand Down Expand Up @@ -851,14 +857,14 @@ func TestPatchHelper(t *testing.T) {
cluster := &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-",
Namespace: "default",
Namespace: ns.Name,
},
}

machineSet := &clusterv1.MachineSet{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-ms",
Namespace: "default",
Namespace: ns.Name,
},
Spec: clusterv1.MachineSetSpec{
ClusterName: "test1",
Expand Down
26 changes: 13 additions & 13 deletions util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestMachineToInfrastructureMapFunc(t *testing.T) {
},
request: &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Namespace: metav1.NamespaceDefault,
Name: "test-1",
},
Spec: clusterv1.MachineSpec{
Expand All @@ -67,7 +67,7 @@ func TestMachineToInfrastructureMapFunc(t *testing.T) {
output: []reconcile.Request{
{
NamespacedName: client.ObjectKey{
Namespace: "default",
Namespace: metav1.NamespaceDefault,
Name: "infra-1",
},
},
Expand All @@ -82,7 +82,7 @@ func TestMachineToInfrastructureMapFunc(t *testing.T) {
},
request: &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Namespace: metav1.NamespaceDefault,
Name: "test-1",
},
Spec: clusterv1.MachineSpec{
Expand Down Expand Up @@ -124,7 +124,7 @@ func TestClusterToInfrastructureMapFunc(t *testing.T) {
},
request: &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Namespace: metav1.NamespaceDefault,
Name: "test-1",
},
Spec: clusterv1.ClusterSpec{
Expand All @@ -138,7 +138,7 @@ func TestClusterToInfrastructureMapFunc(t *testing.T) {
output: []reconcile.Request{
{
NamespacedName: client.ObjectKey{
Namespace: "default",
Namespace: metav1.NamespaceDefault,
Name: "infra-1",
},
},
Expand All @@ -153,7 +153,7 @@ func TestClusterToInfrastructureMapFunc(t *testing.T) {
},
request: &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Namespace: metav1.NamespaceDefault,
Name: "test-1",
},
Spec: clusterv1.ClusterSpec{
Expand Down Expand Up @@ -395,7 +395,7 @@ func TestGetOwnerClusterSuccessByName(t *testing.T) {
myCluster := &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: "my-cluster",
Namespace: "my-ns",
Namespace: metav1.NamespaceDefault,
},
}

Expand All @@ -411,7 +411,7 @@ func TestGetOwnerClusterSuccessByName(t *testing.T) {
Name: "my-cluster",
},
},
Namespace: "my-ns",
Namespace: metav1.NamespaceDefault,
Name: "my-resource-owned-by-cluster",
}
cluster, err := GetOwnerCluster(ctx, c, objm)
Expand All @@ -431,7 +431,7 @@ func TestGetOwnerMachineSuccessByName(t *testing.T) {
myMachine := &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{
Name: "my-machine",
Namespace: "my-ns",
Namespace: metav1.NamespaceDefault,
},
}

Expand All @@ -447,7 +447,7 @@ func TestGetOwnerMachineSuccessByName(t *testing.T) {
Name: "my-machine",
},
},
Namespace: "my-ns",
Namespace: metav1.NamespaceDefault,
Name: "my-resource-owned-by-machine",
}
machine, err := GetOwnerMachine(ctx, c, objm)
Expand All @@ -461,7 +461,7 @@ func TestGetOwnerMachineSuccessByNameFromDifferentVersion(t *testing.T) {
myMachine := &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{
Name: "my-machine",
Namespace: "my-ns",
Namespace: metav1.NamespaceDefault,
},
}

Expand All @@ -477,7 +477,7 @@ func TestGetOwnerMachineSuccessByNameFromDifferentVersion(t *testing.T) {
Name: "my-machine",
},
},
Namespace: "my-ns",
Namespace: metav1.NamespaceDefault,
Name: "my-resource-owned-by-machine",
}
machine, err := GetOwnerMachine(ctx, c, objm)
Expand All @@ -491,7 +491,7 @@ func TestGetMachinesForCluster(t *testing.T) {
cluster := &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: "my-cluster",
Namespace: "my-ns",
Namespace: metav1.NamespaceDefault,
},
}

Expand Down