@@ -18,6 +18,7 @@ package main
1818
1919import (
2020 "fmt"
21+ "io"
2122 "net/http"
2223 "os"
2324 "path/filepath"
@@ -29,63 +30,47 @@ import (
2930 "gotest.tools/v3/assert"
3031 "gotest.tools/v3/icmd"
3132 "gotest.tools/v3/poll"
32-
33- . "github.com/docker/compose-ecs/utils/e2e"
3433)
3534
3635var binDir string
3736
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-
5037func 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
5440 t .Run ("create secret" , func (t * testing.T ) {
55- secretFile := filepath .Join (cmd . BinDir , "secret.txt" )
41+ secretFile := filepath .Join (t . TempDir () , "secret.txt" )
5642 err := os .WriteFile (secretFile , []byte ("pass1" ), 0644 )
5743 assert .Check (t , err == nil )
58- res := cmd . RunDockerCmd ( "secret" , "create" , secretName , secretFile )
44+ res := icmd . RunCommand ( "compose-ecs" , "secret" , "create" , secretName , secretFile )
5945 assert .Check (t , strings .Contains (res .Stdout (), secretName ), res .Stdout ())
6046 })
6147
6248 t .Run ("list secrets" , func (t * testing.T ) {
63- res := cmd . RunDockerCmd ( "secret" , "list" )
49+ res := icmd . RunCommand ( "compose-ecs" , "secret" , "list" )
6450 assert .Check (t , strings .Contains (res .Stdout (), secretName ), res .Stdout ())
6551 })
6652
6753 t .Run ("inspect secret" , func (t * testing.T ) {
68- res := cmd . RunDockerCmd ( "secret" , "inspect" , secretName )
54+ res := icmd . RunCommand ( "compose-ecs" , "secret" , "inspect" , secretName )
6955 assert .Check (t , strings .Contains (res .Stdout (), `"Name": "` + secretName + `"` ), res .Stdout ())
7056 })
7157
7258 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" )
7561 assert .Check (t , ! strings .Contains (res .Stdout (), secretName ), res .Stdout ())
7662 })
7763}
7864
7965func 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 )
8469 })
8570
8671 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" )
8974 lines := strings .Split (strings .TrimSpace (res .Stdout ()), "\n " )
9075
9176 assert .Equal (t , 5 , len (lines ))
@@ -123,17 +108,6 @@ func TestCompose(t *testing.T) {
123108 assert .Check (t , secretsDisplayed )
124109 })
125110
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-
137111 t .Run ("Words GET validating cross service connection" , func (t * testing.T ) {
138112 out := HTTPGetWithRetry (t , wordsURL , http .StatusOK , 5 * time .Second , 300 * time .Second )
139113 assert .Assert (t , strings .Contains (out , `"word":` ))
@@ -153,9 +127,8 @@ func TestCompose(t *testing.T) {
153127 assert .Equal (t , out , "myPassword1\n " )
154128 })
155129
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" )
159132
160133 checkUp := func (t poll.LogT ) poll.Result {
161134 out := res .Combined ()
@@ -168,30 +141,40 @@ func TestCompose(t *testing.T) {
168141 })
169142}
170143
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 ())
189167 }
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 ""
197180}
0 commit comments