-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
mocks.go
128 lines (105 loc) · 3.98 KB
/
mocks.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package testutil // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/testutil"
import (
context "context"
"testing"
"time"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zaptest"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator"
)
// NewMockOperator will return a basic operator mock
func NewMockOperator(id string) *Operator {
mockOutput := &Operator{}
mockOutput.On("ID").Return(id)
mockOutput.On("CanProcess").Return(true)
mockOutput.On("CanOutput").Return(true)
return mockOutput
}
// FakeOutput is an empty output used primarily for testing
type FakeOutput struct {
Received chan *entry.Entry
*zap.SugaredLogger
}
// NewFakeOutput creates a new fake output with default settings
func NewFakeOutput(t testing.TB) *FakeOutput {
return &FakeOutput{
Received: make(chan *entry.Entry, 100),
SugaredLogger: zaptest.NewLogger(t).Sugar(),
}
}
// CanOutput always returns false for a fake output
func (f *FakeOutput) CanOutput() bool { return false }
// CanProcess always returns true for a fake output
func (f *FakeOutput) CanProcess() bool { return true }
// ID always returns `fake` as the ID of a fake output operator
func (f *FakeOutput) ID() string { return "fake" }
// Logger returns the logger of a fake output
func (f *FakeOutput) Logger() *zap.SugaredLogger { return f.SugaredLogger }
// Outputs always returns nil for a fake output
func (f *FakeOutput) Outputs() []operator.Operator { return nil }
// Outputs always returns nil for a fake output
func (f *FakeOutput) GetOutputIDs() []string { return nil }
// SetOutputs immediately returns nil for a fake output
func (f *FakeOutput) SetOutputs(_ []operator.Operator) error { return nil }
// SetOutputIDs immediately returns nil for a fake output
func (f *FakeOutput) SetOutputIDs(_ []string) {}
// Start immediately returns nil for a fake output
func (f *FakeOutput) Start(_ operator.Persister) error { return nil }
// Stop immediately returns nil for a fake output
func (f *FakeOutput) Stop() error { return nil }
// Type always return `fake_output` for a fake output
func (f *FakeOutput) Type() string { return "fake_output" }
// Process will place all incoming entries on the Received channel of a fake output
func (f *FakeOutput) Process(_ context.Context, entry *entry.Entry) error {
f.Received <- entry
return nil
}
// ExpectBody expects that a body will be received by the fake operator within a second
// and that it is equal to the given body
func (f *FakeOutput) ExpectBody(t testing.TB, body any) {
select {
case e := <-f.Received:
require.Equal(t, body, e.Body)
case <-time.After(time.Second):
require.FailNowf(t, "Timed out waiting for entry", "%s", body)
}
}
// ExpectEntry expects that an entry will be received by the fake operator within a second
// and that it is equal to the given body
func (f *FakeOutput) ExpectEntry(t testing.TB, expected *entry.Entry) {
select {
case e := <-f.Received:
require.Equal(t, expected, e)
case <-time.After(time.Second):
require.FailNowf(t, "Timed out waiting for entry", "%v", expected)
}
}
// ExpectEntries expects that the given entries will be received in any order
func (f *FakeOutput) ExpectEntries(t testing.TB, expected []*entry.Entry) {
entries := make([]*entry.Entry, 0, len(expected))
for i := 0; i < len(expected); i++ {
select {
case e := <-f.Received:
entries = append(entries, e)
case <-time.After(time.Second):
require.Fail(t, "Timed out waiting for entry")
}
if t.Failed() {
break
}
}
require.ElementsMatch(t, expected, entries)
}
// ExpectNoEntry expects that no entry will be received within the specified time
func (f *FakeOutput) ExpectNoEntry(t testing.TB, timeout time.Duration) {
select {
case <-f.Received:
require.FailNow(t, "Should not have received entry")
case <-time.After(timeout):
return
}
}