2015-05-22 22:21:03 +00:00
|
|
|
/*
|
|
|
|
Copyright 2015 The Kubernetes Authors All rights reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2015-05-22 22:21:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestEnvVarsToMap(t *testing.T) {
|
|
|
|
vars := []EnvVar{
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
Value: "bar",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "zoo",
|
|
|
|
Value: "baz",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
varMap := EnvVarsToMap(vars)
|
|
|
|
|
|
|
|
if e, a := len(vars), len(varMap); e != a {
|
|
|
|
t.Error("Unexpected map length; expected: %v, got %v", e, a)
|
|
|
|
}
|
|
|
|
|
|
|
|
if a := varMap["foo"]; a != "bar" {
|
|
|
|
t.Errorf("Unexpected value of key 'foo': %v", a)
|
|
|
|
}
|
|
|
|
|
|
|
|
if a := varMap["zoo"]; a != "baz" {
|
|
|
|
t.Errorf("Unexpected value of key 'zoo': %v", a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExpandCommandAndArgs(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
container *api.Container
|
|
|
|
envs []EnvVar
|
|
|
|
expectedCommand []string
|
|
|
|
expectedArgs []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "none",
|
|
|
|
container: &api.Container{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "command expanded",
|
|
|
|
container: &api.Container{
|
|
|
|
Command: []string{"foo", "$(VAR_TEST)", "$(VAR_TEST2)"},
|
|
|
|
},
|
|
|
|
envs: []EnvVar{
|
|
|
|
{
|
|
|
|
Name: "VAR_TEST",
|
|
|
|
Value: "zoo",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "VAR_TEST2",
|
|
|
|
Value: "boo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedCommand: []string{"foo", "zoo", "boo"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "args expanded",
|
|
|
|
container: &api.Container{
|
|
|
|
Args: []string{"zap", "$(VAR_TEST)", "$(VAR_TEST2)"},
|
|
|
|
},
|
|
|
|
envs: []EnvVar{
|
|
|
|
{
|
|
|
|
Name: "VAR_TEST",
|
|
|
|
Value: "hap",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "VAR_TEST2",
|
|
|
|
Value: "trap",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedArgs: []string{"zap", "hap", "trap"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "both expanded",
|
|
|
|
container: &api.Container{
|
|
|
|
Command: []string{"$(VAR_TEST2)--$(VAR_TEST)", "foo", "$(VAR_TEST3)"},
|
|
|
|
Args: []string{"foo", "$(VAR_TEST)", "$(VAR_TEST2)"},
|
|
|
|
},
|
|
|
|
envs: []EnvVar{
|
|
|
|
{
|
|
|
|
Name: "VAR_TEST",
|
|
|
|
Value: "zoo",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "VAR_TEST2",
|
|
|
|
Value: "boo",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "VAR_TEST3",
|
|
|
|
Value: "roo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedCommand: []string{"boo--zoo", "foo", "roo"},
|
|
|
|
expectedArgs: []string{"foo", "zoo", "boo"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
actualCommand, actualArgs := ExpandContainerCommandAndArgs(tc.container, tc.envs)
|
|
|
|
|
|
|
|
if e, a := tc.expectedCommand, actualCommand; !reflect.DeepEqual(e, a) {
|
|
|
|
t.Errorf("%v: unexpected command; expected %v, got %v", tc.name, e, a)
|
|
|
|
}
|
|
|
|
|
|
|
|
if e, a := tc.expectedArgs, actualArgs; !reflect.DeepEqual(e, a) {
|
|
|
|
t.Errorf("%v: unexpected args; expected %v, got %v", tc.name, e, a)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|