-
Notifications
You must be signed in to change notification settings - Fork 0
/
golden_test.go
129 lines (106 loc) · 2.74 KB
/
golden_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"bytes"
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"testing"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)
const (
GOLDEN_PATH = "tests/golden"
)
var (
ext = regexp.MustCompile(`\.ya?ml$`)
)
type Instance struct {
Environments map[string]Environment `yaml:"environments"`
}
type Environment struct {
Refs []string `yaml:"refs"`
Vars map[string]string `yaml:"vars"`
Params []string `yaml:"params"`
}
func init() {
logrus.SetOutput(io.Discard)
}
// GetRefs returns the list of refs defined in the test spec, or just an empty
// string if no refs have been defined.
func (e *Environment) GetRefs() []string {
if len(e.Refs) < 1 {
return []string{""}
}
return e.Refs
}
func TestGolden(t *testing.T) {
instances := findInstances()
for _, i := range instances {
t.Run(i, func(t *testing.T) {
testInstance(t, i)
})
}
}
func findInstances() []string {
c, err := os.ReadDir(GOLDEN_PATH)
check(err, "finding instances")
instances := make([]string, 0)
ext := regexp.MustCompile(`\.ya?ml$`)
for _, e := range c {
if ext.MatchString(e.Name()) && !e.IsDir() {
instances = append(instances, e.Name())
}
}
return instances
}
func testInstance(t *testing.T, instanceFile string) {
f, err := os.Open(filepath.Join(GOLDEN_PATH, instanceFile))
check(err, "reading golden test definition")
instance := ext.ReplaceAllString(instanceFile, "")
i := Instance{}
check(yaml.NewDecoder(f).Decode(&i), "decoding golden test definition")
old, err := os.Getwd()
check(err, "determining current working directory")
root := filepath.Join(GOLDEN_PATH, instance)
check(os.Chdir(root), "changing working directory")
for name, env := range i.Environments {
t.Run(name, func(t *testing.T) {
testEnvironment(t, name, env)
})
}
check(os.Chdir(old), "resetting working directory")
}
func testEnvironment(t *testing.T, envName string, env Environment) {
for k, v := range env.Vars {
t.Setenv(k, v)
}
for _, ref := range env.GetRefs() {
t.Run(ref, func(t *testing.T) {
args := []string{"k8ify", envName, ref}
args = append(args, env.Params...)
fmt.Printf("Running %v", args)
var logs bytes.Buffer
logrus.SetOutput(&logs)
if c := Main(args); c != 0 {
t.Errorf("k8ify exited with code %v while compiling with args '%v'\n%s", c, args, logs.String())
}
cmd := exec.Command("git", "diff", "--exit-code", "--minimal", "--", "manifests/")
if out, err := cmd.CombinedOutput(); err != nil {
t.Errorf("error from git diff: %v", err)
fmt.Println(string(out))
}
})
}
}
func check(err error, context string) {
if err != nil {
_, cf, cl, _ := runtime.Caller(0)
cf = filepath.Base(cf)
log.Fatalf("%s:%d: Error %s: %v", cf, cl, context, err)
}
}