Skip to content

Commit

Permalink
Fix instance profile integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-innis committed Oct 22, 2023
1 parent a51adbb commit d3ee048
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
9 changes: 9 additions & 0 deletions test/pkg/environment/aws/expectations.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ import (
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/mitchellh/hashstructure/v2"
. "github.com/onsi/ginkgo/v2" //nolint:revive,stylecheck
. "github.com/onsi/gomega" //nolint:revive,stylecheck
"github.com/samber/lo"
"go.uber.org/multierr"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

"github.com/aws/karpenter/pkg/apis/v1beta1"
)

// Spot Interruption experiment details partially copied from
Expand Down Expand Up @@ -121,6 +124,12 @@ func (env *Environment) ExpectInstanceProfileExists(profileName string) iam.Inst
return lo.FromPtr(out.InstanceProfile)
}

// GetInstanceProfileName gets the string for the profile name based on the cluster name, region and the NodeClass name.
// The length of this string can never exceed the maximum instance profile name limit of 128 characters.
func (env *Environment) GetInstanceProfileName(nodeClass *v1beta1.EC2NodeClass) string {
return fmt.Sprintf("%s_%d", env.ClusterName, lo.Must(hashstructure.Hash(fmt.Sprintf("%s%s", env.Region, nodeClass.Name), hashstructure.FormatV2, nil)))
}

func (env *Environment) GetInstance(nodeName string) ec2.Instance {
node := env.Environment.GetNode(nodeName)
return env.GetInstanceByID(env.ExpectParsedProviderID(node.Spec.ProviderID))
Expand Down
4 changes: 2 additions & 2 deletions test/suites/beta/integration/instance_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var _ = Describe("InstanceProfile Generation", func() {

instance := env.GetInstance(node.Name)
Expect(instance.IamInstanceProfile).ToNot(BeNil())
Expect(instance.IamInstanceProfile.Arn).To(ContainSubstring(nodeClass.Spec.Role))
Expect(lo.FromPtr(instance.IamInstanceProfile.Arn)).To(ContainSubstring(nodeClass.Status.InstanceProfile))

instanceProfile := env.ExpectInstanceProfileExists(instanceprofile.GetProfileName(env.Context, env.Region, nodeClass))
Expect(instanceProfile.Roles).To(HaveLen(1))
Expand All @@ -50,7 +50,7 @@ var _ = Describe("InstanceProfile Generation", func() {
env.ExpectDeleted(nodePool, nodeClass)
Eventually(func(g Gomega) {
_, err := env.IAMAPI.GetInstanceProfileWithContext(env.Context, &iam.GetInstanceProfileInput{
InstanceProfileName: aws.String(instanceprofile.GetProfileName(env.Context, env.Region, nodeClass)),
InstanceProfileName: aws.String(env.GetInstanceProfileName(nodeClass)),
})
g.Expect(awserrors.IsNotFound(err)).To(BeTrue())
}).Should(Succeed())
Expand Down

0 comments on commit d3ee048

Please sign in to comment.