Skip to content

Commit 404b345

Browse files
authored
chore(atlas): simplify host-port calculation in tests (#3300)
* chore: simpler host-port detection in tests * chore: ignore coverage files * docs: wrong module * chore: consistent read of docker's multiplexed stream
1 parent 6e14bda commit 404b345

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ TEST-*.xml
2020

2121
# Environment variables
2222
.env
23+
24+
# Coverage files
25+
coverage.out

modules/mongodb/atlaslocal/atlaslocal_test.go

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"io"
66
"math/rand/v2"
7+
"net"
78
"os"
89
"path/filepath"
910
"strings"
@@ -17,6 +18,7 @@ import (
1718
"go.mongodb.org/mongo-driver/v2/x/mongo/driver/connstring"
1819

1920
"github.com/testcontainers/testcontainers-go"
21+
"github.com/testcontainers/testcontainers-go/exec"
2022
"github.com/testcontainers/testcontainers-go/modules/mongodb/atlaslocal"
2123
)
2224

@@ -541,8 +543,11 @@ func TestConnectionString(t *testing.T) {
541543
require.NoError(t, err, "Failed to parse connection string")
542544

543545
require.Equal(t, "mongodb", connString.Scheme)
544-
require.Equal(t, "localhost", connString.Hosts[0][:9])
545-
require.NotEmpty(t, connString.Hosts[0][10:], "Port should be non-empty")
546+
host, port, err := net.SplitHostPort(connString.Hosts[0])
547+
require.NoError(t, err, "Invalid host:port")
548+
require.NotEmpty(t, host)
549+
require.NotEmpty(t, port)
550+
546551
require.Equal(t, tc.wantUsername, connString.Username)
547552
require.Equal(t, tc.wantPassword, connString.Password)
548553
require.Equal(t, tc.wantDatabase, connString.Database)
@@ -556,18 +561,16 @@ func TestConnectionString(t *testing.T) {
556561
func requireEnvVar(t *testing.T, ctr testcontainers.Container, envVarName, expected string) {
557562
t.Helper()
558563

559-
exitCode, reader, err := ctr.Exec(context.Background(), []string{"sh", "-c", "echo $" + envVarName})
564+
// testcontainers-go's Exec() returns a multiplexed stream in the same format
565+
// used by the Docker API. Each frame is prefixed with an 8-byte header.
566+
exitCode, reader, err := ctr.Exec(context.Background(), []string{"sh", "-c", "echo $" + envVarName}, exec.Multiplexed())
560567
require.NoError(t, err)
561568
require.Equal(t, 0, exitCode)
562569

563570
outBytes, err := io.ReadAll(reader)
564571
require.NoError(t, err)
565572

566-
// testcontainers-go's Exec() returns a multiplexed stream in the same format
567-
// used by the Docker API. Each frame is prefixed with an 8-byte header.
568-
require.Greater(t, len(outBytes), 8, "Exec output too short to contain env var value")
569-
570-
out := strings.TrimSpace(string(outBytes[8:]))
573+
out := strings.TrimSpace(string(outBytes))
571574
require.Equal(t, expected, out, "DO_NOT_TRACK env var value mismatch")
572575
}
573576

@@ -723,7 +726,7 @@ func requireInitScriptsExist(t *testing.T, ctr testcontainers.Container, expecte
723726

724727
const dstDir = "/docker-entrypoint-initdb.d"
725728

726-
exit, r, err := ctr.Exec(context.Background(), []string{"sh", "-lc", "ls -l " + dstDir})
729+
exit, r, err := ctr.Exec(context.Background(), []string{"sh", "-lc", "ls -l " + dstDir}, exec.Multiplexed())
727730
require.NoError(t, err)
728731

729732
// If the map is empty, the command returns exit code 2.
@@ -759,14 +762,14 @@ func requireInitScriptsDoesNotExist(t *testing.T, ctr testcontainers.Container,
759762

760763
// Sanity check to verify that all scripts are present.
761764
for filename := range expectedScripts {
762-
cmd := []string{"sh", "-c", "cd docker-entrypoint-initdb.d && ls -l"}
765+
cmd := []string{"sh", "-lc", "ls -1 /docker-entrypoint-initdb.d 2>/dev/null || true"}
766+
_, reader, err := ctr.Exec(context.Background(), cmd, exec.Multiplexed())
767+
require.NoError(t, err)
763768

764-
exitCode, reader, err := ctr.Exec(context.Background(), cmd)
769+
raw, err := io.ReadAll(reader)
765770
require.NoError(t, err)
766-
require.Zero(t, exitCode, "Expected exit code 0 for command: %v", cmd)
767771

768-
content, _ := io.ReadAll(reader)
769-
require.NotContains(t, string(content), filename)
772+
require.NotContains(t, string(raw), filename)
770773
}
771774
}
772775

@@ -792,7 +795,7 @@ func newAuthFiles(t *testing.T) (string, string, string) {
792795
// Create username and password files.
793796
usernameFilepath := filepath.Join(tmpDir, "username.txt")
794797

795-
err := os.WriteFile(usernameFilepath, []byte("file_testuser"), 0o755)
798+
err := os.WriteFile(usernameFilepath, []byte("file_testuser"), 0o600)
796799
require.NoError(t, err)
797800

798801
_, err = os.Stat(usernameFilepath)
@@ -801,7 +804,7 @@ func newAuthFiles(t *testing.T) (string, string, string) {
801804
// Create the password file.
802805
passwordFilepath := filepath.Join(tmpDir, "password.txt")
803806

804-
err = os.WriteFile(passwordFilepath, []byte("file_testpass"), 0o755)
807+
err = os.WriteFile(passwordFilepath, []byte("file_testpass"), 0o600)
805808
require.NoError(t, err)
806809

807810
_, err = os.Stat(passwordFilepath)

modules/mongodb/atlaslocal/options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func (opts options) parsePassword() (string, error) {
145145
return strings.TrimSpace(string(r)), err
146146
}
147147

148-
// Option is an option for the Redpanda container.
148+
// Option is an option for the Atlas Local container.
149149
type Option func(*options) error
150150

151151
// Customize is a NOOP. It's defined to satisfy the testcontainers.ContainerCustomizer interface.

0 commit comments

Comments
 (0)