-
Notifications
You must be signed in to change notification settings - Fork 1
/
coprocess_bundle_test.go
71 lines (62 loc) · 1.78 KB
/
coprocess_bundle_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
// +build !python
package main
import (
"crypto/md5"
"fmt"
"io"
"os"
"path/filepath"
"testing"
)
var (
testBundlesPath = filepath.Join(testMiddlewarePath, "bundles")
)
var grpcBundleWithAuthCheck = map[string]string{
"manifest.json": `
{
"file_list": [],
"custom_middleware": {
"driver": "grpc",
"auth_check": {
"name": "MyAuthHook"
}
}
}
`,
}
func TestBundleLoader(t *testing.T) {
bundleID := registerBundle("grpc_with_auth_check", grpcBundleWithAuthCheck)
t.Run("Nonexistent bundle", func(t *testing.T) {
specs := buildAndLoadAPI(func(spec *APISpec) {
spec.CustomMiddlewareBundle = "nonexistent.zip"
})
err := loadBundle(specs[0])
if err == nil {
t.Fatal("Fetching a nonexistent bundle, expected an error")
}
})
t.Run("Existing bundle with auth check", func(t *testing.T) {
specs := buildAndLoadAPI(func(spec *APISpec) {
spec.CustomMiddlewareBundle = bundleID
})
spec := specs[0]
err := loadBundle(spec)
if err != nil {
t.Fatalf("Bundle not found: %s\n", bundleID)
}
bundleNameHash := md5.New()
io.WriteString(bundleNameHash, spec.CustomMiddlewareBundle)
bundleDir := fmt.Sprintf("%s_%x", spec.APIID, bundleNameHash.Sum(nil))
savedBundlePath := filepath.Join(testBundlesPath, bundleDir)
if _, err = os.Stat(savedBundlePath); os.IsNotExist(err) {
t.Fatalf("Bundle wasn't saved to disk: %s", err.Error())
}
// Check bundle contents:
if spec.CustomMiddleware.AuthCheck.Name != "MyAuthHook" {
t.Fatalf("Auth check function doesn't match: got %s, expected %s\n", spec.CustomMiddleware.AuthCheck.Name, "MyAuthHook")
}
if string(spec.CustomMiddleware.Driver) != "grpc" {
t.Fatalf("Driver doesn't match: got %s, expected %s\n", spec.CustomMiddleware.Driver, "grpc")
}
})
}