2020-07-08 17:06:44 +00:00
|
|
|
/*
|
|
|
|
Copyright 2020 VMware, Inc.
|
|
|
|
SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-07-13 19:30:16 +00:00
|
|
|
"context"
|
2020-07-08 17:06:44 +00:00
|
|
|
"testing"
|
2020-07-13 19:30:16 +00:00
|
|
|
"time"
|
2020-07-08 17:06:44 +00:00
|
|
|
|
2020-07-13 19:30:16 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-07-08 17:06:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const knownGoodUsage = `Usage:
|
|
|
|
placeholder-name [flags]
|
|
|
|
|
|
|
|
Flags:
|
|
|
|
-c, --config string path to configuration file (default "placeholder-name.yaml")
|
|
|
|
-h, --help help for placeholder-name
|
|
|
|
`
|
|
|
|
|
|
|
|
func TestCommand(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
|
|
|
|
wantConfigPath string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "NoArgsSucceeds",
|
|
|
|
args: []string{},
|
|
|
|
wantConfigPath: "placeholder-name.yaml",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "OneArgFails",
|
|
|
|
args: []string{"tuna"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ShortConfigFlagSucceeds",
|
|
|
|
args: []string{"-c", "some/path/to/config.yaml"},
|
|
|
|
wantConfigPath: "some/path/to/config.yaml",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "LongConfigFlagSucceeds",
|
|
|
|
args: []string{"--config", "some/path/to/config.yaml"},
|
|
|
|
wantConfigPath: "some/path/to/config.yaml",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "OneArgWithConfigFlagFails",
|
|
|
|
args: []string{
|
|
|
|
"--config", "some/path/to/config.yaml",
|
|
|
|
"tuna",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
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) {
|
2020-07-13 19:30:16 +00:00
|
|
|
expect := require.New(t)
|
2020-07-08 17:06:44 +00:00
|
|
|
|
|
|
|
stdout := bytes.NewBuffer([]byte{})
|
|
|
|
stderr := bytes.NewBuffer([]byte{})
|
|
|
|
|
|
|
|
configPaths := make([]string, 0, 1)
|
2020-07-13 19:30:16 +00:00
|
|
|
runFunc := func(ctx context.Context, configPath string) error {
|
2020-07-08 17:06:44 +00:00
|
|
|
configPaths = append(configPaths, configPath)
|
2020-07-13 19:30:16 +00:00
|
|
|
return nil
|
2020-07-08 17:06:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
a := New(test.args, stdout, stderr)
|
|
|
|
a.runFunc = runFunc
|
|
|
|
err := a.Run()
|
|
|
|
|
|
|
|
if test.wantConfigPath != "" {
|
2020-07-13 19:30:16 +00:00
|
|
|
expect.Equal(1, len(configPaths))
|
|
|
|
expect.Equal(test.wantConfigPath, configPaths[0])
|
2020-07-08 17:06:44 +00:00
|
|
|
} else {
|
|
|
|
expect.Error(err)
|
|
|
|
expect.Contains(stdout.String(), knownGoodUsage)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-07-13 19:30:16 +00:00
|
|
|
|
|
|
|
func TestServeApp(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
a := App{
|
|
|
|
healthAddr: "127.0.0.1:0",
|
|
|
|
mainAddr: "127.0.0.1:8443",
|
|
|
|
}
|
|
|
|
err := a.serve(ctx, "some/path/to/config.yaml")
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("failure", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
a := App{
|
|
|
|
healthAddr: "127.0.0.1:8081",
|
|
|
|
mainAddr: "127.0.0.1:8081",
|
|
|
|
}
|
|
|
|
err := a.serve(ctx, "some/path/to/config.yaml")
|
|
|
|
require.EqualError(t, err, "listen tcp 127.0.0.1:8081: bind: address already in use")
|
|
|
|
})
|
|
|
|
}
|