Skip to content

Commit

Permalink
add sql server container
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikus1993 committed Mar 14, 2023
1 parent 7bb7577 commit 1f762d3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
19 changes: 19 additions & 0 deletions sqlserver/sqlserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package sqlserver
import (
"context"
"fmt"
"io"
"strings"

"github.com/docker/go-connections/nat"
"github.com/google/uuid"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
Expand Down Expand Up @@ -49,3 +52,19 @@ func (container *msSqlContainer) ConnectionString(ctx context.Context) (string,

return uri, nil
}

func (container *msSqlContainer) ExecCommand(ctx context.Context, scriptContent string) (string, error) {
scriptFilePath := strings.Join([]string{"./tmp", fmt.Sprintf("%s.sql", uuid.NewString())}, "/")
err := container.CopyToContainer(ctx, []byte(scriptContent), scriptFilePath, 493)
if err != nil {
return "", err
}
_, reader, err := container.Exec(ctx, []string{"/opt/mssql-tools/bin/sqlcmd", "-b", "-r", "1", "-U", container.config.username, "-P", container.config.password, "-i", scriptFilePath})
if err != nil {
return "", err
}
if b, err := io.ReadAll(reader); err == nil {
return string(b), nil
}
return "", err
}
25 changes: 25 additions & 0 deletions sqlserver/sqlserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,28 @@ func TestSqlServerContainerPing(t *testing.T) {
subject := client.QueryRow("SELECT 1")
assert.NoError(t, subject.Err())
}

func TestSqlServerContainerExecCommand(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
config := NewSqlServerContainerConfigurationBuilder().Build()
// Arrange
ctx := context.Background()

container, err := StartContainer(ctx, config)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
if err := container.Terminate(ctx); err != nil {
t.Fatalf("failed to terminate container: %s", err)
}
})

result, err := container.ExecCommand(ctx, "SELECT 1;")

assert.NoError(t, err)

assert.Contains(t, result, "1 rows affected")
}

0 comments on commit 1f762d3

Please sign in to comment.