2020-09-16 14:19:51 +00:00
|
|
|
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2020-07-08 17:06:44 +00:00
|
|
|
|
2020-07-27 20:32:14 +00:00
|
|
|
package server
|
2020-07-08 17:06:44 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-07-13 19:30:16 +00:00
|
|
|
"context"
|
2020-07-16 19:24:30 +00:00
|
|
|
"strings"
|
2020-07-08 17:06:44 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-07-23 15:05:21 +00:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2020-07-16 19:24:30 +00:00
|
|
|
"github.com/spf13/cobra"
|
2020-07-13 19:30:16 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-07-08 17:06:44 +00:00
|
|
|
)
|
|
|
|
|
2020-07-16 19:24:30 +00:00
|
|
|
const knownGoodUsage = `
|
2020-08-20 17:54:15 +00:00
|
|
|
pinniped-server provides a generic API for mapping an external
|
2020-07-16 19:24:30 +00:00
|
|
|
credential from somewhere to an internal credential to be used for
|
|
|
|
authenticating to the Kubernetes API.
|
|
|
|
|
|
|
|
Usage:
|
2020-08-20 17:54:15 +00:00
|
|
|
pinniped-server [flags]
|
2020-07-08 17:06:44 +00:00
|
|
|
|
|
|
|
Flags:
|
2020-08-20 19:17:18 +00:00
|
|
|
-c, --config string path to configuration file (default "pinniped.yaml")
|
|
|
|
--downward-api-path string path to Downward API volume mount (default "/etc/podinfo")
|
|
|
|
-h, --help help for pinniped-server
|
|
|
|
--log-flush-frequency duration Maximum number of seconds between log flushes (default 5s)
|
2020-07-08 17:06:44 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
func TestCommand(t *testing.T) {
|
|
|
|
tests := []struct {
|
2020-07-16 19:24:30 +00:00
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
wantErr string
|
|
|
|
wantStdout string
|
2020-07-08 17:06:44 +00:00
|
|
|
}{
|
|
|
|
{
|
2020-07-16 19:24:30 +00:00
|
|
|
name: "NoArgsSucceeds",
|
|
|
|
args: []string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Usage",
|
|
|
|
args: []string{"-h"},
|
|
|
|
wantStdout: knownGoodUsage,
|
2020-07-08 17:06:44 +00:00
|
|
|
},
|
|
|
|
{
|
2020-07-16 19:24:30 +00:00
|
|
|
name: "OneArgFails",
|
|
|
|
args: []string{"tuna"},
|
2020-08-20 17:54:15 +00:00
|
|
|
wantErr: `unknown command "tuna" for "pinniped-server"`,
|
2020-07-08 17:06:44 +00:00
|
|
|
},
|
|
|
|
{
|
2020-07-16 19:24:30 +00:00
|
|
|
name: "ShortConfigFlagSucceeds",
|
|
|
|
args: []string{"-c", "some/path/to/config.yaml"},
|
2020-07-08 17:06:44 +00:00
|
|
|
},
|
|
|
|
{
|
2020-07-16 19:24:30 +00:00
|
|
|
name: "LongConfigFlagSucceeds",
|
|
|
|
args: []string{"--config", "some/path/to/config.yaml"},
|
2020-07-08 17:06:44 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "OneArgWithConfigFlagFails",
|
|
|
|
args: []string{
|
|
|
|
"--config", "some/path/to/config.yaml",
|
|
|
|
"tuna",
|
|
|
|
},
|
2020-08-20 17:54:15 +00:00
|
|
|
wantErr: `unknown command "tuna" for "pinniped-server"`,
|
2020-07-08 17:06:44 +00:00
|
|
|
},
|
|
|
|
}
|
2020-07-13 19:30:16 +00:00
|
|
|
for _, test := range tests {
|
|
|
|
test := test
|
2020-07-08 17:06:44 +00:00
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
stdout := bytes.NewBuffer([]byte{})
|
|
|
|
stderr := bytes.NewBuffer([]byte{})
|
|
|
|
|
2020-07-23 15:05:21 +00:00
|
|
|
a := New(context.Background(), test.args, stdout, stderr)
|
2020-07-16 19:24:30 +00:00
|
|
|
a.cmd.RunE = func(cmd *cobra.Command, args []string) error {
|
2020-07-13 19:30:16 +00:00
|
|
|
return nil
|
2020-07-08 17:06:44 +00:00
|
|
|
}
|
|
|
|
err := a.Run()
|
2020-07-16 19:24:30 +00:00
|
|
|
if test.wantErr != "" {
|
|
|
|
require.EqualError(t, err, test.wantErr)
|
2020-07-08 17:06:44 +00:00
|
|
|
} else {
|
2020-07-16 19:24:30 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
if test.wantStdout != "" {
|
2020-07-23 15:05:21 +00:00
|
|
|
require.Equal(t, strings.TrimSpace(test.wantStdout), strings.TrimSpace(stdout.String()), cmp.Diff(test.wantStdout, stdout.String()))
|
2020-07-08 17:06:44 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|