Skip to content

Commit

Permalink
WIP: unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pmorie committed Apr 14, 2015
1 parent 22f83f0 commit 3c74773
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
16 changes: 10 additions & 6 deletions pkg/kubelet/kubelet.go
Original file line number Diff line number Diff line change
Expand Up @@ -778,9 +778,9 @@ func (kl *Kubelet) runtimeEnvVarValue(envVar api.EnvVar, pod *api.Pod) (string,

switch {
case envVar.ObjectInfo != nil:
return objectInfoRuntimeValue(envVar.ObjectInfo, pod)
return kl.objectInfoRuntimeValue(envVar.ObjectInfo, pod)
case envVar.HostInfo != nil:
return hostInfoRuntimeValue(envVar.HostInfo)
return kl.hostInfoRuntimeValue(envVar.HostInfo)
}

return runtimeVal, nil
Expand All @@ -793,6 +793,8 @@ func (kl *Kubelet) objectInfoRuntimeValue(objInfo *api.ObjectReference, pod *api
return "", err
}

runtimeVal := ""

// TODO: currently there is no generic utility for retrieving a field of
// an object using a fieldPath expression. For now, support only the
// Name and Namespace fields.
Expand All @@ -801,15 +803,17 @@ func (kl *Kubelet) objectInfoRuntimeValue(objInfo *api.ObjectReference, pod *api
runtimeVal = pod.Name
case "metadata.namespace":
runtimeVal = pod.Namespace
default:
err = fmt.Errorf("Unsupported fieldPath: %v", internalFieldPath)
}

return "", fmt.Errorf("Unsupported fieldPath: %v", internalFieldPath)
return runtimeVal, err
}

func (kl *Kubelet) hostInfoRuntimeValue(hostInfo *api.HostInfo) (string, error) {
func (kl *Kubelet) hostInfoRuntimeValue(hostInfo *api.HostInfoSource) (string, error) {
switch {
case envVar.HostInfo.HostName:
return kl.hostName, nil
case hostInfo.HostName:
return kl.hostname, nil
}

// should be unreachable
Expand Down
53 changes: 52 additions & 1 deletion pkg/kubelet/kubelet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ type TestKubelet struct {
fakeMirrorClient *fakeMirrorClient
}

const testKubeletHostname = "testnode"

func newTestKubelet(t *testing.T) *TestKubelet {
fakeDocker := &dockertools.FakeDockerClient{Errors: make(map[string]error), RemovedImages: util.StringSet{}}

Expand Down Expand Up @@ -2089,6 +2091,48 @@ func TestMakeEnvironmentVariables(t *testing.T) {
"KUBERNETES_RO_PORT_8087_TCP_ADDR=1.2.3.7"),
21,
},
{
"downward api pod",
"downward-api",
&api.Container{
Env: []api.EnvVar{
{
Name: "POD_NAME",
EnvVarSource: &api.EnvVarSource{
ObjectInfo: &api.ObjectReference{
APIVersion: "v1beta3",
FieldPath: "metadata.name",
},
},
},
{
Name: "POD_NAMESPACE",
EnvVarSource: &api.EnvVarSource{
ObjectInfo: &api.ObjectReference{
APIVersion: "v1beta3",
FieldPath: "metadata.namespace",
},
},
},
{
Name: "NODE_HOSTNAME",
EnvVarSource: &api.EnvVarSource{
HostInfo: &api.HostInfoSource{
HostName: true,
},
},
},
},
},
"nothing",
true,
util.NewStringSet(
"POD_NAME=dapi-test-pod-name",
"POD_NAMESPACE=downward-api",
fmt.Sprintf("NODE_HOSTNAME=%v", testKubeletHostname),
),
3,
},
}

for _, tc := range testCases {
Expand All @@ -2101,7 +2145,14 @@ func TestMakeEnvironmentVariables(t *testing.T) {
kl.serviceLister = testServiceLister{services}
}

result, err := kl.makeEnvironmentVariables(tc.ns, tc.container)
testPod := &api.Pod{
ObjectMeta: api.ObjectMeta{
Namespace: tc.ns,
Name: "dapi-test-pod-name",
},
}

result, err := kl.makeEnvironmentVariables(testPod, tc.container)
if err != nil {
t.Errorf("[%v] Unexpected error: %v", tc.name, err)
}
Expand Down

0 comments on commit 3c74773

Please sign in to comment.