2014-02-12 08:04:39 +04:00
package runconfig
2013-11-14 10:08:08 +04:00
import (
2014-06-26 02:46:41 +04:00
"fmt"
2014-03-11 00:24:15 +04:00
"strings"
2013-11-14 10:08:08 +04:00
"testing"
2014-05-21 01:11:33 +04:00
2014-07-25 02:19:50 +04:00
"github.com/docker/docker/nat"
2013-11-14 10:08:08 +04:00
)
2014-03-11 00:24:15 +04:00
func parse ( t * testing . T , args string ) ( * Config , * HostConfig , error ) {
config , hostConfig , _ , err := Parse ( strings . Split ( args + " ubuntu bash" , " " ) , nil )
return config , hostConfig , err
}
func mustParse ( t * testing . T , args string ) ( * Config , * HostConfig ) {
config , hostConfig , err := parse ( t , args )
if err != nil {
t . Fatal ( err )
}
return config , hostConfig
}
2014-06-26 02:46:41 +04:00
// check if (a == c && b == d) || (a == d && b == c)
// because maps are randomized
func compareRandomizedStrings ( a , b , c , d string ) error {
if a == c && b == d {
return nil
}
if a == d && b == c {
return nil
}
return fmt . Errorf ( "strings don't match" )
}
2014-03-11 00:24:15 +04:00
func TestParseRunLinks ( t * testing . T ) {
2014-03-13 21:46:02 +04:00
if _ , hostConfig := mustParse ( t , "--link a:b" ) ; len ( hostConfig . Links ) == 0 || hostConfig . Links [ 0 ] != "a:b" {
2014-03-11 00:24:15 +04:00
t . Fatalf ( "Error parsing links. Expected []string{\"a:b\"}, received: %v" , hostConfig . Links )
}
2014-03-13 21:46:02 +04:00
if _ , hostConfig := mustParse ( t , "--link a:b --link c:d" ) ; len ( hostConfig . Links ) < 2 || hostConfig . Links [ 0 ] != "a:b" || hostConfig . Links [ 1 ] != "c:d" {
2014-03-11 00:24:15 +04:00
t . Fatalf ( "Error parsing links. Expected []string{\"a:b\", \"c:d\"}, received: %v" , hostConfig . Links )
}
if _ , hostConfig := mustParse ( t , "" ) ; len ( hostConfig . Links ) != 0 {
t . Fatalf ( "Error parsing links. No link expected, received: %v" , hostConfig . Links )
}
2014-03-13 21:46:02 +04:00
if _ , _ , err := parse ( t , "--link a" ) ; err == nil {
t . Fatalf ( "Error parsing links. `--link a` should be an error but is not" )
2014-03-11 00:24:15 +04:00
}
2014-03-13 21:46:02 +04:00
if _ , _ , err := parse ( t , "--link" ) ; err == nil {
t . Fatalf ( "Error parsing links. `--link` should be an error but is not" )
2014-03-11 00:24:15 +04:00
}
}
func TestParseRunAttach ( t * testing . T ) {
if config , _ := mustParse ( t , "-a stdin" ) ; ! config . AttachStdin || config . AttachStdout || config . AttachStderr {
t . Fatalf ( "Error parsing attach flags. Expect only Stdin enabled. Received: in: %v, out: %v, err: %v" , config . AttachStdin , config . AttachStdout , config . AttachStderr )
}
if config , _ := mustParse ( t , "-a stdin -a stdout" ) ; ! config . AttachStdin || ! config . AttachStdout || config . AttachStderr {
t . Fatalf ( "Error parsing attach flags. Expect only Stdin and Stdout enabled. Received: in: %v, out: %v, err: %v" , config . AttachStdin , config . AttachStdout , config . AttachStderr )
}
if config , _ := mustParse ( t , "-a stdin -a stdout -a stderr" ) ; ! config . AttachStdin || ! config . AttachStdout || ! config . AttachStderr {
t . Fatalf ( "Error parsing attach flags. Expect all attach enabled. Received: in: %v, out: %v, err: %v" , config . AttachStdin , config . AttachStdout , config . AttachStderr )
}
if config , _ := mustParse ( t , "" ) ; config . AttachStdin || ! config . AttachStdout || ! config . AttachStderr {
t . Fatalf ( "Error parsing attach flags. Expect Stdin disabled. Received: in: %v, out: %v, err: %v" , config . AttachStdin , config . AttachStdout , config . AttachStderr )
}
if _ , _ , err := parse ( t , "-a" ) ; err == nil {
t . Fatalf ( "Error parsing attach flags, `-a` should be an error but is not" )
}
if _ , _ , err := parse ( t , "-a invalid" ) ; err == nil {
t . Fatalf ( "Error parsing attach flags, `-a invalid` should be an error but is not" )
}
if _ , _ , err := parse ( t , "-a invalid -a stdout" ) ; err == nil {
t . Fatalf ( "Error parsing attach flags, `-a stdout -a invalid` should be an error but is not" )
}
if _ , _ , err := parse ( t , "-a stdout -a stderr -d" ) ; err == nil {
t . Fatalf ( "Error parsing attach flags, `-a stdout -a stderr -d` should be an error but is not" )
}
if _ , _ , err := parse ( t , "-a stdin -d" ) ; err == nil {
t . Fatalf ( "Error parsing attach flags, `-a stdin -d` should be an error but is not" )
}
if _ , _ , err := parse ( t , "-a stdout -d" ) ; err == nil {
t . Fatalf ( "Error parsing attach flags, `-a stdout -d` should be an error but is not" )
}
if _ , _ , err := parse ( t , "-a stderr -d" ) ; err == nil {
t . Fatalf ( "Error parsing attach flags, `-a stderr -d` should be an error but is not" )
}
2014-03-13 21:46:02 +04:00
if _ , _ , err := parse ( t , "-d --rm" ) ; err == nil {
t . Fatalf ( "Error parsing attach flags, `-d --rm` should be an error but is not" )
2014-03-11 00:24:15 +04:00
}
}
func TestParseRunVolumes ( t * testing . T ) {
if config , hostConfig := mustParse ( t , "-v /tmp" ) ; hostConfig . Binds != nil {
t . Fatalf ( "Error parsing volume flags, `-v /tmp` should not mount-bind anything. Received %v" , hostConfig . Binds )
} else if _ , exists := config . Volumes [ "/tmp" ] ; ! exists {
t . Fatalf ( "Error parsing volume flags, `-v /tmp` is missing from volumes. Received %v" , config . Volumes )
}
if config , hostConfig := mustParse ( t , "-v /tmp -v /var" ) ; hostConfig . Binds != nil {
t . Fatalf ( "Error parsing volume flags, `-v /tmp -v /var` should not mount-bind anything. Received %v" , hostConfig . Binds )
} else if _ , exists := config . Volumes [ "/tmp" ] ; ! exists {
t . Fatalf ( "Error parsing volume flags, `-v /tmp` is missing from volumes. Recevied %v" , config . Volumes )
} else if _ , exists := config . Volumes [ "/var" ] ; ! exists {
t . Fatalf ( "Error parsing volume flags, `-v /var` is missing from volumes. Received %v" , config . Volumes )
}
2014-05-21 01:11:33 +04:00
if _ , hostConfig := mustParse ( t , "-v /hostTmp:/containerTmp" ) ; hostConfig . Binds == nil || hostConfig . Binds [ 0 ] != "/hostTmp:/containerTmp" {
2014-03-11 00:24:15 +04:00
t . Fatalf ( "Error parsing volume flags, `-v /hostTmp:/containerTmp` should mount-bind /hostTmp into /containeTmp. Received %v" , hostConfig . Binds )
}
2014-06-26 02:46:41 +04:00
if _ , hostConfig := mustParse ( t , "-v /hostTmp:/containerTmp -v /hostVar:/containerVar" ) ; hostConfig . Binds == nil || compareRandomizedStrings ( hostConfig . Binds [ 0 ] , hostConfig . Binds [ 1 ] , "/hostTmp:/containerTmp" , "/hostVar:/containerVar" ) != nil {
2014-03-11 00:24:15 +04:00
t . Fatalf ( "Error parsing volume flags, `-v /hostTmp:/containerTmp -v /hostVar:/containerVar` should mount-bind /hostTmp into /containeTmp and /hostVar into /hostContainer. Received %v" , hostConfig . Binds )
}
2014-06-26 02:46:41 +04:00
if _ , hostConfig := mustParse ( t , "-v /hostTmp:/containerTmp:ro -v /hostVar:/containerVar:rw" ) ; hostConfig . Binds == nil || compareRandomizedStrings ( hostConfig . Binds [ 0 ] , hostConfig . Binds [ 1 ] , "/hostTmp:/containerTmp:ro" , "/hostVar:/containerVar:rw" ) != nil {
2014-03-11 00:24:15 +04:00
t . Fatalf ( "Error parsing volume flags, `-v /hostTmp:/containerTmp:ro -v /hostVar:/containerVar:rw` should mount-bind /hostTmp into /containeTmp and /hostVar into /hostContainer. Received %v" , hostConfig . Binds )
}
if config , hostConfig := mustParse ( t , "-v /hostTmp:/containerTmp -v /containerVar" ) ; hostConfig . Binds == nil || len ( hostConfig . Binds ) > 1 || hostConfig . Binds [ 0 ] != "/hostTmp:/containerTmp" {
t . Fatalf ( "Error parsing volume flags, `-v /hostTmp:/containerTmp -v /containerVar` should mount-bind only /hostTmp into /containeTmp. Received %v" , hostConfig . Binds )
} else if _ , exists := config . Volumes [ "/containerVar" ] ; ! exists {
t . Fatalf ( "Error parsing volume flags, `-v /containerVar` is missing from volumes. Received %v" , config . Volumes )
}
if config , hostConfig := mustParse ( t , "" ) ; hostConfig . Binds != nil {
t . Fatalf ( "Error parsing volume flags, without volume, nothing should be mount-binded. Received %v" , hostConfig . Binds )
} else if len ( config . Volumes ) != 0 {
t . Fatalf ( "Error parsing volume flags, without volume, no volume should be present. Received %v" , config . Volumes )
}
if _ , _ , err := parse ( t , "-v /" ) ; err == nil {
t . Fatalf ( "Expected error, but got none" )
}
if _ , _ , err := parse ( t , "-v /:/" ) ; err == nil {
t . Fatalf ( "Error parsing volume flags, `-v /:/` should fail but didn't" )
}
if _ , _ , err := parse ( t , "-v" ) ; err == nil {
t . Fatalf ( "Error parsing volume flags, `-v` should fail but didn't" )
}
if _ , _ , err := parse ( t , "-v /tmp:" ) ; err == nil {
t . Fatalf ( "Error parsing volume flags, `-v /tmp:` should fail but didn't" )
}
if _ , _ , err := parse ( t , "-v /tmp:ro" ) ; err == nil {
t . Fatalf ( "Error parsing volume flags, `-v /tmp:ro` should fail but didn't" )
}
if _ , _ , err := parse ( t , "-v /tmp::" ) ; err == nil {
t . Fatalf ( "Error parsing volume flags, `-v /tmp::` should fail but didn't" )
}
if _ , _ , err := parse ( t , "-v :" ) ; err == nil {
t . Fatalf ( "Error parsing volume flags, `-v :` should fail but didn't" )
}
if _ , _ , err := parse ( t , "-v ::" ) ; err == nil {
t . Fatalf ( "Error parsing volume flags, `-v ::` should fail but didn't" )
}
if _ , _ , err := parse ( t , "-v /tmp:/tmp:/tmp:/tmp" ) ; err == nil {
t . Fatalf ( "Error parsing volume flags, `-v /tmp:/tmp:/tmp:/tmp` should fail but didn't" )
}
}
2014-02-12 08:04:39 +04:00
func TestCompare ( t * testing . T ) {
2013-11-14 10:08:08 +04:00
volumes1 := make ( map [ string ] struct { } )
volumes1 [ "/test1" ] = struct { } { }
config1 := Config {
2014-04-08 13:26:09 +04:00
PortSpecs : [ ] string { "1111:1111" , "2222:2222" } ,
Env : [ ] string { "VAR1=1" , "VAR2=2" } ,
Volumes : volumes1 ,
2013-11-14 10:08:08 +04:00
}
config3 := Config {
2014-04-08 13:26:09 +04:00
PortSpecs : [ ] string { "0000:0000" , "2222:2222" } ,
Env : [ ] string { "VAR1=1" , "VAR2=2" } ,
Volumes : volumes1 ,
2013-11-14 10:08:08 +04:00
}
volumes2 := make ( map [ string ] struct { } )
volumes2 [ "/test2" ] = struct { } { }
config5 := Config {
2014-04-08 13:26:09 +04:00
PortSpecs : [ ] string { "0000:0000" , "2222:2222" } ,
Env : [ ] string { "VAR1=1" , "VAR2=2" } ,
Volumes : volumes2 ,
2013-11-14 10:08:08 +04:00
}
2014-02-12 08:04:39 +04:00
if Compare ( & config1 , & config3 ) {
t . Fatalf ( "Compare should return false, PortSpecs are different" )
2013-11-14 10:08:08 +04:00
}
2014-02-12 08:04:39 +04:00
if Compare ( & config1 , & config5 ) {
t . Fatalf ( "Compare should return false, Volumes are different" )
2013-11-14 10:08:08 +04:00
}
2014-02-12 08:04:39 +04:00
if ! Compare ( & config1 , & config1 ) {
t . Fatalf ( "Compare should return true" )
2013-11-14 10:08:08 +04:00
}
}
2014-02-12 08:04:39 +04:00
func TestMerge ( t * testing . T ) {
2013-11-14 10:08:08 +04:00
volumesImage := make ( map [ string ] struct { } )
volumesImage [ "/test1" ] = struct { } { }
volumesImage [ "/test2" ] = struct { } { }
configImage := & Config {
2014-04-08 13:26:09 +04:00
PortSpecs : [ ] string { "1111:1111" , "2222:2222" } ,
Env : [ ] string { "VAR1=1" , "VAR2=2" } ,
Volumes : volumesImage ,
2013-11-14 10:08:08 +04:00
}
volumesUser := make ( map [ string ] struct { } )
volumesUser [ "/test3" ] = struct { } { }
configUser := & Config {
PortSpecs : [ ] string { "3333:2222" , "3333:3333" } ,
Env : [ ] string { "VAR2=3" , "VAR3=3" } ,
Volumes : volumesUser ,
}
2014-02-12 08:04:39 +04:00
if err := Merge ( configUser , configImage ) ; err != nil {
2013-11-14 10:08:08 +04:00
t . Error ( err )
}
if len ( configUser . ExposedPorts ) != 3 {
t . Fatalf ( "Expected 3 ExposedPorts, 1111, 2222 and 3333, found %d" , len ( configUser . ExposedPorts ) )
}
for portSpecs := range configUser . ExposedPorts {
if portSpecs . Port ( ) != "1111" && portSpecs . Port ( ) != "2222" && portSpecs . Port ( ) != "3333" {
t . Fatalf ( "Expected 1111 or 2222 or 3333, found %s" , portSpecs )
}
}
if len ( configUser . Env ) != 3 {
t . Fatalf ( "Expected 3 env var, VAR1=1, VAR2=3 and VAR3=3, found %d" , len ( configUser . Env ) )
}
for _ , env := range configUser . Env {
if env != "VAR1=1" && env != "VAR2=3" && env != "VAR3=3" {
t . Fatalf ( "Expected VAR1=1 or VAR2=3 or VAR3=3, found %s" , env )
}
}
if len ( configUser . Volumes ) != 3 {
t . Fatalf ( "Expected 3 volumes, /test1, /test2 and /test3, found %d" , len ( configUser . Volumes ) )
}
for v := range configUser . Volumes {
if v != "/test1" && v != "/test2" && v != "/test3" {
t . Fatalf ( "Expected /test1 or /test2 or /test3, found %s" , v )
}
}
2014-02-12 04:48:44 +04:00
ports , _ , err := nat . ParsePortSpecs ( [ ] string { "0000" } )
2013-11-14 10:08:08 +04:00
if err != nil {
t . Error ( err )
}
configImage2 := & Config {
ExposedPorts : ports ,
}
2014-02-12 08:04:39 +04:00
if err := Merge ( configUser , configImage2 ) ; err != nil {
2013-11-14 10:08:08 +04:00
t . Error ( err )
}
if len ( configUser . ExposedPorts ) != 4 {
t . Fatalf ( "Expected 4 ExposedPorts, 0000, 1111, 2222 and 3333, found %d" , len ( configUser . ExposedPorts ) )
}
for portSpecs := range configUser . ExposedPorts {
if portSpecs . Port ( ) != "0000" && portSpecs . Port ( ) != "1111" && portSpecs . Port ( ) != "2222" && portSpecs . Port ( ) != "3333" {
t . Fatalf ( "Expected 0000 or 1111 or 2222 or 3333, found %s" , portSpecs )
}
}
}