forked from rjocoleman/app
/
main_test.go
107 lines (81 loc) · 2.39 KB
/
main_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
package main
import (
"fmt"
"io/ioutil"
"path/filepath"
"reflect"
"strings"
"testing"
"github.com/convox/app/Godeps/_workspace/src/github.com/aryann/difflib"
yaml "github.com/convox/app/Godeps/_workspace/src/gopkg.in/yaml.v2"
)
type Cases []struct {
got, want interface{}
}
func TestManifestEntryNames(t *testing.T) {
var manifest Manifest
man := readFile(t, "fixtures", "web_postgis.yml")
yaml.Unmarshal(man, &manifest)
cases := Cases{
{manifest.EntryNames(), []string{"postgres", "web"}},
}
_assert(t, cases)
}
func TestStagingWebPostgis(t *testing.T) {
manifest := readManifest(t, "fixtures", "web_postgis.yml")
template := readFile(t, "fixtures", "web_postgis.json")
data, _ := buildTemplate("staging", "formation", func() string { return "12345" }, manifest)
cases := Cases{
{strings.TrimSpace(data), strings.TrimSpace(string(template))},
}
_assert(t, cases)
}
func TestStagingWorker(t *testing.T) {
manifest := readManifest(t, "fixtures", "worker.yml")
template := readFile(t, "fixtures", "worker.json")
data, _ := buildTemplate("staging", "formation", func() string { return "12345" }, manifest)
cases := Cases{
{strings.TrimSpace(data), strings.TrimSpace(string(template))},
}
_assert(t, cases)
}
func readFile(t *testing.T, dir string, name string) []byte {
filename := filepath.Join(dir, name)
dat, err := ioutil.ReadFile(filename)
if err != nil {
t.Errorf("ERROR readFile %v %v", filename, err)
}
return dat
}
func readManifest(t *testing.T, dir string, name string) Manifest {
man := readFile(t, dir, name)
var manifest Manifest
err := yaml.Unmarshal(man, &manifest)
if err != nil {
t.Errorf("ERROR readManifest %v %v", filepath.Join(dir, name), err)
}
return manifest
}
func _assert(t *testing.T, cases Cases) {
for _, c := range cases {
if !reflect.DeepEqual(c.got, c.want) {
if s1, ok := c.got.(string); ok {
if s2, ok := c.want.(string); ok {
diff := difflib.Diff(strings.Split(s1, "\n"), strings.Split(s2, "\n"))
diffs := []string{}
for l, d := range diff {
switch d.Delta {
case difflib.LeftOnly:
diffs = append(diffs, fmt.Sprintf("%04d - %s", l, d.Payload))
case difflib.RightOnly:
diffs = append(diffs, fmt.Sprintf("%04d + %s", l, d.Payload))
}
}
t.Errorf("Unexpected result:\n%s", strings.Join(diffs, "\n"))
return
}
}
t.Errorf("%q\n%q\n", c.got, c.want)
}
}
}