...

Source file src/sigs.k8s.io/cli-utils/test/e2e/continue_on_error_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  
     9  	. "github.com/onsi/ginkgo/v2"
    10  	. "github.com/onsi/gomega"
    11  
    12  	"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
    13  	apierrors "k8s.io/apimachinery/pkg/api/errors"
    14  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    15  	"k8s.io/apimachinery/pkg/util/validation/field"
    16  	cmdutil "k8s.io/kubectl/pkg/cmd/util"
    17  	"sigs.k8s.io/cli-utils/pkg/apply"
    18  	applyerror "sigs.k8s.io/cli-utils/pkg/apply/error"
    19  	"sigs.k8s.io/cli-utils/pkg/apply/event"
    20  	"sigs.k8s.io/cli-utils/pkg/object"
    21  	"sigs.k8s.io/cli-utils/pkg/testutil"
    22  	"sigs.k8s.io/cli-utils/test/e2e/e2eutil"
    23  	"sigs.k8s.io/cli-utils/test/e2e/invconfig"
    24  	"sigs.k8s.io/controller-runtime/pkg/client"
    25  )
    26  
    27  func continueOnErrorTest(ctx context.Context, c client.Client, invConfig invconfig.InventoryConfig, inventoryName, namespaceName string) {
    28  	By("apply an invalid CRD")
    29  	applier := invConfig.ApplierFactoryFunc()
    30  
    31  	inv := invConfig.InvWrapperFunc(invConfig.FactoryFunc(inventoryName, namespaceName, "test"))
    32  
    33  	invalidCrdObj := e2eutil.ManifestToUnstructured(invalidCrd)
    34  	pod1Obj := e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(pod1), namespaceName)
    35  	resources := []*unstructured.Unstructured{
    36  		invalidCrdObj,
    37  		pod1Obj,
    38  	}
    39  
    40  	applierEvents := e2eutil.RunCollect(applier.Run(ctx, inv, resources, apply.ApplierOptions{}))
    41  
    42  	expEvents := []testutil.ExpEvent{
    43  		{
    44  			// InitTask
    45  			EventType: event.InitType,
    46  			InitEvent: &testutil.ExpInitEvent{},
    47  		},
    48  		{
    49  			// InvAddTask start
    50  			EventType: event.ActionGroupType,
    51  			ActionGroupEvent: &testutil.ExpActionGroupEvent{
    52  				Action:    event.InventoryAction,
    53  				GroupName: "inventory-add-0",
    54  				Type:      event.Started,
    55  			},
    56  		},
    57  		{
    58  			// InvAddTask finished
    59  			EventType: event.ActionGroupType,
    60  			ActionGroupEvent: &testutil.ExpActionGroupEvent{
    61  				Action:    event.InventoryAction,
    62  				GroupName: "inventory-add-0",
    63  				Type:      event.Finished,
    64  			},
    65  		},
    66  		{
    67  			// ApplyTask start
    68  			EventType: event.ActionGroupType,
    69  			ActionGroupEvent: &testutil.ExpActionGroupEvent{
    70  				Action:    event.ApplyAction,
    71  				GroupName: "apply-0",
    72  				Type:      event.Started,
    73  			},
    74  		},
    75  		{
    76  			// Apply invalidCrd fails
    77  			EventType: event.ApplyType,
    78  			ApplyEvent: &testutil.ExpApplyEvent{
    79  				GroupName:  "apply-0",
    80  				Status:     event.ApplyFailed,
    81  				Identifier: object.UnstructuredToObjMetadata(invalidCrdObj),
    82  				Error: testutil.EqualError(
    83  					applyerror.NewApplyRunError(
    84  						cmdutil.AddSourceToErr(
    85  							"creating",
    86  							"unstructured",
    87  							apierrors.NewInvalid(
    88  								invalidCrdObj.GroupVersionKind().GroupKind(),
    89  								invalidCrdObj.GetName(),
    90  								field.ErrorList{
    91  									&field.Error{
    92  										Type:     field.ErrorTypeInvalid,
    93  										Field:    "spec.versions",
    94  										BadValue: []apiextensions.CustomResourceDefinitionVersion(nil),
    95  										Detail:   "must have exactly one version marked as storage version",
    96  									},
    97  									&field.Error{
    98  										Type:     field.ErrorTypeInvalid,
    99  										Field:    "status.storedVersions",
   100  										BadValue: []string(nil),
   101  										Detail:   "must have at least one stored version",
   102  									},
   103  								},
   104  							),
   105  						),
   106  					),
   107  				),
   108  			},
   109  		},
   110  		{
   111  			// Create pod1
   112  			EventType: event.ApplyType,
   113  			ApplyEvent: &testutil.ExpApplyEvent{
   114  				GroupName:  "apply-0",
   115  				Status:     event.ApplySuccessful,
   116  				Identifier: object.UnstructuredToObjMetadata(pod1Obj),
   117  				Error:      nil,
   118  			},
   119  		},
   120  		{
   121  			// ApplyTask finished
   122  			EventType: event.ActionGroupType,
   123  			ActionGroupEvent: &testutil.ExpActionGroupEvent{
   124  				Action:    event.ApplyAction,
   125  				GroupName: "apply-0",
   126  				Type:      event.Finished,
   127  			},
   128  		},
   129  		{
   130  			// WaitTask start
   131  			EventType: event.ActionGroupType,
   132  			ActionGroupEvent: &testutil.ExpActionGroupEvent{
   133  				Action:    event.WaitAction,
   134  				GroupName: "wait-0",
   135  				Type:      event.Started,
   136  			},
   137  		},
   138  		{
   139  			// Pod1 reconcile Pending.
   140  			EventType: event.WaitType,
   141  			WaitEvent: &testutil.ExpWaitEvent{
   142  				GroupName:  "wait-0",
   143  				Status:     event.ReconcilePending,
   144  				Identifier: object.UnstructuredToObjMetadata(pod1Obj),
   145  			},
   146  		},
   147  		{
   148  			// CRD reconcile Skipped.
   149  			EventType: event.WaitType,
   150  			WaitEvent: &testutil.ExpWaitEvent{
   151  				GroupName:  "wait-0",
   152  				Status:     event.ReconcileSkipped,
   153  				Identifier: object.UnstructuredToObjMetadata(invalidCrdObj),
   154  			},
   155  		},
   156  		{
   157  			// Pod1 reconcile Successful.
   158  			EventType: event.WaitType,
   159  			WaitEvent: &testutil.ExpWaitEvent{
   160  				GroupName:  "wait-0",
   161  				Status:     event.ReconcileSuccessful,
   162  				Identifier: object.UnstructuredToObjMetadata(pod1Obj),
   163  			},
   164  		},
   165  		{
   166  			// WaitTask finished
   167  			EventType: event.ActionGroupType,
   168  			ActionGroupEvent: &testutil.ExpActionGroupEvent{
   169  				Action:    event.WaitAction,
   170  				GroupName: "wait-0",
   171  				Type:      event.Finished,
   172  			},
   173  		},
   174  		{
   175  			// InvSetTask start
   176  			EventType: event.ActionGroupType,
   177  			ActionGroupEvent: &testutil.ExpActionGroupEvent{
   178  				Action:    event.InventoryAction,
   179  				GroupName: "inventory-set-0",
   180  				Type:      event.Started,
   181  			},
   182  		},
   183  		{
   184  			// InvSetTask finished
   185  			EventType: event.ActionGroupType,
   186  			ActionGroupEvent: &testutil.ExpActionGroupEvent{
   187  				Action:    event.InventoryAction,
   188  				GroupName: "inventory-set-0",
   189  				Type:      event.Finished,
   190  			},
   191  		},
   192  	}
   193  	receivedEvents := testutil.EventsToExpEvents(applierEvents)
   194  
   195  	expEvents, receivedEvents = e2eutil.FilterOptionalEvents(expEvents, receivedEvents)
   196  
   197  	// sort to allow comparison of multiple ApplyTasks in the same task group
   198  	testutil.SortExpEvents(receivedEvents)
   199  
   200  	Expect(receivedEvents).To(testutil.Equal(expEvents))
   201  
   202  	By("Verify pod1 created")
   203  	e2eutil.AssertUnstructuredExists(ctx, c, pod1Obj)
   204  
   205  	By("Verify CRD not created")
   206  	e2eutil.AssertUnstructuredDoesNotExist(ctx, c, invalidCrdObj)
   207  }
   208  

View as plain text