Skip to content

Commit

Permalink
util tests namespace creation refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
GrigoriyMikhalkin committed Jul 17, 2021
1 parent 831ed7d commit 4a42134
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 35 deletions.
104 changes: 82 additions & 22 deletions util/patch/patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,25 @@ import (
)

func TestPatchHelper(t *testing.T) {
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",
},
},
}
namespacePrefix := "test-patch-helper"

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

obj := obj.DeepCopy()
ns, err := env.CreateNamespace(ctx, namespacePrefix)
g.Expect(err).NotTo(HaveOccurred())

obj := &unstructured.Unstructured{
Object: map[string]interface{}{
"kind": "BootstrapMachine",
"apiVersion": "bootstrap.cluster.x-k8s.io/v1alpha4",
"metadata": map[string]interface{}{
"generateName": "test-bootstrap-",
"namespace": ns.Name,
},
},
}

t.Log("Creating the unstructured object")
g.Expect(env.Create(ctx, obj)).To(Succeed())
Expand Down Expand Up @@ -109,11 +112,14 @@ func TestPatchHelper(t *testing.T) {
t.Run("on a corev1.Node object", func(t *testing.T) {
g := NewWithT(t)

conditionTime := metav1.Date(2015, 1, 1, 12, 0, 0, 0, metav1.Now().Location())
ns, err := env.CreateNamespace(ctx, namespacePrefix)
g.Expect(err).NotTo(HaveOccurred())

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,14 +171,17 @@ func TestPatchHelper(t *testing.T) {
obj := &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-",
Namespace: "default",
},
}

t.Run("should mark it ready", func(t *testing.T) {
g := NewWithT(t)

ns, err := env.CreateNamespace(ctx, namespacePrefix)
g.Expect(err).NotTo(HaveOccurred())

obj := obj.DeepCopy()
obj.ObjectMeta.Namespace = ns.Name

t.Log("Creating the object")
g.Expect(env.Create(ctx, obj)).To(Succeed())
Expand Down Expand Up @@ -210,7 +219,11 @@ func TestPatchHelper(t *testing.T) {
t.Run("should recover if there is a resolvable conflict", func(t *testing.T) {
g := NewWithT(t)

ns, err := env.CreateNamespace(ctx, namespacePrefix)
g.Expect(err).NotTo(HaveOccurred())

obj := obj.DeepCopy()
obj.ObjectMeta.Namespace = ns.Name

t.Log("Creating the object")
g.Expect(env.Create(ctx, obj)).To(Succeed())
Expand Down Expand Up @@ -264,7 +277,11 @@ func TestPatchHelper(t *testing.T) {
t.Run("should recover if there is a resolvable conflict, incl. patch spec and status", func(t *testing.T) {
g := NewWithT(t)

ns, err := env.CreateNamespace(ctx, namespacePrefix)
g.Expect(err).NotTo(HaveOccurred())

obj := obj.DeepCopy()
obj.ObjectMeta.Namespace = ns.Name

t.Log("Creating the object")
g.Expect(env.Create(ctx, obj)).To(Succeed())
Expand Down Expand Up @@ -325,7 +342,11 @@ func TestPatchHelper(t *testing.T) {
t.Run("should return an error if there is an unresolvable conflict", func(t *testing.T) {
g := NewWithT(t)

ns, err := env.CreateNamespace(ctx, namespacePrefix)
g.Expect(err).NotTo(HaveOccurred())

obj := obj.DeepCopy()
obj.ObjectMeta.Namespace = ns.Name

t.Log("Creating the object")
g.Expect(env.Create(ctx, obj)).To(Succeed())
Expand Down Expand Up @@ -373,7 +394,11 @@ func TestPatchHelper(t *testing.T) {
t.Run("should not return an error if there is an unresolvable conflict but the conditions is owned by the controller", func(t *testing.T) {
g := NewWithT(t)

ns, err := env.CreateNamespace(ctx, namespacePrefix)
g.Expect(err).NotTo(HaveOccurred())

obj := obj.DeepCopy()
obj.ObjectMeta.Namespace = ns.Name

t.Log("Creating the object")
g.Expect(env.Create(ctx, obj)).To(Succeed())
Expand Down Expand Up @@ -424,7 +449,11 @@ func TestPatchHelper(t *testing.T) {
t.Run("should not return an error if there is an unresolvable conflict when force overwrite is enabled", func(t *testing.T) {
g := NewWithT(t)

ns, err := env.CreateNamespace(ctx, namespacePrefix)
g.Expect(err).NotTo(HaveOccurred())

obj := obj.DeepCopy()
obj.ObjectMeta.Namespace = ns.Name

t.Log("Creating the object")
g.Expect(env.Create(ctx, obj)).To(Succeed())
Expand Down Expand Up @@ -478,14 +507,17 @@ func TestPatchHelper(t *testing.T) {
obj := &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-",
Namespace: "default",
},
}

t.Run("add a finalizer", func(t *testing.T) {
g := NewWithT(t)

ns, err := env.CreateNamespace(ctx, namespacePrefix)
g.Expect(err).NotTo(HaveOccurred())

obj := obj.DeepCopy()
obj.ObjectMeta.Namespace = ns.Name

t.Log("Creating the object")
g.Expect(env.Create(ctx, obj)).To(Succeed())
Expand Down Expand Up @@ -524,8 +556,12 @@ func TestPatchHelper(t *testing.T) {
t.Run("removing finalizers", func(t *testing.T) {
g := NewWithT(t)

ns, err := env.CreateNamespace(ctx, namespacePrefix)
g.Expect(err).NotTo(HaveOccurred())

obj := obj.DeepCopy()
obj.Finalizers = append(obj.Finalizers, clusterv1.ClusterFinalizer)
obj.ObjectMeta.Namespace = ns.Name

t.Log("Creating the object")
g.Expect(env.Create(ctx, obj)).To(Succeed())
Expand Down Expand Up @@ -564,8 +600,11 @@ func TestPatchHelper(t *testing.T) {
t.Run("updating spec", func(t *testing.T) {
g := NewWithT(t)

ns, err := env.CreateNamespace(ctx, namespacePrefix)
g.Expect(err).NotTo(HaveOccurred())

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

t.Log("Creating the object")
g.Expect(env.Create(ctx, obj)).To(Succeed())
Expand All @@ -589,7 +628,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 All @@ -610,7 +649,11 @@ func TestPatchHelper(t *testing.T) {
t.Run("updating status", func(t *testing.T) {
g := NewWithT(t)

ns, err := env.CreateNamespace(ctx, namespacePrefix)
g.Expect(err).NotTo(HaveOccurred())

obj := obj.DeepCopy()
obj.ObjectMeta.Namespace = ns.Name

t.Log("Creating the object")
g.Expect(env.Create(ctx, obj)).To(Succeed())
Expand Down Expand Up @@ -648,8 +691,11 @@ func TestPatchHelper(t *testing.T) {
t.Run("updating both spec, status, and adding a condition", func(t *testing.T) {
g := NewWithT(t)

ns, err := env.CreateNamespace(ctx, namespacePrefix)
g.Expect(err).NotTo(HaveOccurred())

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

t.Log("Creating the object")
g.Expect(env.Create(ctx, obj)).To(Succeed())
Expand All @@ -673,7 +719,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 +749,6 @@ func TestPatchHelper(t *testing.T) {
obj := &clusterv1.MachineSet{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-ms",
Namespace: "default",
},
Spec: clusterv1.MachineSetSpec{
ClusterName: "test1",
Expand All @@ -718,7 +763,11 @@ func TestPatchHelper(t *testing.T) {
t.Run("when updating spec", func(t *testing.T) {
g := NewWithT(t)

ns, err := env.CreateNamespace(ctx, namespacePrefix)
g.Expect(err).NotTo(HaveOccurred())

obj := obj.DeepCopy()
obj.ObjectMeta.Namespace = ns.Name

t.Log("Creating the MachineSet object")
g.Expect(env.Create(ctx, obj)).To(Succeed())
Expand Down Expand Up @@ -758,7 +807,11 @@ func TestPatchHelper(t *testing.T) {
t.Run("when updating spec, status, and metadata", func(t *testing.T) {
g := NewWithT(t)

ns, err := env.CreateNamespace(ctx, namespacePrefix)
g.Expect(err).NotTo(HaveOccurred())

obj := obj.DeepCopy()
obj.ObjectMeta.Namespace = ns.Name

t.Log("Creating the MachineSet object")
g.Expect(env.Create(ctx, obj)).To(Succeed())
Expand Down Expand Up @@ -808,7 +861,11 @@ func TestPatchHelper(t *testing.T) {
t.Run("without any changes", func(t *testing.T) {
g := NewWithT(t)

ns, err := env.CreateNamespace(ctx, namespacePrefix)
g.Expect(err).NotTo(HaveOccurred())

obj := obj.DeepCopy()
obj.ObjectMeta.Namespace = ns.Name

t.Log("Creating the MachineSet object")
g.Expect(env.Create(ctx, obj)).To(Succeed())
Expand Down Expand Up @@ -848,17 +905,20 @@ func TestPatchHelper(t *testing.T) {
t.Run("Should error if the object isn't the same", func(t *testing.T) {
g := NewWithT(t)

ns, err := env.CreateNamespace(ctx, namespacePrefix)
g.Expect(err).NotTo(HaveOccurred())

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
Loading

0 comments on commit 4a42134

Please sign in to comment.