2019-04-29 13:35:13 -05:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"flag"
|
|
|
|
"io/ioutil"
|
2019-05-01 16:56:42 -05:00
|
|
|
"os"
|
2019-04-29 13:35:13 -05:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2019-05-07 10:07:54 -05:00
|
|
|
"github.com/ian-howell/airshipctl/cmd"
|
2019-05-21 13:20:26 -05:00
|
|
|
"github.com/ian-howell/airshipctl/pkg/environment"
|
|
|
|
"k8s.io/client-go/kubernetes/fake"
|
2019-04-29 13:35:13 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// UpdateGolden writes out the golden files with the latest values, rather than failing the test.
|
2019-05-01 16:56:42 -05:00
|
|
|
var shouldUpdateGolden = flag.Bool("update", false, "update golden files")
|
2019-04-29 13:35:13 -05:00
|
|
|
|
2019-05-01 16:56:42 -05:00
|
|
|
const (
|
|
|
|
testdataDir = "testdata"
|
|
|
|
goldenDirSuffix = "GoldenOutput"
|
|
|
|
goldenFileSuffix = ".golden"
|
|
|
|
)
|
2019-04-29 13:35:13 -05:00
|
|
|
|
2019-05-17 14:01:17 -05:00
|
|
|
// CmdTest is a command to be run on the command line as a test
|
2019-04-29 13:35:13 -05:00
|
|
|
type CmdTest struct {
|
|
|
|
Name string
|
|
|
|
Command string
|
|
|
|
}
|
|
|
|
|
2019-05-17 14:01:17 -05:00
|
|
|
// RunCmdTests checks all of the tests actual output against their expected outputs
|
2019-04-29 13:35:13 -05:00
|
|
|
func RunCmdTests(t *testing.T, tests []CmdTest) {
|
|
|
|
t.Helper()
|
|
|
|
for _, test := range tests {
|
2019-05-01 16:56:42 -05:00
|
|
|
cmdOutput := executeCmd(t, test.Command)
|
|
|
|
if *shouldUpdateGolden {
|
|
|
|
updateGolden(t, test, cmdOutput)
|
|
|
|
} else {
|
|
|
|
assertEqualGolden(t, test, cmdOutput)
|
|
|
|
}
|
2019-04-29 13:35:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-01 16:56:42 -05:00
|
|
|
func executeCmd(t *testing.T, command string) []byte {
|
2019-04-29 13:35:13 -05:00
|
|
|
var actual bytes.Buffer
|
2019-05-21 13:20:26 -05:00
|
|
|
settings := &environment.AirshipCTLSettings{
|
|
|
|
KubeClient: fake.NewSimpleClientset(),
|
|
|
|
}
|
2019-04-29 13:35:13 -05:00
|
|
|
// TODO(howell): switch to shellwords (or similar)
|
|
|
|
args := strings.Fields(command)
|
2019-05-21 13:20:26 -05:00
|
|
|
rootCmd, err := cmd.NewRootCmd(&actual, settings, args)
|
2019-05-06 08:58:16 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
|
2019-04-29 13:35:13 -05:00
|
|
|
rootCmd.SetArgs(args)
|
|
|
|
|
2019-05-01 12:27:03 -05:00
|
|
|
if err := rootCmd.Execute(); err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
2019-04-29 13:35:13 -05:00
|
|
|
|
2019-05-01 16:56:42 -05:00
|
|
|
return actual.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateGolden(t *testing.T, test CmdTest, actual []byte) {
|
|
|
|
goldenDir := filepath.Join(testdataDir, t.Name()+goldenDirSuffix)
|
|
|
|
if err := os.MkdirAll(goldenDir, 0775); err != nil {
|
|
|
|
t.Fatalf("failed to create golden directory %s: %s", goldenDir, err)
|
|
|
|
}
|
|
|
|
t.Logf("Created %s", goldenDir)
|
|
|
|
goldenFilePath := filepath.Join(goldenDir, test.Name+goldenFileSuffix)
|
|
|
|
t.Logf("updating golden file: %s", goldenFilePath)
|
|
|
|
if err := ioutil.WriteFile(goldenFilePath, normalize(actual), 0666); err != nil {
|
|
|
|
t.Fatalf("failed to update golden file: %s", err)
|
2019-04-29 13:35:13 -05:00
|
|
|
}
|
2019-05-01 16:56:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func assertEqualGolden(t *testing.T, test CmdTest, actual []byte) {
|
|
|
|
goldenDir := filepath.Join(testdataDir, t.Name()+goldenDirSuffix)
|
|
|
|
goldenFilePath := filepath.Join(goldenDir, test.Name+goldenFileSuffix)
|
2019-04-29 13:35:13 -05:00
|
|
|
golden, err := ioutil.ReadFile(goldenFilePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed while reading golden file: %s", err)
|
|
|
|
}
|
2019-05-01 16:56:42 -05:00
|
|
|
if !bytes.Equal(actual, golden) {
|
2019-04-29 13:35:13 -05:00
|
|
|
errFmt := "output does not match golden file: %s\nEXPECTED:\n%s\nGOT:\n%s"
|
2019-05-01 16:56:42 -05:00
|
|
|
t.Errorf(errFmt, goldenFilePath, string(golden), string(actual))
|
2019-04-29 13:35:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func normalize(in []byte) []byte {
|
|
|
|
return bytes.Replace(in, []byte("\r\n"), []byte("\n"), -1)
|
|
|
|
}
|