Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable using stdin when starting docker compose #457

Closed
Show file tree
Hide file tree
Changes from 2 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
44 changes: 42 additions & 2 deletions compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type LocalDockerCompose struct {
Services map[string]interface{}
waitStrategySupplied bool
WaitStrategyMap map[waitService]wait.Strategy
tmpFileName string
}

type (
Expand Down Expand Up @@ -103,8 +104,16 @@ func NewLocalDockerCompose(filePaths []string, identifier string, opts ...LocalD

dc.absComposeFilePaths = make([]string, len(filePaths))
for i, cfp := range dc.ComposeFilePaths {
abs, _ := filepath.Abs(cfp)
dc.absComposeFilePaths[i] = abs
if cfp == "-" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmm I do not get this line. Is it expected to receive a - as a file path for a compose file?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is

if err := dc.makeTmpFile(); err != nil {
continue
}
abs, _ := filepath.Abs(dc.tmpFileName)
dc.absComposeFilePaths[i] = abs
} else {
abs, _ := filepath.Abs(cfp)
dc.absComposeFilePaths[i] = abs
}
}

_ = dc.validate()
Expand All @@ -118,6 +127,11 @@ func NewLocalDockerCompose(filePaths []string, identifier string, opts ...LocalD

// Down executes docker-compose down
func (dc *LocalDockerCompose) Down() ExecError {
defer func() {
if dc.tmpFileName != "" {
_ = os.Remove(dc.tmpFileName)
}
}()
return executeCompose(dc, []string{"down", "--remove-orphans", "--volumes"})
}

Expand Down Expand Up @@ -402,3 +416,29 @@ func which(binary string) error {

return err
}

func readSTDIN() ([]byte, error) {
return ioutil.ReadAll(os.Stdin)
}

func (dc *LocalDockerCompose) makeTmpFile() error {
data, err := readSTDIN()
mal-as marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}

file, err := ioutil.TempFile("", "__tmp_")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you foresee any collision between multiple runs? Do you think we could use the compose identifier as part of the file name in that case 🤔?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed it

if err != nil {
return err
}

defer file.Close()

if _, err = file.Write(data); err != nil {
return err
}

dc.tmpFileName = file.Name()

return file.Sync()
}
50 changes: 50 additions & 0 deletions compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package testcontainers

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"testing"
Expand Down Expand Up @@ -468,3 +470,51 @@ func executeAndGetOutput(command string, args []string) (string, ExecError) {
StdoutOutput: out,
}
}

func TestLocalDockerCompose_STDIN(t *testing.T) {
// mocks stdin
oldStdin := os.Stdin
data, err := ioutil.ReadFile("./testresources/docker-compose-simple.yml")
if err != nil {
t.Fatalf("Failed when reading ./testresources/docker-compose-simple.yml: %v", err)
}
file := mockStdin(t, data)
defer func() {
os.Stdin = oldStdin
file.Close()
os.Remove(file.Name())
}()
// --

path := "-"
identifier := strings.ToLower(uuid.New().String())

compose := NewLocalDockerCompose([]string{path}, identifier, WithLogger(TestLogger(t)))
defer func() {
checkIfError(t, compose.Down())
}()

exErr := compose.
WithCommand([]string{"up", "-d"}).
Invoke()
checkIfError(t, exErr)
}

func mockStdin(t *testing.T, content []byte) *os.File {
tmpfile, err := ioutil.TempFile("", "_test_tmp_")
if err != nil {
t.Fatal(err)
}

if _, err := tmpfile.Write(content); err != nil {
t.Fatal(err)
}

if _, err := tmpfile.Seek(0, 0); err != nil {
t.Fatal(err)
}

os.Stdin = tmpfile

return tmpfile
}