1
1
package wrapper
2
2
3
3
import (
4
+ "fmt"
5
+ "log"
6
+ "os"
7
+ "os/exec"
8
+ "path/filepath"
9
+ "strings"
4
10
"testing"
5
11
)
6
12
7
- func TestCommand (t * testing.T ) {
13
+ func setup (t * testing.T ) * ComposeWrapper {
8
14
w , err := NewComposeWrapper ("" )
9
15
if err != nil {
10
16
t .Fatal (err )
11
17
}
12
18
19
+ return w
20
+ }
21
+
22
+ func TestCommand (t * testing.T ) {
23
+ w := setup (t )
24
+
13
25
file := "docker-compose-test.yml"
14
- _ , err = w .Up (file , "" , "" , "" )
26
+ _ , err : = w .Up (file , "" , "" , "" )
15
27
if err != nil {
16
28
t .Fatal (err )
17
29
}
@@ -22,3 +34,77 @@ func TestCommand(t *testing.T) {
22
34
}
23
35
24
36
}
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