From 15cf3a39262cc339ff0363cbbe278abbc8f4a059 Mon Sep 17 00:00:00 2001 From: Amanuel Engeda <74629455+engedaam@users.noreply.github.com> Date: Mon, 8 Apr 2024 09:37:11 -0700 Subject: [PATCH] test: Add unit cache tests for security groups (#5990) --- pkg/providers/securitygroup/suite_test.go | 65 +++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/pkg/providers/securitygroup/suite_test.go b/pkg/providers/securitygroup/suite_test.go index 7756f747222d..e6006127cc8a 100644 --- a/pkg/providers/securitygroup/suite_test.go +++ b/pkg/providers/securitygroup/suite_test.go @@ -265,6 +265,71 @@ var _ = Describe("SecurityGroupProvider", func() { }, }, securityGroups) }) + Context("Provider Cache", func() { + It("should resolve security groups from cache that are filtered by id", func() { + expectedSecurityGroups := awsEnv.EC2API.DescribeSecurityGroupsOutput.Clone().SecurityGroups + for _, sg := range expectedSecurityGroups { + nodeClass.Spec.SecurityGroupSelectorTerms = []v1beta1.SecurityGroupSelectorTerm{ + { + ID: *sg.GroupId, + }, + } + // Call list to request from aws and store in the cache + _, err := awsEnv.SecurityGroupProvider.List(ctx, nodeClass) + Expect(err).To(BeNil()) + } + + for _, cachedObject := range awsEnv.SecurityGroupCache.Items() { + cachedSecurityGroup := cachedObject.Object.([]*ec2.SecurityGroup) + Expect(cachedSecurityGroup).To(HaveLen(1)) + lo.Contains(expectedSecurityGroups, cachedSecurityGroup[0]) + } + }) + It("should resolve security groups from cache that are filtered by Name", func() { + expectedSecurityGroups := awsEnv.EC2API.DescribeSecurityGroupsOutput.Clone().SecurityGroups + for _, sg := range expectedSecurityGroups { + nodeClass.Spec.SecurityGroupSelectorTerms = []v1beta1.SecurityGroupSelectorTerm{ + { + Name: *sg.GroupName, + }, + } + // Call list to request from aws and store in the cache + _, err := awsEnv.SecurityGroupProvider.List(ctx, nodeClass) + Expect(err).To(BeNil()) + } + + for _, cachedObject := range awsEnv.SecurityGroupCache.Items() { + cachedSecurityGroup := cachedObject.Object.([]*ec2.SecurityGroup) + Expect(cachedSecurityGroup).To(HaveLen(1)) + lo.Contains(expectedSecurityGroups, cachedSecurityGroup[0]) + } + }) + It("should resolve security groups from cache that are filtered by tags", func() { + expectedSecurityGroups := awsEnv.EC2API.DescribeSecurityGroupsOutput.Clone().SecurityGroups + tagSet := lo.Map(expectedSecurityGroups, func(sg *ec2.SecurityGroup, _ int) map[string]string { + tag, _ := lo.Find(sg.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.SecurityGroupSelectorTerms = []v1beta1.SecurityGroupSelectorTerm{ + { + Tags: tag, + }, + } + // Call list to request from aws and store in the cache + _, err := awsEnv.SecurityGroupProvider.List(ctx, nodeClass) + Expect(err).To(BeNil()) + } + + for _, cachedObject := range awsEnv.SubnetCache.Items() { + cachedSecurityGroup := cachedObject.Object.([]*ec2.SecurityGroup) + Expect(cachedSecurityGroup).To(HaveLen(1)) + lo.Contains(expectedSecurityGroups, cachedSecurityGroup[0]) + } + }) + }) }) func ExpectConsistsOfSecurityGroups(expected, actual []*ec2.SecurityGroup) {