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

[chore][receiver/sshcheck] Enable goleak check #32181

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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