Skip to content

Commit

Permalink
Add e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsillydog committed Feb 28, 2025
1 parent ef2dc56 commit 9e7b22e
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 9 deletions.
93 changes: 93 additions & 0 deletions cmd/opampsupervisor/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,99 @@ func TestSupervisorBootstrapsCollector(t *testing.T) {
}, 5*time.Second, 250*time.Millisecond)
}

func TestSupervisorBootstrapsCollectorAvailableComponents(t *testing.T) {
agentDescription := atomic.Value{}
availableComponents := atomic.Value{}

// Load the Supervisor config so we can get the location of
// the Collector that will be run.
var cfg config.Supervisor
cfgFile := getSupervisorConfig(t, "reports_available_components", map[string]string{})
k := koanf.New("::")
err := k.Load(file.Provider(cfgFile.Name()), yaml.Parser())
require.NoError(t, err)
err = k.UnmarshalWithConf("", &cfg, koanf.UnmarshalConf{
Tag: "mapstructure",
})
require.NoError(t, err)

// Get the binary name and version from the Collector binary
// using the `components` command that prints a YAML-encoded
// map of information about the Collector build. Some of this
// information will be used as defaults for the telemetry
// attributes.
agentPath := cfg.Agent.Executable
componentsInfo, err := exec.Command(agentPath, "components").Output()
require.NoError(t, err)
k = koanf.New("::")
err = k.Load(rawbytes.Provider(componentsInfo), yaml.Parser())
require.NoError(t, err)
buildinfo := k.StringMap("buildinfo")
command := buildinfo["command"]
version := buildinfo["version"]

server := newOpAMPServer(
t,
defaultConnectingHandler,
types.ConnectionCallbacks{
OnMessage: func(_ context.Context, _ types.Connection, message *protobufs.AgentToServer) *protobufs.ServerToAgent {
if message.AgentDescription != nil {
agentDescription.Store(message.AgentDescription)
}

response := &protobufs.ServerToAgent{}
if message.AvailableComponents != nil {
availableComponents.Store(message.AvailableComponents)

if message.GetAvailableComponents().GetComponents() == nil {
response.Flags = uint64(protobufs.ServerToAgentFlags_ServerToAgentFlags_ReportAvailableComponents)
}
}

return response
},
})

s := newSupervisor(t, "reports_available_components", map[string]string{"url": server.addr})

require.Nil(t, s.Start())
defer s.Shutdown()

waitForSupervisorConnection(server.supervisorConnected, true)

require.Eventually(t, func() bool {
ac, ok := availableComponents.Load().(*protobufs.AvailableComponents)
if !ok {
return false
}

if ac.GetComponents() == nil {
return false
}

ad, ok := agentDescription.Load().(*protobufs.AgentDescription)
if !ok {
return false
}

var agentName, agentVersion string
identAttr := ad.IdentifyingAttributes
for _, attr := range identAttr {
switch attr.Key {
case semconv.AttributeServiceName:
agentName = attr.Value.GetStringValue()
case semconv.AttributeServiceVersion:
agentVersion = attr.Value.GetStringValue()
}
}

// By default the Collector should report its name and version
// from the component.BuildInfo struct built into the Collector
// binary.
return agentName == command && agentVersion == version
}, 10*time.Second, 250*time.Millisecond)
}

func TestSupervisorReportsEffectiveConfig(t *testing.T) {
var agentConfig atomic.Value
server := newOpAMPServer(
Expand Down
1 change: 1 addition & 0 deletions cmd/opampsupervisor/supervisor/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ func DefaultSupervisor() Supervisor {
ReportsOwnMetrics: true,
ReportsHealth: true,
ReportsRemoteConfig: false,
ReportsAvailableComponents: false,
},
Storage: Storage{
Directory: defaultStorageDir,
Expand Down
14 changes: 9 additions & 5 deletions cmd/opampsupervisor/supervisor/supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,10 @@ func (s *Supervisor) getBootstrapInfo() (err error) {
if availableComponentsOk {
// must have a full list of components if available components have been reported
if availableComponents.GetComponents() != nil {
done <- nil
if !doneReported.Load() {
done <- nil
doneReported.Store(true)
}
} else {
// if we don't have a full component list, ask for it
response.Flags = uint64(protobufs.ServerToAgentFlags_ServerToAgentFlags_ReportAvailableComponents)
Expand Down Expand Up @@ -812,10 +815,11 @@ func (s *Supervisor) composeOpAMPExtensionConfig() []byte {

var cfg bytes.Buffer
tplVars := map[string]any{
"InstanceUid": s.persistentState.InstanceID.String(),
"SupervisorPort": s.opampServerPort,
"PID": s.pidProvider.PID(),
"PPIDPollInterval": orphanPollInterval,
"InstanceUid": s.persistentState.InstanceID.String(),
"SupervisorPort": s.opampServerPort,
"PID": s.pidProvider.PID(),
"PPIDPollInterval": orphanPollInterval,
"ReportsAvailableComponents": s.config.Capabilities.ReportsAvailableComponents,
}
err := s.opampextensionTemplate.Execute(
&cfg,
Expand Down
60 changes: 56 additions & 4 deletions cmd/opampsupervisor/supervisor/supervisor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,8 @@ func Test_onMessage(t *testing.T) {
health_check:
endpoint: localhost:8000
opamp:
capabilities:
reports_available_components: false
instance_uid: 018fee23-4a51-7303-a441-73faed7d9deb
ppid: 88888
ppid_poll_interval: 5s
Expand Down Expand Up @@ -479,6 +481,8 @@ service:
health_check:
endpoint: localhost:8000
opamp:
capabilities:
reports_available_components: false
instance_uid: 018fee23-4a51-7303-a441-73faed7d9deb
ppid: 88888
ppid_poll_interval: 5s
Expand Down Expand Up @@ -1044,10 +1048,6 @@ func (m mockOpAMPClient) SetAvailableComponents(_ *protobufs.AvailableComponents

func (m mockOpAMPClient) SetFlags(_ protobufs.AgentToServerFlags) {}

func (m mockOpAMPClient) SetAvailableComponents(_ *protobufs.AvailableComponents) error {
return nil
}

type mockConn struct {
sendFunc func(ctx context.Context, message *protobufs.ServerToAgent) error
}
Expand Down Expand Up @@ -1224,6 +1224,8 @@ func TestSupervisor_loadAndWriteInitialMergedConfig(t *testing.T) {
health_check:
endpoint: ""
opamp:
capabilities:
reports_available_components: false
instance_uid: 018fee23-4a51-7303-a441-73faed7d9deb
ppid: 1234
ppid_poll_interval: 5s
Expand Down Expand Up @@ -1329,6 +1331,51 @@ func TestSupervisor_composeNoopConfig(t *testing.T) {
nop: null
extensions:
opamp:
capabilities:
reports_available_components: false
instance_uid: 018fee23-4a51-7303-a441-73faed7d9deb
ppid: 1234
ppid_poll_interval: 5s
server:
ws:
endpoint: ws://127.0.0.1:0/v1/opamp
tls:
insecure: true
receivers:
nop: null
service:
extensions:
- opamp
pipelines:
traces:
exporters:
- nop
receivers:
- nop
`
s := Supervisor{
persistentState: &persistentState{
InstanceID: uuid.MustParse("018fee23-4a51-7303-a441-73faed7d9deb"),
},
pidProvider: staticPIDProvider(1234),
}

require.NoError(t, s.createTemplates())

noopConfigBytes, err := s.composeNoopConfig()
noopConfig := strings.ReplaceAll(string(noopConfigBytes), "\r\n", "\n")

require.NoError(t, err)
require.Equal(t, expectedConfig, noopConfig)
}

func TestSupervisor_composeNoopConfigReportAvailableComponents(t *testing.T) {
const expectedConfig = `exporters:
nop: null
extensions:
opamp:
capabilities:
reports_available_components: true
instance_uid: 018fee23-4a51-7303-a441-73faed7d9deb
ppid: 1234
ppid_poll_interval: 5s
Expand All @@ -1354,6 +1401,11 @@ service:
InstanceID: uuid.MustParse("018fee23-4a51-7303-a441-73faed7d9deb"),
},
pidProvider: staticPIDProvider(1234),
config: config.Supervisor{
Capabilities: config.Capabilities{
ReportsAvailableComponents: true,
},
},
}

require.NoError(t, s.createTemplates())
Expand Down
2 changes: 2 additions & 0 deletions cmd/opampsupervisor/supervisor/templates/opampextension.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ extensions:
insecure: true
ppid: {{.PID}}
ppid_poll_interval: {{.PPIDPollInterval}}
capabilities:
reports_available_components: {{.ReportsAvailableComponents}}

service:
extensions: [opamp]
2 changes: 2 additions & 0 deletions cmd/opampsupervisor/testdata/collector/effective_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ extensions:
health_check:
endpoint: localhost:8000
opamp:
capabilities:
reports_available_components: false
instance_uid: 00000000-0000-0000-0000-000000000000
ppid: 1234
ppid_poll_interval: 5s
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ server:
endpoint: ws://{{.url}}/v1/opamp

capabilities:
reports_available_components: false
reports_effective_config: false
reports_own_metrics: false
reports_health: false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
server:
endpoint: ws://{{.url}}/v1/opamp

capabilities:
reports_available_components: true
reports_effective_config: false
reports_own_metrics: false
reports_health: false
accepts_remote_config: false
reports_remote_config: false

storage:
directory: '{{.storage_dir}}'

agent:
executable: ../../bin/otelcontribcol_{{.goos}}_{{.goarch}}{{.extension}}

0 comments on commit 9e7b22e

Please sign in to comment.