mirror of https://github.com/fluxcd/cli-utils.git
347 lines
9.6 KiB
Go
347 lines
9.6 KiB
Go
// Copyright 2020 The Kubernetes Authors.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package e2e
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
. "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/object"
|
|
"sigs.k8s.io/cli-utils/pkg/testutil"
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
)
|
|
|
|
// High priority use cases:
|
|
// - IAMPolicyMember .spec.member injected with apply-time-mutation using a name
|
|
// that contains Project .status.number
|
|
// https://github.com/GoogleCloudPlatform/k8s-config-connector/issues/340
|
|
// - Service .spec.loadBalancerIP injected with apply-time-mutation from
|
|
// ComputeAddress .spec.address
|
|
// https://github.com/GoogleCloudPlatform/k8s-config-connector/issues/334
|
|
//
|
|
// However, since both of these use Config Connector resources, which use CRDs
|
|
// that are copyright to Google, we can't use them as e2e tests here. Instead,
|
|
// we test a toy example with a pod-a depending on pod-b, injecting the ip and
|
|
// port from pod-b into an environment variable of pod-a.
|
|
|
|
func mutationTest(c client.Client, invConfig InventoryConfig, inventoryName, namespaceName string) {
|
|
By("apply resources in order with substitutions based on apply-time-mutation annotation")
|
|
applier := invConfig.ApplierFactoryFunc()
|
|
|
|
inv := invConfig.InvWrapperFunc(invConfig.InventoryFactoryFunc(inventoryName, namespaceName, "test"))
|
|
|
|
// Dependency order: podA -> podB
|
|
// Apply order: podB, podA
|
|
resources := []*unstructured.Unstructured{
|
|
withNamespace(manifestToUnstructured(podA), namespaceName),
|
|
withNamespace(manifestToUnstructured(podB), namespaceName),
|
|
}
|
|
|
|
ch := applier.Run(context.TODO(), inv, resources, apply.Options{
|
|
EmitStatusEvents: false,
|
|
})
|
|
|
|
var applierEvents []event.Event
|
|
for e := range ch {
|
|
applierEvents = append(applierEvents, e)
|
|
}
|
|
expEvents := []testutil.ExpEvent{
|
|
{
|
|
// InitTask
|
|
EventType: event.InitType,
|
|
InitEvent: &testutil.ExpInitEvent{},
|
|
},
|
|
{
|
|
// InvAddTask start
|
|
EventType: event.ActionGroupType,
|
|
ActionGroupEvent: &testutil.ExpActionGroupEvent{
|
|
Action: event.InventoryAction,
|
|
Name: "inventory-add-0",
|
|
Type: event.Started,
|
|
},
|
|
},
|
|
{
|
|
// InvAddTask finished
|
|
EventType: event.ActionGroupType,
|
|
ActionGroupEvent: &testutil.ExpActionGroupEvent{
|
|
Action: event.InventoryAction,
|
|
Name: "inventory-add-0",
|
|
Type: event.Finished,
|
|
},
|
|
},
|
|
{
|
|
// ApplyTask start
|
|
EventType: event.ActionGroupType,
|
|
ActionGroupEvent: &testutil.ExpActionGroupEvent{
|
|
Action: event.ApplyAction,
|
|
Name: "apply-0",
|
|
Type: event.Started,
|
|
},
|
|
},
|
|
{
|
|
// Apply PodB first
|
|
EventType: event.ApplyType,
|
|
ApplyEvent: &testutil.ExpApplyEvent{
|
|
Operation: event.Created,
|
|
Identifier: object.UnstructuredToObjMetaOrDie(withNamespace(manifestToUnstructured(podB), namespaceName)),
|
|
Error: nil,
|
|
},
|
|
},
|
|
{
|
|
// ApplyTask finished
|
|
EventType: event.ActionGroupType,
|
|
ActionGroupEvent: &testutil.ExpActionGroupEvent{
|
|
Action: event.ApplyAction,
|
|
Name: "apply-0",
|
|
Type: event.Finished,
|
|
},
|
|
},
|
|
{
|
|
// WaitTask start
|
|
EventType: event.ActionGroupType,
|
|
ActionGroupEvent: &testutil.ExpActionGroupEvent{
|
|
Action: event.WaitAction,
|
|
Name: "wait-0",
|
|
Type: event.Started,
|
|
},
|
|
},
|
|
{
|
|
// WaitTask finished
|
|
EventType: event.ActionGroupType,
|
|
ActionGroupEvent: &testutil.ExpActionGroupEvent{
|
|
Action: event.WaitAction,
|
|
Name: "wait-0",
|
|
Type: event.Finished,
|
|
},
|
|
},
|
|
{
|
|
// ApplyTask start
|
|
EventType: event.ActionGroupType,
|
|
ActionGroupEvent: &testutil.ExpActionGroupEvent{
|
|
Action: event.ApplyAction,
|
|
Name: "apply-1",
|
|
Type: event.Started,
|
|
},
|
|
},
|
|
{
|
|
// Apply pod3 second
|
|
EventType: event.ApplyType,
|
|
ApplyEvent: &testutil.ExpApplyEvent{
|
|
Operation: event.Created,
|
|
Identifier: object.UnstructuredToObjMetaOrDie(withNamespace(manifestToUnstructured(podA), namespaceName)),
|
|
Error: nil,
|
|
},
|
|
},
|
|
{
|
|
// ApplyTask finished
|
|
EventType: event.ActionGroupType,
|
|
ActionGroupEvent: &testutil.ExpActionGroupEvent{
|
|
Action: event.ApplyAction,
|
|
Name: "apply-1",
|
|
Type: event.Finished,
|
|
},
|
|
},
|
|
{
|
|
// WaitTask start
|
|
EventType: event.ActionGroupType,
|
|
ActionGroupEvent: &testutil.ExpActionGroupEvent{
|
|
Action: event.WaitAction,
|
|
Name: "wait-1",
|
|
Type: event.Started,
|
|
},
|
|
},
|
|
{
|
|
// WaitTask finished
|
|
EventType: event.ActionGroupType,
|
|
ActionGroupEvent: &testutil.ExpActionGroupEvent{
|
|
Action: event.WaitAction,
|
|
Name: "wait-1",
|
|
Type: event.Finished,
|
|
},
|
|
},
|
|
{
|
|
// InvSetTask start
|
|
EventType: event.ActionGroupType,
|
|
ActionGroupEvent: &testutil.ExpActionGroupEvent{
|
|
Action: event.InventoryAction,
|
|
Name: "inventory-set-0",
|
|
Type: event.Started,
|
|
},
|
|
},
|
|
{
|
|
// InvSetTask finished
|
|
EventType: event.ActionGroupType,
|
|
ActionGroupEvent: &testutil.ExpActionGroupEvent{
|
|
Action: event.InventoryAction,
|
|
Name: "inventory-set-0",
|
|
Type: event.Finished,
|
|
},
|
|
},
|
|
}
|
|
Expect(testutil.EventsToExpEvents(applierEvents)).To(testutil.Equal(expEvents))
|
|
|
|
By("verify podB is created and ready")
|
|
result := assertUnstructuredExists(c, withNamespace(manifestToUnstructured(podB), namespaceName))
|
|
|
|
podIP, found, err := testutil.NestedField(result.Object, "status", "podIP")
|
|
Expect(err).NotTo(HaveOccurred())
|
|
Expect(found).To(BeTrue())
|
|
Expect(podIP).NotTo(BeEmpty()) // use podIP as proxy for readiness
|
|
|
|
containerPort, found, err := testutil.NestedField(result.Object, "spec", "containers", 0, "ports", 0, "containerPort")
|
|
Expect(err).NotTo(HaveOccurred())
|
|
Expect(found).To(BeTrue())
|
|
Expect(containerPort).To(Equal(int64(80)))
|
|
|
|
host := fmt.Sprintf("%s:%d", podIP, containerPort)
|
|
|
|
By("verify podA is mutated, created, and ready")
|
|
result = assertUnstructuredExists(c, withNamespace(manifestToUnstructured(podA), namespaceName))
|
|
|
|
podIP, found, err = testutil.NestedField(result.Object, "status", "podIP")
|
|
Expect(err).NotTo(HaveOccurred())
|
|
Expect(found).To(BeTrue())
|
|
Expect(podIP).NotTo(BeEmpty()) // use podIP as proxy for readiness
|
|
|
|
envValue, found, err := testutil.NestedField(result.Object, "spec", "containers", 0, "env", 0, "value")
|
|
Expect(err).NotTo(HaveOccurred())
|
|
Expect(found).To(BeTrue())
|
|
Expect(envValue).To(Equal(host))
|
|
|
|
By("destroy resources in opposite order")
|
|
destroyer := invConfig.DestroyerFactoryFunc()
|
|
options := apply.DestroyerOptions{InventoryPolicy: inventory.AdoptIfNoInventory}
|
|
destroyerEvents := runCollect(destroyer.Run(inv, options))
|
|
|
|
expEvents = []testutil.ExpEvent{
|
|
{
|
|
// InitTask
|
|
EventType: event.InitType,
|
|
InitEvent: &testutil.ExpInitEvent{},
|
|
},
|
|
{
|
|
// PruneTask start
|
|
EventType: event.ActionGroupType,
|
|
ActionGroupEvent: &testutil.ExpActionGroupEvent{
|
|
Action: event.DeleteAction,
|
|
Name: "prune-0",
|
|
Type: event.Started,
|
|
},
|
|
},
|
|
{
|
|
// Delete podA first
|
|
EventType: event.DeleteType,
|
|
DeleteEvent: &testutil.ExpDeleteEvent{
|
|
Operation: event.Deleted,
|
|
Identifier: object.UnstructuredToObjMetaOrDie(withNamespace(manifestToUnstructured(podA), namespaceName)),
|
|
Error: nil,
|
|
},
|
|
},
|
|
{
|
|
// PruneTask finished
|
|
EventType: event.ActionGroupType,
|
|
ActionGroupEvent: &testutil.ExpActionGroupEvent{
|
|
Action: event.DeleteAction,
|
|
Name: "prune-0",
|
|
Type: event.Finished,
|
|
},
|
|
},
|
|
{
|
|
// WaitTask start
|
|
EventType: event.ActionGroupType,
|
|
ActionGroupEvent: &testutil.ExpActionGroupEvent{
|
|
Action: event.WaitAction,
|
|
Name: "wait-0",
|
|
Type: event.Started,
|
|
},
|
|
},
|
|
{
|
|
// WaitTask finished
|
|
EventType: event.ActionGroupType,
|
|
ActionGroupEvent: &testutil.ExpActionGroupEvent{
|
|
Action: event.WaitAction,
|
|
Name: "wait-0",
|
|
Type: event.Finished,
|
|
},
|
|
},
|
|
{
|
|
// PruneTask start
|
|
EventType: event.ActionGroupType,
|
|
ActionGroupEvent: &testutil.ExpActionGroupEvent{
|
|
Action: event.DeleteAction,
|
|
Name: "prune-1",
|
|
Type: event.Started,
|
|
},
|
|
},
|
|
{
|
|
// Delete pod3 second
|
|
EventType: event.DeleteType,
|
|
DeleteEvent: &testutil.ExpDeleteEvent{
|
|
Operation: event.Deleted,
|
|
Identifier: object.UnstructuredToObjMetaOrDie(withNamespace(manifestToUnstructured(podB), namespaceName)),
|
|
Error: nil,
|
|
},
|
|
},
|
|
{
|
|
// PruneTask finished
|
|
EventType: event.ActionGroupType,
|
|
ActionGroupEvent: &testutil.ExpActionGroupEvent{
|
|
Action: event.DeleteAction,
|
|
Name: "prune-1",
|
|
Type: event.Finished,
|
|
},
|
|
},
|
|
{
|
|
// WaitTask start
|
|
EventType: event.ActionGroupType,
|
|
ActionGroupEvent: &testutil.ExpActionGroupEvent{
|
|
Action: event.WaitAction,
|
|
Name: "wait-1",
|
|
Type: event.Started,
|
|
},
|
|
},
|
|
{
|
|
// WaitTask finished
|
|
EventType: event.ActionGroupType,
|
|
ActionGroupEvent: &testutil.ExpActionGroupEvent{
|
|
Action: event.WaitAction,
|
|
Name: "wait-1",
|
|
Type: event.Finished,
|
|
},
|
|
},
|
|
{
|
|
// DeleteInvTask start
|
|
EventType: event.ActionGroupType,
|
|
ActionGroupEvent: &testutil.ExpActionGroupEvent{
|
|
Action: event.InventoryAction,
|
|
Name: "delete-inventory-0",
|
|
Type: event.Started,
|
|
},
|
|
},
|
|
{
|
|
// DeleteInvTask finished
|
|
EventType: event.ActionGroupType,
|
|
ActionGroupEvent: &testutil.ExpActionGroupEvent{
|
|
Action: event.InventoryAction,
|
|
Name: "delete-inventory-0",
|
|
Type: event.Finished,
|
|
},
|
|
},
|
|
}
|
|
|
|
Expect(testutil.EventsToExpEvents(destroyerEvents)).To(testutil.Equal(expEvents))
|
|
|
|
By("verify podB deleted")
|
|
assertUnstructuredDoesNotExist(c, withNamespace(manifestToUnstructured(podB), namespaceName))
|
|
|
|
By("verify podA deleted")
|
|
assertUnstructuredDoesNotExist(c, withNamespace(manifestToUnstructured(podA), namespaceName))
|
|
}
|