-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtemplates_test.go
94 lines (91 loc) · 3.03 KB
/
templates_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
package main
import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"testing"
cfclient "github.com/cloudfoundry-community/go-cfclient"
)
func TestGetNotifyEmail(t *testing.T) {
rootDataPath := filepath.Join("testdata", "mail", "notify")
updatedBuildpacksSingleApp := []buildpackReleaseInfo{
{
"python_buildpack",
"v1.7.43",
"https://github.com/cloudfoundry/python-buildpack/releases/tags/v1.7.43",
},
}
updatedBuildpacksMultipleApps := []buildpackReleaseInfo{
{
"python_buildpack",
"v1.7.43",
"https://github.com/cloudfoundry/python-buildpack/releases/tags/v1.7.43",
},
{
"ruby_buildpack",
"v1.8.43",
"https://github.com/cloudfoundry/ruby-buildpack/releases/tags/v1.8.43",
},
}
testCases := []struct {
name string
email notifyEmail
expectedEmail string
}{
{
"single app",
notifyEmail{"[email protected]", []cfclient.App{{Name: "my-drupal-app",
SpaceData: cfclient.SpaceResource{Entity: cfclient.Space{Name: "dev",
OrgData: cfclient.OrgResource{Entity: cfclient.Org{Name: "sandbox"}},
}},
}}, false, updatedBuildpacksSingleApp},
filepath.Join(rootDataPath, "single_app.txt"),
},
{
"multiple apps",
notifyEmail{"[email protected]", []cfclient.App{
{Name: "my-drupal-app",
SpaceData: cfclient.SpaceResource{Entity: cfclient.Space{Name: "dev",
OrgData: cfclient.OrgResource{Entity: cfclient.Org{Name: "sandbox"}},
}},
},
{Name: "my-wordpress-app",
SpaceData: cfclient.SpaceResource{Entity: cfclient.Space{Name: "staging",
OrgData: cfclient.OrgResource{Entity: cfclient.Org{Name: "paid-org"}},
}},
},
}, true, updatedBuildpacksMultipleApps},
filepath.Join(rootDataPath, "multiple_apps.txt"),
},
}
for _, tc := range testCases {
templates, err := initTemplates()
if err != nil {
t.Fatalf("Unable to init templates. Error %s", err.Error())
}
t.Run(tc.name, func(t *testing.T) {
body := new(bytes.Buffer)
err := templates.getNotifyEmail(body, tc.email)
if err != nil {
t.Errorf("Can't construct final email. Error %s", err.Error())
}
if os.Getenv("OVERRIDE_TEMPLATES") == "1" {
err := ioutil.WriteFile(tc.expectedEmail, body.Bytes(), 0644)
if err != nil {
t.Errorf("Can't save expected email. Error %s", err.Error())
}
}
expectedBody, err := ioutil.ReadFile(tc.expectedEmail)
if err != nil {
t.Fatalf("Unable to read expected file. %s", err.Error())
}
if string(expectedBody) != string(body.Bytes()) {
t.Logf("\n===========Expected %s e-mail case BEGIN===========\n%s\n===========Expected %s e-mail case END===========\n", tc.name, string(expectedBody), tc.name)
t.Logf("\n===========Actual %s e-mail case BEGIN===========\n%s\n===========Actual %s e-mail case END===========\n", tc.name, string(body.Bytes()), tc.name)
t.Errorf("Test %s failed. For the actual output, inspect %s.returned.", tc.name, filepath.Base(tc.expectedEmail))
ioutil.WriteFile(filepath.Join(rootDataPath, filepath.Base(tc.expectedEmail)+".returned"), body.Bytes(), 0644)
}
})
}
}