-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlogging_test.go
47 lines (39 loc) · 1.42 KB
/
logging_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
package common_test
import (
"context"
"encoding/json"
"math/rand"
"strconv"
"testing"
"time"
. "github.com/cloudtrust/common-healthcheck"
"github.com/cloudtrust/common-healthcheck/mock"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
)
//go:generate mockgen --build_flags=--mod=mod -destination=./mock/logging.go -package=mock -mock_names=Logger=Logger github.com/go-kit/kit/log Logger
func init() {
rand.Seed(time.Now().UnixNano())
}
func TestLoggingMW(t *testing.T) {
var mockCtrl = gomock.NewController(t)
defer mockCtrl.Finish()
var mockLogger = mock.NewLogger(mockCtrl)
var mockHealthChecker = mock.NewHealthChecker(mockCtrl)
var (
m = MakeHealthCheckerLoggingMW(mockLogger)(mockHealthChecker)
corrID = strconv.FormatUint(rand.Uint64(), 10)
ctx = context.WithValue(context.Background(), "correlation_id", corrID)
healthCheckName = "name"
rep = json.RawMessage(`{"key":"value"}`)
)
mockHealthChecker.EXPECT().HealthCheck(ctx, healthCheckName).Return(rep, nil).Times(1)
mockLogger.EXPECT().Log("unit", "HealthCheck", "correlation_id", corrID, "took", gomock.Any()).Return(nil).Times(1)
m.HealthCheck(ctx, healthCheckName)
// Without correlation ID.
mockHealthChecker.EXPECT().HealthCheck(context.Background(), healthCheckName).Return(rep, nil).Times(1)
var f = func() {
m.HealthCheck(context.Background(), healthCheckName)
}
assert.Panics(t, f)
}