Skip to content

Commit 4539b9c

Browse files
authored
Merge pull request kubernetes#133660 from scaventz/109717-PreStartContainer
Test: Add unit test for PreStartContainer
2 parents 8de3b30 + 5d4d6cc commit 4539b9c

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cm
18+
19+
import (
20+
"context"
21+
"testing"
22+
23+
v1 "k8s.io/api/core/v1"
24+
"k8s.io/kubernetes/pkg/kubelet/cm/cpumanager"
25+
"k8s.io/kubernetes/pkg/kubelet/cm/memorymanager"
26+
"k8s.io/kubernetes/pkg/kubelet/cm/topologymanager"
27+
)
28+
29+
type mockCPUManager struct {
30+
called bool
31+
cpumanager.Manager
32+
}
33+
34+
func (cpuManager *mockCPUManager) AddContainer(*v1.Pod, *v1.Container, string) {
35+
cpuManager.called = true
36+
}
37+
38+
type mockMemoryManager struct {
39+
called bool
40+
memorymanager.Manager
41+
}
42+
43+
func (memoryManager *mockMemoryManager) AddContainer(context.Context, *v1.Pod, *v1.Container, string) {
44+
memoryManager.called = true
45+
}
46+
47+
type mockTopologyManager struct {
48+
called bool
49+
topologymanager.Manager
50+
}
51+
52+
func (topologyManager *mockTopologyManager) AddContainer(*v1.Pod, *v1.Container, string) {
53+
topologyManager.called = true
54+
}
55+
56+
func TestPreStartContainer(t *testing.T) {
57+
pod := &v1.Pod{}
58+
container := &v1.Container{}
59+
60+
tests := []struct {
61+
name string
62+
lifecycle internalContainerLifecycleImpl
63+
}{
64+
{
65+
name: "When a CPU manager is provided it has AddContainer called",
66+
lifecycle: internalContainerLifecycleImpl{
67+
cpuManager: &mockCPUManager{},
68+
memoryManager: nil,
69+
topologyManager: &mockTopologyManager{},
70+
},
71+
}, {
72+
name: "When a Memory manager is provided it has AddContainer called",
73+
lifecycle: internalContainerLifecycleImpl{
74+
cpuManager: nil,
75+
memoryManager: &mockMemoryManager{},
76+
topologyManager: &mockTopologyManager{},
77+
},
78+
}, {
79+
name: "When a CPU manager/Memory manager is not provided, it's ok",
80+
lifecycle: internalContainerLifecycleImpl{
81+
cpuManager: nil,
82+
memoryManager: nil,
83+
topologyManager: &mockTopologyManager{},
84+
},
85+
},
86+
}
87+
88+
for _, test := range tests {
89+
t.Run(test.name, func(t *testing.T) {
90+
_ = test.lifecycle.PreStartContainer(pod, container, "42")
91+
})
92+
93+
cManager := test.lifecycle.cpuManager
94+
mManager := test.lifecycle.memoryManager
95+
tManager := test.lifecycle.topologyManager
96+
if cManager != nil && !cManager.(*mockCPUManager).called {
97+
t.Errorf("When a CPU manager is provided it must have AddContainer called")
98+
}
99+
if mManager != nil && !mManager.(*mockMemoryManager).called {
100+
t.Errorf("When a Memory manager is provided it must have AddContainer called")
101+
}
102+
if !tManager.(*mockTopologyManager).called {
103+
t.Errorf("TopologyManager's AddContainer method must be called during container startup")
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)