Path: blob/main/pkg/integrations/v2/controller_updateintegration_test.go
5287 views
package integrations12import (3"context"4"sync"5"testing"67"github.com/go-kit/log"8"github.com/grafana/agent/pkg/util"9"github.com/stretchr/testify/require"10"go.uber.org/atomic"11)1213//14// Tests for controller's utilization of the UpdateIntegration interface.15//1617// Test_controller_UpdateIntegration ensures that the controller will call18// UpdateIntegration for integrations that support it.19func Test_controller_UpdateIntegration(t *testing.T) {20var (21integrationStartWg sync.WaitGroup22applies, starts atomic.Uint6423)2425mockIntegration := mockUpdateIntegration{26Integration: FuncIntegration(func(ctx context.Context) error {27starts.Inc()28integrationStartWg.Done()29<-ctx.Done()30return nil31}),32ApplyConfigFunc: func(Config, Globals) error {33applies.Inc()34return nil35},36}3738cfg := controllerConfig{39mockConfig{40NameFunc: func() string { return mockIntegrationName },41ConfigEqualsFunc: func(Config) bool { return false },42ApplyDefaultsFunc: func(g Globals) error { return nil },43IdentifierFunc: func(Globals) (string, error) {44return mockIntegrationName, nil45},46NewIntegrationFunc: func(log.Logger, Globals) (Integration, error) {47integrationStartWg.Add(1)48return mockIntegration, nil49},50},51}5253ctrl, err := newController(util.TestLogger(t), cfg, Globals{})54require.NoError(t, err, "failed to create controller")5556sc := newSyncController(t, ctrl)5758// Wait for our integration to start.59integrationStartWg.Wait()6061// Try to apply again.62require.NoError(t, sc.UpdateController(cfg, ctrl.globals), "failed to re-apply config")63integrationStartWg.Wait()6465sc.Stop()6667require.Equal(t, uint64(1), applies.Load(), "dynamic reload should have occurred")68require.Equal(t, uint64(1), starts.Load(), "restart should not have occurred")69}7071type mockUpdateIntegration struct {72Integration73ApplyConfigFunc func(Config, Globals) error74}7576func (m mockUpdateIntegration) ApplyConfig(c Config, g Globals) error {77return m.ApplyConfigFunc(c, g)78}798081