From f8bfd1e19306a2ce7fb9340f679f5e95fd057b85 Mon Sep 17 00:00:00 2001 From: Amanuel Engeda <74629455+engedaam@users.noreply.github.com> Date: Mon, 8 Apr 2024 09:22:24 -0700 Subject: [PATCH] test: Add unit cache tests for subnets (#5978) --- pkg/providers/subnet/suite_test.go | 46 ++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/pkg/providers/subnet/suite_test.go b/pkg/providers/subnet/suite_test.go index 31daa9058f31..aca881e59fa2 100644 --- a/pkg/providers/subnet/suite_test.go +++ b/pkg/providers/subnet/suite_test.go @@ -240,6 +240,52 @@ var _ = Describe("SubnetProvider", func() { Expect(onlyPrivate).To(BeTrue()) }) }) + Context("Provider Cache", func() { + It("should resolve subnets from cache that are filtered by id", func() { + expectedSubnets := awsEnv.EC2API.DescribeSubnetsOutput.Clone().Subnets + for _, subnet := range expectedSubnets { + nodeClass.Spec.SubnetSelectorTerms = []v1beta1.SubnetSelectorTerm{ + { + ID: *subnet.SubnetId, + }, + } + // Call list to request from aws and store in the cache + _, err := awsEnv.SubnetProvider.List(ctx, nodeClass) + Expect(err).To(BeNil()) + } + + for _, cachedObject := range awsEnv.SubnetCache.Items() { + cachedSubnet := cachedObject.Object.([]*ec2.Subnet) + Expect(cachedSubnet).To(HaveLen(1)) + lo.Contains(expectedSubnets, cachedSubnet[0]) + } + }) + It("should resolve subnets from cache that are filtered by tags", func() { + expectedSubnets := awsEnv.EC2API.DescribeSubnetsOutput.Clone().Subnets + tagSet := lo.Map(expectedSubnets, func(subnet *ec2.Subnet, _ int) map[string]string { + tag, _ := lo.Find(subnet.Tags, func(tag *ec2.Tag) bool { + return lo.FromPtr(tag.Key) == "Name" + }) + return map[string]string{"Name": lo.FromPtr(tag.Value)} + }) + for _, tag := range tagSet { + nodeClass.Spec.SubnetSelectorTerms = []v1beta1.SubnetSelectorTerm{ + { + Tags: tag, + }, + } + // Call list to request from aws and store in the cache + _, err := awsEnv.SubnetProvider.List(ctx, nodeClass) + Expect(err).To(BeNil()) + } + + for _, cachedObject := range awsEnv.SubnetCache.Items() { + cachedSubnet := cachedObject.Object.([]*ec2.Subnet) + Expect(cachedSubnet).To(HaveLen(1)) + lo.Contains(expectedSubnets, cachedSubnet[0]) + } + }) + }) }) func ExpectConsistsOfSubnets(expected, actual []*ec2.Subnet) {