From edf80b0aff232592edd1b61bb94b786ab53c5ba9 Mon Sep 17 00:00:00 2001 From: Jigisha Patil <89548848+jigisha620@users.noreply.github.com> Date: Wed, 13 Nov 2024 11:13:38 -0800 Subject: [PATCH] test: Add test for instance profile tags (#7363) --- pkg/providers/instanceprofile/suite_test.go | 103 ++++++++++++++++++++ pkg/test/nodeclass.go | 8 ++ 2 files changed, 111 insertions(+) create mode 100644 pkg/providers/instanceprofile/suite_test.go diff --git a/pkg/providers/instanceprofile/suite_test.go b/pkg/providers/instanceprofile/suite_test.go new file mode 100644 index 000000000000..99b15e8b6928 --- /dev/null +++ b/pkg/providers/instanceprofile/suite_test.go @@ -0,0 +1,103 @@ +/* +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package instanceprofile_test + +import ( + "context" + "testing" + + v1 "github.com/aws/karpenter-provider-aws/pkg/apis/v1" + + "sigs.k8s.io/karpenter/pkg/test/v1alpha1" + + "github.com/aws/karpenter-provider-aws/pkg/apis" + "github.com/aws/karpenter-provider-aws/pkg/operator/options" + "github.com/aws/karpenter-provider-aws/pkg/test" + + coreoptions "sigs.k8s.io/karpenter/pkg/operator/options" + coretest "sigs.k8s.io/karpenter/pkg/test" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + . "sigs.k8s.io/karpenter/pkg/test/expectations" + . "sigs.k8s.io/karpenter/pkg/utils/testing" +) + +var ctx context.Context +var stop context.CancelFunc +var env *coretest.Environment +var awsEnv *test.Environment +var nodeClass test.TestNodeClass + +func TestAWS(t *testing.T) { + ctx = TestContextWithLogger(t) + RegisterFailHandler(Fail) + RunSpecs(t, "InstanceProfileProvider") +} + +var _ = BeforeSuite(func() { + env = coretest.NewEnvironment(coretest.WithCRDs(apis.CRDs...), coretest.WithCRDs(v1alpha1.CRDs...)) + ctx = coreoptions.ToContext(ctx, coretest.Options()) + ctx = options.ToContext(ctx, test.Options()) + ctx, stop = context.WithCancel(ctx) + awsEnv = test.NewEnvironment(ctx, env) +}) + +var _ = AfterSuite(func() { + stop() + Expect(env.Stop()).To(Succeed(), "Failed to stop environment") +}) + +var _ = BeforeEach(func() { + ctx = coreoptions.ToContext(ctx, coretest.Options()) + ctx = options.ToContext(ctx, test.Options()) + nodeClass = test.TestNodeClass{ + EC2NodeClass: v1.EC2NodeClass{ + Spec: v1.EC2NodeClassSpec{ + AMISelectorTerms: []v1.AMISelectorTerm{{ + Alias: "al2@latest", + }}, + SubnetSelectorTerms: []v1.SubnetSelectorTerm{ + { + Tags: map[string]string{ + "*": "*", + }, + }, + }, + SecurityGroupSelectorTerms: []v1.SecurityGroupSelectorTerm{ + { + Tags: map[string]string{ + "*": "*", + }, + }, + }, + }, + }, + } + awsEnv.Reset() +}) + +var _ = AfterEach(func() { + ExpectCleanedUp(ctx, env.Client) +}) + +var _ = Describe("InstanceProfileProvider", func() { + It("should not add any tags if InstanceProfileTags() returns empty map", func() { + instanceProfile, err := awsEnv.InstanceProfileProvider.Create(ctx, &nodeClass) + Expect(err).To(BeNil()) + Expect(instanceProfile).ToNot(BeNil()) + Expect(awsEnv.IAMAPI.InstanceProfiles[instanceProfile].Tags).To(HaveLen(0)) + }) +}) diff --git a/pkg/test/nodeclass.go b/pkg/test/nodeclass.go index 66bf626c962a..f04d80817bf2 100644 --- a/pkg/test/nodeclass.go +++ b/pkg/test/nodeclass.go @@ -139,3 +139,11 @@ func EC2NodeClassFieldIndexer(ctx context.Context) func(cache.Cache) error { }) } } + +type TestNodeClass struct { + v1.EC2NodeClass +} + +func (t *TestNodeClass) InstanceProfileTags(clusterName string) map[string]string { + return nil +}