mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
Merge pull request #18997 from mqliang/env-variable
Auto commit by PR queue bot
This commit is contained in:
commit
7f25a4f662
@ -593,11 +593,16 @@ func (BasicPod) Generate(genericParams map[string]interface{}) (runtime.Object,
|
||||
func parseEnvs(envArray []string) ([]api.EnvVar, error) {
|
||||
envs := []api.EnvVar{}
|
||||
for _, env := range envArray {
|
||||
parts := strings.Split(env, "=")
|
||||
if len(parts) != 2 || !validation.IsCIdentifier(parts[0]) || len(parts[1]) == 0 {
|
||||
pos := strings.Index(env, "=")
|
||||
if pos == -1 {
|
||||
return nil, fmt.Errorf("invalid env: %v", env)
|
||||
}
|
||||
envVar := api.EnvVar{Name: parts[0], Value: parts[1]}
|
||||
name := env[:pos]
|
||||
value := env[pos+1:]
|
||||
if len(name) == 0 || !validation.IsCIdentifier(name) || len(value) == 0 {
|
||||
return nil, fmt.Errorf("invalid env: %v", env)
|
||||
}
|
||||
envVar := api.EnvVar{Name: name, Value: value}
|
||||
envs = append(envs, envVar)
|
||||
}
|
||||
return envs, nil
|
||||
|
@ -811,3 +811,73 @@ func TestGenerateJob(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseEnv(t *testing.T) {
|
||||
tests := []struct {
|
||||
envArray []string
|
||||
expected []api.EnvVar
|
||||
expectErr bool
|
||||
test string
|
||||
}{
|
||||
{
|
||||
envArray: []string{
|
||||
"THIS_ENV=isOK",
|
||||
"HAS_COMMAS=foo,bar",
|
||||
"HAS_EQUALS=jJnro54iUu75xNy==",
|
||||
},
|
||||
expected: []api.EnvVar{
|
||||
{
|
||||
Name: "THIS_ENV",
|
||||
Value: "isOK",
|
||||
},
|
||||
{
|
||||
Name: "HAS_COMMAS",
|
||||
Value: "foo,bar",
|
||||
},
|
||||
{
|
||||
Name: "HAS_EQUALS",
|
||||
Value: "jJnro54iUu75xNy==",
|
||||
},
|
||||
},
|
||||
expectErr: false,
|
||||
test: "test case 1",
|
||||
},
|
||||
{
|
||||
envArray: []string{
|
||||
"WITH_OUT_EQUALS",
|
||||
},
|
||||
expected: []api.EnvVar{},
|
||||
expectErr: true,
|
||||
test: "test case 2",
|
||||
},
|
||||
{
|
||||
envArray: []string{
|
||||
"WITH_OUT_VALUES=",
|
||||
},
|
||||
expected: []api.EnvVar{},
|
||||
expectErr: true,
|
||||
test: "test case 3",
|
||||
},
|
||||
{
|
||||
envArray: []string{
|
||||
"=WITH_OUT_NAME",
|
||||
},
|
||||
expected: []api.EnvVar{},
|
||||
expectErr: true,
|
||||
test: "test case 4",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
envs, err := parseEnvs(test.envArray)
|
||||
if !test.expectErr && err != nil {
|
||||
t.Errorf("unexpected error: %v (%s)", err, test.test)
|
||||
}
|
||||
if test.expectErr && err != nil {
|
||||
continue
|
||||
}
|
||||
if !reflect.DeepEqual(envs, test.expected) {
|
||||
t.Errorf("\nexpected:\n%#v\nsaw:\n%#v (%s)", test.expected, envs, test.test)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user