Skip to content

Commit

Permalink
chore: support for exec
Browse files Browse the repository at this point in the history
  • Loading branch information
achettyiitr committed Jun 23, 2023
1 parent a2cf79d commit a520029
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
31 changes: 27 additions & 4 deletions compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func randSeq(n int) string {

type Compose struct {
file File
names map[string]string
ports map[string]map[int]int
env map[string]map[string]string
labels map[string]string
Expand Down Expand Up @@ -72,8 +73,7 @@ func (c *Compose) Stop(ctx context.Context) error {
)
o, err := cmd.CombinedOutput()
if err != nil {
fmt.Fprintln(os.Stderr, string(o))
return fmt.Errorf("docker compose down: %w", err)
return fmt.Errorf("docker compose down: %w: %s", err, string(o))
}

return nil
Expand All @@ -93,8 +93,6 @@ func (c *Compose) Start(ctx context.Context) error {

o, err := cmd.CombinedOutput()
if err != nil {
fmt.Fprintln(os.Stderr, cmd.String())
fmt.Fprintln(os.Stderr, string(o))
return fmt.Errorf("docker compose up: %w: %s", err, string(o))
}

Expand All @@ -120,6 +118,28 @@ func (c *Compose) Port(service string, port int) (int, error) {
return p, nil
}

func (c *Compose) Exec(ctx context.Context, service string, command ...string) (string, error) {
name, ok := c.names[service]
if !ok {
return "", fmt.Errorf("no service %q found", service)
}

args := []string{
"exec",
"-t",
name,
}
args = append(args, command...)

cmd := exec.CommandContext(ctx, "docker", args...)
o, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("docker exec: %w: %s", err, string(o))
}

return string(o), nil
}

func (c *Compose) Env(service, name string) (string, error) {
kv, ok := c.env[service]
if !ok {
Expand All @@ -140,10 +160,13 @@ func (c *Compose) extractServiceInfo(ctx context.Context) error {
return err
}

c.names = make(map[string]string)
c.ports = make(map[string]map[int]int)

ids := make([]string, len(psInfo))
for i, info := range psInfo {
c.names[info.Service] = info.ID

p := make(map[int]int)
for _, pub := range info.Publishers {
p[pub.TargetPort] = pub.PublishedPort
Expand Down
4 changes: 4 additions & 0 deletions compose/compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,9 @@ func sanityTest(t *testing.T, c *compose.Compose) {

_, err = conn.Exec(context.Background(), "CREATE TABLE test (id int)")
require.NoError(t, err)

output, err := c.Exec(context.Background(), "postgresDB", "psql", "-U", user, "-d", dbName, "-c", "INSERT INTO test (id) VALUES (1);")
require.NoError(t, err)
require.Contains(t, output, "INSERT 0 1")
})
}
11 changes: 11 additions & 0 deletions testcompose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ func (tc *TestingCompose) Port(service string, port int) int {
return p
}

func (tc *TestingCompose) Exec(ctx context.Context, service string, command ...string) string {
tc.t.Helper()

out, err := tc.compose.Exec(ctx, service, command...)
if err != nil {
tc.t.Fatalf("compose library exec: %v", err)
}

return out
}

func (tc *TestingCompose) Env(service, name string) string {
tc.t.Helper()

Expand Down
10 changes: 8 additions & 2 deletions testcompose/compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ func TestComposeTesting(t *testing.T) {
require.NotEqual(t, 5432, port)
require.NotEqual(t, 0, port)

user := c.Env("postgresDB", "POSTGRES_USER")
password := c.Env("postgresDB", "POSTGRES_PASSWORD")

dbURL := fmt.Sprintf("postgres://%s:%s@localhost:%d/postgres?sslmode=disable",
c.Env("postgresDB", "POSTGRES_USER"),
c.Env("postgresDB", "POSTGRES_PASSWORD"),
user,
password,
port,
)

Expand All @@ -44,5 +47,8 @@ func TestComposeTesting(t *testing.T) {

_, err = conn.Exec(context.Background(), "CREATE TABLE test (id int)")
require.NoError(t, err)

output := c.Exec(context.Background(), "postgresDB", "psql", "-U", user, "-d", "postgres", "-c", "INSERT INTO test (id) VALUES (1);")
require.Contains(t, output, "INSERT 0 1")
})
}

0 comments on commit a520029

Please sign in to comment.