-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathpackage_test.go
363 lines (343 loc) · 9.99 KB
/
package_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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package serve
import (
"crypto/sha256"
_ "embed"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/cloudquery/plugin-sdk/v4/internal/memdb"
"github.com/cloudquery/plugin-sdk/v4/plugin"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"
)
//go:embed testdata/memdbtables.json
var memDBPackageJSON string
//go:embed testdata/source_spec_schema.json
var sourceSpecSchema string
//go:embed testdata/destination_spec_schema.json
var destinationSpecSchema string
func TestPluginPackage_Source(t *testing.T) {
_, filename, _, ok := runtime.Caller(0)
if !ok {
t.Fatal("failed to get current file path")
}
dir := filepath.Dir(filepath.Dir(filename))
simplePluginPath := filepath.Join(dir, "examples/simple_plugin")
packageVersion := "v1.2.3"
p := plugin.NewPlugin(
"test-plugin",
"development",
memdb.NewMemDBClient,
plugin.WithBuildTargets([]plugin.BuildTarget{
{OS: plugin.GoOSLinux, Arch: plugin.GoArchAmd64},
{OS: plugin.GoOSWindows, Arch: plugin.GoArchAmd64},
}),
plugin.WithKind("source"),
plugin.WithTeam("test-team"),
plugin.WithJSONSchema(sourceSpecSchema),
)
msg := `Test message
with multiple lines and **markdown**`
testCases := []struct {
name string
message string
wantErr bool
}{
{
name: "inline message",
message: msg,
},
{
name: "message from file",
message: "@testdata/message.txt",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
t.Setenv("CGO_ENABLED", "0") // disable CGO to ensure we environmental differences don't interfere with the test
srv := Plugin(p)
cmd := srv.newCmdPluginRoot()
distDir := t.TempDir()
cmd.SetArgs([]string{"package", "--dist-dir", distDir, "-m", tc.message, packageVersion, simplePluginPath})
err := cmd.Execute()
if tc.wantErr && err == nil {
t.Fatalf("expected error, got nil")
} else if err != nil {
t.Fatalf("unexpected error: %v", err)
}
files, err := os.ReadDir(distDir)
if err != nil {
t.Fatal(err)
}
expect := []string{
"docs",
"package.json",
"plugin-test-plugin-v1.2.3-linux-amd64.zip",
"plugin-test-plugin-v1.2.3-windows-amd64.zip",
"spec_json_schema.json",
"tables.json",
}
if diff := cmp.Diff(expect, fileNames(files)); diff != "" {
t.Fatalf("unexpected files in dist directory (-want +got):\n%s", diff)
}
// expect SHA-256 for the zip files to differ
sha1 := sha256sum(filepath.Join(distDir, "plugin-test-plugin-v1.2.3-linux-amd64.zip"))
sha2 := sha256sum(filepath.Join(distDir, "plugin-test-plugin-v1.2.3-windows-amd64.zip"))
if sha1 == sha2 {
t.Fatalf("expected SHA-256 for linux and windows zip files to differ, but they are the same: %s", sha1)
}
expectPackage := PackageJSON{
SchemaVersion: 1,
Name: "test-plugin",
Team: "test-team",
Kind: "source",
Message: msg,
Version: packageVersion,
Protocols: []int{3},
SupportedTargets: []TargetBuild{
{OS: plugin.GoOSLinux, Arch: plugin.GoArchAmd64, Path: "plugin-test-plugin-v1.2.3-linux-amd64.zip", Checksum: "sha256:" + sha256sum(filepath.Join(distDir, "plugin-test-plugin-v1.2.3-linux-amd64.zip"))},
{OS: plugin.GoOSWindows, Arch: plugin.GoArchAmd64, Path: "plugin-test-plugin-v1.2.3-windows-amd64.zip", Checksum: "sha256:" + sha256sum(filepath.Join(distDir, "plugin-test-plugin-v1.2.3-windows-amd64.zip"))},
},
PackageType: plugin.PackageTypeNative,
}
checkPackageJSONContents(t, filepath.Join(distDir, "package.json"), expectPackage)
expectDocs := []string{
"configuration.md",
"overview.md",
}
checkDocs(t, filepath.Join(distDir, "docs"), expectDocs)
checkTables(t, distDir)
checkFileContent(t, filepath.Join(distDir, "spec_json_schema.json"), sourceSpecSchema)
})
}
}
func TestPluginPackage_Destination(t *testing.T) {
_, filename, _, ok := runtime.Caller(0)
if !ok {
t.Fatal("failed to get current file path")
}
dir := filepath.Dir(filepath.Dir(filename))
simplePluginPath := filepath.Join(dir, "examples/simple_plugin")
packageVersion := "v1.2.3"
p := plugin.NewPlugin(
"test-plugin",
"development",
memdb.NewMemDBClient,
plugin.WithBuildTargets([]plugin.BuildTarget{
{OS: plugin.GoOSWindows, Arch: plugin.GoArchAmd64},
{OS: plugin.GoOSDarwin, Arch: plugin.GoArchAmd64},
}),
plugin.WithKind("destination"),
plugin.WithTeam("test-team"),
plugin.WithJSONSchema(destinationSpecSchema),
)
msg := `Test message
with multiple lines and **markdown**`
testCases := []struct {
name string
message string
wantErr bool
}{
{
name: "inline message",
message: msg,
},
{
name: "message from file",
message: "@testdata/message.txt",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
t.Setenv("CGO_ENABLED", "0") // disable CGO to ensure we environmental differences don't interfere with the test
srv := Plugin(p)
cmd := srv.newCmdPluginRoot()
distDir := t.TempDir()
cmd.SetArgs([]string{"package", "--dist-dir", distDir, "-m", tc.message, packageVersion, simplePluginPath})
err := cmd.Execute()
if tc.wantErr && err == nil {
t.Fatalf("expected error, got nil")
} else if err != nil {
t.Fatalf("unexpected error: %v", err)
}
files, err := os.ReadDir(distDir)
if err != nil {
t.Fatal(err)
}
expect := []string{
"docs",
"package.json",
"plugin-test-plugin-v1.2.3-darwin-amd64.zip",
"plugin-test-plugin-v1.2.3-windows-amd64.zip",
"spec_json_schema.json",
}
if diff := cmp.Diff(expect, fileNames(files)); diff != "" {
t.Fatalf("unexpected files in dist directory (-want +got):\n%s", diff)
}
// expect SHA-256 for the zip files to differ
sha1 := sha256sum(filepath.Join(distDir, "plugin-test-plugin-v1.2.3-windows-amd64.zip"))
sha2 := sha256sum(filepath.Join(distDir, "plugin-test-plugin-v1.2.3-darwin-amd64.zip"))
if sha1 == sha2 {
t.Fatalf("expected SHA-256 for windows and darwin zip files to differ, but they are the same: %s", sha1)
}
expectPackage := PackageJSON{
SchemaVersion: 1,
Team: "test-team",
Kind: "destination",
Name: "test-plugin",
Message: msg,
Version: "v1.2.3",
Protocols: []int{3},
SupportedTargets: []TargetBuild{
{OS: plugin.GoOSWindows, Arch: plugin.GoArchAmd64, Path: "plugin-test-plugin-v1.2.3-windows-amd64.zip", Checksum: "sha256:" + sha256sum(filepath.Join(distDir, "plugin-test-plugin-v1.2.3-windows-amd64.zip"))},
{OS: plugin.GoOSDarwin, Arch: plugin.GoArchAmd64, Path: "plugin-test-plugin-v1.2.3-darwin-amd64.zip", Checksum: "sha256:" + sha256sum(filepath.Join(distDir, "plugin-test-plugin-v1.2.3-darwin-amd64.zip"))},
},
PackageType: plugin.PackageTypeNative,
}
checkPackageJSONContents(t, filepath.Join(distDir, "package.json"), expectPackage)
expectDocs := []string{
"configuration.md",
"overview.md",
}
checkDocs(t, filepath.Join(distDir, "docs"), expectDocs)
checkFileContent(t, filepath.Join(distDir, "spec_json_schema.json"), destinationSpecSchema)
})
}
}
func TestVersionRegex(t *testing.T) {
t.Parallel()
re := Plugin(&plugin.Plugin{}).versionRegex()
testCases := []struct {
input string
want bool
}{
{
input: `var Version = ""`,
want: true,
},
{
input: `var (
Version = ""
)
`,
want: true,
},
{
input: ` var Version = ""`,
want: false,
},
{
input: `var (
Version = ""
)
`,
},
{
input: `var (
Version = ""
)
`,
want: true,
},
}
for i, tc := range testCases {
tc := tc
t.Run(fmt.Sprintf("Case %d", i+1), func(t *testing.T) {
t.Parallel()
// only match the line with "Version"
lines := strings.Split(tc.input, "\n")
realInput := ""
for _, line := range lines {
if strings.Contains(line, "Version") {
realInput = line
break
}
}
if realInput == "" {
t.Fatalf("failed to find line with Version: %q", tc.input)
}
got := re.MatchString(realInput)
require.Equalf(t, tc.want, got, "input: %q", realInput)
})
}
}
func sha256sum(filename string) string {
f, err := os.Open(filename)
if err != nil {
panic(err)
}
defer f.Close()
h := sha256.New()
_, err = io.Copy(h, f)
if err != nil {
panic(err)
}
return fmt.Sprintf("%x", h.Sum(nil))
}
func checkDocs(t *testing.T, dir string, expect []string) {
files, err := os.ReadDir(dir)
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(expect, fileNames(files)); diff != "" {
t.Fatalf("unexpected files in docs directory (-want +got):\n%s", diff)
}
}
func checkTables(t *testing.T, distDir string) {
content, err := os.ReadFile(filepath.Join(distDir, "tables.json"))
if err != nil {
t.Fatal(err)
}
var actual any
err = json.Unmarshal(content, &actual)
require.NoError(t, err)
var expected any
err = json.Unmarshal([]byte(memDBPackageJSON), &expected)
require.NoError(t, err)
require.Equal(t, expected, actual, "tables.json content mismatch")
}
func checkPackageJSONContents(t *testing.T, filename string, expect PackageJSON) {
f, err := os.Open(filename)
if err != nil {
t.Fatalf("failed to open package.json: %v", err)
}
defer f.Close()
b, err := io.ReadAll(f)
if err != nil {
t.Fatalf("failed to read package.json: %v", err)
}
j := PackageJSON{}
err = json.Unmarshal(b, &j)
if err != nil {
t.Fatalf("failed to unmarshal package.json: %v", err)
}
if diff := cmp.Diff(expect, j); diff != "" {
t.Fatalf("package.json contents mismatch (-want +got):\n%s", diff)
}
}
func checkFileContent(t *testing.T, filename string, expect string) {
f, err := os.Open(filename)
if err != nil {
t.Fatalf("failed to open %s: %v", filename, err)
}
defer f.Close()
b, err := io.ReadAll(f)
if err != nil {
t.Fatalf("failed to read %s: %v", filename, err)
}
if diff := cmp.Diff(expect, string(b)); diff != "" {
t.Fatalf("%s contents mismatch (-want +got):\n%s", filename, diff)
}
}
func fileNames(files []os.DirEntry) []string {
names := make([]string, 0, len(files))
for _, file := range files {
names = append(names, file.Name())
}
return names
}