2020-06-18 16:13:24 +02:00
/ *
2020-09-22 12:13:00 +02:00
Copyright 2020 Docker Compose CLI authors
2020-06-18 16:13:24 +02:00
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
2020-05-22 17:05:46 +02:00
package e2e
import (
2021-01-19 13:55:19 +01:00
"encoding/json"
2020-08-06 15:40:36 +02:00
"fmt"
"os"
"strings"
2020-05-22 17:05:46 +02:00
"testing"
2020-10-30 17:23:02 +01:00
"time"
2020-05-22 17:05:46 +02:00
2020-11-23 19:46:47 +01:00
"gotest.tools/v3/assert"
2020-08-06 15:40:36 +02:00
"gotest.tools/v3/icmd"
2020-10-30 17:23:02 +01:00
"gotest.tools/v3/poll"
2020-05-22 17:05:46 +02:00
2021-01-19 13:55:19 +01:00
"github.com/docker/compose-cli/cli/cmd"
2021-01-19 15:34:54 +01:00
. "github.com/docker/compose-cli/utils/e2e"
2020-05-22 17:05:46 +02:00
)
2020-08-06 15:40:36 +02:00
var binDir string
func TestMain ( m * testing . M ) {
p , cleanup , err := SetupExistingCLI ( )
if err != nil {
fmt . Println ( err )
os . Exit ( 1 )
}
binDir = p
exitCode := m . Run ( )
cleanup ( )
os . Exit ( exitCode )
2020-05-22 17:05:46 +02:00
}
2020-11-04 10:43:50 +01:00
func TestLocalBackendRun ( t * testing . T ) {
2020-08-06 15:40:36 +02:00
c := NewParallelE2eCLI ( t , binDir )
2020-08-24 10:23:14 +02:00
c . RunDockerCmd ( "context" , "create" , "local" , "test-context" ) . Assert ( t , icmd . Success )
c . RunDockerCmd ( "context" , "use" , "test-context" ) . Assert ( t , icmd . Success )
2020-08-06 15:40:36 +02:00
t . Run ( "run" , func ( t * testing . T ) {
2020-11-04 10:43:50 +01:00
t . Parallel ( )
2020-08-24 10:23:14 +02:00
res := c . RunDockerCmd ( "run" , "-d" , "nginx" )
2020-08-06 15:40:36 +02:00
containerName := strings . TrimSpace ( res . Combined ( ) )
t . Cleanup ( func ( ) {
2020-08-24 10:23:14 +02:00
_ = c . RunDockerOrExitError ( "rm" , "-f" , containerName )
2020-08-06 15:40:36 +02:00
} )
2020-08-24 10:23:14 +02:00
res = c . RunDockerCmd ( "inspect" , containerName )
2020-08-06 15:40:36 +02:00
res . Assert ( t , icmd . Expected { Out : ` "Status": "running" ` } )
} )
2020-10-30 17:23:02 +01:00
t . Run ( "run rm" , func ( t * testing . T ) {
2020-11-04 10:43:50 +01:00
t . Parallel ( )
2020-10-30 17:23:02 +01:00
res := c . RunDockerCmd ( "run" , "--rm" , "-d" , "nginx" )
containerName := strings . TrimSpace ( res . Combined ( ) )
t . Cleanup ( func ( ) {
_ = c . RunDockerOrExitError ( "rm" , "-f" , containerName )
} )
_ = c . RunDockerCmd ( "stop" , containerName )
checkRemoved := func ( t poll . LogT ) poll . Result {
res = c . RunDockerOrExitError ( "inspect" , containerName )
if res . ExitCode == 1 && strings . Contains ( res . Stderr ( ) , "No such container" ) {
return poll . Success ( )
}
return poll . Continue ( "waiting for container to be removed" )
}
poll . WaitOn ( t , checkRemoved , poll . WithDelay ( 1 * time . Second ) , poll . WithTimeout ( 10 * time . Second ) )
} )
2020-08-06 15:40:36 +02:00
t . Run ( "run with ports" , func ( t * testing . T ) {
2021-01-19 13:55:19 +01:00
res := c . RunDockerCmd ( "run" , "-d" , "-p" , "85:80" , "nginx" )
2020-08-06 15:40:36 +02:00
containerName := strings . TrimSpace ( res . Combined ( ) )
t . Cleanup ( func ( ) {
2020-08-24 10:23:14 +02:00
_ = c . RunDockerOrExitError ( "rm" , "-f" , containerName )
2020-08-06 15:40:36 +02:00
} )
2020-08-24 10:23:14 +02:00
res = c . RunDockerCmd ( "inspect" , containerName )
2021-01-19 13:55:19 +01:00
inspect := & cmd . ContainerInspectView { }
err := json . Unmarshal ( [ ] byte ( res . Stdout ( ) ) , inspect )
assert . NilError ( t , err )
assert . Equal ( t , inspect . Status , "running" )
nginxID := inspect . ID
2020-08-24 10:23:14 +02:00
res = c . RunDockerCmd ( "ps" )
2021-01-19 13:55:19 +01:00
nginxFound := false
lines := Lines ( res . Stdout ( ) )
for _ , line := range lines {
fields := strings . Fields ( line )
if fields [ 0 ] == nginxID {
nginxFound = true
2021-02-12 09:45:00 +01:00
assert . Equal ( t , fields [ 1 ] , "nginx" , res . Combined ( ) )
assert . Equal ( t , fields [ 2 ] , "/docker-entrypoint.sh" , res . Combined ( ) )
assert . Equal ( t , fields [ len ( fields ) - 1 ] , "0.0.0.0:85->80/tcp" , res . Combined ( ) )
2021-01-19 13:55:19 +01:00
}
}
assert . Assert ( t , nginxFound , res . Stdout ( ) )
res = c . RunDockerCmd ( "ps" , "--format" , "json" )
res . Assert ( t , icmd . Expected { Out : ` "Image":"nginx","Status":"Up Less than a second","Command":"/docker-entrypoint.sh nginx -g 'daemon off;'","Ports":["0.0.0.0:85->80/tcp" ` } )
res = c . RunDockerCmd ( "ps" , "--quiet" )
res . Assert ( t , icmd . Expected { Out : nginxID + "\n" } )
2020-08-06 15:40:36 +02:00
} )
2020-11-04 10:43:50 +01:00
t . Run ( "run with volume" , func ( t * testing . T ) {
t . Parallel ( )
t . Cleanup ( func ( ) {
_ = c . RunDockerOrExitError ( "volume" , "rm" , "local-test" )
} )
c . RunDockerCmd ( "volume" , "create" , "local-test" )
c . RunDockerCmd ( "run" , "--rm" , "-d" , "--volume" , "local-test:/data" , "alpine" , "sh" , "-c" , ` echo "testdata" > /data/test ` )
// FIXME: Remove sleep when race to attach to dead container is fixed
res := c . RunDockerOrExitError ( "run" , "--rm" , "--volume" , "local-test:/data" , "alpine" , "sh" , "-c" , "cat /data/test && sleep 1" )
res . Assert ( t , icmd . Expected { Out : "testdata" } )
} )
2020-08-06 15:40:36 +02:00
t . Run ( "inspect not found" , func ( t * testing . T ) {
2020-11-04 10:43:50 +01:00
t . Parallel ( )
2020-08-24 10:23:14 +02:00
res := c . RunDockerOrExitError ( "inspect" , "nonexistentcontainer" )
2020-08-06 15:40:36 +02:00
res . Assert ( t , icmd . Expected {
ExitCode : 1 ,
Err : "Error: No such container: nonexistentcontainer" ,
} )
} )
2020-05-22 17:05:46 +02:00
}
2020-11-04 10:43:50 +01:00
func TestLocalBackendVolumes ( t * testing . T ) {
c := NewParallelE2eCLI ( t , binDir )
c . RunDockerCmd ( "context" , "create" , "local" , "test-context" ) . Assert ( t , icmd . Success )
c . RunDockerCmd ( "context" , "use" , "test-context" ) . Assert ( t , icmd . Success )
t . Run ( "volume crud" , func ( t * testing . T ) {
t . Parallel ( )
name := "crud"
t . Cleanup ( func ( ) {
_ = c . RunDockerOrExitError ( "volume" , "rm" , name )
} )
res := c . RunDockerCmd ( "volume" , "create" , name )
res . Assert ( t , icmd . Expected { Out : name } )
res = c . RunDockerCmd ( "volume" , "ls" )
res . Assert ( t , icmd . Expected { Out : name } )
res = c . RunDockerCmd ( "volume" , "inspect" , name )
res . Assert ( t , icmd . Expected { Out : fmt . Sprintf ( ` "ID": "%s" ` , name ) } )
res = c . RunDockerCmd ( "volume" , "rm" , name )
res . Assert ( t , icmd . Expected { Out : name } )
res = c . RunDockerOrExitError ( "volume" , "inspect" , name )
res . Assert ( t , icmd . Expected { ExitCode : 1 } )
} )
}