Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix nil pointer dereference in supervisor example #166

Merged
merged 3 commits into from
Jun 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion internal/examples/supervisor/supervisor/supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,11 @@ func (s *Supervisor) runAgentProcess() {
restartTimer.Stop()

for {
var healthCheckTickerCh <-chan time.Time
if s.healthCheckTicker != nil {
healthCheckTickerCh = s.healthCheckTicker.C
}

select {
case <-s.hasNewConfig:
restartTimer.Stop()
Expand All @@ -498,7 +503,7 @@ func (s *Supervisor) runAgentProcess() {
case <-restartTimer.C:
s.startAgent()

case <-s.healthCheckTicker.C:
case <-healthCheckTickerCh:
s.healthCheck()
}
}
Expand Down
63 changes: 63 additions & 0 deletions internal/examples/supervisor/supervisor/supervisor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package supervisor

import (
"fmt"
"os"
"testing"

"github.com/open-telemetry/opamp-go/internal"
"github.com/open-telemetry/opamp-go/internal/examples/server/data"
"github.com/open-telemetry/opamp-go/internal/examples/server/opampsrv"
"github.com/stretchr/testify/assert"
haoqixu marked this conversation as resolved.
Show resolved Hide resolved
)

func changeCurrentDir(t *testing.T) string {
t.Helper()

tmp := t.TempDir()

oldCWD, err := os.Getwd()
if err != nil {
t.Fatalf("getting working directory: %v", err)
}

if err := os.Chdir(tmp); err != nil {
t.Fatalf("changing working directory: %v", err)
}
t.Cleanup(func() {
if err := os.Chdir(oldCWD); err != nil {
t.Fatalf("restoring working directory: %v", err)
}
})

return tmp
}

func startOpampServer(t *testing.T) {
t.Helper()

opampSrv := opampsrv.NewServer(&data.AllAgents)
opampSrv.Start()

t.Cleanup(func() {
opampSrv.Stop()
})
}

func TestNewSupervisor(t *testing.T) {
tmpDir := changeCurrentDir(t)
os.WriteFile("supervisor.yaml", []byte(fmt.Sprintf(`
server:
endpoint: ws://127.0.0.1:4320/v1/opamp
agent:
executable: %s/dummy_agent.sh`, tmpDir)), 0644)

os.WriteFile("dummy_agent.sh", []byte("#!/bin/sh\nsleep 9999\n"), 0755)

startOpampServer(t)

supervisor, err := NewSupervisor(&internal.NopLogger{})
assert.Nil(t, err)
haoqixu marked this conversation as resolved.
Show resolved Hide resolved

supervisor.Shutdown()
}