@@ -18,6 +18,7 @@ package main
18
18
19
19
import (
20
20
"fmt"
21
+ "io"
21
22
"net/http"
22
23
"os"
23
24
"path/filepath"
@@ -29,63 +30,47 @@ import (
29
30
"gotest.tools/v3/assert"
30
31
"gotest.tools/v3/icmd"
31
32
"gotest.tools/v3/poll"
32
-
33
- . "github.com/docker/compose-ecs/utils/e2e"
34
33
)
35
34
36
35
var binDir string
37
36
38
- func TestMain (m * testing.M ) {
39
- p , cleanup , err := SetupExistingCLI ()
40
- if err != nil {
41
- fmt .Println (err )
42
- os .Exit (1 )
43
- }
44
- binDir = p
45
- exitCode := m .Run ()
46
- cleanup ()
47
- os .Exit (exitCode )
48
- }
49
-
50
37
func TestSecrets (t * testing.T ) {
51
- cmd , testID := setupTest (t )
52
- secretName := "secret" + testID
53
-
38
+ startTime := strconv .Itoa (int (time .Now ().UnixNano ()))
39
+ secretName := "secret" + strings .ToLower (t .Name ()) + startTime
54
40
t .Run ("create secret" , func (t * testing.T ) {
55
- secretFile := filepath .Join (cmd . BinDir , "secret.txt" )
41
+ secretFile := filepath .Join (t . TempDir () , "secret.txt" )
56
42
err := os .WriteFile (secretFile , []byte ("pass1" ), 0644 )
57
43
assert .Check (t , err == nil )
58
- res := cmd . RunDockerCmd ( "secret" , "create" , secretName , secretFile )
44
+ res := icmd . RunCommand ( "compose-ecs" , "secret" , "create" , secretName , secretFile )
59
45
assert .Check (t , strings .Contains (res .Stdout (), secretName ), res .Stdout ())
60
46
})
61
47
62
48
t .Run ("list secrets" , func (t * testing.T ) {
63
- res := cmd . RunDockerCmd ( "secret" , "list" )
49
+ res := icmd . RunCommand ( "compose-ecs" , "secret" , "list" )
64
50
assert .Check (t , strings .Contains (res .Stdout (), secretName ), res .Stdout ())
65
51
})
66
52
67
53
t .Run ("inspect secret" , func (t * testing.T ) {
68
- res := cmd . RunDockerCmd ( "secret" , "inspect" , secretName )
54
+ res := icmd . RunCommand ( "compose-ecs" , "secret" , "inspect" , secretName )
69
55
assert .Check (t , strings .Contains (res .Stdout (), `"Name": "` + secretName + `"` ), res .Stdout ())
70
56
})
71
57
72
58
t .Run ("rm secret" , func (t * testing.T ) {
73
- cmd . RunDockerCmd ( "secret" , "rm" , secretName )
74
- res := cmd . RunDockerCmd ("secret" , "list" )
59
+ icmd . RunCommand ( "compose-ecs" , "secret" , "rm" , secretName )
60
+ res := icmd . RunCommand ("secret" , "list" )
75
61
assert .Check (t , ! strings .Contains (res .Stdout (), secretName ), res .Stdout ())
76
62
})
77
63
}
78
64
79
65
func TestCompose (t * testing.T ) {
80
- c , stack := setupTest (t )
81
-
82
- t .Run ("compose up" , func (t * testing.T ) {
83
- c .RunDockerCmd ("compose" , "--project-name" , stack , "-f" , "./multi_port_secrets.yaml" , "up" )
66
+ t .Run ("compose-ecs up" , func (t * testing.T ) {
67
+ res := icmd .RunCommand ("compose-ecs" , "up" )
68
+ res .Assert (t , icmd .Success )
84
69
})
85
70
86
71
var webURL , wordsURL , secretsURL string
87
- t .Run ("compose ps" , func (t * testing.T ) {
88
- res := c . RunDockerCmd ("compose" , "--project-name" , stack , "ps" )
72
+ t .Run ("compose-ecs ps" , func (t * testing.T ) {
73
+ res := icmd . RunCommand ("compose-ecs" , "ps" )
89
74
lines := strings .Split (strings .TrimSpace (res .Stdout ()), "\n " )
90
75
91
76
assert .Equal (t , 5 , len (lines ))
@@ -123,17 +108,6 @@ func TestCompose(t *testing.T) {
123
108
assert .Check (t , secretsDisplayed )
124
109
})
125
110
126
- t .Run ("compose ls" , func (t * testing.T ) {
127
- res := c .RunDockerCmd ("compose" , "ls" , "--filter" , "name=" + stack )
128
- lines := strings .Split (strings .TrimSpace (res .Stdout ()), "\n " )
129
-
130
- assert .Equal (t , 2 , len (lines ))
131
- fields := strings .Fields (lines [1 ])
132
- assert .Equal (t , 2 , len (fields ))
133
- assert .Equal (t , fields [0 ], stack )
134
- assert .Equal (t , "Running" , fields [1 ])
135
- })
136
-
137
111
t .Run ("Words GET validating cross service connection" , func (t * testing.T ) {
138
112
out := HTTPGetWithRetry (t , wordsURL , http .StatusOK , 5 * time .Second , 300 * time .Second )
139
113
assert .Assert (t , strings .Contains (out , `"word":` ))
@@ -153,9 +127,8 @@ func TestCompose(t *testing.T) {
153
127
assert .Equal (t , out , "myPassword1\n " )
154
128
})
155
129
156
- t .Run ("compose down" , func (t * testing.T ) {
157
- cmd := c .NewDockerCmd ("compose" , "--project-name" , stack , "down" )
158
- res := icmd .StartCmd (cmd )
130
+ t .Run ("compose-ecs down" , func (t * testing.T ) {
131
+ res := icmd .RunCommand ("compose-ecs" , "down" )
159
132
160
133
checkUp := func (t poll.LogT ) poll.Result {
161
134
out := res .Combined ()
@@ -168,30 +141,40 @@ func TestCompose(t *testing.T) {
168
141
})
169
142
}
170
143
171
- func setupTest (t * testing.T ) (* E2eCLI , string ) {
172
- startTime := strconv .Itoa (int (time .Now ().UnixNano ()))
173
- c := NewParallelE2eCLI (t , binDir )
174
- contextName := "e2e" + strings .ToLower (t .Name ()) + startTime
175
- stack := contextName
176
- t .Run ("create context" , func (t * testing.T ) {
177
- localTestProfile := os .Getenv ("TEST_AWS_PROFILE" )
178
- var res * icmd.Result
179
- if localTestProfile != "" {
180
- res = c .RunDockerCmd ("context" , "create" , "ecs" , contextName , "--profile" , localTestProfile )
181
- } else {
182
- region := os .Getenv ("AWS_DEFAULT_REGION" )
183
- secretKey := os .Getenv ("AWS_SECRET_ACCESS_KEY" )
184
- keyID := os .Getenv ("AWS_ACCESS_KEY_ID" )
185
- assert .Check (t , keyID != "" )
186
- assert .Check (t , secretKey != "" )
187
- assert .Check (t , region != "" )
188
- res = c .RunDockerCmd ("context" , "create" , "ecs" , contextName , "--from-env" )
144
+ // HTTPGetWithRetry performs an HTTP GET on an `endpoint`, using retryDelay also as a request timeout.
145
+ // In the case of an error or the response status is not the expected one, it retries the same request,
146
+ // returning the response body as a string (empty if we could not reach it)
147
+ func HTTPGetWithRetry (
148
+ t testing.TB ,
149
+ endpoint string ,
150
+ expectedStatus int ,
151
+ retryDelay time.Duration ,
152
+ timeout time.Duration ,
153
+ ) string {
154
+ t .Helper ()
155
+ var (
156
+ r * http.Response
157
+ err error
158
+ )
159
+ client := & http.Client {
160
+ Timeout : retryDelay ,
161
+ }
162
+ fmt .Printf ("\t [%s] GET %s\n " , t .Name (), endpoint )
163
+ checkUp := func (t poll.LogT ) poll.Result {
164
+ r , err = client .Get (endpoint )
165
+ if err != nil {
166
+ return poll .Continue ("reaching %q: Error %s" , endpoint , err .Error ())
189
167
}
190
- res .Assert (t , icmd.Expected {Out : "Successfully created ecs context \" " + contextName + "\" " })
191
- res = c .RunDockerCmd ("context" , "use" , contextName )
192
- res .Assert (t , icmd.Expected {Out : contextName })
193
- res = c .RunDockerCmd ("context" , "ls" )
194
- res .Assert (t , icmd.Expected {Out : contextName + " *" })
195
- })
196
- return c , stack
168
+ if r .StatusCode == expectedStatus {
169
+ return poll .Success ()
170
+ }
171
+ return poll .Continue ("reaching %q: %d != %d" , endpoint , r .StatusCode , expectedStatus )
172
+ }
173
+ poll .WaitOn (t , checkUp , poll .WithDelay (retryDelay ), poll .WithTimeout (timeout ))
174
+ if r != nil {
175
+ b , err := io .ReadAll (r .Body )
176
+ assert .NilError (t , err )
177
+ return string (b )
178
+ }
179
+ return ""
197
180
}
0 commit comments