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