2020-07-27 23:49:43 +00:00
|
|
|
/*
|
|
|
|
Copyright 2020 VMware, Inc.
|
|
|
|
SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-07-28 14:10:40 +00:00
|
|
|
"context"
|
2020-07-27 23:49:43 +00:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
2020-07-28 14:10:40 +00:00
|
|
|
"time"
|
2020-07-27 23:49:43 +00:00
|
|
|
|
|
|
|
"github.com/sclevine/spec"
|
|
|
|
"github.com/sclevine/spec/report"
|
2020-07-28 13:42:25 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-07-29 00:22:17 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2020-07-28 20:59:16 +00:00
|
|
|
clientauthenticationv1beta1 "k8s.io/client-go/pkg/apis/clientauthentication/v1beta1"
|
2020-07-28 13:42:25 +00:00
|
|
|
|
2020-07-28 15:44:43 +00:00
|
|
|
"github.com/suzerain-io/placeholder-name/test/library"
|
|
|
|
)
|
2020-07-28 13:42:25 +00:00
|
|
|
|
2020-07-27 23:49:43 +00:00
|
|
|
func TestRun(t *testing.T) {
|
2020-07-29 00:22:17 +00:00
|
|
|
spec.Run(t, "main.run", func(t *testing.T, when spec.G, it spec.S) {
|
|
|
|
var r *require.Assertions
|
2020-07-27 23:49:43 +00:00
|
|
|
var buffer *bytes.Buffer
|
|
|
|
var tokenExchanger tokenExchanger
|
|
|
|
var fakeEnv map[string]string
|
|
|
|
|
|
|
|
var envGetter envGetter = func(envVarName string) (string, bool) {
|
|
|
|
value, present := fakeEnv[envVarName]
|
|
|
|
if !present {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
return value, true
|
|
|
|
}
|
|
|
|
|
|
|
|
it.Before(func() {
|
2020-07-29 00:22:17 +00:00
|
|
|
r = require.New(t)
|
2020-07-27 23:49:43 +00:00
|
|
|
buffer = new(bytes.Buffer)
|
|
|
|
fakeEnv = map[string]string{
|
|
|
|
"PLACEHOLDER_NAME_TOKEN": "token from env",
|
|
|
|
"PLACEHOLDER_NAME_CA_BUNDLE": "ca bundle from env",
|
|
|
|
"PLACEHOLDER_NAME_K8S_API_ENDPOINT": "k8s api from env",
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
when("env vars are missing", func() {
|
|
|
|
it("returns an error when PLACEHOLDER_NAME_TOKEN is missing", func() {
|
2020-07-29 00:22:17 +00:00
|
|
|
delete(fakeEnv, "PLACEHOLDER_NAME_TOKEN")
|
2020-07-28 14:10:40 +00:00
|
|
|
err := run(envGetter, tokenExchanger, buffer, 30*time.Second)
|
2020-07-29 00:22:17 +00:00
|
|
|
r.EqualError(err, "failed to login: environment variable not set: PLACEHOLDER_NAME_TOKEN")
|
2020-07-27 23:49:43 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it("returns an error when PLACEHOLDER_NAME_CA_BUNDLE is missing", func() {
|
2020-07-29 00:22:17 +00:00
|
|
|
delete(fakeEnv, "PLACEHOLDER_NAME_CA_BUNDLE")
|
2020-07-28 14:10:40 +00:00
|
|
|
err := run(envGetter, tokenExchanger, buffer, 30*time.Second)
|
2020-07-29 00:22:17 +00:00
|
|
|
r.EqualError(err, "failed to login: environment variable not set: PLACEHOLDER_NAME_CA_BUNDLE")
|
2020-07-27 23:49:43 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it("returns an error when PLACEHOLDER_NAME_K8S_API_ENDPOINT is missing", func() {
|
2020-07-29 00:22:17 +00:00
|
|
|
delete(fakeEnv, "PLACEHOLDER_NAME_K8S_API_ENDPOINT")
|
2020-07-28 14:10:40 +00:00
|
|
|
err := run(envGetter, tokenExchanger, buffer, 30*time.Second)
|
2020-07-29 00:22:17 +00:00
|
|
|
r.EqualError(err, "failed to login: environment variable not set: PLACEHOLDER_NAME_K8S_API_ENDPOINT")
|
2020-07-27 23:49:43 +00:00
|
|
|
})
|
2020-07-30 01:18:42 +00:00
|
|
|
})
|
2020-07-27 23:49:43 +00:00
|
|
|
|
|
|
|
when("the token exchange fails", func() {
|
|
|
|
it.Before(func() {
|
2020-07-28 20:59:16 +00:00
|
|
|
tokenExchanger = func(ctx context.Context, token, caBundle, apiEndpoint string) (*clientauthenticationv1beta1.ExecCredential, error) {
|
2020-07-27 23:49:43 +00:00
|
|
|
return nil, fmt.Errorf("some error")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it("returns an error", func() {
|
2020-07-28 14:10:40 +00:00
|
|
|
err := run(envGetter, tokenExchanger, buffer, 30*time.Second)
|
2020-07-29 00:22:17 +00:00
|
|
|
r.EqualError(err, "failed to login: some error")
|
2020-07-27 23:49:43 +00:00
|
|
|
})
|
2020-07-30 01:18:42 +00:00
|
|
|
})
|
2020-07-27 23:49:43 +00:00
|
|
|
|
2020-07-28 13:42:25 +00:00
|
|
|
when("the JSON encoder fails", func() {
|
|
|
|
it.Before(func() {
|
2020-07-28 20:59:16 +00:00
|
|
|
tokenExchanger = func(ctx context.Context, token, caBundle, apiEndpoint string) (*clientauthenticationv1beta1.ExecCredential, error) {
|
|
|
|
return &clientauthenticationv1beta1.ExecCredential{
|
|
|
|
Status: &clientauthenticationv1beta1.ExecCredentialStatus{Token: "some token"},
|
2020-07-28 13:42:25 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it("returns an error", func() {
|
2020-07-28 15:44:43 +00:00
|
|
|
err := run(envGetter, tokenExchanger, &library.ErrorWriter{ReturnError: fmt.Errorf("some IO error")}, 30*time.Second)
|
2020-07-29 00:22:17 +00:00
|
|
|
r.EqualError(err, "failed to marshal response to stdout: some IO error")
|
2020-07-28 13:42:25 +00:00
|
|
|
})
|
2020-07-30 01:18:42 +00:00
|
|
|
})
|
2020-07-28 13:42:25 +00:00
|
|
|
|
2020-07-28 14:10:40 +00:00
|
|
|
when("the token exchange times out", func() {
|
|
|
|
it.Before(func() {
|
2020-07-28 20:59:16 +00:00
|
|
|
tokenExchanger = func(ctx context.Context, token, caBundle, apiEndpoint string) (*clientauthenticationv1beta1.ExecCredential, error) {
|
2020-07-28 14:10:40 +00:00
|
|
|
select {
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
2020-07-28 20:59:16 +00:00
|
|
|
return &clientauthenticationv1beta1.ExecCredential{
|
|
|
|
Status: &clientauthenticationv1beta1.ExecCredentialStatus{Token: "some token"},
|
2020-07-28 14:10:40 +00:00
|
|
|
}, nil
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it("returns an error", func() {
|
|
|
|
err := run(envGetter, tokenExchanger, buffer, 1*time.Millisecond)
|
2020-07-29 00:22:17 +00:00
|
|
|
r.EqualError(err, "failed to login: context deadline exceeded")
|
2020-07-28 14:10:40 +00:00
|
|
|
})
|
2020-07-30 01:18:42 +00:00
|
|
|
})
|
2020-07-28 14:10:40 +00:00
|
|
|
|
2020-07-27 23:49:43 +00:00
|
|
|
when("the token exchange succeeds", func() {
|
|
|
|
var actualToken, actualCaBundle, actualAPIEndpoint string
|
|
|
|
|
|
|
|
it.Before(func() {
|
2020-07-28 20:59:16 +00:00
|
|
|
tokenExchanger = func(ctx context.Context, token, caBundle, apiEndpoint string) (*clientauthenticationv1beta1.ExecCredential, error) {
|
2020-07-27 23:49:43 +00:00
|
|
|
actualToken, actualCaBundle, actualAPIEndpoint = token, caBundle, apiEndpoint
|
2020-07-28 20:59:16 +00:00
|
|
|
return &clientauthenticationv1beta1.ExecCredential{
|
|
|
|
TypeMeta: metav1.TypeMeta{
|
|
|
|
Kind: "ExecCredential",
|
|
|
|
APIVersion: "client.authentication.k8s.io/v1beta1",
|
|
|
|
},
|
|
|
|
Status: &clientauthenticationv1beta1.ExecCredentialStatus{Token: "some token"},
|
2020-07-27 23:49:43 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it("writes the execCredential to the given writer", func() {
|
2020-07-28 14:10:40 +00:00
|
|
|
err := run(envGetter, tokenExchanger, buffer, 30*time.Second)
|
2020-07-29 00:22:17 +00:00
|
|
|
r.NoError(err)
|
|
|
|
r.Equal(fakeEnv["PLACEHOLDER_NAME_TOKEN"], actualToken)
|
|
|
|
r.Equal(fakeEnv["PLACEHOLDER_NAME_CA_BUNDLE"], actualCaBundle)
|
|
|
|
r.Equal(fakeEnv["PLACEHOLDER_NAME_K8S_API_ENDPOINT"], actualAPIEndpoint)
|
|
|
|
expected := `{
|
2020-07-28 20:59:16 +00:00
|
|
|
"kind": "ExecCredential",
|
|
|
|
"apiVersion": "client.authentication.k8s.io/v1beta1",
|
|
|
|
"spec": {},
|
|
|
|
"status": {
|
|
|
|
"token": "some token"
|
|
|
|
}
|
2020-07-29 00:22:17 +00:00
|
|
|
}`
|
|
|
|
r.JSONEq(expected, buffer.String())
|
2020-07-27 23:49:43 +00:00
|
|
|
})
|
2020-07-30 01:18:42 +00:00
|
|
|
})
|
|
|
|
}, spec.Parallel(), spec.Report(report.Terminal{}))
|
2020-07-27 23:49:43 +00:00
|
|
|
}
|