...

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

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

     1  // Copyright 2020 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/apply/event"
    16  	"sigs.k8s.io/cli-utils/pkg/common"
    17  	"sigs.k8s.io/cli-utils/pkg/inventory"
    18  	"sigs.k8s.io/cli-utils/pkg/kstatus/status"
    19  	"sigs.k8s.io/cli-utils/pkg/object"
    20  	"sigs.k8s.io/cli-utils/pkg/testutil"
    21  	"sigs.k8s.io/cli-utils/test/e2e/e2eutil"
    22  	"sigs.k8s.io/cli-utils/test/e2e/invconfig"
    23  	"sigs.k8s.io/controller-runtime/pkg/client"
    24  )
    25  
    26  func dryRunTest(ctx context.Context, c client.Client, invConfig invconfig.InventoryConfig, inventoryName, namespaceName string) {
    27  	By("Apply with DryRun")
    28  	applier := invConfig.ApplierFactoryFunc()
    29  	inventoryID := fmt.Sprintf("%s-%s", inventoryName, namespaceName)
    30  
    31  	inventoryInfo := invconfig.CreateInventoryInfo(invConfig, inventoryName, namespaceName, inventoryID)
    32  
    33  	namespace1Name := fmt.Sprintf("%s-ns1", namespaceName)
    34  
    35  	fields := struct{ Namespace string }{Namespace: namespace1Name}
    36  	namespace1Obj := e2eutil.TemplateToUnstructured(namespaceTemplate, fields)
    37  	podBObj := e2eutil.TemplateToUnstructured(podBTemplate, fields)
    38  
    39  	// Dependency order: podB -> namespace1
    40  	// Apply order: namespace1, podB
    41  	resources := []*unstructured.Unstructured{
    42  		namespace1Obj,
    43  		podBObj,
    44  	}
    45  
    46  	applierEvents := e2eutil.RunCollect(applier.Run(ctx, inventoryInfo, resources, apply.ApplierOptions{
    47  		ReconcileTimeout: 2 * time.Minute,
    48  		EmitStatusEvents: true,
    49  		DryRunStrategy:   common.DryRunClient,
    50  	}))
    51  
    52  	expEvents := []testutil.ExpEvent{
    53  		{
    54  			// InitTask
    55  			EventType: event.InitType,
    56  			InitEvent: &testutil.ExpInitEvent{},
    57  		},
    58  		{
    59  			// InvAddTask start
    60  			EventType: event.ActionGroupType,
    61  			ActionGroupEvent: &testutil.ExpActionGroupEvent{
    62  				Action:    event.InventoryAction,
    63  				GroupName: "inventory-add-0",
    64  				Type:      event.Started,
    65  			},
    66  		},
    67  		{
    68  			// InvAddTask finished
    69  			EventType: event.ActionGroupType,
    70  			ActionGroupEvent: &testutil.ExpActionGroupEvent{
    71  				Action:    event.InventoryAction,
    72  				GroupName: "inventory-add-0",
    73  				Type:      event.Finished,
    74  			},
    75  		},
    76  		{
    77  			// ApplyTask start
    78  			EventType: event.ActionGroupType,
    79  			ActionGroupEvent: &testutil.ExpActionGroupEvent{
    80  				Action:    event.ApplyAction,
    81  				GroupName: "apply-0",
    82  				Type:      event.Started,
    83  			},
    84  		},
    85  		{
    86  			// Create namespace
    87  			EventType: event.ApplyType,
    88  			ApplyEvent: &testutil.ExpApplyEvent{
    89  				GroupName:  "apply-0",
    90  				Status:     event.ApplySuccessful,
    91  				Identifier: object.UnstructuredToObjMetadata(namespace1Obj),
    92  				Error:      nil,
    93  			},
    94  		},
    95  		{
    96  			// ApplyTask finished
    97  			EventType: event.ActionGroupType,
    98  			ActionGroupEvent: &testutil.ExpActionGroupEvent{
    99  				Action:    event.ApplyAction,
   100  				GroupName: "apply-0",
   101  				Type:      event.Finished,
   102  			},
   103  		},
   104  		{
   105  			// ApplyTask start
   106  			EventType: event.ActionGroupType,
   107  			ActionGroupEvent: &testutil.ExpActionGroupEvent{
   108  				Action:    event.ApplyAction,
   109  				GroupName: "apply-1",
   110  				Type:      event.Started,
   111  			},
   112  		},
   113  		{
   114  			// Create pod
   115  			EventType: event.ApplyType,
   116  			ApplyEvent: &testutil.ExpApplyEvent{
   117  				GroupName:  "apply-1",
   118  				Status:     event.ApplySuccessful,
   119  				Identifier: object.UnstructuredToObjMetadata(podBObj),
   120  				Error:      nil,
   121  			},
   122  		},
   123  		{
   124  			// ApplyTask finished
   125  			EventType: event.ActionGroupType,
   126  			ActionGroupEvent: &testutil.ExpActionGroupEvent{
   127  				Action:    event.ApplyAction,
   128  				GroupName: "apply-1",
   129  				Type:      event.Finished,
   130  			},
   131  		},
   132  		// No Wait Tasks for Dry Run
   133  		{
   134  			// InvSetTask start
   135  			EventType: event.ActionGroupType,
   136  			ActionGroupEvent: &testutil.ExpActionGroupEvent{
   137  				Action:    event.InventoryAction,
   138  				GroupName: "inventory-set-0",
   139  				Type:      event.Started,
   140  			},
   141  		},
   142  		{
   143  			// InvSetTask finished
   144  			EventType: event.ActionGroupType,
   145  			ActionGroupEvent: &testutil.ExpActionGroupEvent{
   146  				Action:    event.InventoryAction,
   147  				GroupName: "inventory-set-0",
   148  				Type:      event.Finished,
   149  			},
   150  		},
   151  	}
   152  	receivedEvents := testutil.EventsToExpEvents(applierEvents)
   153  
   154  	// handle optional async NotFound StatusEvent for pod
   155  	expected := testutil.ExpEvent{
   156  		EventType: event.StatusType,
   157  		StatusEvent: &testutil.ExpStatusEvent{
   158  			Identifier: object.UnstructuredToObjMetadata(podBObj),
   159  			Status:     status.NotFoundStatus,
   160  			Error:      nil,
   161  		},
   162  	}
   163  	receivedEvents, _ = testutil.RemoveEqualEvents(receivedEvents, expected)
   164  
   165  	// handle optional async NotFound StatusEvent for namespace
   166  	expected = testutil.ExpEvent{
   167  		EventType: event.StatusType,
   168  		StatusEvent: &testutil.ExpStatusEvent{
   169  			Identifier: object.UnstructuredToObjMetadata(namespace1Obj),
   170  			Status:     status.NotFoundStatus,
   171  			Error:      nil,
   172  		},
   173  	}
   174  	receivedEvents, _ = testutil.RemoveEqualEvents(receivedEvents, expected)
   175  
   176  	Expect(receivedEvents).To(testutil.Equal(expEvents))
   177  
   178  	By("Verify pod NotFound")
   179  	e2eutil.AssertUnstructuredDoesNotExist(ctx, c, podBObj)
   180  
   181  	By("Verify inventory NotFound")
   182  	invConfig.InvNotExistsFunc(ctx, c, inventoryName, namespaceName, inventoryID)
   183  
   184  	By("Apply")
   185  	e2eutil.RunWithNoErr(applier.Run(ctx, inventoryInfo, resources, apply.ApplierOptions{
   186  		ReconcileTimeout: 2 * time.Minute,
   187  	}))
   188  
   189  	By("Verify pod created")
   190  	e2eutil.AssertUnstructuredExists(ctx, c, podBObj)
   191  
   192  	By("Verify inventory size")
   193  	invConfig.InvSizeVerifyFunc(ctx, c, inventoryName, namespaceName, inventoryID, 2, 2)
   194  
   195  	By("Destroy with DryRun")
   196  	destroyer := invConfig.DestroyerFactoryFunc()
   197  
   198  	destroyerEvents := e2eutil.RunCollect(destroyer.Run(ctx, inventoryInfo, apply.DestroyerOptions{
   199  		InventoryPolicy:  inventory.PolicyAdoptIfNoInventory,
   200  		EmitStatusEvents: true,
   201  		DryRunStrategy:   common.DryRunClient,
   202  	}))
   203  
   204  	expEvents = []testutil.ExpEvent{
   205  		{
   206  			// InitTask
   207  			EventType: event.InitType,
   208  			InitEvent: &testutil.ExpInitEvent{},
   209  		},
   210  		{
   211  			// PruneTask start
   212  			EventType: event.ActionGroupType,
   213  			ActionGroupEvent: &testutil.ExpActionGroupEvent{
   214  				Action:    event.DeleteAction,
   215  				GroupName: "prune-0",
   216  				Type:      event.Started,
   217  			},
   218  		},
   219  		{
   220  			// Delete pod
   221  			EventType: event.DeleteType,
   222  			DeleteEvent: &testutil.ExpDeleteEvent{
   223  				GroupName:  "prune-0",
   224  				Status:     event.DeleteSuccessful,
   225  				Identifier: object.UnstructuredToObjMetadata(podBObj),
   226  				Error:      nil,
   227  			},
   228  		},
   229  		{
   230  			// PruneTask finished
   231  			EventType: event.ActionGroupType,
   232  			ActionGroupEvent: &testutil.ExpActionGroupEvent{
   233  				Action:    event.DeleteAction,
   234  				GroupName: "prune-0",
   235  				Type:      event.Finished,
   236  			},
   237  		},
   238  		{
   239  			// PruneTask start
   240  			EventType: event.ActionGroupType,
   241  			ActionGroupEvent: &testutil.ExpActionGroupEvent{
   242  				Action:    event.DeleteAction,
   243  				GroupName: "prune-1",
   244  				Type:      event.Started,
   245  			},
   246  		},
   247  		{
   248  			// Delete namespace
   249  			EventType: event.DeleteType,
   250  			DeleteEvent: &testutil.ExpDeleteEvent{
   251  				GroupName:  "prune-1",
   252  				Status:     event.DeleteSuccessful,
   253  				Identifier: object.UnstructuredToObjMetadata(namespace1Obj),
   254  				Error:      nil,
   255  			},
   256  		},
   257  		{
   258  			// PruneTask finished
   259  			EventType: event.ActionGroupType,
   260  			ActionGroupEvent: &testutil.ExpActionGroupEvent{
   261  				Action:    event.DeleteAction,
   262  				GroupName: "prune-1",
   263  				Type:      event.Finished,
   264  			},
   265  		},
   266  		// No Wait Tasks for Dry Run
   267  		{
   268  			// DeleteInvTask start
   269  			EventType: event.ActionGroupType,
   270  			ActionGroupEvent: &testutil.ExpActionGroupEvent{
   271  				Action:    event.InventoryAction,
   272  				GroupName: "delete-inventory-0",
   273  				Type:      event.Started,
   274  			},
   275  		},
   276  		{
   277  			// DeleteInvTask finished
   278  			EventType: event.ActionGroupType,
   279  			ActionGroupEvent: &testutil.ExpActionGroupEvent{
   280  				Action:    event.InventoryAction,
   281  				GroupName: "delete-inventory-0",
   282  				Type:      event.Finished,
   283  			},
   284  		},
   285  	}
   286  	Expect(testutil.EventsToExpEvents(destroyerEvents)).To(testutil.Equal(expEvents))
   287  
   288  	By("Verify pod still exists")
   289  	e2eutil.AssertUnstructuredExists(ctx, c, podBObj)
   290  
   291  	By("Destroy")
   292  	e2eutil.RunWithNoErr(destroyer.Run(ctx, inventoryInfo, apply.DestroyerOptions{
   293  		InventoryPolicy: inventory.PolicyAdoptIfNoInventory,
   294  	}))
   295  
   296  	By("Verify pod deleted")
   297  	e2eutil.AssertUnstructuredDoesNotExist(ctx, c, podBObj)
   298  
   299  	By("Verify inventory deleted")
   300  	invConfig.InvNotExistsFunc(ctx, c, inventoryName, namespaceName, inventoryID)
   301  }
   302  

View as plain text