Skip to content

Commit 09fdae4

Browse files
committed
pkg/kubelet/userns: add idsPerPod tests
Signed-off-by: Akihiro Suda <[email protected]>
1 parent 1592bfa commit 09fdae4

File tree

1 file changed

+71
-41
lines changed

1 file changed

+71
-41
lines changed

pkg/kubelet/userns/userns_manager_test.go

Lines changed: 71 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -102,51 +102,81 @@ func (m *testUserNsPodsManager) GetUserNamespacesIDsPerPod() uint32 {
102102
func TestUserNsManagerAllocate(t *testing.T) {
103103
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, pkgfeatures.UserNamespacesSupport, true)
104104

105-
testUserNsPodsManager := &testUserNsPodsManager{}
106-
m, err := MakeUserNsManager(testUserNsPodsManager)
107-
require.NoError(t, err)
105+
customUserNsLength := uint32(1048576)
108106

109-
allocated, length, err := m.allocateOne("one")
110-
assert.NoError(t, err)
111-
assert.Equal(t, testUserNsLength, length, "m.isSet(%d).length=%v", allocated, length)
112-
assert.True(t, m.isSet(allocated), "m.isSet(%d)", allocated)
107+
cases := []struct {
108+
name string
109+
userNsLength uint32
110+
mappingFirstID uint32
111+
mappingLen uint32
112+
}{
113+
{
114+
name: "default",
115+
userNsLength: testUserNsLength,
116+
mappingFirstID: minimumMappingUID,
117+
mappingLen: mappingLen,
118+
},
119+
{
120+
name: "custom",
121+
userNsLength: customUserNsLength,
122+
mappingFirstID: customUserNsLength,
123+
mappingLen: customUserNsLength * 2000,
124+
},
125+
}
113126

114-
allocated2, length2, err := m.allocateOne("two")
115-
assert.NoError(t, err)
116-
assert.NotEqual(t, allocated, allocated2, "allocated != allocated2")
117-
assert.Equal(t, length, length2, "length == length2")
127+
for _, tc := range cases {
128+
t.Run(tc.name, func(t *testing.T) {
129+
testUserNsPodsManager := &testUserNsPodsManager{
130+
userNsLength: tc.userNsLength,
131+
mappingFirstID: tc.mappingFirstID,
132+
mappingLen: tc.mappingLen,
133+
}
134+
m, err := MakeUserNsManager(testUserNsPodsManager)
135+
require.NoError(t, err)
118136

119-
// verify that re-adding the same pod with the same settings won't fail
120-
err = m.record("two", allocated2, length2)
121-
assert.NoError(t, err)
122-
// but it fails if anyting is different
123-
err = m.record("two", allocated2+1, length2)
124-
assert.Error(t, err)
137+
allocated, length, err := m.allocateOne("one")
138+
require.NoError(t, err)
139+
assert.Equal(t, tc.userNsLength, length, "m.isSet(%d).length=%v", allocated, length)
140+
assert.True(t, m.isSet(allocated), "m.isSet(%d)", allocated)
125141

126-
m.Release("one")
127-
m.Release("two")
128-
assert.False(t, m.isSet(allocated), "m.isSet(%d)", allocated)
129-
assert.False(t, m.isSet(allocated2), "m.nsSet(%d)", allocated2)
130-
131-
var allocs []uint32
132-
for i := 0; i < 1000; i++ {
133-
allocated, length, err = m.allocateOne(types.UID(fmt.Sprintf("%d", i)))
134-
assert.Equal(t, testUserNsLength, length, "length is not the expected. iter: %v", i)
135-
assert.NoError(t, err)
136-
assert.GreaterOrEqual(t, allocated, uint32(minimumMappingUID))
137-
// The last ID of the userns range (allocated+userNsLength) should be within bounds.
138-
assert.LessOrEqual(t, allocated, uint32(minimumMappingUID+mappingLen-testUserNsLength))
139-
allocs = append(allocs, allocated)
140-
}
141-
for i, v := range allocs {
142-
assert.True(t, m.isSet(v), "m.isSet(%d) should be true", v)
143-
m.Release(types.UID(fmt.Sprintf("%d", i)))
144-
assert.False(t, m.isSet(v), "m.isSet(%d) should be false", v)
145-
146-
err = m.record(types.UID(fmt.Sprintf("%d", i)), v, testUserNsLength)
147-
assert.NoError(t, err)
148-
m.Release(types.UID(fmt.Sprintf("%d", i)))
149-
assert.False(t, m.isSet(v), "m.isSet(%d) should be false", v)
142+
allocated2, length2, err := m.allocateOne("two")
143+
require.NoError(t, err)
144+
assert.NotEqual(t, allocated, allocated2, "allocated != allocated2")
145+
assert.Equal(t, length, length2, "length == length2")
146+
147+
// verify that re-adding the same pod with the same settings won't fail
148+
err = m.record("two", allocated2, length2)
149+
require.NoError(t, err)
150+
// but it fails if anyting is different
151+
err = m.record("two", allocated2+1, length2)
152+
require.Error(t, err)
153+
154+
m.Release("one")
155+
m.Release("two")
156+
assert.False(t, m.isSet(allocated), "m.isSet(%d)", allocated)
157+
assert.False(t, m.isSet(allocated2), "m.nsSet(%d)", allocated2)
158+
159+
var allocs []uint32
160+
for i := 0; i < 1000; i++ {
161+
allocated, length, err = m.allocateOne(types.UID(fmt.Sprintf("%d", i)))
162+
assert.Equal(t, tc.userNsLength, length, "length is not the expected. iter: %v", i)
163+
require.NoError(t, err)
164+
assert.GreaterOrEqual(t, allocated, tc.mappingFirstID)
165+
// The last ID of the userns range (allocated+userNsLength) should be within bounds.
166+
assert.LessOrEqual(t, allocated, tc.mappingFirstID+tc.mappingLen-tc.userNsLength)
167+
allocs = append(allocs, allocated)
168+
}
169+
for i, v := range allocs {
170+
assert.True(t, m.isSet(v), "m.isSet(%d) should be true", v)
171+
m.Release(types.UID(fmt.Sprintf("%d", i)))
172+
assert.False(t, m.isSet(v), "m.isSet(%d) should be false", v)
173+
174+
err = m.record(types.UID(fmt.Sprintf("%d", i)), v, tc.userNsLength)
175+
require.NoError(t, err)
176+
m.Release(types.UID(fmt.Sprintf("%d", i)))
177+
assert.False(t, m.isSet(v), "m.isSet(%d) should be false", v)
178+
}
179+
})
150180
}
151181
}
152182

0 commit comments

Comments
 (0)