-
Notifications
You must be signed in to change notification settings - Fork 73
/
integration_test.go
189 lines (164 loc) · 5.21 KB
/
integration_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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package integration_test
import (
"bufio"
"bytes"
"context"
"encoding/json"
"flag"
"os"
"path/filepath"
"sort"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/mbrt/gmailctl/internal/engine/api"
"github.com/mbrt/gmailctl/internal/engine/apply"
"github.com/mbrt/gmailctl/internal/engine/config"
"github.com/mbrt/gmailctl/internal/engine/config/v1alpha3"
"github.com/mbrt/gmailctl/internal/engine/export/xml"
"github.com/mbrt/gmailctl/internal/engine/rimport"
"github.com/mbrt/gmailctl/internal/fakegmail"
)
// update is useful to regenerate the golden files
// Make sure the new version makes sense!!
var update = flag.Bool("update", false, "update golden files")
var fixedTime = mustParseTime("2006-01-02 15:04", "2018-03-08 17:00")
func globTestdataPaths(t *testing.T, pattern string) []string {
t.Helper()
fs, err := filepath.Glob(filepath.Join("testdata", pattern))
if err != nil {
t.Fatal(err)
}
sort.Strings(fs)
return fs
}
func TestIntegration(t *testing.T) {
cfgPaths := globTestdataPaths(t, "valid/*.jsonnet")
gapi := api.NewFromService(fakegmail.NewService(context.Background(), t))
for _, cfgPath := range cfgPaths {
name := strings.TrimSuffix(cfgPath, ".jsonnet")
t.Run(name, func(t *testing.T) {
// Parse the config.
cfg, err := config.ReadFile(cfgPath, filepath.Join("testdata", cfgPath))
require.Nil(t, err)
pres, err := apply.FromConfig(cfg)
require.Nil(t, err)
// Export.
xmlexp := xml.NewWithTime(func() time.Time { return fixedTime })
var cfgxml bytes.Buffer
bw := bufio.NewWriter(&cfgxml)
author := v1alpha3.Author{Name: "Me", Email: "me@gmail.com"}
err = xmlexp.Export(author, pres.Filters, bw)
bw.Flush() // Make sure everything is written out.
require.Nil(t, err)
// Fetch the upstream filters.
upres, err := apply.FromAPI(gapi)
require.Nil(t, err)
// Apply the diff.
d, err := apply.Diff(pres.GmailConfig, upres, false)
require.Nil(t, err)
err = apply.Apply(d, gapi, true)
require.Nil(t, err)
// Import.
upres, err = apply.FromAPI(gapi)
require.Nil(t, err)
icfg, err := rimport.Import(upres.Filters, upres.Labels)
require.Nil(t, err)
icfgJSON, err := json.MarshalIndent(icfg, "", " ")
require.Nil(t, err)
// There should be no diff now with upstream.
assertEmptyDiff(t, pres.GmailConfig, upres)
// Import and convert to Jsonnet.
var buf bytes.Buffer
w := bufio.NewWriter(&buf)
err = rimport.MarshalJsonnet(icfg, w, "// Download header.\n")
require.Nil(t, err)
err = w.Flush()
require.Nil(t, err)
// Compare the results with the golden files (or update the golden files).
if *update {
// Import.
err := os.WriteFile(name+".json", icfgJSON, 0o600)
require.Nil(t, err)
// Diff.
err = os.WriteFile(name+".diff", []byte(d.String()), 0o600)
require.Nil(t, err)
// Export.
err = os.WriteFile(name+".xml", cfgxml.Bytes(), 0o600)
require.Nil(t, err)
return
}
// Import.
b, err := os.ReadFile(name + ".json")
require.Nil(t, err)
assert.Equal(t, string(b), string(icfgJSON))
// Diff.
b, err = os.ReadFile(name + ".diff")
require.Nil(t, err)
assert.Equal(t, string(b), d.String())
// Export
b, err = os.ReadFile(name + ".xml")
require.Nil(t, err)
assert.Equal(t, string(b), cfgxml.String())
})
}
}
func TestIntegrationImportExport(t *testing.T) {
cfgPaths := globTestdataPaths(t, "valid/*.jsonnet")
gapi := api.NewFromService(fakegmail.NewService(context.Background(), t))
for _, cfgPath := range cfgPaths {
name := strings.TrimSuffix(cfgPath, ".jsonnet")
t.Run(name, func(t *testing.T) {
// Parse the config.
cfg, err := config.ReadFile(cfgPath, filepath.Join("testdata", cfgPath))
require.Nil(t, err)
pres, err := apply.FromConfig(cfg)
require.Nil(t, err)
// Fetch the upstream filters.
upres, err := apply.FromAPI(gapi)
require.Nil(t, err)
// Apply the diff.
d, err := apply.Diff(pres.GmailConfig, upres, false)
require.Nil(t, err)
err = apply.Apply(d, gapi, true)
require.Nil(t, err)
// Import.
upres, err = apply.FromAPI(gapi)
require.Nil(t, err)
icfg, err := rimport.Import(upres.Filters, upres.Labels)
require.Nil(t, err)
// There should be no diff between the original and the imported config.
ipres, err := apply.FromConfig(icfg)
require.Nil(t, err)
assertEmptyDiff(t, ipres.GmailConfig, upres)
// Convert to Jsonnet.
var buf bytes.Buffer
w := bufio.NewWriter(&buf)
err = rimport.MarshalJsonnet(icfg, w, "// Download header.\n")
require.Nil(t, err)
err = w.Flush()
require.Nil(t, err)
// There should be no diff between the original and the converted config.
ijcfg, err := config.ReadJsonnet("", buf.Bytes())
require.Nil(t, err)
ijpres, err := apply.FromConfig(ijcfg)
require.Nil(t, err)
assertEmptyDiff(t, ijpres.GmailConfig, upres)
})
}
}
func assertEmptyDiff(t *testing.T, local, remote apply.GmailConfig) {
d, err := apply.Diff(local, remote, false)
require.Nil(t, err)
assert.Empty(t, d.FiltersDiff)
assert.Empty(t, d.LabelsDiff)
}
func mustParseTime(layout, value string) time.Time {
t, err := time.Parse(layout, value)
if err != nil {
panic(err)
}
return t
}