mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
add an exception case and map changge to struct with expect error
Signed-off-by: chchliang <chen.chuanliang@zte.com.cn> add an exception case and map changge to struct with expect error Signed-off-by: chchliang <chen.chuanliang@zte.com.cn>
This commit is contained in:
parent
184cea5ff7
commit
76e9f0d6d4
1 changed files with 107 additions and 25 deletions
128
opts/env_test.go
128
opts/env_test.go
|
@ -8,35 +8,117 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestValidateEnv(t *testing.T) {
|
func TestValidateEnv(t *testing.T) {
|
||||||
valids := map[string]string{
|
testcase := []struct {
|
||||||
"a": "a",
|
value string
|
||||||
"something": "something",
|
expected string
|
||||||
"_=a": "_=a",
|
err error
|
||||||
"env1=value1": "env1=value1",
|
}{
|
||||||
"_env1=value1": "_env1=value1",
|
{
|
||||||
"env2=value2=value3": "env2=value2=value3",
|
value: "a",
|
||||||
"env3=abc!qwe": "env3=abc!qwe",
|
expected: "a",
|
||||||
"env_4=value 4": "env_4=value 4",
|
},
|
||||||
"PATH": fmt.Sprintf("PATH=%v", os.Getenv("PATH")),
|
{
|
||||||
"PATH=something": "PATH=something",
|
value: "something",
|
||||||
"asd!qwe": "asd!qwe",
|
expected: "something",
|
||||||
"1asd": "1asd",
|
},
|
||||||
"123": "123",
|
{
|
||||||
"some space": "some space",
|
value: "_=a",
|
||||||
" some space before": " some space before",
|
expected: "_=a",
|
||||||
"some space after ": "some space after ",
|
},
|
||||||
|
{
|
||||||
|
value: "env1=value1",
|
||||||
|
expected: "env1=value1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
value: "_env1=value1",
|
||||||
|
expected: "_env1=value1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
value: "env2=value2=value3",
|
||||||
|
expected: "env2=value2=value3",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
value: "env3=abc!qwe",
|
||||||
|
expected: "env3=abc!qwe",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
value: "env_4=value 4",
|
||||||
|
expected: "env_4=value 4",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
value: "PATH",
|
||||||
|
expected: fmt.Sprintf("PATH=%v", os.Getenv("PATH")),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
value: "=a",
|
||||||
|
err: fmt.Errorf(fmt.Sprintf("invalid environment variable: %s", "=a")),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
value: "PATH=something",
|
||||||
|
expected: "PATH=something",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
value: "asd!qwe",
|
||||||
|
expected: "asd!qwe",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
value: "1asd",
|
||||||
|
expected: "1asd",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
value: "123",
|
||||||
|
expected: "123",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
value: "some space",
|
||||||
|
expected: "some space",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
value: " some space before",
|
||||||
|
expected: " some space before",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
value: "some space after ",
|
||||||
|
expected: "some space after ",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
value: "=",
|
||||||
|
err: fmt.Errorf(fmt.Sprintf("invalid environment variable: %s", "=")),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Environment variables are case in-sensitive on Windows
|
// Environment variables are case in-sensitive on Windows
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
valids["PaTh"] = fmt.Sprintf("PaTh=%v", os.Getenv("PATH"))
|
tmp := struct {
|
||||||
|
value string
|
||||||
|
expected string
|
||||||
|
err error
|
||||||
|
}{
|
||||||
|
value: "PaTh",
|
||||||
|
expected: fmt.Sprintf("PaTh=%v", os.Getenv("PATH")),
|
||||||
}
|
}
|
||||||
for value, expected := range valids {
|
testcase = append(testcase, tmp)
|
||||||
actual, err := ValidateEnv(value)
|
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, r := range testcase {
|
||||||
|
actual, err := ValidateEnv(r.value)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
if r.err == nil {
|
||||||
|
t.Fatalf("Expected err is nil, got err[%v]", err)
|
||||||
}
|
}
|
||||||
if actual != expected {
|
if err.Error() != r.err.Error() {
|
||||||
t.Fatalf("Expected [%v], got [%v]", expected, actual)
|
t.Fatalf("Expected err[%v], got err[%v]", r.err, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err == nil && r.err != nil {
|
||||||
|
t.Fatalf("Expected err[%v], but err is nil", r.err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if actual != r.expected {
|
||||||
|
t.Fatalf("Expected [%v], got [%v]", r.expected, actual)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue