github.com/reds/docker@v1.11.2-rc1/runconfig/opts/opts_test.go (about) 1 package opts 2 3 import ( 4 "fmt" 5 "os" 6 "strings" 7 "testing" 8 ) 9 10 func TestValidateAttach(t *testing.T) { 11 valid := []string{ 12 "stdin", 13 "stdout", 14 "stderr", 15 "STDIN", 16 "STDOUT", 17 "STDERR", 18 } 19 if _, err := ValidateAttach("invalid"); err == nil { 20 t.Fatalf("Expected error with [valid streams are STDIN, STDOUT and STDERR], got nothing") 21 } 22 23 for _, attach := range valid { 24 value, err := ValidateAttach(attach) 25 if err != nil { 26 t.Fatal(err) 27 } 28 if value != strings.ToLower(attach) { 29 t.Fatalf("Expected [%v], got [%v]", attach, value) 30 } 31 } 32 } 33 34 func TestValidateEnv(t *testing.T) { 35 valids := map[string]string{ 36 "a": "a", 37 "something": "something", 38 "_=a": "_=a", 39 "env1=value1": "env1=value1", 40 "_env1=value1": "_env1=value1", 41 "env2=value2=value3": "env2=value2=value3", 42 "env3=abc!qwe": "env3=abc!qwe", 43 "env_4=value 4": "env_4=value 4", 44 "PATH": fmt.Sprintf("PATH=%v", os.Getenv("PATH")), 45 "PATH=something": "PATH=something", 46 "asd!qwe": "asd!qwe", 47 "1asd": "1asd", 48 "123": "123", 49 "some space": "some space", 50 " some space before": " some space before", 51 "some space after ": "some space after ", 52 } 53 for value, expected := range valids { 54 actual, err := ValidateEnv(value) 55 if err != nil { 56 t.Fatal(err) 57 } 58 if actual != expected { 59 t.Fatalf("Expected [%v], got [%v]", expected, actual) 60 } 61 } 62 } 63 64 func TestValidateExtraHosts(t *testing.T) { 65 valid := []string{ 66 `myhost:192.168.0.1`, 67 `thathost:10.0.2.1`, 68 `anipv6host:2003:ab34:e::1`, 69 `ipv6local:::1`, 70 } 71 72 invalid := map[string]string{ 73 `myhost:192.notanipaddress.1`: `invalid IP`, 74 `thathost-nosemicolon10.0.0.1`: `bad format`, 75 `anipv6host:::::1`: `invalid IP`, 76 `ipv6local:::0::`: `invalid IP`, 77 } 78 79 for _, extrahost := range valid { 80 if _, err := ValidateExtraHost(extrahost); err != nil { 81 t.Fatalf("ValidateExtraHost(`"+extrahost+"`) should succeed: error %v", err) 82 } 83 } 84 85 for extraHost, expectedError := range invalid { 86 if _, err := ValidateExtraHost(extraHost); err == nil { 87 t.Fatalf("ValidateExtraHost(`%q`) should have failed validation", extraHost) 88 } else { 89 if !strings.Contains(err.Error(), expectedError) { 90 t.Fatalf("ValidateExtraHost(`%q`) error should contain %q", extraHost, expectedError) 91 } 92 } 93 } 94 } 95 96 func TestValidateMACAddress(t *testing.T) { 97 if _, err := ValidateMACAddress(`92:d0:c6:0a:29:33`); err != nil { 98 t.Fatalf("ValidateMACAddress(`92:d0:c6:0a:29:33`) got %s", err) 99 } 100 101 if _, err := ValidateMACAddress(`92:d0:c6:0a:33`); err == nil { 102 t.Fatalf("ValidateMACAddress(`92:d0:c6:0a:33`) succeeded; expected failure on invalid MAC") 103 } 104 105 if _, err := ValidateMACAddress(`random invalid string`); err == nil { 106 t.Fatalf("ValidateMACAddress(`random invalid string`) succeeded; expected failure on invalid MAC") 107 } 108 }