Skip to content

Commit 9036772

Browse files
committed
Adding/updating kubelet/kuberuntime tests
1 parent 03479e4 commit 9036772

File tree

3 files changed

+76
-4
lines changed

3 files changed

+76
-4
lines changed

pkg/kubelet/container/testing/fake_runtime.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"sync"
2626
"time"
2727

28-
"k8s.io/api/core/v1"
28+
v1 "k8s.io/api/core/v1"
2929
"k8s.io/apimachinery/pkg/types"
3030
"k8s.io/client-go/util/flowcontrol"
3131
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
@@ -300,6 +300,13 @@ func (f *FakeRuntime) PullImage(image kubecontainer.ImageSpec, pullSecrets []v1.
300300
defer f.Unlock()
301301

302302
f.CalledFunctions = append(f.CalledFunctions, "PullImage")
303+
if f.Err == nil {
304+
i := kubecontainer.Image{
305+
ID: image.Image,
306+
Spec: image,
307+
}
308+
f.ImageList = append(f.ImageList, i)
309+
}
303310
return image.Image, f.Err
304311
}
305312

pkg/kubelet/images/image_manager_test.go

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"time"
2323

2424
"github.com/stretchr/testify/assert"
25-
"k8s.io/api/core/v1"
25+
v1 "k8s.io/api/core/v1"
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2727
"k8s.io/apimachinery/pkg/util/clock"
2828
"k8s.io/client-go/tools/record"
@@ -202,3 +202,48 @@ func TestApplyDefaultImageTag(t *testing.T) {
202202
}
203203
}
204204
}
205+
206+
func TestPullAndListImageWithPodAnnotations(t *testing.T) {
207+
pod := &v1.Pod{
208+
ObjectMeta: metav1.ObjectMeta{
209+
Name: "test_pod",
210+
Namespace: "test-ns",
211+
UID: "bar",
212+
ResourceVersion: "42",
213+
SelfLink: "/api/v1/pods/foo",
214+
Annotations: map[string]string{
215+
"kubernetes.io/runtimehandler": "handler_name",
216+
},
217+
}}
218+
c := pullerTestCase{ // pull missing image
219+
containerImage: "missing_image",
220+
policy: v1.PullIfNotPresent,
221+
inspectErr: nil,
222+
pullerErr: nil,
223+
expected: []pullerExpects{
224+
{[]string{"GetImageRef", "PullImage"}, nil},
225+
}}
226+
227+
useSerializedEnv := true
228+
puller, fakeClock, fakeRuntime, container := pullerTestEnv(c, useSerializedEnv)
229+
fakeRuntime.CalledFunctions = nil
230+
fakeRuntime.ImageList = []Image{}
231+
fakeClock.Step(time.Second)
232+
233+
_, _, err := puller.EnsureImageExists(pod, container, nil, nil)
234+
assert.NoError(t, fakeRuntime.AssertCalls(c.expected[0].calls), "tick=%d", 0)
235+
assert.Equal(t, c.expected[0].err, err, "tick=%d", 0)
236+
237+
images, _ := fakeRuntime.ListImages()
238+
assert.Equal(t, 1, len(images), "ListImages() count")
239+
240+
image := images[0]
241+
assert.Equal(t, "missing_image:latest", image.ID, "Image ID")
242+
243+
expectedAnnotations := []Annotation{
244+
{
245+
Name: "kubernetes.io/runtimehandler",
246+
Value: "handler_name",
247+
}}
248+
assert.Equal(t, expectedAnnotations, image.Spec.Annotations, "image spec annotations")
249+
}

pkg/kubelet/kuberuntime/kuberuntime_image_test.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"github.com/stretchr/testify/assert"
2424
"github.com/stretchr/testify/require"
2525

26-
"k8s.io/api/core/v1"
26+
v1 "k8s.io/api/core/v1"
2727
"k8s.io/apimachinery/pkg/util/sets"
2828
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
2929
"k8s.io/kubernetes/pkg/credentialprovider"
@@ -168,6 +168,26 @@ func TestPullWithSecrets(t *testing.T) {
168168

169169
_, err = fakeManager.PullImage(kubecontainer.ImageSpec{Image: test.imageName}, test.passedSecrets, nil)
170170
require.NoError(t, err)
171-
fakeImageService.AssertImagePulledWithAuth(t, &runtimeapi.ImageSpec{Image: test.imageName}, test.expectedAuth, description)
171+
fakeImageService.AssertImagePulledWithAuth(t, &runtimeapi.ImageSpec{Image: test.imageName, Annotations: make(map[string]string)}, test.expectedAuth, description)
172172
}
173173
}
174+
175+
func TestPullThenListWithAnnotations(t *testing.T) {
176+
_, _, fakeManager, err := createTestRuntimeManager()
177+
assert.NoError(t, err)
178+
179+
imageSpec := kubecontainer.ImageSpec{
180+
Image: "12345",
181+
Annotations: []kubecontainer.Annotation{
182+
{Name: "kubernetes.io/runtimehandler", Value: "handler_name"},
183+
},
184+
}
185+
186+
_, err = fakeManager.PullImage(imageSpec, nil, nil)
187+
assert.NoError(t, err)
188+
189+
images, err := fakeManager.ListImages()
190+
assert.NoError(t, err)
191+
assert.Equal(t, 1, len(images))
192+
assert.Equal(t, images[0].Spec, imageSpec)
193+
}

0 commit comments

Comments
 (0)