Skip to content

Commit

Permalink
chore: make the optional logging test use the common SSH server (#470)
Browse files Browse the repository at this point in the history
Migrated `TestOptionalLogging(…)` to use the SSH server abstraction.
  • Loading branch information
theneva authored Jun 18, 2024
1 parent fc0d188 commit ede0e6c
Showing 1 changed file with 17 additions and 26 deletions.
43 changes: 17 additions & 26 deletions provider/pkg/provider/remote/commandController_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package remote

import (
"context"
"fmt"
"io"
"strings"
"testing"
Expand All @@ -29,31 +28,23 @@ import (
)

func TestOptionalLogging(t *testing.T) {
const (
host = "localhost"
port = 2222
)
for _, sharedLogMode := range Logging.Values(LogStdoutAndStderr) {
// Get a copy of the log mode that doesn't get changed by the next iteration in the for loop,
// to allow running in parallel.
// See 'The long-standing "for" loop gotcha' here: https://go.dev/blog/go1.22
// This can be removed after upgrading to go 1.22.
logMode := sharedLogMode
t.Run(logMode.Name, func(t *testing.T) {
t.Parallel()

// This SSH server always writes "foo" to stdout and "bar" to stderr, no matter the command.
server := &ssh.Server{
Addr: fmt.Sprintf("%s:%d", host, port),
Handler: func(s ssh.Session) {
_, err := io.WriteString(s, "foo")
require.NoError(t, err)
_, err = io.WriteString(s.Stderr(), "bar")
require.NoError(t, err)
},
}
go func() {
// "ListenAndServe always returns a non-nil error."
_ = server.ListenAndServe()
}()
t.Cleanup(func() {
_ = server.Close()
})
// This SSH server always writes "foo" to stdout and "bar" to stderr, no matter the command.
server := testutil.NewTestSshServer(t, func(s ssh.Session) {
_, err := io.WriteString(s, "foo")
require.NoError(t, err)
_, err = io.WriteString(s.Stderr(), "bar")
require.NoError(t, err)
})

for _, logMode := range Logging.Values(LogStdoutAndStderr) {
t.Run(logMode.Name, func(t *testing.T) {
cmd := Command{}

ctx := testutil.TestContext{Context: context.Background()}
Expand All @@ -64,8 +55,8 @@ func TestOptionalLogging(t *testing.T) {
},
Connection: &Connection{
connectionBase: connectionBase{
Host: pulumi.StringRef(host),
Port: pulumi.Float64Ref(float64(port)),
Host: pulumi.StringRef(server.Host),
Port: pulumi.Float64Ref(float64(server.Port)),
User: pulumi.StringRef("user"), // unused but prevents nil panic
PerDialTimeout: pulumi.IntRef(1), // unused but prevents nil panic
},
Expand Down

0 comments on commit ede0e6c

Please sign in to comment.