Skip to content

Commit

Permalink
test: Add unit cache tests for security groups (#5990)
Browse files Browse the repository at this point in the history
  • Loading branch information
engedaam authored Apr 8, 2024
1 parent f8bfd1e commit 15cf3a3
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions pkg/providers/securitygroup/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 15cf3a3

Please sign in to comment.