Skip to content

Commit

Permalink
Merge pull request #565 from karlkfi/karl-stress-test
Browse files Browse the repository at this point in the history
chore: add stress test
  • Loading branch information
k8s-ci-robot authored Mar 11, 2022
2 parents d84328f + 34a8a7b commit a9d2ca7
Show file tree
Hide file tree
Showing 28 changed files with 1,110 additions and 535 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ test-e2e: $(MYGOBIN)/ginkgo $(MYGOBIN)/kind
kind delete cluster --name=cli-utils-e2e && kind create cluster --name=cli-utils-e2e
$(GOPATH)/bin/ginkgo ./test/e2e/...

test-stress: $(MYGOBIN)/ginkgo $(MYGOBIN)/kind
kind delete cluster --name=cli-utils-e2e && kind create cluster --name=cli-utils-e2e
$(GOPATH)/bin/ginkgo -v ./test/stress/... -- -v 5

vet:
go vet ./...

Expand Down
27 changes: 9 additions & 18 deletions test/e2e/apply_and_destroy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,24 @@ import (
"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/cli-utils/test/e2e/e2eutil"
"sigs.k8s.io/cli-utils/test/e2e/invconfig"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func applyAndDestroyTest(ctx context.Context, c client.Client, invConfig InventoryConfig, inventoryName, namespaceName string) {
func applyAndDestroyTest(ctx context.Context, c client.Client, invConfig invconfig.InventoryConfig, inventoryName, namespaceName string) {
By("Apply resources")
applier := invConfig.ApplierFactoryFunc()
inventoryID := fmt.Sprintf("%s-%s", inventoryName, namespaceName)

inventoryInfo := createInventoryInfo(invConfig, inventoryName, namespaceName, inventoryID)
inventoryInfo := invconfig.CreateInventoryInfo(invConfig, inventoryName, namespaceName, inventoryID)

deployment1Obj := withNamespace(manifestToUnstructured(deployment1), namespaceName)
deployment1Obj := e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(deployment1), namespaceName)
resources := []*unstructured.Unstructured{
deployment1Obj,
}

applierEvents := runCollect(applier.Run(ctx, inventoryInfo, resources, apply.ApplierOptions{
applierEvents := e2eutil.RunCollect(applier.Run(ctx, inventoryInfo, resources, apply.ApplierOptions{
ReconcileTimeout: 2 * time.Minute,
EmitStatusEvents: true,
}))
Expand Down Expand Up @@ -184,7 +186,7 @@ func applyAndDestroyTest(ctx context.Context, c client.Client, invConfig Invento
Expect(received).To(testutil.Equal(expEvents))

By("Verify deployment created")
assertUnstructuredExists(ctx, c, deployment1Obj)
e2eutil.AssertUnstructuredExists(ctx, c, deployment1Obj)

By("Verify inventory")
invConfig.InvSizeVerifyFunc(ctx, c, inventoryName, namespaceName, inventoryID, 1, 1)
Expand All @@ -193,7 +195,7 @@ func applyAndDestroyTest(ctx context.Context, c client.Client, invConfig Invento
destroyer := invConfig.DestroyerFactoryFunc()

options := apply.DestroyerOptions{InventoryPolicy: inventory.PolicyAdoptIfNoInventory}
destroyerEvents := runCollect(destroyer.Run(ctx, inventoryInfo, options))
destroyerEvents := e2eutil.RunCollect(destroyer.Run(ctx, inventoryInfo, options))

expEvents = []testutil.ExpEvent{
{
Expand Down Expand Up @@ -288,16 +290,5 @@ func applyAndDestroyTest(ctx context.Context, c client.Client, invConfig Invento
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))
}
e2eutil.AssertUnstructuredDoesNotExist(ctx, c, deployment1Obj)
}
14 changes: 8 additions & 6 deletions test/e2e/continue_on_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,25 @@ import (
"sigs.k8s.io/cli-utils/pkg/apply/event"
"sigs.k8s.io/cli-utils/pkg/object"
"sigs.k8s.io/cli-utils/pkg/testutil"
"sigs.k8s.io/cli-utils/test/e2e/e2eutil"
"sigs.k8s.io/cli-utils/test/e2e/invconfig"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func continueOnErrorTest(ctx context.Context, c client.Client, invConfig InventoryConfig, inventoryName, namespaceName string) {
func continueOnErrorTest(ctx context.Context, c client.Client, invConfig invconfig.InventoryConfig, inventoryName, namespaceName string) {
By("apply an invalid CRD")
applier := invConfig.ApplierFactoryFunc()

inv := invConfig.InvWrapperFunc(invConfig.FactoryFunc(inventoryName, namespaceName, "test"))

invalidCrdObj := manifestToUnstructured(invalidCrd)
pod1Obj := withNamespace(manifestToUnstructured(pod1), namespaceName)
invalidCrdObj := e2eutil.ManifestToUnstructured(invalidCrd)
pod1Obj := e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(pod1), namespaceName)
resources := []*unstructured.Unstructured{
invalidCrdObj,
pod1Obj,
}

applierEvents := runCollect(applier.Run(ctx, inv, resources, apply.ApplierOptions{}))
applierEvents := e2eutil.RunCollect(applier.Run(ctx, inv, resources, apply.ApplierOptions{}))

expEvents := []testutil.ExpEvent{
{
Expand Down Expand Up @@ -167,8 +169,8 @@ func continueOnErrorTest(ctx context.Context, c client.Client, invConfig Invento
Expect(receivedEvents).To(testutil.Equal(expEvents))

By("Verify pod1 created")
assertUnstructuredExists(ctx, c, pod1Obj)
e2eutil.AssertUnstructuredExists(ctx, c, pod1Obj)

By("Verify CRD not created")
assertUnstructuredDoesNotExist(ctx, c, invalidCrdObj)
e2eutil.AssertUnstructuredDoesNotExist(ctx, c, invalidCrdObj)
}
12 changes: 7 additions & 5 deletions test/e2e/crd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,27 @@ import (
"sigs.k8s.io/cli-utils/pkg/inventory"
"sigs.k8s.io/cli-utils/pkg/object"
"sigs.k8s.io/cli-utils/pkg/testutil"
"sigs.k8s.io/cli-utils/test/e2e/e2eutil"
"sigs.k8s.io/cli-utils/test/e2e/invconfig"
"sigs.k8s.io/controller-runtime/pkg/client"
)

//nolint:dupl // expEvents similar to mutation tests
func crdTest(ctx context.Context, _ client.Client, invConfig InventoryConfig, inventoryName, namespaceName string) {
func crdTest(ctx context.Context, _ client.Client, invConfig invconfig.InventoryConfig, inventoryName, namespaceName string) {
By("apply a set of resources that includes both a crd and a cr")
applier := invConfig.ApplierFactoryFunc()

inv := invConfig.InvWrapperFunc(invConfig.FactoryFunc(inventoryName, namespaceName, "test"))

crdObj := manifestToUnstructured(crd)
crObj := manifestToUnstructured(cr)
crdObj := e2eutil.ManifestToUnstructured(crd)
crObj := e2eutil.ManifestToUnstructured(cr)

resources := []*unstructured.Unstructured{
crObj,
crdObj,
}

applierEvents := runCollect(applier.Run(ctx, inv, resources, apply.ApplierOptions{
applierEvents := e2eutil.RunCollect(applier.Run(ctx, inv, resources, apply.ApplierOptions{
ReconcileTimeout: 2 * time.Minute,
EmitStatusEvents: false,
}))
Expand Down Expand Up @@ -215,7 +217,7 @@ func crdTest(ctx context.Context, _ client.Client, invConfig InventoryConfig, in
By("destroy the resources, including the crd")
destroyer := invConfig.DestroyerFactoryFunc()
options := apply.DestroyerOptions{InventoryPolicy: inventory.PolicyAdoptIfNoInventory}
destroyerEvents := runCollect(destroyer.Run(ctx, inv, options))
destroyerEvents := e2eutil.RunCollect(destroyer.Run(ctx, inv, options))

expEvents = []testutil.ExpEvent{
{
Expand Down
6 changes: 0 additions & 6 deletions test/e2e/customprovider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ spec:
openAPIV3Schema:
description: Example for cli-utils e2e tests
properties:
apiVersion:
type: string
kind:
type: string
metadata:
type: object
spec:
properties:
objects:
Expand Down
40 changes: 21 additions & 19 deletions test/e2e/deletion_prevention_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,85 +14,87 @@ import (
"sigs.k8s.io/cli-utils/pkg/apply"
"sigs.k8s.io/cli-utils/pkg/common"
"sigs.k8s.io/cli-utils/pkg/inventory"
"sigs.k8s.io/cli-utils/test/e2e/e2eutil"
"sigs.k8s.io/cli-utils/test/e2e/invconfig"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func deletionPreventionTest(ctx context.Context, c client.Client, invConfig InventoryConfig, inventoryName, namespaceName string) {
func deletionPreventionTest(ctx context.Context, c client.Client, invConfig invconfig.InventoryConfig, inventoryName, namespaceName string) {
By("Apply resources")
applier := invConfig.ApplierFactoryFunc()
inventoryID := fmt.Sprintf("%s-%s", inventoryName, namespaceName)

inventoryInfo := createInventoryInfo(invConfig, inventoryName, namespaceName, inventoryID)
inventoryInfo := invconfig.CreateInventoryInfo(invConfig, inventoryName, namespaceName, inventoryID)

resources := []*unstructured.Unstructured{
withNamespace(manifestToUnstructured(deployment1), namespaceName),
withAnnotation(withNamespace(manifestToUnstructured(pod1), namespaceName), common.OnRemoveAnnotation, common.OnRemoveKeep),
withAnnotation(withNamespace(manifestToUnstructured(pod2), namespaceName), common.LifecycleDeleteAnnotation, common.PreventDeletion),
e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(deployment1), namespaceName),
e2eutil.WithAnnotation(e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(pod1), namespaceName), common.OnRemoveAnnotation, common.OnRemoveKeep),
e2eutil.WithAnnotation(e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(pod2), namespaceName), common.LifecycleDeleteAnnotation, common.PreventDeletion),
}

runCollect(applier.Run(ctx, inventoryInfo, resources, apply.ApplierOptions{
e2eutil.RunCollect(applier.Run(ctx, inventoryInfo, resources, apply.ApplierOptions{
ReconcileTimeout: 2 * time.Minute,
}))

By("Verify deployment created")
obj := assertUnstructuredExists(ctx, c, withNamespace(manifestToUnstructured(deployment1), namespaceName))
obj := e2eutil.AssertUnstructuredExists(ctx, c, e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(deployment1), namespaceName))
Expect(obj.GetAnnotations()[inventory.OwningInventoryKey]).To(Equal(inventoryInfo.ID()))

By("Verify pod1 created")
obj = assertUnstructuredExists(ctx, c, withNamespace(manifestToUnstructured(pod1), namespaceName))
obj = e2eutil.AssertUnstructuredExists(ctx, c, e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(pod1), namespaceName))
Expect(obj.GetAnnotations()[inventory.OwningInventoryKey]).To(Equal(inventoryInfo.ID()))

By("Verify pod2 created")
obj = assertUnstructuredExists(ctx, c, withNamespace(manifestToUnstructured(pod2), namespaceName))
obj = e2eutil.AssertUnstructuredExists(ctx, c, e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(pod2), namespaceName))
Expect(obj.GetAnnotations()[inventory.OwningInventoryKey]).To(Equal(inventoryInfo.ID()))

By("Verify the inventory size is 3")
invConfig.InvSizeVerifyFunc(ctx, c, inventoryName, namespaceName, inventoryID, 3, 3)

By("Dry-run apply resources")
resources = []*unstructured.Unstructured{
withNamespace(manifestToUnstructured(deployment1), namespaceName),
e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(deployment1), namespaceName),
}

runCollect(applier.Run(ctx, inventoryInfo, resources, apply.ApplierOptions{
e2eutil.RunCollect(applier.Run(ctx, inventoryInfo, resources, apply.ApplierOptions{
ReconcileTimeout: 2 * time.Minute,
DryRunStrategy: common.DryRunClient,
}))

By("Verify deployment still exists and has the config.k8s.io/owning-inventory annotation")
obj = assertUnstructuredExists(ctx, c, withNamespace(manifestToUnstructured(deployment1), namespaceName))
obj = e2eutil.AssertUnstructuredExists(ctx, c, e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(deployment1), namespaceName))
Expect(obj.GetAnnotations()[inventory.OwningInventoryKey]).To(Equal(inventoryInfo.ID()))

By("Verify pod1 still exits and does not have the config.k8s.io/owning-inventory annotation")
obj = assertUnstructuredExists(ctx, c, withNamespace(manifestToUnstructured(pod1), namespaceName))
obj = e2eutil.AssertUnstructuredExists(ctx, c, e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(pod1), namespaceName))
Expect(obj.GetAnnotations()[inventory.OwningInventoryKey]).To(Equal(inventoryInfo.ID()))

By("Verify pod2 still exits and does not have the config.k8s.io/owning-inventory annotation")
obj = assertUnstructuredExists(ctx, c, withNamespace(manifestToUnstructured(pod2), namespaceName))
obj = e2eutil.AssertUnstructuredExists(ctx, c, e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(pod2), namespaceName))
Expect(obj.GetAnnotations()[inventory.OwningInventoryKey]).To(Equal(inventoryInfo.ID()))

By("Verify the inventory size is still 3")
invConfig.InvSizeVerifyFunc(ctx, c, inventoryName, namespaceName, inventoryID, 3, 3)

By("Apply resources")
resources = []*unstructured.Unstructured{
withNamespace(manifestToUnstructured(deployment1), namespaceName),
e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(deployment1), namespaceName),
}

runCollect(applier.Run(ctx, inventoryInfo, resources, apply.ApplierOptions{
e2eutil.RunCollect(applier.Run(ctx, inventoryInfo, resources, apply.ApplierOptions{
ReconcileTimeout: 2 * time.Minute,
}))

By("Verify deployment still exists and has the config.k8s.io/owning-inventory annotation")
obj = assertUnstructuredExists(ctx, c, withNamespace(manifestToUnstructured(deployment1), namespaceName))
obj = e2eutil.AssertUnstructuredExists(ctx, c, e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(deployment1), namespaceName))
Expect(obj.GetAnnotations()[inventory.OwningInventoryKey]).To(Equal(inventoryInfo.ID()))

By("Verify pod1 still exits and does not have the config.k8s.io/owning-inventory annotation")
obj = assertUnstructuredExists(ctx, c, withNamespace(manifestToUnstructured(pod1), namespaceName))
obj = e2eutil.AssertUnstructuredExists(ctx, c, e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(pod1), namespaceName))
Expect(obj.GetAnnotations()[inventory.OwningInventoryKey]).To(Equal(""))

By("Verify pod2 still exits and does not have the config.k8s.io/owning-inventory annotation")
obj = assertUnstructuredExists(ctx, c, withNamespace(manifestToUnstructured(pod2), namespaceName))
obj = e2eutil.AssertUnstructuredExists(ctx, c, e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(pod2), namespaceName))
Expect(obj.GetAnnotations()[inventory.OwningInventoryKey]).To(Equal(""))

By("Verify the inventory size is 1")
Expand Down
24 changes: 13 additions & 11 deletions test/e2e/dependency_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@ import (
"sigs.k8s.io/cli-utils/pkg/object"
"sigs.k8s.io/cli-utils/pkg/object/validation"
"sigs.k8s.io/cli-utils/pkg/testutil"
"sigs.k8s.io/cli-utils/test/e2e/e2eutil"
"sigs.k8s.io/cli-utils/test/e2e/invconfig"
"sigs.k8s.io/controller-runtime/pkg/client"
)

//nolint:dupl // expEvents similar to other tests
func dependencyFilterTest(ctx context.Context, c client.Client, invConfig InventoryConfig, inventoryName, namespaceName string) {
func dependencyFilterTest(ctx context.Context, c client.Client, invConfig invconfig.InventoryConfig, inventoryName, namespaceName string) {
By("apply resources in order based on depends-on annotation")
applier := invConfig.ApplierFactoryFunc()

inv := invConfig.InvWrapperFunc(invConfig.FactoryFunc(inventoryName, namespaceName, "test"))

pod1Obj := withDependsOn(withNamespace(manifestToUnstructured(pod1), namespaceName), fmt.Sprintf("/namespaces/%s/Pod/pod2", namespaceName))
pod2Obj := withNamespace(manifestToUnstructured(pod2), namespaceName)
pod1Obj := e2eutil.WithDependsOn(e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(pod1), namespaceName), fmt.Sprintf("/namespaces/%s/Pod/pod2", namespaceName))
pod2Obj := e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(pod2), namespaceName)

// Dependency order: pod1 -> pod2
// Apply order: pod2, pod1
Expand All @@ -37,11 +39,11 @@ func dependencyFilterTest(ctx context.Context, c client.Client, invConfig Invent

// Cleanup
defer func(ctx context.Context, c client.Client) {
deleteUnstructuredIfExists(ctx, c, pod1Obj)
deleteUnstructuredIfExists(ctx, c, pod2Obj)
e2eutil.DeleteUnstructuredIfExists(ctx, c, pod1Obj)
e2eutil.DeleteUnstructuredIfExists(ctx, c, pod2Obj)
}(ctx, c)

applierEvents := runCollect(applier.Run(ctx, inv, resources, apply.ApplierOptions{
applierEvents := e2eutil.RunCollect(applier.Run(ctx, inv, resources, apply.ApplierOptions{
EmitStatusEvents: false,
}))

Expand Down Expand Up @@ -219,14 +221,14 @@ func dependencyFilterTest(ctx context.Context, c client.Client, invConfig Invent
Expect(testutil.EventsToExpEvents(applierEvents)).To(testutil.Equal(expEvents))

By("verify pod1 created and ready")
result := assertUnstructuredExists(ctx, c, pod1Obj)
result := e2eutil.AssertUnstructuredExists(ctx, c, pod1Obj)
podIP, found, err := object.NestedField(result.Object, "status", "podIP")
Expect(err).NotTo(HaveOccurred())
Expect(found).To(BeTrue())
Expect(podIP).NotTo(BeEmpty()) // use podIP as proxy for readiness

By("verify pod2 created and ready")
result = assertUnstructuredExists(ctx, c, pod2Obj)
result = e2eutil.AssertUnstructuredExists(ctx, c, pod2Obj)
podIP, found, err = object.NestedField(result.Object, "status", "podIP")
Expect(err).NotTo(HaveOccurred())
Expect(found).To(BeTrue())
Expand All @@ -237,7 +239,7 @@ func dependencyFilterTest(ctx context.Context, c client.Client, invConfig Invent
pod1Obj,
}

applierEvents = runCollect(applier.Run(ctx, inv, resources, apply.ApplierOptions{
applierEvents = e2eutil.RunCollect(applier.Run(ctx, inv, resources, apply.ApplierOptions{
EmitStatusEvents: false,
ValidationPolicy: validation.SkipInvalid,
}))
Expand Down Expand Up @@ -398,13 +400,13 @@ func dependencyFilterTest(ctx context.Context, c client.Client, invConfig Invent
Expect(testutil.EventsToExpEvents(applierEvents)).To(testutil.Equal(expEvents))

By("verify pod1 not deleted")
result = assertUnstructuredExists(ctx, c, pod1Obj)
result = e2eutil.AssertUnstructuredExists(ctx, c, pod1Obj)
ts, found, err := object.NestedField(result.Object, "metadata", "deletionTimestamp")
Expect(err).NotTo(HaveOccurred())
Expect(found).To(BeFalse(), "deletionTimestamp found: ", ts)

By("verify pod2 not deleted")
result = assertUnstructuredExists(ctx, c, pod2Obj)
result = e2eutil.AssertUnstructuredExists(ctx, c, pod2Obj)
ts, found, err = object.NestedField(result.Object, "metadata", "deletionTimestamp")
Expect(err).NotTo(HaveOccurred())
Expect(found).To(BeFalse(), "deletionTimestamp found: ", ts)
Expand Down
Loading

0 comments on commit a9d2ca7

Please sign in to comment.