2018-02-05 16:05:59 -05:00
|
|
|
package opts // import "github.com/docker/docker/opts"
|
2016-12-23 14:09:12 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
2020-08-14 10:54:08 -04:00
|
|
|
|
|
|
|
"gotest.tools/v3/assert"
|
2016-12-23 14:09:12 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestValidateEnv(t *testing.T) {
|
2020-08-14 10:54:08 -04:00
|
|
|
type testCase struct {
|
2017-08-30 23:36:37 -04:00
|
|
|
value string
|
|
|
|
expected string
|
|
|
|
err error
|
2020-08-14 10:54:08 -04:00
|
|
|
}
|
|
|
|
tests := []testCase{
|
2017-08-30 23:36:37 -04:00
|
|
|
{
|
|
|
|
value: "a",
|
|
|
|
expected: "a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "something",
|
|
|
|
expected: "something",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "_=a",
|
|
|
|
expected: "_=a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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",
|
2020-08-14 10:54:08 -04:00
|
|
|
err: fmt.Errorf("invalid environment variable: =a"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "PATH=",
|
|
|
|
expected: "PATH=",
|
2017-08-30 23:36:37 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
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: "=",
|
2020-08-14 10:54:08 -04:00
|
|
|
err: fmt.Errorf("invalid environment variable: ="),
|
2017-08-30 23:36:37 -04:00
|
|
|
},
|
2016-12-23 14:09:12 -05:00
|
|
|
}
|
2017-08-30 23:36:37 -04:00
|
|
|
|
2016-12-23 14:09:12 -05:00
|
|
|
if runtime.GOOS == "windows" {
|
2020-08-14 10:54:08 -04:00
|
|
|
// Environment variables are case in-sensitive on Windows
|
|
|
|
tests = append(tests, testCase{
|
2017-08-30 23:36:37 -04:00
|
|
|
value: "PaTh",
|
|
|
|
expected: fmt.Sprintf("PaTh=%v", os.Getenv("PATH")),
|
2019-08-05 11:50:51 -04:00
|
|
|
err: nil,
|
2020-08-14 10:54:08 -04:00
|
|
|
})
|
2016-12-23 14:09:12 -05:00
|
|
|
}
|
2017-08-30 23:36:37 -04:00
|
|
|
|
2020-08-14 10:54:08 -04:00
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.value, func(t *testing.T) {
|
|
|
|
actual, err := ValidateEnv(tc.value)
|
2017-08-30 23:36:37 -04:00
|
|
|
|
2020-08-14 10:54:08 -04:00
|
|
|
if tc.err == nil {
|
|
|
|
assert.NilError(t, err)
|
|
|
|
} else {
|
|
|
|
assert.Error(t, err, tc.err.Error())
|
2017-08-30 23:36:37 -04:00
|
|
|
}
|
2020-08-14 10:54:08 -04:00
|
|
|
assert.Equal(t, actual, tc.expected)
|
|
|
|
})
|
2016-12-23 14:09:12 -05:00
|
|
|
}
|
|
|
|
}
|