forked from convox/rack
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fixtures_test.go
110 lines (89 loc) · 2.29 KB
/
fixtures_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
package models_test
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/aryann/difflib"
"github.com/convox/rack/api/models"
"github.com/convox/rack/manifest"
"github.com/stretchr/testify/require"
)
func init() {
os.Setenv("AWS_REGION", "test")
os.Setenv("CLUSTER", "convox-test")
}
func TestFixtures(t *testing.T) {
fixtures, err := availableFixtures()
require.NotNil(t, fixtures)
require.NoError(t, err)
for _, fixture := range fixtures {
assertFixture(t, fixture)
}
}
func availableFixtures() ([]string, error) {
fixtures := []string{}
err := filepath.Walk("fixtures/", func(path string, info os.FileInfo, err error) error {
if strings.HasSuffix(path, ".yml") {
file := filepath.Base(path)
name := file[0 : len(file)-4]
fixtures = append(fixtures, name)
}
return nil
})
if err != nil {
return nil, err
}
return fixtures, nil
}
func assertFixture(t *testing.T, name string) {
orig := manifest.ManifestRandomPorts
manifest.ManifestRandomPorts = false
defer func() { manifest.ManifestRandomPorts = orig }()
app := models.App{
Name: "httpd",
Tags: map[string]string{
"Name": "httpd",
"Type": "app",
"System": "convox",
"Rack": "convox-test",
},
}
data, err := ioutil.ReadFile(fmt.Sprintf("fixtures/%s.yml", name))
require.NoError(t, err)
manifest, err := manifest.Load(data)
require.NoError(t, err)
formation, err := app.Formation(*manifest)
require.NoError(t, err)
pretty, err := models.PrettyJSON(formation)
require.NoError(t, err)
data, err = ioutil.ReadFile(fmt.Sprintf("fixtures/%s.json", name))
require.NoError(t, err)
diff1 := strings.Split(strings.TrimSpace(string(data)), "\n")
diff2 := strings.Split(strings.TrimSpace(pretty), "\n")
diff := difflib.Diff(diff1, diff2)
diffs := []string{}
// bigger than max
prev := 1000000
for l, d := range diff {
switch d.Delta {
case difflib.LeftOnly:
if (l - prev) > 1 {
diffs = append(diffs, "")
}
diffs = append(diffs, fmt.Sprintf("%04d - %s", l, d.Payload))
prev = l
case difflib.RightOnly:
if (l - prev) > 1 {
diffs = append(diffs, "")
}
diffs = append(diffs, fmt.Sprintf("%04d + %s", l, d.Payload))
prev = l
}
}
if len(diffs) > 0 {
t.Errorf("Unexpected results for %s:\n%s", name, strings.Join(diffs, "\n"))
}
}