Skip to content
This repository was archived by the owner on Jun 26, 2023. It is now read-only.

Commit b375f97

Browse files
committed
Fix issue with arguments. Use long args. Fix tests
1 parent b1a3bb2 commit b375f97

File tree

3 files changed

+47
-22
lines changed

3 files changed

+47
-22
lines changed

compose/compose_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package compose_test
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"log"
78
"os"
@@ -13,7 +14,15 @@ import (
1314
"github.com/portainer/docker-compose-wrapper/compose"
1415
)
1516

17+
func checkPrerequisites(t *testing.T) {
18+
if _, err := os.Stat("docker-compose"); errors.Is(err, os.ErrNotExist) {
19+
t.Fatal("docker-compose binary not found, please run download.sh and re-run this suite")
20+
}
21+
}
22+
1623
func Test_UpAndDown(t *testing.T) {
24+
checkPrerequisites(t)
25+
1726
deployer, _ := compose.NewComposeDeployer("", "")
1827

1928
const composeFileContent = `
@@ -49,7 +58,7 @@ func Test_UpAndDown(t *testing.T) {
4958

5059
ctx := context.Background()
5160

52-
err = deployer.Deploy(ctx, "", "", "test1", []string{filePathOriginal, filePathOverride}, "", false)
61+
err = deployer.Deploy(ctx, "", "", "", []string{filePathOriginal, filePathOverride}, "", false)
5362
if err != nil {
5463
t.Fatal(err)
5564
}
@@ -58,7 +67,7 @@ func Test_UpAndDown(t *testing.T) {
5867
t.Fatal("container should exist")
5968
}
6069

61-
err = deployer.Remove(ctx, "", "", "test1", []string{filePathOriginal, filePathOverride}, "")
70+
err = deployer.Remove(ctx, "", "", "", []string{filePathOriginal, filePathOverride}, "")
6271
if err != nil {
6372
t.Fatal(err)
6473
}

compose/internal/composeplugin/composeplugin.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -119,19 +119,19 @@ func (wrapper *PluginWrapper) command(command composeCommand, workingDir, host,
119119
}
120120

121121
type composeCommand struct {
122-
command []string
123-
args []string
122+
globalArgs []string // docker-compose global arguments: --host host -f file.yaml
123+
subCommandAndArgs []string // docker-compose subcommand: up, down folllowed by subcommand arguments
124124
}
125125

126126
func newCommand(command []string, filePaths []string) composeCommand {
127-
var args []string
127+
args := []string{}
128128
for _, path := range filePaths {
129129
args = append(args, "-f")
130130
args = append(args, strings.TrimSpace(path))
131131
}
132132
return composeCommand{
133-
args: args,
134-
command: command,
133+
globalArgs: args,
134+
subCommandAndArgs: command,
135135
}
136136
}
137137

@@ -151,18 +151,20 @@ func newPullCommand(filePaths []string) composeCommand {
151151
return newCommand([]string{"pull"}, filePaths)
152152
}
153153

154-
func (command *composeCommand) WithProjectName(projectName string) {
155-
command.args = append(command.args, "-p", projectName)
154+
func (command *composeCommand) WithHost(host string) {
155+
// prepend compatibility flags such as this one as they must appear before the
156+
// regular global args otherwise docker-compose will throw an error
157+
command.globalArgs = append([]string{"--host", host}, command.globalArgs...)
156158
}
157159

158-
func (command *composeCommand) WithEnvFilePath(envFilePath string) {
159-
command.args = append(command.args, "--env-file", envFilePath)
160+
func (command *composeCommand) WithProjectName(projectName string) {
161+
command.globalArgs = append(command.globalArgs, "--project-name", projectName)
160162
}
161163

162-
func (command *composeCommand) WithHost(host string) {
163-
command.args = append(command.args, "-H", host)
164+
func (command *composeCommand) WithEnvFilePath(envFilePath string) {
165+
command.globalArgs = append(command.globalArgs, "--env-file", envFilePath)
164166
}
165167

166168
func (command *composeCommand) ToArgs() []string {
167-
return append(command.args, command.command...)
169+
return append(command.globalArgs, command.subCommandAndArgs...)
168170
}

compose/internal/composeplugin/composeplugin_test.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package composeplugin
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"log"
78
"os"
@@ -14,6 +15,12 @@ import (
1415
libstack "github.com/portainer/docker-compose-wrapper"
1516
)
1617

18+
func checkPrerequisites(t *testing.T) {
19+
if _, err := os.Stat("docker-compose"); errors.Is(err, os.ErrNotExist) {
20+
t.Fatal("docker-compose binary not found, please run download.sh and re-run this suite")
21+
}
22+
}
23+
1724
func setup(t *testing.T) libstack.Deployer {
1825
w, err := NewPluginWrapper("", "")
1926
if err != nil {
@@ -24,30 +31,37 @@ func setup(t *testing.T) libstack.Deployer {
2431
}
2532

2633
func Test_NewCommand_SingleFilePath(t *testing.T) {
34+
checkPrerequisites(t)
35+
2736
cmd := newCommand([]string{"up", "-d"}, []string{"docker-compose.yml"})
2837
expected := []string{"-f", "docker-compose.yml"}
29-
if !reflect.DeepEqual(cmd.args, expected) {
30-
t.Errorf("wrong output args, want: %v, got: %v", expected, cmd.args)
38+
if !reflect.DeepEqual(cmd.globalArgs, expected) {
39+
t.Errorf("wrong output args, want: %v, got: %v", expected, cmd.globalArgs)
3140
}
3241
}
3342

3443
func Test_NewCommand_MultiFilePaths(t *testing.T) {
44+
checkPrerequisites(t)
45+
3546
cmd := newCommand([]string{"up", "-d"}, []string{"docker-compose.yml", "docker-compose-override.yml"})
3647
expected := []string{"-f", "docker-compose.yml", "-f", "docker-compose-override.yml"}
37-
if !reflect.DeepEqual(cmd.args, expected) {
38-
t.Errorf("wrong output args, want: %v, got: %v", expected, cmd.args)
48+
if !reflect.DeepEqual(cmd.globalArgs, expected) {
49+
t.Errorf("wrong output args, want: %v, got: %v", expected, cmd.globalArgs)
3950
}
4051
}
4152

4253
func Test_NewCommand_MultiFilePaths_WithSpaces(t *testing.T) {
54+
checkPrerequisites(t)
55+
4356
cmd := newCommand([]string{"up", "-d"}, []string{" docker-compose.yml", "docker-compose-override.yml "})
4457
expected := []string{"-f", "docker-compose.yml", "-f", "docker-compose-override.yml"}
45-
if !reflect.DeepEqual(cmd.args, expected) {
46-
t.Errorf("wrong output args, want: %v, got: %v", expected, cmd.args)
58+
if !reflect.DeepEqual(cmd.globalArgs, expected) {
59+
t.Errorf("wrong output args, want: %v, got: %v", expected, cmd.globalArgs)
4760
}
4861
}
4962

5063
func Test_UpAndDown(t *testing.T) {
64+
checkPrerequisites(t)
5165

5266
const composeFileContent = `version: "3.9"
5367
services:
@@ -79,7 +93,7 @@ services:
7993
}
8094

8195
ctx := context.Background()
82-
err = w.Deploy(ctx, "", "", "test1", []string{filePathOriginal, filePathOverride}, "", false)
96+
err = w.Deploy(ctx, "", "", "", []string{filePathOriginal, filePathOverride}, "", false)
8397
if err != nil {
8498
t.Fatal(err)
8599
}
@@ -88,7 +102,7 @@ services:
88102
t.Fatal("container should exist")
89103
}
90104

91-
err = w.Remove(ctx, "", "", "test1", []string{filePathOriginal, filePathOverride})
105+
err = w.Remove(ctx, "", "", "", []string{filePathOriginal, filePathOverride}, "")
92106
if err != nil {
93107
t.Fatal(err)
94108
}

0 commit comments

Comments
 (0)