2019-07-11 08:29:15 -05:00
|
|
|
package testutil
|
2019-04-29 13:35:13 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"flag"
|
|
|
|
"io/ioutil"
|
2019-05-01 16:56:42 -05:00
|
|
|
"os"
|
2019-04-29 13:35:13 -05:00
|
|
|
"path/filepath"
|
2019-05-24 08:47:49 -05:00
|
|
|
"strings"
|
2019-04-29 13:35:13 -05:00
|
|
|
"testing"
|
2019-05-24 08:47:49 -05:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
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 {
|
2019-07-11 08:29:15 -05:00
|
|
|
// The name of the test. This will be used when generating golden
|
|
|
|
// files
|
|
|
|
Name string
|
|
|
|
|
|
|
|
// The values that would be inputted to airshipctl as commands, flags,
|
|
|
|
// and arguments. The initial "airshipctl" is implied
|
2019-05-23 14:34:28 -05:00
|
|
|
CmdLine string
|
2019-07-11 08:29:15 -05:00
|
|
|
|
|
|
|
// The instatiated version of the root airshipctl command to test
|
|
|
|
Cmd *cobra.Command
|
2019-05-01 16:56:42 -05:00
|
|
|
}
|
|
|
|
|
2019-05-29 15:11:55 -05:00
|
|
|
// RunTest either asserts that a specific command's output matches the expected
|
|
|
|
// output from its golden file, or generates golden files if the -update flag
|
|
|
|
// is passed
|
2019-07-11 08:29:15 -05:00
|
|
|
func RunTest(t *testing.T, test *CmdTest) {
|
|
|
|
cmd := test.Cmd
|
|
|
|
|
2019-05-29 15:11:55 -05:00
|
|
|
actual := &bytes.Buffer{}
|
|
|
|
cmd.SetOutput(actual)
|
2019-07-11 08:29:15 -05:00
|
|
|
|
2019-05-24 08:47:49 -05:00
|
|
|
args := strings.Fields(test.CmdLine)
|
|
|
|
cmd.SetArgs(args)
|
2019-07-11 08:29:15 -05:00
|
|
|
|
2019-05-24 08:47:49 -05:00
|
|
|
if err := cmd.Execute(); err != nil {
|
|
|
|
t.Fatalf("Unexpected error: %s", err.Error())
|
|
|
|
}
|
2019-07-11 08:29:15 -05:00
|
|
|
|
2019-05-24 08:47:49 -05:00
|
|
|
if *shouldUpdateGolden {
|
|
|
|
updateGolden(t, test, actual.Bytes())
|
|
|
|
} else {
|
|
|
|
assertEqualGolden(t, test, actual.Bytes())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-29 15:11:55 -05:00
|
|
|
func updateGolden(t *testing.T, test *CmdTest, actual []byte) {
|
2019-05-01 16:56:42 -05:00
|
|
|
goldenDir := filepath.Join(testdataDir, t.Name()+goldenDirSuffix)
|
|
|
|
if err := os.MkdirAll(goldenDir, 0775); err != nil {
|
2019-07-11 08:29:15 -05:00
|
|
|
t.Fatalf("Failed to create golden directory %s: %s", goldenDir, err)
|
2019-05-01 16:56:42 -05:00
|
|
|
}
|
|
|
|
t.Logf("Created %s", goldenDir)
|
|
|
|
goldenFilePath := filepath.Join(goldenDir, test.Name+goldenFileSuffix)
|
2019-07-11 08:29:15 -05:00
|
|
|
t.Logf("Updating golden file: %s", goldenFilePath)
|
2019-05-01 16:56:42 -05:00
|
|
|
if err := ioutil.WriteFile(goldenFilePath, normalize(actual), 0666); err != nil {
|
2019-07-11 08:29:15 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-05-29 15:11:55 -05:00
|
|
|
func assertEqualGolden(t *testing.T, test *CmdTest, actual []byte) {
|
2019-05-01 16:56:42 -05:00
|
|
|
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 {
|
2019-07-11 08:29:15 -05:00
|
|
|
t.Fatalf("Failed while reading golden file: %s", err)
|
2019-04-29 13:35:13 -05:00
|
|
|
}
|
2019-05-01 16:56:42 -05:00
|
|
|
if !bytes.Equal(actual, golden) {
|
2019-07-11 08:29:15 -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)
|
|
|
|
}
|