...

Source file src/sigs.k8s.io/cli-utils/test/e2e/deletion_prevention_test.go

Documentation: sigs.k8s.io/cli-utils/test/e2e

     1  // Copyright 2021 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package e2e
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"time"
    10  
    11  	. "github.com/onsi/ginkgo/v2"
    12  	. "github.com/onsi/gomega"
    13  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    14  	"sigs.k8s.io/cli-utils/pkg/apply"
    15  	"sigs.k8s.io/cli-utils/pkg/common"
    16  	"sigs.k8s.io/cli-utils/pkg/inventory"
    17  	"sigs.k8s.io/cli-utils/test/e2e/e2eutil"
    18  	"sigs.k8s.io/cli-utils/test/e2e/invconfig"
    19  	"sigs.k8s.io/controller-runtime/pkg/client"
    20  )
    21  
    22  func deletionPreventionTest(ctx context.Context, c client.Client, invConfig invconfig.InventoryConfig, inventoryName, namespaceName string) {
    23  	By("Apply resources")
    24  	applier := invConfig.ApplierFactoryFunc()
    25  	inventoryID := fmt.Sprintf("%s-%s", inventoryName, namespaceName)
    26  
    27  	inventoryInfo := invconfig.CreateInventoryInfo(invConfig, inventoryName, namespaceName, inventoryID)
    28  
    29  	resources := []*unstructured.Unstructured{
    30  		e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(deployment1), namespaceName),
    31  		e2eutil.WithAnnotation(e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(pod1), namespaceName), common.OnRemoveAnnotation, common.OnRemoveKeep),
    32  		e2eutil.WithAnnotation(e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(pod2), namespaceName), common.LifecycleDeleteAnnotation, common.PreventDeletion),
    33  	}
    34  
    35  	e2eutil.RunCollect(applier.Run(ctx, inventoryInfo, resources, apply.ApplierOptions{
    36  		ReconcileTimeout: 2 * time.Minute,
    37  	}))
    38  
    39  	By("Verify deployment created")
    40  	obj := e2eutil.AssertUnstructuredExists(ctx, c, e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(deployment1), namespaceName))
    41  	Expect(obj.GetAnnotations()[inventory.OwningInventoryKey]).To(Equal(inventoryInfo.ID()))
    42  
    43  	By("Verify pod1 created")
    44  	obj = e2eutil.AssertUnstructuredExists(ctx, c, e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(pod1), namespaceName))
    45  	Expect(obj.GetAnnotations()[inventory.OwningInventoryKey]).To(Equal(inventoryInfo.ID()))
    46  
    47  	By("Verify pod2 created")
    48  	obj = e2eutil.AssertUnstructuredExists(ctx, c, e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(pod2), namespaceName))
    49  	Expect(obj.GetAnnotations()[inventory.OwningInventoryKey]).To(Equal(inventoryInfo.ID()))
    50  
    51  	By("Verify the inventory size is 3")
    52  	invConfig.InvSizeVerifyFunc(ctx, c, inventoryName, namespaceName, inventoryID, 3, 3)
    53  
    54  	By("Dry-run apply resources")
    55  	resources = []*unstructured.Unstructured{
    56  		e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(deployment1), namespaceName),
    57  	}
    58  
    59  	e2eutil.RunCollect(applier.Run(ctx, inventoryInfo, resources, apply.ApplierOptions{
    60  		ReconcileTimeout: 2 * time.Minute,
    61  		DryRunStrategy:   common.DryRunClient,
    62  	}))
    63  
    64  	By("Verify deployment still exists and has the config.k8s.io/owning-inventory annotation")
    65  	obj = e2eutil.AssertUnstructuredExists(ctx, c, e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(deployment1), namespaceName))
    66  	Expect(obj.GetAnnotations()[inventory.OwningInventoryKey]).To(Equal(inventoryInfo.ID()))
    67  
    68  	By("Verify pod1 still exits and does not have the config.k8s.io/owning-inventory annotation")
    69  	obj = e2eutil.AssertUnstructuredExists(ctx, c, e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(pod1), namespaceName))
    70  	Expect(obj.GetAnnotations()[inventory.OwningInventoryKey]).To(Equal(inventoryInfo.ID()))
    71  
    72  	By("Verify pod2 still exits and does not have the config.k8s.io/owning-inventory annotation")
    73  	obj = e2eutil.AssertUnstructuredExists(ctx, c, e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(pod2), namespaceName))
    74  	Expect(obj.GetAnnotations()[inventory.OwningInventoryKey]).To(Equal(inventoryInfo.ID()))
    75  
    76  	By("Verify the inventory size is still 3")
    77  	invConfig.InvSizeVerifyFunc(ctx, c, inventoryName, namespaceName, inventoryID, 3, 3)
    78  
    79  	By("Apply resources")
    80  	resources = []*unstructured.Unstructured{
    81  		e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(deployment1), namespaceName),
    82  	}
    83  
    84  	e2eutil.RunCollect(applier.Run(ctx, inventoryInfo, resources, apply.ApplierOptions{
    85  		ReconcileTimeout: 2 * time.Minute,
    86  	}))
    87  
    88  	By("Verify deployment still exists and has the config.k8s.io/owning-inventory annotation")
    89  	obj = e2eutil.AssertUnstructuredExists(ctx, c, e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(deployment1), namespaceName))
    90  	Expect(obj.GetAnnotations()[inventory.OwningInventoryKey]).To(Equal(inventoryInfo.ID()))
    91  
    92  	By("Verify pod1 still exits and does not have the config.k8s.io/owning-inventory annotation")
    93  	obj = e2eutil.AssertUnstructuredExists(ctx, c, e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(pod1), namespaceName))
    94  	Expect(obj.GetAnnotations()[inventory.OwningInventoryKey]).To(Equal(""))
    95  
    96  	By("Verify pod2 still exits and does not have the config.k8s.io/owning-inventory annotation")
    97  	obj = e2eutil.AssertUnstructuredExists(ctx, c, e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(pod2), namespaceName))
    98  	Expect(obj.GetAnnotations()[inventory.OwningInventoryKey]).To(Equal(""))
    99  
   100  	By("Verify the inventory size is 1")
   101  	invConfig.InvSizeVerifyFunc(ctx, c, inventoryName, namespaceName, inventoryID, 1, 3)
   102  }
   103  

View as plain text