123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- /*
- Copyright 2016 The Kubernetes Authors.
- 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 kuberuntime
- import (
- "reflect"
- "testing"
- "k8s.io/kubernetes/pkg/api"
- kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
- "k8s.io/kubernetes/pkg/util/intstr"
- )
- func TestContainerLabels(t *testing.T) {
- deletionGracePeriod := int64(10)
- terminationGracePeriod := int64(10)
- lifecycle := &api.Lifecycle{
- // Left PostStart as nil
- PreStop: &api.Handler{
- Exec: &api.ExecAction{
- Command: []string{"action1", "action2"},
- },
- HTTPGet: &api.HTTPGetAction{
- Path: "path",
- Host: "host",
- Port: intstr.FromInt(8080),
- Scheme: "scheme",
- },
- TCPSocket: &api.TCPSocketAction{
- Port: intstr.FromString("80"),
- },
- },
- }
- container := &api.Container{
- Name: "test_container",
- TerminationMessagePath: "/somepath",
- Lifecycle: lifecycle,
- }
- pod := &api.Pod{
- ObjectMeta: api.ObjectMeta{
- Name: "test_pod",
- Namespace: "test_pod_namespace",
- UID: "test_pod_uid",
- DeletionGracePeriodSeconds: &deletionGracePeriod,
- },
- Spec: api.PodSpec{
- Containers: []api.Container{*container},
- TerminationGracePeriodSeconds: &terminationGracePeriod,
- },
- }
- expected := &labeledContainerInfo{
- PodName: pod.Name,
- PodNamespace: pod.Namespace,
- PodUID: pod.UID,
- ContainerName: container.Name,
- }
- // Test whether we can get right information from label
- labels := newContainerLabels(container, pod)
- containerInfo := getContainerInfoFromLabels(labels)
- if !reflect.DeepEqual(containerInfo, expected) {
- t.Errorf("expected %v, got %v", expected, containerInfo)
- }
- }
- func TestContainerAnnotations(t *testing.T) {
- restartCount := 5
- deletionGracePeriod := int64(10)
- terminationGracePeriod := int64(10)
- lifecycle := &api.Lifecycle{
- // Left PostStart as nil
- PreStop: &api.Handler{
- Exec: &api.ExecAction{
- Command: []string{"action1", "action2"},
- },
- HTTPGet: &api.HTTPGetAction{
- Path: "path",
- Host: "host",
- Port: intstr.FromInt(8080),
- Scheme: "scheme",
- },
- TCPSocket: &api.TCPSocketAction{
- Port: intstr.FromString("80"),
- },
- },
- }
- containerPorts := []api.ContainerPort{
- {
- Name: "http",
- HostPort: 80,
- ContainerPort: 8080,
- Protocol: api.ProtocolTCP,
- },
- {
- Name: "https",
- HostPort: 443,
- ContainerPort: 6443,
- Protocol: api.ProtocolTCP,
- },
- }
- container := &api.Container{
- Name: "test_container",
- Ports: containerPorts,
- TerminationMessagePath: "/somepath",
- Lifecycle: lifecycle,
- }
- pod := &api.Pod{
- ObjectMeta: api.ObjectMeta{
- Name: "test_pod",
- Namespace: "test_pod_namespace",
- UID: "test_pod_uid",
- DeletionGracePeriodSeconds: &deletionGracePeriod,
- },
- Spec: api.PodSpec{
- Containers: []api.Container{*container},
- TerminationGracePeriodSeconds: &terminationGracePeriod,
- },
- }
- expected := &annotatedContainerInfo{
- ContainerPorts: containerPorts,
- PodDeletionGracePeriod: pod.DeletionGracePeriodSeconds,
- PodTerminationGracePeriod: pod.Spec.TerminationGracePeriodSeconds,
- Hash: kubecontainer.HashContainer(container),
- RestartCount: restartCount,
- TerminationMessagePath: container.TerminationMessagePath,
- PreStopHandler: container.Lifecycle.PreStop,
- }
- // Test whether we can get right information from label
- annotations := newContainerAnnotations(container, pod, restartCount)
- containerInfo := getContainerInfoFromAnnotations(annotations)
- if !reflect.DeepEqual(containerInfo, expected) {
- t.Errorf("expected %v, got %v", expected, containerInfo)
- }
- // Test when DeletionGracePeriodSeconds, TerminationGracePeriodSeconds and Lifecycle are nil,
- // the information got from annotations should also be nil
- container.Lifecycle = nil
- pod.DeletionGracePeriodSeconds = nil
- pod.Spec.TerminationGracePeriodSeconds = nil
- expected.PodDeletionGracePeriod = nil
- expected.PodTerminationGracePeriod = nil
- expected.PreStopHandler = nil
- // Because container is changed, the Hash should be updated
- expected.Hash = kubecontainer.HashContainer(container)
- annotations = newContainerAnnotations(container, pod, restartCount)
- containerInfo = getContainerInfoFromAnnotations(annotations)
- if !reflect.DeepEqual(containerInfo, expected) {
- t.Errorf("expected %v, got %v", expected, containerInfo)
- }
- }
- func TestPodLabels(t *testing.T) {
- pod := &api.Pod{
- ObjectMeta: api.ObjectMeta{
- Name: "test_pod",
- Namespace: "test_pod_namespace",
- UID: "test_pod_uid",
- Labels: map[string]string{"foo": "bar"},
- },
- Spec: api.PodSpec{
- Containers: []api.Container{},
- },
- }
- expected := &labeledPodSandboxInfo{
- Labels: pod.Labels,
- PodName: pod.Name,
- PodNamespace: pod.Namespace,
- PodUID: pod.UID,
- }
- // Test whether we can get right information from label
- labels := newPodLabels(pod)
- podSandboxInfo := getPodSandboxInfoFromLabels(labels)
- if !reflect.DeepEqual(podSandboxInfo, expected) {
- t.Errorf("expected %v, got %v", expected, podSandboxInfo)
- }
- }
- func TestPodAnnotations(t *testing.T) {
- pod := &api.Pod{
- ObjectMeta: api.ObjectMeta{
- Name: "test_pod",
- Namespace: "test_pod_namespace",
- UID: "test_pod_uid",
- Annotations: map[string]string{"foo": "bar"},
- },
- Spec: api.PodSpec{
- Containers: []api.Container{},
- },
- }
- expected := &annotatedPodSandboxInfo{
- Annotations: map[string]string{"foo": "bar"},
- }
- // Test whether we can get right information from annotations
- annotations := newPodAnnotations(pod)
- podSandboxInfo := getPodSandboxInfoFromAnnotations(annotations)
- if !reflect.DeepEqual(podSandboxInfo, expected) {
- t.Errorf("expected %v, got %v", expected, podSandboxInfo)
- }
- }
|