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

Commit fb78615

Browse files
committed
chore: add test utils
1 parent 3b442dc commit fb78615

File tree

3 files changed

+100
-2
lines changed

3 files changed

+100
-2
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/portainer/docker-compose-wrapper
22

33
go 1.15
4+
5+
require github.com/stretchr/testify v1.7.0 // indirect

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
6+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
7+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
8+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
9+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
10+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

wrapper_test.go

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
11
package wrapper
22

33
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"os/exec"
8+
"path/filepath"
9+
"strings"
410
"testing"
511
)
612

7-
func TestCommand(t *testing.T) {
13+
func setup(t *testing.T) *ComposeWrapper {
814
w, err := NewComposeWrapper("")
915
if err != nil {
1016
t.Fatal(err)
1117
}
1218

19+
return w
20+
}
21+
22+
func TestCommand(t *testing.T) {
23+
w := setup(t)
24+
1325
file := "docker-compose-test.yml"
14-
_, err = w.Up(file, "", "", "")
26+
_, err := w.Up(file, "", "", "")
1527
if err != nil {
1628
t.Fatal(err)
1729
}
@@ -22,3 +34,77 @@ func TestCommand(t *testing.T) {
2234
}
2335

2436
}
37+
38+
const composeFile = `version: "3.9"
39+
services:
40+
busybox:
41+
image: "alpine:latest"
42+
container_name: "compose_wrapper_test"`
43+
const composedContainerName = "compose_wrapper_test"
44+
45+
type composeOptions struct {
46+
filePath string
47+
url string
48+
envFile string
49+
projectName string
50+
}
51+
52+
func createFile(dir, fileName, content string) (string, error) {
53+
filePath := filepath.Join(dir, fileName)
54+
f, err := os.Create(filePath)
55+
if err != nil {
56+
return "", err
57+
}
58+
59+
f.WriteString(content)
60+
f.Close()
61+
62+
return filePath, nil
63+
}
64+
65+
func createEnvFile(dir, envFileContent string) (string, error) {
66+
return createFile(dir, "stack.env", envFileContent)
67+
}
68+
69+
func createComposeFile(dir, composeFileContent string) (string, error) {
70+
return createFile(dir, "docmer-compose.yml", composeFileContent)
71+
}
72+
73+
// func Test_UpAndDown(t *testing.T) {
74+
75+
// stack, endpoint := setup(t)
76+
77+
// w, err := NewComposeStackManager("", nil)
78+
// if err != nil {
79+
// t.Fatalf("Failed creating manager: %s", err)
80+
// }
81+
82+
// err = w.Up(stack, endpoint)
83+
// if err != nil {
84+
// t.Fatalf("Error calling docker-compose up: %s", err)
85+
// }
86+
87+
// if !containerExists(composedContainerName) {
88+
// t.Fatal("container should exist")
89+
// }
90+
91+
// err = w.Down(stack, endpoint)
92+
// if err != nil {
93+
// t.Fatalf("Error calling docker-compose down: %s", err)
94+
// }
95+
96+
// if containerExists(composedContainerName) {
97+
// t.Fatal("container should be removed")
98+
// }
99+
// }
100+
101+
func containerExists(containerName string) bool {
102+
cmd := exec.Command("docker", "ps", "-a", "-f", fmt.Sprintf("name=%s", containerName))
103+
104+
out, err := cmd.Output()
105+
if err != nil {
106+
log.Fatalf("failed to list containers: %s", err)
107+
}
108+
109+
return strings.Contains(string(out), containerName)
110+
}

0 commit comments

Comments
 (0)