2016-09-23 21:04:17 +00:00
|
|
|
/*
|
|
|
|
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 (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2017-06-22 17:25:57 +00:00
|
|
|
"k8s.io/api/core/v1"
|
2017-06-22 18:24:23 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2017-05-19 18:18:10 +00:00
|
|
|
runtimetesting "k8s.io/kubernetes/pkg/kubelet/apis/cri/testing"
|
2017-06-07 08:54:28 +00:00
|
|
|
runtimeapi "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
|
2017-05-19 18:18:10 +00:00
|
|
|
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
2016-09-23 21:04:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStableKey(t *testing.T) {
|
2016-11-18 20:50:58 +00:00
|
|
|
container := &v1.Container{
|
2016-09-23 21:04:17 +00:00
|
|
|
Name: "test_container",
|
|
|
|
Image: "foo/image:v1",
|
|
|
|
}
|
2016-11-18 20:50:58 +00:00
|
|
|
pod := &v1.Pod{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-09-23 21:04:17 +00:00
|
|
|
Name: "test_pod",
|
|
|
|
Namespace: "test_pod_namespace",
|
|
|
|
UID: "test_pod_uid",
|
|
|
|
},
|
2016-11-18 20:50:58 +00:00
|
|
|
Spec: v1.PodSpec{
|
|
|
|
Containers: []v1.Container{*container},
|
2016-09-23 21:04:17 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
oldKey := getStableKey(pod, container)
|
|
|
|
|
|
|
|
// Updating the container image should change the key.
|
|
|
|
container.Image = "foo/image:v2"
|
|
|
|
newKey := getStableKey(pod, container)
|
|
|
|
assert.NotEqual(t, oldKey, newKey)
|
|
|
|
}
|
2017-05-12 07:41:15 +00:00
|
|
|
|
|
|
|
// TestGetSystclsFromAnnotations tests the logic of getting sysctls from annotations.
|
|
|
|
func TestGetSystclsFromAnnotations(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
annotations map[string]string
|
|
|
|
expectedSysctls map[string]string
|
|
|
|
}{{
|
|
|
|
annotations: map[string]string{
|
|
|
|
v1.SysctlsPodAnnotationKey: "kernel.shmmni=32768,kernel.shmmax=1000000000",
|
|
|
|
v1.UnsafeSysctlsPodAnnotationKey: "knet.ipv4.route.min_pmtu=1000",
|
|
|
|
},
|
|
|
|
expectedSysctls: map[string]string{
|
|
|
|
"kernel.shmmni": "32768",
|
|
|
|
"kernel.shmmax": "1000000000",
|
|
|
|
"knet.ipv4.route.min_pmtu": "1000",
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
annotations: map[string]string{
|
|
|
|
v1.SysctlsPodAnnotationKey: "kernel.shmmni=32768,kernel.shmmax=1000000000",
|
|
|
|
},
|
|
|
|
expectedSysctls: map[string]string{
|
|
|
|
"kernel.shmmni": "32768",
|
|
|
|
"kernel.shmmax": "1000000000",
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
annotations: map[string]string{
|
|
|
|
v1.UnsafeSysctlsPodAnnotationKey: "knet.ipv4.route.min_pmtu=1000",
|
|
|
|
},
|
|
|
|
expectedSysctls: map[string]string{
|
|
|
|
"knet.ipv4.route.min_pmtu": "1000",
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
actualSysctls, err := getSysctlsFromAnnotations(test.annotations)
|
|
|
|
assert.NoError(t, err, "TestCase[%d]", i)
|
|
|
|
assert.Len(t, actualSysctls, len(test.expectedSysctls), "TestCase[%d]", i)
|
|
|
|
assert.Equal(t, test.expectedSysctls, actualSysctls, "TestCase[%d]", i)
|
|
|
|
}
|
|
|
|
}
|
2017-05-19 18:18:10 +00:00
|
|
|
|
|
|
|
func TestToKubeContainer(t *testing.T) {
|
|
|
|
c := &runtimeapi.Container{
|
|
|
|
Id: "test-id",
|
|
|
|
Metadata: &runtimeapi.ContainerMetadata{
|
|
|
|
Name: "test-name",
|
|
|
|
Attempt: 1,
|
|
|
|
},
|
|
|
|
Image: &runtimeapi.ImageSpec{Image: "test-image"},
|
|
|
|
ImageRef: "test-image-ref",
|
|
|
|
State: runtimeapi.ContainerState_CONTAINER_RUNNING,
|
|
|
|
Annotations: map[string]string{
|
|
|
|
containerHashLabel: "1234",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
expect := &kubecontainer.Container{
|
|
|
|
ID: kubecontainer.ContainerID{
|
|
|
|
Type: runtimetesting.FakeRuntimeName,
|
|
|
|
ID: "test-id",
|
|
|
|
},
|
|
|
|
Name: "test-name",
|
|
|
|
ImageID: "test-image-ref",
|
|
|
|
Image: "test-image",
|
|
|
|
Hash: uint64(0x1234),
|
|
|
|
State: kubecontainer.ContainerStateRunning,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, m, err := createTestRuntimeManager()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
got, err := m.toKubeContainer(c)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expect, got)
|
|
|
|
}
|
2017-05-30 10:46:06 +00:00
|
|
|
|
|
|
|
func TestGetImageUser(t *testing.T) {
|
|
|
|
_, i, m, err := createTestRuntimeManager()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
type image struct {
|
|
|
|
name string
|
|
|
|
uid *runtimeapi.Int64Value
|
|
|
|
username string
|
|
|
|
}
|
|
|
|
|
|
|
|
type imageUserValues struct {
|
|
|
|
// getImageUser can return (*int64)(nil) so comparing with *uid will break
|
|
|
|
// type cannot be *int64 as Golang does not allow to take the address of a numeric constant"
|
|
|
|
uid interface{}
|
|
|
|
username string
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
description string
|
|
|
|
originalImage image
|
|
|
|
expectedImageUserValues imageUserValues
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"image without username and uid should return (new(int64), \"\", nil)",
|
|
|
|
image{
|
|
|
|
name: "test-image-ref1",
|
|
|
|
uid: (*runtimeapi.Int64Value)(nil),
|
|
|
|
username: "",
|
|
|
|
},
|
|
|
|
imageUserValues{
|
|
|
|
uid: int64(0),
|
|
|
|
username: "",
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"image with username and no uid should return ((*int64)nil, imageStatus.Username, nil)",
|
|
|
|
image{
|
|
|
|
name: "test-image-ref2",
|
|
|
|
uid: (*runtimeapi.Int64Value)(nil),
|
|
|
|
username: "testUser",
|
|
|
|
},
|
|
|
|
imageUserValues{
|
|
|
|
uid: (*int64)(nil),
|
|
|
|
username: "testUser",
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"image with uid should return (*int64, \"\", nil)",
|
|
|
|
image{
|
|
|
|
name: "test-image-ref3",
|
|
|
|
uid: &runtimeapi.Int64Value{
|
|
|
|
Value: 2,
|
|
|
|
},
|
|
|
|
username: "whatever",
|
|
|
|
},
|
|
|
|
imageUserValues{
|
|
|
|
uid: int64(2),
|
|
|
|
username: "",
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
i.SetFakeImages([]string{"test-image-ref1", "test-image-ref2", "test-image-ref3"})
|
|
|
|
for j, test := range tests {
|
|
|
|
i.Images[test.originalImage.name].Username = test.originalImage.username
|
|
|
|
i.Images[test.originalImage.name].Uid = test.originalImage.uid
|
|
|
|
|
|
|
|
uid, username, error := m.getImageUser(test.originalImage.name)
|
|
|
|
assert.NoError(t, error, "TestCase[%d]", j)
|
|
|
|
|
|
|
|
if test.expectedImageUserValues.uid == (*int64)(nil) {
|
|
|
|
assert.Equal(t, test.expectedImageUserValues.uid, uid, "TestCase[%d]", j)
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, test.expectedImageUserValues.uid, *uid, "TestCase[%d]", j)
|
|
|
|
}
|
|
|
|
assert.Equal(t, test.expectedImageUserValues.username, username, "TestCase[%d]", j)
|
|
|
|
}
|
|
|
|
}
|