kubectl: fix 'create deployment' to set container name correctly

This commit is contained in:
Michail Kargakis 2016-12-14 15:26:36 +01:00
parent 9705bb728e
commit bf78c00a96
3 changed files with 21 additions and 5 deletions

View File

@ -331,6 +331,7 @@ runTests() {
second_port_field="(index .spec.ports 1).port"
second_port_name="(index .spec.ports 1).name"
image_field="(index .spec.containers 0).image"
container_name_field="(index .spec.template.spec.containers 0).name"
hpa_min_field=".spec.minReplicas"
hpa_max_field=".spec.maxReplicas"
hpa_cpu_field=".spec.targetCPUUtilizationPercentage"
@ -1219,6 +1220,13 @@ __EOF__
# Clean up
kubectl delete deployment nginx "${kube_flags[@]}"
# Test kubectl create deployment
kubectl create deployment test-nginx --image=gcr.io/google-containers/nginx:test-cmd
# Post-Condition: Deployment has 2 replicas defined in its spec.
kube::test::get_object_assert 'deploy test-nginx' "{{$container_name_field}}" 'nginx'
# Clean up
kubectl delete deployment test-nginx "${kube_flags[@]}"
###############
# Kubectl get #
###############

View File

@ -67,8 +67,15 @@ func (s *DeploymentBasicGeneratorV1) StructuredGenerate() (runtime.Object, error
podSpec := api.PodSpec{Containers: []api.Container{}}
for _, imageString := range s.Images {
// Retain just the image name
imageSplit := strings.Split(imageString, "/")
name := imageSplit[len(imageSplit)-1]
// Remove any tag or hash
if strings.Contains(name, ":") {
name = strings.Split(name, ":")[0]
} else if strings.Contains(name, "@") {
name = strings.Split(name, "@")[0]
}
podSpec.Containers = append(podSpec.Containers, api.Container{Name: name, Image: imageString})
}

View File

@ -49,7 +49,7 @@ func TestDeploymentGenerate(t *testing.T) {
Labels: map[string]string{"app": "foo"},
},
Spec: api.PodSpec{
Containers: []api.Container{{Name: "app:v4", Image: "abc/app:v4"}},
Containers: []api.Container{{Name: "app", Image: "abc/app:v4"}},
},
},
},
@ -74,7 +74,7 @@ func TestDeploymentGenerate(t *testing.T) {
Labels: map[string]string{"app": "foo"},
},
Spec: api.PodSpec{
Containers: []api.Container{{Name: "app:v4", Image: "abc/app:v4"},
Containers: []api.Container{{Name: "app", Image: "abc/app:v4"},
{Name: "ape", Image: "zyx/ape"}},
},
},
@ -114,21 +114,22 @@ func TestDeploymentGenerate(t *testing.T) {
}
generator := DeploymentBasicGeneratorV1{}
for index, test := range tests {
t.Logf("running scenario %d", index)
obj, err := generator.Generate(test.params)
switch {
case test.expectErr && err != nil:
continue // loop, since there's no output to check
case test.expectErr && err == nil:
t.Errorf("%v: expected error and didn't get one", index)
t.Errorf("expected error and didn't get one")
continue // loop, no expected output object
case !test.expectErr && err != nil:
t.Errorf("%v: unexpected error %v", index, err)
t.Errorf("unexpected error %v", err)
continue // loop, no output object
case !test.expectErr && err == nil:
// do nothing and drop through
}
if !reflect.DeepEqual(obj.(*extensions.Deployment), test.expected) {
t.Errorf("%v\nexpected:\n%#v\nsaw:\n%#v", index, test.expected, obj.(*extensions.Deployment))
t.Errorf("expected:\n%#v\nsaw:\n%#v", test.expected, obj.(*extensions.Deployment))
}
}
}