-
Notifications
You must be signed in to change notification settings - Fork 479
/
componenttest.go
181 lines (153 loc) · 4.24 KB
/
componenttest.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Package componenttest provides utilities for testing Flow components.
package componenttest
import (
"context"
"fmt"
"os"
"reflect"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
"go.opentelemetry.io/otel/trace"
"go.uber.org/atomic"
"github.com/go-kit/log"
"github.com/grafana/agent/component"
"github.com/grafana/agent/pkg/flow/logging"
)
// A Controller is a testing controller which controls a single component.
type Controller struct {
reg component.Registration
log log.Logger
onRun sync.Once
running chan struct{}
runError atomic.Error
innerMut sync.Mutex
inner component.Component
exportsMut sync.Mutex
exports component.Exports
exportsCh chan struct{}
}
// NewControllerFromID returns a new testing Controller for the component with
// the provided name.
func NewControllerFromID(l log.Logger, componentName string) (*Controller, error) {
reg, ok := component.Get(componentName)
if !ok {
return nil, fmt.Errorf("no such component %q", componentName)
}
return NewControllerFromReg(l, reg), nil
}
// NewControllerFromReg registers a new testing Controller for a component with
// the given registration. This can be used for testing fake components which
// aren't really registered.
func NewControllerFromReg(l log.Logger, reg component.Registration) *Controller {
if l == nil {
l = log.NewNopLogger()
}
return &Controller{
reg: reg,
log: l,
running: make(chan struct{}, 1),
exportsCh: make(chan struct{}, 1),
}
}
func (c *Controller) onStateChange(e component.Exports) {
c.exportsMut.Lock()
changed := !reflect.DeepEqual(c.exports, e)
c.exports = e
c.exportsMut.Unlock()
if !changed {
return
}
select {
case c.exportsCh <- struct{}{}:
default:
}
}
// WaitRunning blocks until the Controller is running up to the provided
// timeout.
func (c *Controller) WaitRunning(timeout time.Duration) error {
select {
case <-time.After(timeout):
return fmt.Errorf("timed out waiting for the controller to start running")
case <-c.running:
if err := c.runError.Load(); err != nil {
return fmt.Errorf("component failed to start: %w", err)
}
return nil
}
}
// WaitExports blocks until new Exports are available up to the provided
// timeout.
func (c *Controller) WaitExports(timeout time.Duration) error {
select {
case <-time.After(timeout):
return fmt.Errorf("timed out waiting for exports")
case <-c.exportsCh:
return nil
}
}
// Exports gets the most recent exports for a component.
func (c *Controller) Exports() component.Exports {
c.exportsMut.Lock()
defer c.exportsMut.Unlock()
return c.exports
}
// Run starts the controller, building and running the component. Run blocks
// until ctx is canceled, the component exits, or if there was an error.
//
// Run may only be called once per Controller.
func (c *Controller) Run(ctx context.Context, args component.Arguments) error {
dataPath, err := os.MkdirTemp("", "controller-*")
if err != nil {
return err
}
defer func() {
_ = os.RemoveAll(dataPath)
}()
run, err := c.buildComponent(dataPath, args)
// We close c.running before checking the error, since the component will
// never run if we return an error anyway.
c.onRun.Do(func() {
c.runError.Store(err)
close(c.running)
})
if err != nil {
return err
}
return run.Run(ctx)
}
func (c *Controller) buildComponent(dataPath string, args component.Arguments) (component.Component, error) {
c.innerMut.Lock()
defer c.innerMut.Unlock()
writerAdapter := log.NewStdlibAdapter(c.log)
l, err := logging.New(writerAdapter, logging.Options{
Level: logging.LevelDebug,
Format: logging.FormatLogfmt,
})
if err != nil {
return nil, err
}
opts := component.Options{
ID: c.reg.Name + ".test",
Logger: l,
Tracer: trace.NewNoopTracerProvider(),
DataPath: dataPath,
OnStateChange: c.onStateChange,
Registerer: prometheus.NewRegistry(),
}
inner, err := c.reg.Build(opts, args)
if err != nil {
return nil, err
}
c.inner = inner
return inner, nil
}
// Update updates the running component. Should only be called after Run.
func (c *Controller) Update(args component.Arguments) error {
c.innerMut.Lock()
defer c.innerMut.Unlock()
if c.inner == nil {
return fmt.Errorf("component is not running")
}
return c.inner.Update(args)
}