Skip to content

Commit

Permalink
[chore][receiver/sshcheck] Enable goleak check (#32181)
Browse files Browse the repository at this point in the history
This enables `goleak` checks for the sshcheck receiver to help ensure no
goroutines are being leaked. This is a test only change. The test
package's SSH Server needed to be modified to properly shutdown.

**Link to tracking Issue:** #30438
  • Loading branch information
crobert-1 authored Apr 5, 2024
1 parent c3e7965 commit 78179f5
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 11 deletions.
14 changes: 14 additions & 0 deletions receiver/sshcheckreceiver/package_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package sshcheckreceiver

import (
"testing"

"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}
56 changes: 45 additions & 11 deletions receiver/sshcheckreceiver/scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,24 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
)

func setupSSHServer(t *testing.T) string {
type sshServer struct {
listener net.Listener
done chan struct{}
}

func newSSHServer(network, endpoint string) (*sshServer, error) {
listener, err := net.Listen(network, endpoint)
if err != nil {
return nil, err
}

return &sshServer{
listener: listener,
done: make(chan struct{}),
}, nil
}

func (s *sshServer) runSSHServer(t *testing.T) string {
config := &ssh.ServerConfig{
NoClientAuth: true,
PasswordCallback: func(c ssh.ConnMetadata, pass []byte) (*ssh.Permissions, error) {
Expand All @@ -43,14 +60,16 @@ func setupSSHServer(t *testing.T) string {

config.AddHostKey(private)

listener, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)

go func() {
for {
conn, err := listener.Accept()
conn, err := s.listener.Accept()
if err != nil {
break
select {
case <-s.done:
return
default:
require.NoError(t, err)
}
}
_, chans, reqs, err := ssh.NewServerConn(conn, config)
if err != nil {
Expand All @@ -62,7 +81,12 @@ func setupSSHServer(t *testing.T) string {
}
}()

return listener.Addr().String()
return s.listener.Addr().String()
}

func (s *sshServer) shutdown() {
close(s.done)
s.listener.Close()
}

func handleChannels(chans <-chan ssh.NewChannel) {
Expand Down Expand Up @@ -116,8 +140,12 @@ func TestScraper(t *testing.T) {
if !supportedOS() {
t.Skip("Skip tests if not running on one of: [linux, darwin, freebsd, openbsd]")
}
endpoint := setupSSHServer(t)

s, err := newSSHServer("tcp", "127.0.0.1:0")
require.NoError(t, err)
endpoint := s.runSSHServer(t)
require.NotEmpty(t, endpoint)
defer s.shutdown()

testCases := []struct {
name string
Expand Down Expand Up @@ -187,8 +215,11 @@ func TestScraperPropagatesResourceAttributes(t *testing.T) {
if !supportedOS() {
t.Skip("Skip tests if not running on one of: [linux, darwin, freebsd, openbsd]")
}
endpoint := setupSSHServer(t)
s, err := newSSHServer("tcp", "127.0.0.1:0")
require.NoError(t, err)
endpoint := s.runSSHServer(t)
require.NotEmpty(t, endpoint)
defer s.shutdown()

f := NewFactory()
cfg := f.CreateDefaultConfig().(*Config)
Expand Down Expand Up @@ -223,8 +254,11 @@ func TestScraperDoesNotErrForSSHErr(t *testing.T) {
if !supportedOS() {
t.Skip("Skip tests if not running on one of: [linux, darwin, freebsd, openbsd]")
}
endpoint := setupSSHServer(t)
s, err := newSSHServer("tcp", "127.0.0.1:0")
require.NoError(t, err)
endpoint := s.runSSHServer(t)
require.NotEmpty(t, endpoint)
defer s.shutdown()

f := NewFactory()
cfg := f.CreateDefaultConfig().(*Config)
Expand All @@ -239,7 +273,7 @@ func TestScraperDoesNotErrForSSHErr(t *testing.T) {
scraper := newScraper(cfg, settings)
require.NoError(t, scraper.start(context.Background(), componenttest.NewNopHost()), "should not err to start")

_, err := scraper.scrape(context.Background())
_, err = scraper.scrape(context.Background())
require.NoError(t, err, "should not err")
}

Expand Down

0 comments on commit 78179f5

Please sign in to comment.