Skip to content
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ TEST-*.xml

# Environment variables
.env

# Coverage files
coverage.out
35 changes: 19 additions & 16 deletions modules/mongodb/atlaslocal/atlaslocal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"io"
"math/rand/v2"
"net"
"os"
"path/filepath"
"strings"
Expand All @@ -17,6 +18,7 @@ import (
"go.mongodb.org/mongo-driver/v2/x/mongo/driver/connstring"

"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/exec"
"github.com/testcontainers/testcontainers-go/modules/mongodb/atlaslocal"
)

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

require.Equal(t, "mongodb", connString.Scheme)
require.Equal(t, "localhost", connString.Hosts[0][:9])
require.NotEmpty(t, connString.Hosts[0][10:], "Port should be non-empty")
host, port, err := net.SplitHostPort(connString.Hosts[0])
require.NoError(t, err, "Invalid host:port")
require.NotEmpty(t, host)
require.NotEmpty(t, port)

require.Equal(t, tc.wantUsername, connString.Username)
require.Equal(t, tc.wantPassword, connString.Password)
require.Equal(t, tc.wantDatabase, connString.Database)
Expand All @@ -556,18 +561,16 @@ func TestConnectionString(t *testing.T) {
func requireEnvVar(t *testing.T, ctr testcontainers.Container, envVarName, expected string) {
t.Helper()

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

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

// testcontainers-go's Exec() returns a multiplexed stream in the same format
// used by the Docker API. Each frame is prefixed with an 8-byte header.
require.Greater(t, len(outBytes), 8, "Exec output too short to contain env var value")

out := strings.TrimSpace(string(outBytes[8:]))
out := strings.TrimSpace(string(outBytes))
require.Equal(t, expected, out, "DO_NOT_TRACK env var value mismatch")
}

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

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

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

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

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

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

content, _ := io.ReadAll(reader)
require.NotContains(t, string(content), filename)
require.NotContains(t, string(raw), filename)
}
}

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

err := os.WriteFile(usernameFilepath, []byte("file_testuser"), 0o755)
err := os.WriteFile(usernameFilepath, []byte("file_testuser"), 0o600)
require.NoError(t, err)

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

err = os.WriteFile(passwordFilepath, []byte("file_testpass"), 0o755)
err = os.WriteFile(passwordFilepath, []byte("file_testpass"), 0o600)
require.NoError(t, err)

_, err = os.Stat(passwordFilepath)
Expand Down
2 changes: 1 addition & 1 deletion modules/mongodb/atlaslocal/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (opts options) parsePassword() (string, error) {
return strings.TrimSpace(string(r)), err
}

// Option is an option for the Redpanda container.
// Option is an option for the Atlas Local container.
type Option func(*options) error

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