/
logger_plugin_test.go
71 lines (60 loc) · 1.82 KB
/
logger_plugin_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
package iopipe
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestLoggerPlugin_LoggerPlugin(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(http.StatusOK)
}))
defer ts.Close()
ts2 := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", "application/json")
signerResponse := &SignerResponse{
JWTAccess: "foobar",
SignedRequest: ts.URL,
URL: "https://some-url",
}
signerResponseJSONBytes, _ := json.Marshal(signerResponse)
fmt.Fprintln(res, string(signerResponseJSONBytes))
}))
defer ts2.Close()
oldRegion := os.Getenv("AWS_REGION")
defer os.Setenv("AWS_REGION", oldRegion)
os.Setenv("AWS_REGION", "mock")
os.Setenv("MOCK_SERVER", ts2.URL)
Convey("Logger plugin should be initialized by agent", t, func() {
a := NewAgent(Config{
Debug: True(),
Plugins: []PluginInstantiator{
LoggerPlugin(LoggerPluginConfig{}),
},
})
So(len(a.plugins), ShouldEqual, 1)
a.Reporter = func(report *Report) error {
return nil
}
hw := NewHandlerWrapper(func(ctx context.Context, payload interface{}) (interface{}, error) {
context, _ := FromContext(ctx)
context.IOpipe.Log.Debug("Some debug message")
context.IOpipe.Log.Info("Some info message")
context.IOpipe.Log.Warning("Some warning message")
context.IOpipe.Log.Error("Some error message")
return nil, nil
}, a)
Convey("Logger plugin invoke hooks fired", func() {
ctx := context.Background()
hw.Invoke(ctx, nil)
Convey("A logger label is added to report", func() {
_, exists := hw.report.labels["@iopipe/plugin-logger"]
So(exists, ShouldBeTrue)
})
})
})
}