mirror of https://github.com/fluxcd/cli-utils.git
304 lines
8.4 KiB
Go
304 lines
8.4 KiB
Go
// Copyright 2020 The Kubernetes Authors.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package e2e
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
. "github.com/onsi/gomega"
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
"sigs.k8s.io/cli-utils/pkg/apply"
|
|
"sigs.k8s.io/cli-utils/pkg/apply/event"
|
|
"sigs.k8s.io/cli-utils/pkg/inventory"
|
|
"sigs.k8s.io/cli-utils/pkg/kstatus/status"
|
|
"sigs.k8s.io/cli-utils/pkg/object"
|
|
"sigs.k8s.io/cli-utils/pkg/testutil"
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
)
|
|
|
|
func applyAndDestroyTest(ctx context.Context, c client.Client, invConfig InventoryConfig, inventoryName, namespaceName string) {
|
|
By("Apply resources")
|
|
applier := invConfig.ApplierFactoryFunc()
|
|
inventoryID := fmt.Sprintf("%s-%s", inventoryName, namespaceName)
|
|
|
|
inventoryInfo := createInventoryInfo(invConfig, inventoryName, namespaceName, inventoryID)
|
|
|
|
deployment1Obj := withNamespace(manifestToUnstructured(deployment1), namespaceName)
|
|
resources := []*unstructured.Unstructured{
|
|
deployment1Obj,
|
|
}
|
|
|
|
applierEvents := runCollect(applier.Run(ctx, inventoryInfo, resources, apply.ApplierOptions{
|
|
ReconcileTimeout: 2 * time.Minute,
|
|
EmitStatusEvents: true,
|
|
}))
|
|
|
|
expEvents := []testutil.ExpEvent{
|
|
{
|
|
// InitTask
|
|
EventType: event.InitType,
|
|
InitEvent: &testutil.ExpInitEvent{},
|
|
},
|
|
{
|
|
// InvAddTask start
|
|
EventType: event.ActionGroupType,
|
|
ActionGroupEvent: &testutil.ExpActionGroupEvent{
|
|
Action: event.InventoryAction,
|
|
GroupName: "inventory-add-0",
|
|
Type: event.Started,
|
|
},
|
|
},
|
|
{
|
|
// InvAddTask finished
|
|
EventType: event.ActionGroupType,
|
|
ActionGroupEvent: &testutil.ExpActionGroupEvent{
|
|
Action: event.InventoryAction,
|
|
GroupName: "inventory-add-0",
|
|
Type: event.Finished,
|
|
},
|
|
},
|
|
{
|
|
// ApplyTask start
|
|
EventType: event.ActionGroupType,
|
|
ActionGroupEvent: &testutil.ExpActionGroupEvent{
|
|
Action: event.ApplyAction,
|
|
GroupName: "apply-0",
|
|
Type: event.Started,
|
|
},
|
|
},
|
|
{
|
|
// Create deployment
|
|
EventType: event.ApplyType,
|
|
ApplyEvent: &testutil.ExpApplyEvent{
|
|
GroupName: "apply-0",
|
|
Operation: event.Created,
|
|
Identifier: object.UnstructuredToObjMetadata(deployment1Obj),
|
|
Error: nil,
|
|
},
|
|
},
|
|
{
|
|
// ApplyTask finished
|
|
EventType: event.ActionGroupType,
|
|
ActionGroupEvent: &testutil.ExpActionGroupEvent{
|
|
Action: event.ApplyAction,
|
|
GroupName: "apply-0",
|
|
Type: event.Finished,
|
|
},
|
|
},
|
|
{
|
|
// WaitTask start
|
|
EventType: event.ActionGroupType,
|
|
ActionGroupEvent: &testutil.ExpActionGroupEvent{
|
|
Action: event.WaitAction,
|
|
GroupName: "wait-0",
|
|
Type: event.Started,
|
|
},
|
|
},
|
|
{
|
|
// Deployment reconcile Pending .
|
|
EventType: event.WaitType,
|
|
WaitEvent: &testutil.ExpWaitEvent{
|
|
GroupName: "wait-0",
|
|
Operation: event.ReconcilePending,
|
|
Identifier: object.UnstructuredToObjMetadata(deployment1Obj),
|
|
},
|
|
},
|
|
{
|
|
// Deployment confirmed Current.
|
|
EventType: event.WaitType,
|
|
WaitEvent: &testutil.ExpWaitEvent{
|
|
GroupName: "wait-0",
|
|
Operation: event.Reconciled,
|
|
Identifier: object.UnstructuredToObjMetadata(deployment1Obj),
|
|
},
|
|
},
|
|
{
|
|
// WaitTask finished
|
|
EventType: event.ActionGroupType,
|
|
ActionGroupEvent: &testutil.ExpActionGroupEvent{
|
|
Action: event.WaitAction,
|
|
GroupName: "wait-0",
|
|
Type: event.Finished,
|
|
},
|
|
},
|
|
{
|
|
// InvSetTask start
|
|
EventType: event.ActionGroupType,
|
|
ActionGroupEvent: &testutil.ExpActionGroupEvent{
|
|
Action: event.InventoryAction,
|
|
GroupName: "inventory-set-0",
|
|
Type: event.Started,
|
|
},
|
|
},
|
|
{
|
|
// InvSetTask finished
|
|
EventType: event.ActionGroupType,
|
|
ActionGroupEvent: &testutil.ExpActionGroupEvent{
|
|
Action: event.InventoryAction,
|
|
GroupName: "inventory-set-0",
|
|
Type: event.Finished,
|
|
},
|
|
},
|
|
}
|
|
received := testutil.EventsToExpEvents(applierEvents)
|
|
|
|
// handle required async NotFound StatusEvents
|
|
expected := testutil.ExpEvent{
|
|
EventType: event.StatusType,
|
|
StatusEvent: &testutil.ExpStatusEvent{
|
|
Identifier: object.UnstructuredToObjMetadata(deployment1Obj),
|
|
Status: status.NotFoundStatus,
|
|
Error: nil,
|
|
},
|
|
}
|
|
received, matches := testutil.RemoveEqualEvents(received, expected)
|
|
Expect(matches).To(BeNumerically(">=", 1), "unexpected number of %q status events", status.NotFoundStatus)
|
|
|
|
// handle optional async InProgress StatusEvents
|
|
expected = testutil.ExpEvent{
|
|
EventType: event.StatusType,
|
|
StatusEvent: &testutil.ExpStatusEvent{
|
|
Identifier: object.UnstructuredToObjMetadata(deployment1Obj),
|
|
Status: status.InProgressStatus,
|
|
Error: nil,
|
|
},
|
|
}
|
|
received, _ = testutil.RemoveEqualEvents(received, expected)
|
|
|
|
// handle required async Current StatusEvents
|
|
expected = testutil.ExpEvent{
|
|
EventType: event.StatusType,
|
|
StatusEvent: &testutil.ExpStatusEvent{
|
|
Identifier: object.UnstructuredToObjMetadata(deployment1Obj),
|
|
Status: status.CurrentStatus,
|
|
Error: nil,
|
|
},
|
|
}
|
|
received, matches = testutil.RemoveEqualEvents(received, expected)
|
|
Expect(matches).To(BeNumerically(">=", 1), "unexpected number of %q status events", status.CurrentStatus)
|
|
|
|
Expect(received).To(testutil.Equal(expEvents))
|
|
|
|
By("Verify deployment created")
|
|
assertUnstructuredExists(ctx, c, deployment1Obj)
|
|
|
|
By("Verify inventory")
|
|
invConfig.InvSizeVerifyFunc(ctx, c, inventoryName, namespaceName, inventoryID, 1)
|
|
|
|
By("Destroy resources")
|
|
destroyer := invConfig.DestroyerFactoryFunc()
|
|
|
|
options := apply.DestroyerOptions{InventoryPolicy: inventory.PolicyAdoptIfNoInventory}
|
|
destroyerEvents := runCollect(destroyer.Run(ctx, inventoryInfo, options))
|
|
|
|
expEvents = []testutil.ExpEvent{
|
|
{
|
|
// InitTask
|
|
EventType: event.InitType,
|
|
InitEvent: &testutil.ExpInitEvent{},
|
|
},
|
|
{
|
|
// PruneTask start
|
|
EventType: event.ActionGroupType,
|
|
ActionGroupEvent: &testutil.ExpActionGroupEvent{
|
|
Action: event.DeleteAction,
|
|
GroupName: "prune-0",
|
|
Type: event.Started,
|
|
},
|
|
},
|
|
{
|
|
// Delete deployment
|
|
EventType: event.DeleteType,
|
|
DeleteEvent: &testutil.ExpDeleteEvent{
|
|
GroupName: "prune-0",
|
|
Operation: event.Deleted,
|
|
Identifier: object.UnstructuredToObjMetadata(deployment1Obj),
|
|
Error: nil,
|
|
},
|
|
},
|
|
{
|
|
// PruneTask finished
|
|
EventType: event.ActionGroupType,
|
|
ActionGroupEvent: &testutil.ExpActionGroupEvent{
|
|
Action: event.DeleteAction,
|
|
GroupName: "prune-0",
|
|
Type: event.Finished,
|
|
},
|
|
},
|
|
{
|
|
// WaitTask start
|
|
EventType: event.ActionGroupType,
|
|
ActionGroupEvent: &testutil.ExpActionGroupEvent{
|
|
Action: event.WaitAction,
|
|
GroupName: "wait-0",
|
|
Type: event.Started,
|
|
},
|
|
},
|
|
{
|
|
// Deployment reconcile Pending .
|
|
EventType: event.WaitType,
|
|
WaitEvent: &testutil.ExpWaitEvent{
|
|
GroupName: "wait-0",
|
|
Operation: event.ReconcilePending,
|
|
Identifier: object.UnstructuredToObjMetadata(deployment1Obj),
|
|
},
|
|
},
|
|
{
|
|
// Deployment confirmed NotFound.
|
|
EventType: event.WaitType,
|
|
WaitEvent: &testutil.ExpWaitEvent{
|
|
GroupName: "wait-0",
|
|
Operation: event.Reconciled,
|
|
Identifier: object.UnstructuredToObjMetadata(deployment1Obj),
|
|
},
|
|
},
|
|
{
|
|
// WaitTask finished
|
|
EventType: event.ActionGroupType,
|
|
ActionGroupEvent: &testutil.ExpActionGroupEvent{
|
|
Action: event.WaitAction,
|
|
GroupName: "wait-0",
|
|
Type: event.Finished,
|
|
},
|
|
},
|
|
{
|
|
// DeleteInvTask start
|
|
EventType: event.ActionGroupType,
|
|
ActionGroupEvent: &testutil.ExpActionGroupEvent{
|
|
Action: event.InventoryAction,
|
|
GroupName: "delete-inventory-0",
|
|
Type: event.Started,
|
|
},
|
|
},
|
|
{
|
|
// DeleteInvTask finished
|
|
EventType: event.ActionGroupType,
|
|
ActionGroupEvent: &testutil.ExpActionGroupEvent{
|
|
Action: event.InventoryAction,
|
|
GroupName: "delete-inventory-0",
|
|
Type: event.Finished,
|
|
},
|
|
},
|
|
}
|
|
|
|
Expect(testutil.EventsToExpEvents(destroyerEvents)).To(testutil.Equal(expEvents))
|
|
|
|
By("Verify deployment deleted")
|
|
assertUnstructuredDoesNotExist(ctx, c, deployment1Obj)
|
|
}
|
|
|
|
func createInventoryInfo(invConfig InventoryConfig, inventoryName, namespaceName, inventoryID string) inventory.Info {
|
|
switch invConfig.Strategy {
|
|
case inventory.NameStrategy:
|
|
return invConfig.InvWrapperFunc(invConfig.FactoryFunc(inventoryName, namespaceName, randomString("inventory-")))
|
|
case inventory.LabelStrategy:
|
|
return invConfig.InvWrapperFunc(invConfig.FactoryFunc(randomString("inventory-"), namespaceName, inventoryID))
|
|
default:
|
|
panic(fmt.Errorf("unknown inventory strategy %q", invConfig.Strategy))
|
|
}
|
|
}
|