2019-11-23 03:52:29 +00:00
|
|
|
package test
|
2015-10-10 08:18:54 +00:00
|
|
|
|
|
|
|
import (
|
2017-09-22 19:58:12 +00:00
|
|
|
"bytes"
|
2015-10-10 08:18:54 +00:00
|
|
|
"fmt"
|
2017-09-22 19:58:12 +00:00
|
|
|
"io/ioutil"
|
2015-10-10 08:18:54 +00:00
|
|
|
"os"
|
2021-06-28 21:05:32 +00:00
|
|
|
"path/filepath"
|
2017-09-20 23:40:33 +00:00
|
|
|
"reflect"
|
2017-09-22 19:58:12 +00:00
|
|
|
"strings"
|
2015-10-10 08:18:54 +00:00
|
|
|
"testing"
|
2017-09-20 23:40:33 +00:00
|
|
|
|
2019-09-03 05:52:55 +00:00
|
|
|
"github.com/spf13/cobra"
|
2019-12-09 02:44:53 +00:00
|
|
|
yaml "gopkg.in/yaml.v3"
|
2015-10-10 08:18:54 +00:00
|
|
|
)
|
|
|
|
|
2017-09-22 19:58:12 +00:00
|
|
|
type resulter struct {
|
|
|
|
Error error
|
|
|
|
Output string
|
|
|
|
Command *cobra.Command
|
|
|
|
}
|
|
|
|
|
2019-11-23 03:52:29 +00:00
|
|
|
func RunCmd(c *cobra.Command, input string) resulter {
|
2017-09-22 19:58:12 +00:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
c.SetOutput(buf)
|
|
|
|
c.SetArgs(strings.Split(input, " "))
|
|
|
|
|
|
|
|
err := c.Execute()
|
|
|
|
output := buf.String()
|
|
|
|
|
|
|
|
return resulter{err, output, c}
|
|
|
|
}
|
|
|
|
|
2019-12-09 02:44:53 +00:00
|
|
|
func ParseData(rawData string) yaml.Node {
|
|
|
|
var parsedData yaml.Node
|
2015-10-10 08:18:54 +00:00
|
|
|
err := yaml.Unmarshal([]byte(rawData), &parsedData)
|
|
|
|
if err != nil {
|
2017-09-20 23:40:33 +00:00
|
|
|
fmt.Printf("Error parsing yaml: %v\n", err)
|
2015-10-10 08:18:54 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
return parsedData
|
|
|
|
}
|
|
|
|
|
2019-11-23 03:52:29 +00:00
|
|
|
func AssertResult(t *testing.T, expectedValue interface{}, actualValue interface{}) {
|
2018-05-04 15:47:08 +00:00
|
|
|
t.Helper()
|
2015-10-10 08:18:54 +00:00
|
|
|
if expectedValue != actualValue {
|
|
|
|
t.Error("Expected <", expectedValue, "> but got <", actualValue, ">", fmt.Sprintf("%T", actualValue))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-23 03:52:29 +00:00
|
|
|
func AssertResultComplex(t *testing.T, expectedValue interface{}, actualValue interface{}) {
|
2018-05-04 15:47:08 +00:00
|
|
|
t.Helper()
|
2017-09-20 23:40:33 +00:00
|
|
|
if !reflect.DeepEqual(expectedValue, actualValue) {
|
2020-09-20 12:47:57 +00:00
|
|
|
t.Error("\nExpected <", expectedValue, ">\nbut got <", actualValue, ">", fmt.Sprintf("%T", actualValue))
|
2017-09-20 23:40:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-16 01:29:26 +00:00
|
|
|
func AssertResultComplexWithContext(t *testing.T, expectedValue interface{}, actualValue interface{}, context interface{}) {
|
|
|
|
t.Helper()
|
|
|
|
if !reflect.DeepEqual(expectedValue, actualValue) {
|
|
|
|
t.Error(context)
|
|
|
|
t.Error("\nExpected <", expectedValue, ">\nbut got <", actualValue, ">", fmt.Sprintf("%T", actualValue))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-23 03:52:29 +00:00
|
|
|
func AssertResultWithContext(t *testing.T, expectedValue interface{}, actualValue interface{}, context interface{}) {
|
2018-05-04 15:47:08 +00:00
|
|
|
t.Helper()
|
2015-10-10 08:18:54 +00:00
|
|
|
if expectedValue != actualValue {
|
|
|
|
t.Error(context)
|
|
|
|
t.Error(": expected <", expectedValue, "> but got <", actualValue, ">")
|
|
|
|
}
|
|
|
|
}
|
2017-09-22 19:58:12 +00:00
|
|
|
|
2019-11-23 03:52:29 +00:00
|
|
|
func WriteTempYamlFile(content string) string {
|
2017-09-22 19:58:12 +00:00
|
|
|
tmpfile, _ := ioutil.TempFile("", "testyaml")
|
|
|
|
defer func() {
|
|
|
|
_ = tmpfile.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
_, _ = tmpfile.Write([]byte(content))
|
|
|
|
return tmpfile.Name()
|
|
|
|
}
|
|
|
|
|
2019-11-23 03:52:29 +00:00
|
|
|
func ReadTempYamlFile(name string) string {
|
2021-06-28 21:05:32 +00:00
|
|
|
content, _ := ioutil.ReadFile(filepath.Clean(name))
|
2017-09-22 19:58:12 +00:00
|
|
|
return string(content)
|
|
|
|
}
|
|
|
|
|
2019-11-23 03:52:29 +00:00
|
|
|
func RemoveTempYamlFile(name string) {
|
2017-09-22 19:58:12 +00:00
|
|
|
_ = os.Remove(name)
|
|
|
|
}
|