Skip to content

Commit

Permalink
tests: update tests to work with ambient AWS_PROFILE for kubeconfigs
Browse files Browse the repository at this point in the history
  • Loading branch information
rquitales committed Mar 21, 2024
1 parent 806b189 commit 4340386
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
3 changes: 3 additions & 0 deletions examples/aws-profile-py/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@

# Export the cluster kubeconfig.
pulumi.export("kubeconfig", cluster.kubeconfig)

# Export the cluster kubeconfig with the AWS_PROFILE set.
pulumi.export("kubeconfig_with_profile", cluster.get_kubeconfig(profile_name=profile_name))
3 changes: 3 additions & 0 deletions examples/aws-profile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ const cluster = new eks.Cluster(`${projectName}`, {

// Export the cluster kubeconfig.
export const kubeconfig = cluster.kubeconfig;

// Export the cluster kubeconfig with the AWS_PROFILE set.
export const kubeconfigWithProfile = cluster.getKubeconfig({profileName: profileName})
6 changes: 5 additions & 1 deletion examples/examples_nodejs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,13 @@ func TestAccAwsProfile(t *testing.T) {
With(integration.ProgramTestOptions{
Dir: path.Join(getCwd(t), "aws-profile"),
ExtraRuntimeValidation: func(t *testing.T, info integration.RuntimeValidationStackInfo) {
// The `cluster.kubeconfig` output should fail as it does not have the right AWS_PROFILE set.
t.Logf("Ensuring cluster.kubeconfig fails without AWS_PROFILE envvar set")
utils.EnsureKubeconfigFails(t, info.Outputs["kubeconfig"])

utils.RunEKSSmokeTest(t,
info.Deployment.Resources,
info.Outputs["kubeconfig"],
info.Outputs["kubeconfigWithProfile"],
)
},
NoParallel: true,
Expand Down
6 changes: 5 additions & 1 deletion examples/examples_py_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ func TestAccAwsProfilePy(t *testing.T) {
NoParallel: true,
Dir: filepath.Join(getCwd(t), "aws-profile-py"),
ExtraRuntimeValidation: func(t *testing.T, info integration.RuntimeValidationStackInfo) {
// The `cluster.kubeconfig` output should fail as it does not have the right AWS_PROFILE set.
t.Logf("Ensuring cluster.kubeconfig fails without AWS_PROFILE envvar set")
utils.EnsureKubeconfigFails(t, info.Outputs["kubeconfig"])

utils.RunEKSSmokeTest(t,
info.Deployment.Resources,
info.Outputs["kubeconfig"],
info.Outputs["kubeconfig_with_profile"],
)
},
})
Expand Down
17 changes: 17 additions & 0 deletions examples/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -652,3 +652,20 @@ func clientSetFromKubeconfig(kubeconfig any) (*kubernetes.Clientset, error) {
}
return clientSet, nil
}

// EnsureKubeconfigFails ensures that the provided kubeconfig fails to authenticate.
func EnsureKubeconfigFails(t *testing.T, kubeconfig any) {
kc, err := json.Marshal(kubeconfig)
if err != nil {
t.Errorf("unable to marshal provided kubeconfig: %s", err)
}
kubeAccess, err := KubeconfigToKubeAccess(kc)
if err != nil {
t.Errorf("unable to create KubeAccess from kubeconfig: %s", err)
}

_, err = kubeAccess.Clientset.Discovery().ServerVersion()
if err == nil {
t.Errorf("expected kubeconfig to fail, but it succeeded to return the server version")
}
}

0 comments on commit 4340386

Please sign in to comment.