2018-07-11 10:31:27 -04:00
package network // import "github.com/docker/docker/integration/network"
import (
"bytes"
"context"
2018-11-05 08:50:33 -05:00
"net/http"
2018-07-11 10:31:27 -04:00
"os/exec"
"strings"
"testing"
"github.com/docker/docker/api/types"
"github.com/docker/docker/integration/internal/container"
2019-08-29 16:52:40 -04:00
"github.com/docker/docker/testutil/daemon"
"github.com/docker/docker/testutil/request"
2018-07-11 10:31:27 -04:00
"gotest.tools/assert"
is "gotest.tools/assert/cmp"
"gotest.tools/skip"
)
func TestRunContainerWithBridgeNone ( t * testing . T ) {
skip . If ( t , testEnv . IsRemoteDaemon , "cannot start daemon on remote test run" )
skip . If ( t , testEnv . DaemonInfo . OSType != "linux" )
skip . If ( t , IsUserNamespace ( ) )
d := daemon . New ( t )
d . StartWithBusybox ( t , "-b" , "none" )
defer d . Stop ( t )
2018-12-22 09:53:02 -05:00
c := d . NewClientT ( t )
2018-07-11 10:31:27 -04:00
ctx := context . Background ( )
2019-06-06 07:15:31 -04:00
id1 := container . Run ( ctx , t , c )
2018-12-22 09:53:02 -05:00
defer c . ContainerRemove ( ctx , id1 , types . ContainerRemoveOptions { Force : true } )
result , err := container . Exec ( ctx , c , id1 , [ ] string { "ip" , "l" } )
2018-07-11 10:31:27 -04:00
assert . NilError ( t , err )
assert . Check ( t , is . Equal ( false , strings . Contains ( result . Combined ( ) , "eth0" ) ) , "There shouldn't be eth0 in container in default(bridge) mode when bridge network is disabled" )
2019-06-06 07:15:31 -04:00
id2 := container . Run ( ctx , t , c , container . WithNetworkMode ( "bridge" ) )
2018-12-22 09:53:02 -05:00
defer c . ContainerRemove ( ctx , id2 , types . ContainerRemoveOptions { Force : true } )
2018-07-11 10:31:27 -04:00
2018-12-22 09:53:02 -05:00
result , err = container . Exec ( ctx , c , id2 , [ ] string { "ip" , "l" } )
2018-07-11 10:31:27 -04:00
assert . NilError ( t , err )
assert . Check ( t , is . Equal ( false , strings . Contains ( result . Combined ( ) , "eth0" ) ) , "There shouldn't be eth0 in container in bridge mode when bridge network is disabled" )
nsCommand := "ls -l /proc/self/ns/net | awk -F '->' '{print $2}'"
cmd := exec . Command ( "sh" , "-c" , nsCommand )
stdout := bytes . NewBuffer ( nil )
cmd . Stdout = stdout
err = cmd . Run ( )
assert . NilError ( t , err , "Failed to get current process network namespace: %+v" , err )
2019-06-06 07:15:31 -04:00
id3 := container . Run ( ctx , t , c , container . WithNetworkMode ( "host" ) )
2018-12-22 09:53:02 -05:00
defer c . ContainerRemove ( ctx , id3 , types . ContainerRemoveOptions { Force : true } )
2018-07-11 10:31:27 -04:00
2018-12-22 09:53:02 -05:00
result , err = container . Exec ( ctx , c , id3 , [ ] string { "sh" , "-c" , nsCommand } )
2018-07-11 10:31:27 -04:00
assert . NilError ( t , err )
2018-11-11 10:07:43 -05:00
assert . Check ( t , is . Equal ( stdout . String ( ) , result . Combined ( ) ) , "The network namespace of container should be the same with host when --net=host and bridge network is disabled" )
2018-07-11 10:31:27 -04:00
}
2018-11-05 08:50:33 -05:00
func TestNetworkInvalidJSON ( t * testing . T ) {
defer setupTest ( t ) ( )
endpoints := [ ] string {
"/networks/create" ,
"/networks/bridge/connect" ,
"/networks/bridge/disconnect" ,
}
for _ , ep := range endpoints {
t . Run ( ep , func ( t * testing . T ) {
t . Parallel ( )
res , body , err := request . Post ( ep , request . RawString ( "{invalid json" ) , request . JSON )
assert . NilError ( t , err )
assert . Equal ( t , res . StatusCode , http . StatusBadRequest )
buf , err := request . ReadBody ( body )
assert . NilError ( t , err )
assert . Check ( t , is . Contains ( string ( buf ) , "invalid character 'i' looking for beginning of object key string" ) )
res , body , err = request . Post ( ep , request . JSON )
assert . NilError ( t , err )
assert . Equal ( t , res . StatusCode , http . StatusBadRequest )
buf , err = request . ReadBody ( body )
assert . NilError ( t , err )
assert . Check ( t , is . Contains ( string ( buf ) , "got EOF while reading request body" ) )
} )
}
}