From ec9911086c00e76785d76df9fb05fe686d734fef Mon Sep 17 00:00:00 2001 From: Sam Heilbron Date: Tue, 11 Jul 2023 09:29:25 -0600 Subject: [PATCH] add snapshot_wrtier test to demonstrate new logic --- test/kube2e/gloo/gloo_suite_test.go | 7 +- test/kube2e/gloo/snapshot_writer_test.go | 88 ++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 test/kube2e/gloo/snapshot_writer_test.go diff --git a/test/kube2e/gloo/gloo_suite_test.go b/test/kube2e/gloo/gloo_suite_test.go index 57aa7c2fbf8..047a9c54dd9 100644 --- a/test/kube2e/gloo/gloo_suite_test.go +++ b/test/kube2e/gloo/gloo_suite_test.go @@ -7,6 +7,8 @@ import ( "testing" "time" + "github.com/avast/retry-go" + "github.com/solo-io/gloo/test/services/envoy" "github.com/solo-io/gloo/test/services" @@ -62,7 +64,10 @@ var _ = BeforeSuite(func() { resourceClientset, err = kube2e.NewDefaultKubeResourceClientSet(ctx) Expect(err).NotTo(HaveOccurred(), "can create kube resource client set") - snapshotWriter = helpers.NewSnapshotWriter(resourceClientset).WithWriteNamespace(testHelper.InstallNamespace) + snapshotWriter = helpers.NewSnapshotWriter(resourceClientset). + WithWriteNamespace(testHelper.InstallNamespace). + // This isn't strictly necessary, but we use to ensure that WithRetryOptions behaves correctly + WithRetryOptions([]retry.Option{retry.Attempts(3)}) envoyFactory = envoy.NewFactory() diff --git a/test/kube2e/gloo/snapshot_writer_test.go b/test/kube2e/gloo/snapshot_writer_test.go new file mode 100644 index 00000000000..eed639778ab --- /dev/null +++ b/test/kube2e/gloo/snapshot_writer_test.go @@ -0,0 +1,88 @@ +package gloo_test + +import ( + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + "github.com/solo-io/gloo/projects/gloo/pkg/api/v1/gloosnapshot" + "github.com/solo-io/solo-kit/pkg/api/v1/resources/core" + + gatewayv1 "github.com/solo-io/gloo/projects/gateway/pkg/api/v1" + gloov1 "github.com/solo-io/gloo/projects/gloo/pkg/api/v1" + "github.com/solo-io/gloo/test/helpers" + + "github.com/solo-io/k8s-utils/testutils/helper" + "github.com/solo-io/solo-kit/pkg/api/v1/clients" +) + +// This tests the gloo/test/helpers/snapshot_writer.go functionality in a Kubernetes cluster + +var _ = Describe("SnapshotWriter Test", func() { + + var ( + glooResources *gloosnapshot.ApiSnapshot + ) + + BeforeEach(func() { + // Create a VirtualService routing directly to the testrunner kubernetes service + testRunnerDestination := &gloov1.Destination{ + DestinationType: &gloov1.Destination_Kube{ + Kube: &gloov1.KubernetesServiceDestination{ + Ref: &core.ResourceRef{ + Namespace: testHelper.InstallNamespace, + Name: helper.TestrunnerName, + }, + Port: uint32(helper.TestRunnerPort), + }, + }, + } + testRunnerVs := helpers.NewVirtualServiceBuilder(). + WithName(helper.TestrunnerName). + WithNamespace(testHelper.InstallNamespace). + WithDomain(helper.TestrunnerName). + WithRoutePrefixMatcher(helper.TestrunnerName, "/"). + WithRouteActionToSingleDestination(helper.TestrunnerName, testRunnerDestination). + Build() + + // The set of resources that these tests will generate + glooResources = &gloosnapshot.ApiSnapshot{ + VirtualServices: gatewayv1.VirtualServiceList{ + // many tests route to the TestRunner Service so it makes sense to just + // always create it + // the other benefit is this ensures that all tests start with a valid Proxy CR + testRunnerVs, + }, + } + }) + + JustBeforeEach(func() { + err := snapshotWriter.WriteSnapshot(glooResources, clients.WriteOpts{ + Ctx: ctx, + OverwriteExisting: false, + }) + Expect(err).NotTo(HaveOccurred()) + }) + + JustAfterEach(func() { + err := snapshotWriter.DeleteSnapshot(glooResources, clients.DeleteOpts{ + Ctx: ctx, + IgnoreNotExist: true, + }) + Expect(err).NotTo(HaveOccurred()) + }) + + Context("DeleteSnapshot", func() { + + It("continues when deleting a resource that does not exist", func() { + // We add a resource that does not exist to our API Snapshot + // In the JustAfterEach, we should attempt to delete the resource, and even though + // it does not exist, we should continue the delete operation and succeed + glooResources.Upstreams = append(glooResources.Upstreams, &gloov1.Upstream{ + Metadata: &core.Metadata{ + Name: "resource-does-not-exist", + Namespace: namespace, + }, + }) + }) + }) + +})