github.com/docker/docker-ce@v17.12.1-ce-rc2+incompatible/components/cli/opts/opts_test.go (about) 1 package opts 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 ) 8 9 func TestValidateIPAddress(t *testing.T) { 10 if ret, err := ValidateIPAddress(`1.2.3.4`); err != nil || ret == "" { 11 t.Fatalf("ValidateIPAddress(`1.2.3.4`) got %s %s", ret, err) 12 } 13 14 if ret, err := ValidateIPAddress(`127.0.0.1`); err != nil || ret == "" { 15 t.Fatalf("ValidateIPAddress(`127.0.0.1`) got %s %s", ret, err) 16 } 17 18 if ret, err := ValidateIPAddress(`::1`); err != nil || ret == "" { 19 t.Fatalf("ValidateIPAddress(`::1`) got %s %s", ret, err) 20 } 21 22 if ret, err := ValidateIPAddress(`127`); err == nil || ret != "" { 23 t.Fatalf("ValidateIPAddress(`127`) got %s %s", ret, err) 24 } 25 26 if ret, err := ValidateIPAddress(`random invalid string`); err == nil || ret != "" { 27 t.Fatalf("ValidateIPAddress(`random invalid string`) got %s %s", ret, err) 28 } 29 30 } 31 32 func TestMapOpts(t *testing.T) { 33 tmpMap := make(map[string]string) 34 o := NewMapOpts(tmpMap, logOptsValidator) 35 o.Set("max-size=1") 36 if o.String() != "map[max-size:1]" { 37 t.Errorf("%s != [map[max-size:1]", o.String()) 38 } 39 40 o.Set("max-file=2") 41 if len(tmpMap) != 2 { 42 t.Errorf("map length %d != 2", len(tmpMap)) 43 } 44 45 if tmpMap["max-file"] != "2" { 46 t.Errorf("max-file = %s != 2", tmpMap["max-file"]) 47 } 48 49 if tmpMap["max-size"] != "1" { 50 t.Errorf("max-size = %s != 1", tmpMap["max-size"]) 51 } 52 if o.Set("dummy-val=3") == nil { 53 t.Error("validator is not being called") 54 } 55 } 56 57 func TestListOptsWithoutValidator(t *testing.T) { 58 o := NewListOpts(nil) 59 o.Set("foo") 60 if o.String() != "[foo]" { 61 t.Errorf("%s != [foo]", o.String()) 62 } 63 o.Set("bar") 64 if o.Len() != 2 { 65 t.Errorf("%d != 2", o.Len()) 66 } 67 o.Set("bar") 68 if o.Len() != 3 { 69 t.Errorf("%d != 3", o.Len()) 70 } 71 if !o.Get("bar") { 72 t.Error("o.Get(\"bar\") == false") 73 } 74 if o.Get("baz") { 75 t.Error("o.Get(\"baz\") == true") 76 } 77 o.Delete("foo") 78 if o.String() != "[bar bar]" { 79 t.Errorf("%s != [bar bar]", o.String()) 80 } 81 listOpts := o.GetAll() 82 if len(listOpts) != 2 || listOpts[0] != "bar" || listOpts[1] != "bar" { 83 t.Errorf("Expected [[bar bar]], got [%v]", listOpts) 84 } 85 mapListOpts := o.GetMap() 86 if len(mapListOpts) != 1 { 87 t.Errorf("Expected [map[bar:{}]], got [%v]", mapListOpts) 88 } 89 90 } 91 92 func TestListOptsWithValidator(t *testing.T) { 93 // Re-using logOptsvalidator (used by MapOpts) 94 o := NewListOpts(logOptsValidator) 95 o.Set("foo") 96 if o.String() != "" { 97 t.Errorf(`%s != ""`, o.String()) 98 } 99 o.Set("foo=bar") 100 if o.String() != "" { 101 t.Errorf(`%s != ""`, o.String()) 102 } 103 o.Set("max-file=2") 104 if o.Len() != 1 { 105 t.Errorf("%d != 1", o.Len()) 106 } 107 if !o.Get("max-file=2") { 108 t.Error("o.Get(\"max-file=2\") == false") 109 } 110 if o.Get("baz") { 111 t.Error("o.Get(\"baz\") == true") 112 } 113 o.Delete("max-file=2") 114 if o.String() != "" { 115 t.Errorf(`%s != ""`, o.String()) 116 } 117 } 118 119 // nolint: lll 120 func TestValidateDNSSearch(t *testing.T) { 121 valid := []string{ 122 `.`, 123 `a`, 124 `a.`, 125 `1.foo`, 126 `17.foo`, 127 `foo.bar`, 128 `foo.bar.baz`, 129 `foo.bar.`, 130 `foo.bar.baz`, 131 `foo1.bar2`, 132 `foo1.bar2.baz`, 133 `1foo.2bar.`, 134 `1foo.2bar.baz`, 135 `foo-1.bar-2`, 136 `foo-1.bar-2.baz`, 137 `foo-1.bar-2.`, 138 `foo-1.bar-2.baz`, 139 `1-foo.2-bar`, 140 `1-foo.2-bar.baz`, 141 `1-foo.2-bar.`, 142 `1-foo.2-bar.baz`, 143 } 144 145 invalid := []string{ 146 ``, 147 ` `, 148 ` `, 149 `17`, 150 `17.`, 151 `.17`, 152 `17-.`, 153 `17-.foo`, 154 `.foo`, 155 `foo-.bar`, 156 `-foo.bar`, 157 `foo.bar-`, 158 `foo.bar-.baz`, 159 `foo.-bar`, 160 `foo.-bar.baz`, 161 `foo.bar.baz.this.should.fail.on.long.name.because.it.is.longer.thanisshouldbethis.should.fail.on.long.name.because.it.is.longer.thanisshouldbethis.should.fail.on.long.name.because.it.is.longer.thanisshouldbethis.should.fail.on.long.name.because.it.is.longer.thanisshouldbe`, 162 } 163 164 for _, domain := range valid { 165 if ret, err := ValidateDNSSearch(domain); err != nil || ret == "" { 166 t.Fatalf("ValidateDNSSearch(`"+domain+"`) got %s %s", ret, err) 167 } 168 } 169 170 for _, domain := range invalid { 171 if ret, err := ValidateDNSSearch(domain); err == nil || ret != "" { 172 t.Fatalf("ValidateDNSSearch(`"+domain+"`) got %s %s", ret, err) 173 } 174 } 175 } 176 177 func TestValidateLabel(t *testing.T) { 178 if _, err := ValidateLabel("label"); err == nil || err.Error() != "bad attribute format: label" { 179 t.Fatalf("Expected an error [bad attribute format: label], go %v", err) 180 } 181 if actual, err := ValidateLabel("key1=value1"); err != nil || actual != "key1=value1" { 182 t.Fatalf("Expected [key1=value1], got [%v,%v]", actual, err) 183 } 184 // Validate it's working with more than one = 185 if actual, err := ValidateLabel("key1=value1=value2"); err != nil { 186 t.Fatalf("Expected [key1=value1=value2], got [%v,%v]", actual, err) 187 } 188 // Validate it's working with one more 189 if actual, err := ValidateLabel("key1=value1=value2=value3"); err != nil { 190 t.Fatalf("Expected [key1=value1=value2=value2], got [%v,%v]", actual, err) 191 } 192 } 193 194 func logOptsValidator(val string) (string, error) { 195 allowedKeys := map[string]string{"max-size": "1", "max-file": "2"} 196 vals := strings.Split(val, "=") 197 if allowedKeys[vals[0]] != "" { 198 return val, nil 199 } 200 return "", fmt.Errorf("invalid key %s", vals[0]) 201 } 202 203 func TestNamedListOpts(t *testing.T) { 204 var v []string 205 o := NewNamedListOptsRef("foo-name", &v, nil) 206 207 o.Set("foo") 208 if o.String() != "[foo]" { 209 t.Errorf("%s != [foo]", o.String()) 210 } 211 if o.Name() != "foo-name" { 212 t.Errorf("%s != foo-name", o.Name()) 213 } 214 if len(v) != 1 { 215 t.Errorf("expected foo to be in the values, got %v", v) 216 } 217 } 218 219 func TestNamedMapOpts(t *testing.T) { 220 tmpMap := make(map[string]string) 221 o := NewNamedMapOpts("max-name", tmpMap, nil) 222 223 o.Set("max-size=1") 224 if o.String() != "map[max-size:1]" { 225 t.Errorf("%s != [map[max-size:1]", o.String()) 226 } 227 if o.Name() != "max-name" { 228 t.Errorf("%s != max-name", o.Name()) 229 } 230 if _, exist := tmpMap["max-size"]; !exist { 231 t.Errorf("expected map-size to be in the values, got %v", tmpMap) 232 } 233 } 234 235 func TestValidateMACAddress(t *testing.T) { 236 if _, err := ValidateMACAddress(`92:d0:c6:0a:29:33`); err != nil { 237 t.Fatalf("ValidateMACAddress(`92:d0:c6:0a:29:33`) got %s", err) 238 } 239 240 if _, err := ValidateMACAddress(`92:d0:c6:0a:33`); err == nil { 241 t.Fatalf("ValidateMACAddress(`92:d0:c6:0a:33`) succeeded; expected failure on invalid MAC") 242 } 243 244 if _, err := ValidateMACAddress(`random invalid string`); err == nil { 245 t.Fatalf("ValidateMACAddress(`random invalid string`) succeeded; expected failure on invalid MAC") 246 } 247 } 248 249 func TestValidateLink(t *testing.T) { 250 valid := []string{ 251 "name", 252 "dcdfbe62ecd0:alias", 253 "7a67485460b7642516a4ad82ecefe7f57d0c4916f530561b71a50a3f9c4e33da", 254 "angry_torvalds:linus", 255 } 256 invalid := map[string]string{ 257 "": "empty string specified for links", 258 "too:much:of:it": "bad format for links: too:much:of:it", 259 } 260 261 for _, link := range valid { 262 if _, err := ValidateLink(link); err != nil { 263 t.Fatalf("ValidateLink(`%q`) should succeed: error %q", link, err) 264 } 265 } 266 267 for link, expectedError := range invalid { 268 if _, err := ValidateLink(link); err == nil { 269 t.Fatalf("ValidateLink(`%q`) should have failed validation", link) 270 } else { 271 if !strings.Contains(err.Error(), expectedError) { 272 t.Fatalf("ValidateLink(`%q`) error should contain %q", link, expectedError) 273 } 274 } 275 } 276 } 277 278 func TestParseLink(t *testing.T) { 279 name, alias, err := ParseLink("name:alias") 280 if err != nil { 281 t.Fatalf("Expected not to error out on a valid name:alias format but got: %v", err) 282 } 283 if name != "name" { 284 t.Fatalf("Link name should have been name, got %s instead", name) 285 } 286 if alias != "alias" { 287 t.Fatalf("Link alias should have been alias, got %s instead", alias) 288 } 289 // short format definition 290 name, alias, err = ParseLink("name") 291 if err != nil { 292 t.Fatalf("Expected not to error out on a valid name only format but got: %v", err) 293 } 294 if name != "name" { 295 t.Fatalf("Link name should have been name, got %s instead", name) 296 } 297 if alias != "name" { 298 t.Fatalf("Link alias should have been name, got %s instead", alias) 299 } 300 // empty string link definition is not allowed 301 if _, _, err := ParseLink(""); err == nil || !strings.Contains(err.Error(), "empty string specified for links") { 302 t.Fatalf("Expected error 'empty string specified for links' but got: %v", err) 303 } 304 // more than two colons are not allowed 305 if _, _, err := ParseLink("link:alias:wrong"); err == nil || !strings.Contains(err.Error(), "bad format for links: link:alias:wrong") { 306 t.Fatalf("Expected error 'bad format for links: link:alias:wrong' but got: %v", err) 307 } 308 }