Skip to content

Commit

Permalink
util pkg tests delete namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
GrigoriyMikhalkin committed Jul 19, 2021
1 parent 4a42134 commit 7d9829f
Showing 1 changed file with 23 additions and 77 deletions.
100 changes: 23 additions & 77 deletions util/patch/patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,31 @@ import (
)

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

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

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())
defer func() {
Expand Down Expand Up @@ -112,9 +117,6 @@ func TestPatchHelper(t *testing.T) {
t.Run("on a corev1.Node object", func(t *testing.T) {
g := NewWithT(t)

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

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 @@ -219,11 +218,7 @@ 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 @@ -277,11 +272,7 @@ 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 @@ -342,11 +333,7 @@ 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 @@ -394,11 +381,7 @@ 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 @@ -449,11 +432,7 @@ 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 @@ -507,17 +486,14 @@ func TestPatchHelper(t *testing.T) {
obj := &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-",
Namespace: ns.Name,
},
}

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 @@ -556,12 +532,8 @@ 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 @@ -600,11 +572,7 @@ 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 = ns.Name

t.Log("Creating the object")
g.Expect(env.Create(ctx, obj)).To(Succeed())
Expand Down Expand Up @@ -649,11 +617,7 @@ 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 @@ -691,11 +655,7 @@ 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 = ns.Name

t.Log("Creating the object")
g.Expect(env.Create(ctx, obj)).To(Succeed())
Expand Down Expand Up @@ -749,6 +709,7 @@ func TestPatchHelper(t *testing.T) {
obj := &clusterv1.MachineSet{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-ms",
Namespace: ns.Name,
},
Spec: clusterv1.MachineSetSpec{
ClusterName: "test1",
Expand All @@ -763,11 +724,7 @@ 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 @@ -807,11 +764,7 @@ 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 @@ -861,11 +814,7 @@ 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 @@ -905,9 +854,6 @@ 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-",
Expand Down

0 comments on commit 7d9829f

Please sign in to comment.