Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/integrations/v2/controller_updateintegration_test.go
5287 views
1
package integrations
2
3
import (
4
"context"
5
"sync"
6
"testing"
7
8
"github.com/go-kit/log"
9
"github.com/grafana/agent/pkg/util"
10
"github.com/stretchr/testify/require"
11
"go.uber.org/atomic"
12
)
13
14
//
15
// Tests for controller's utilization of the UpdateIntegration interface.
16
//
17
18
// Test_controller_UpdateIntegration ensures that the controller will call
19
// UpdateIntegration for integrations that support it.
20
func Test_controller_UpdateIntegration(t *testing.T) {
21
var (
22
integrationStartWg sync.WaitGroup
23
applies, starts atomic.Uint64
24
)
25
26
mockIntegration := mockUpdateIntegration{
27
Integration: FuncIntegration(func(ctx context.Context) error {
28
starts.Inc()
29
integrationStartWg.Done()
30
<-ctx.Done()
31
return nil
32
}),
33
ApplyConfigFunc: func(Config, Globals) error {
34
applies.Inc()
35
return nil
36
},
37
}
38
39
cfg := controllerConfig{
40
mockConfig{
41
NameFunc: func() string { return mockIntegrationName },
42
ConfigEqualsFunc: func(Config) bool { return false },
43
ApplyDefaultsFunc: func(g Globals) error { return nil },
44
IdentifierFunc: func(Globals) (string, error) {
45
return mockIntegrationName, nil
46
},
47
NewIntegrationFunc: func(log.Logger, Globals) (Integration, error) {
48
integrationStartWg.Add(1)
49
return mockIntegration, nil
50
},
51
},
52
}
53
54
ctrl, err := newController(util.TestLogger(t), cfg, Globals{})
55
require.NoError(t, err, "failed to create controller")
56
57
sc := newSyncController(t, ctrl)
58
59
// Wait for our integration to start.
60
integrationStartWg.Wait()
61
62
// Try to apply again.
63
require.NoError(t, sc.UpdateController(cfg, ctrl.globals), "failed to re-apply config")
64
integrationStartWg.Wait()
65
66
sc.Stop()
67
68
require.Equal(t, uint64(1), applies.Load(), "dynamic reload should have occurred")
69
require.Equal(t, uint64(1), starts.Load(), "restart should not have occurred")
70
}
71
72
type mockUpdateIntegration struct {
73
Integration
74
ApplyConfigFunc func(Config, Globals) error
75
}
76
77
func (m mockUpdateIntegration) ApplyConfig(c Config, g Globals) error {
78
return m.ApplyConfigFunc(c, g)
79
}
80
81