github.com/endophage/docker@v1.4.2-0.20161027011718-242853499895/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 TestValidateArg(t *testing.T) { 65 valids := map[string]string{ 66 "_=a": "_=a", 67 "var1=value1": "var1=value1", 68 "_var1=value1": "_var1=value1", 69 "var2=value2=value3": "var2=value2=value3", 70 "var3=abc!qwe": "var3=abc!qwe", 71 "var_4=value 4": "var_4=value 4", 72 "var_5=": "var_5=", 73 } 74 for value, expected := range valids { 75 actual, err := ValidateArg(value) 76 if err != nil { 77 t.Fatal(err) 78 } 79 if actual != expected { 80 t.Fatalf("Expected [%v], got [%v]", expected, actual) 81 } 82 } 83 84 invalid := map[string]string{ 85 "foo": "bad format", 86 "=foo": "bad format", 87 "cc c": "bad format", 88 } 89 for value, expectedError := range invalid { 90 if _, err := ValidateArg(value); err == nil { 91 t.Fatalf("ValidateArg(`%s`) should have failed validation", value) 92 } else { 93 if !strings.Contains(err.Error(), expectedError) { 94 t.Fatalf("ValidateArg(`%s`) error should contain %q", value, expectedError) 95 } 96 } 97 } 98 } 99 100 func TestValidateExtraHosts(t *testing.T) { 101 valid := []string{ 102 `myhost:192.168.0.1`, 103 `thathost:10.0.2.1`, 104 `anipv6host:2003:ab34:e::1`, 105 `ipv6local:::1`, 106 } 107 108 invalid := map[string]string{ 109 `myhost:192.notanipaddress.1`: `invalid IP`, 110 `thathost-nosemicolon10.0.0.1`: `bad format`, 111 `anipv6host:::::1`: `invalid IP`, 112 `ipv6local:::0::`: `invalid IP`, 113 } 114 115 for _, extrahost := range valid { 116 if _, err := ValidateExtraHost(extrahost); err != nil { 117 t.Fatalf("ValidateExtraHost(`"+extrahost+"`) should succeed: error %v", err) 118 } 119 } 120 121 for extraHost, expectedError := range invalid { 122 if _, err := ValidateExtraHost(extraHost); err == nil { 123 t.Fatalf("ValidateExtraHost(`%q`) should have failed validation", extraHost) 124 } else { 125 if !strings.Contains(err.Error(), expectedError) { 126 t.Fatalf("ValidateExtraHost(`%q`) error should contain %q", extraHost, expectedError) 127 } 128 } 129 } 130 } 131 132 func TestValidateMACAddress(t *testing.T) { 133 if _, err := ValidateMACAddress(`92:d0:c6:0a:29:33`); err != nil { 134 t.Fatalf("ValidateMACAddress(`92:d0:c6:0a:29:33`) got %s", err) 135 } 136 137 if _, err := ValidateMACAddress(`92:d0:c6:0a:33`); err == nil { 138 t.Fatalf("ValidateMACAddress(`92:d0:c6:0a:33`) succeeded; expected failure on invalid MAC") 139 } 140 141 if _, err := ValidateMACAddress(`random invalid string`); err == nil { 142 t.Fatalf("ValidateMACAddress(`random invalid string`) succeeded; expected failure on invalid MAC") 143 } 144 }