-
Notifications
You must be signed in to change notification settings - Fork 6
/
query_test.go
105 lines (92 loc) · 2.76 KB
/
query_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
// Copyright 2022 The incite Authors. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package incite
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/mock"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"github.com/stretchr/testify/assert"
)
func TestQuery(t *testing.T) {
t.Run("Nil Context", func(t *testing.T) {
assert.PanicsWithValue(t, nilContextMsg, func() {
_, _ = Query(nil, newMockActions(t), QuerySpec{})
})
})
t.Run("Bad Query", func(t *testing.T) {
// ACT.
r, err := Query(context.Background(), newMockActions(t), QuerySpec{})
// ASSERT.
assert.Nil(t, r)
assert.EqualError(t, err, "incite: blank query text")
})
t.Run("Run to Completion", func(t *testing.T) {
// ARRANGE.
actions := newMockActions(t)
actions.
On("StartQueryWithContext", anyContext, &cloudwatchlogs.StartQueryInput{
QueryString: sp("x"),
LogGroupNames: []*string{sp("y")},
StartTime: startTimeMilliseconds(defaultStart),
EndTime: endTimeMilliseconds(defaultEnd),
Limit: int64p(DefaultLimit),
}).
Return(&cloudwatchlogs.StartQueryOutput{QueryId: sp("ham")}, nil).
Once()
actions.On("GetQueryResultsWithContext", anyContext, &cloudwatchlogs.GetQueryResultsInput{
QueryId: sp("ham"),
}).Return(&cloudwatchlogs.GetQueryResultsOutput{
Status: sp(cloudwatchlogs.QueryStatusComplete),
Results: [][]*cloudwatchlogs.ResultField{},
}, nil).Once()
// ACT.
r, err := Query(context.Background(), actions, QuerySpec{
Text: "x",
Groups: []string{"y"},
Start: defaultStart,
End: defaultEnd,
})
// ASSERT.
assert.Equal(t, []Result{}, r)
assert.NoError(t, err)
actions.AssertExpectations(t)
})
t.Run("Context Cancelled", func(t *testing.T) {
// ARRANGE.
timer := time.NewTimer(50 * time.Millisecond)
actions := newMockActions(t)
actions.
On("StartQueryWithContext", anyContext, anyStartQueryInput).
WaitUntil(timer.C).
Return(&cloudwatchlogs.StartQueryOutput{QueryId: sp("eggs")}, nil).
Maybe()
actions.
On("GetQueryResultsWithContext", anyContext, mock.Anything).
Return(&cloudwatchlogs.GetQueryResultsOutput{
Status: sp(cloudwatchlogs.QueryStatusComplete),
}, nil).
Maybe()
trueValue := true
actions.On("StopQueryWithContext", anyContext, mock.Anything).
Return(&cloudwatchlogs.StopQueryOutput{
Success: &trueValue,
}, nil).
Maybe()
ctx, cancel := context.WithCancel(context.Background())
cancel()
// ACT.
r, err := Query(ctx, actions, QuerySpec{
Text: "x",
Groups: []string{"y"},
Start: defaultStart,
End: defaultEnd,
})
// ASSERT.
assert.Nil(t, r)
assert.Same(t, context.Canceled, err)
actions.AssertExpectations(t)
})
}