pkg/kubelet/userns: add idsPerPod tests

Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
This commit is contained in:
Akihiro Suda 2025-02-14 10:47:03 +09:00
parent 1592bfa4a8
commit 09fdae408f
No known key found for this signature in database
GPG Key ID: 49524C6F9F638F1A

View File

@ -102,26 +102,54 @@ func (m *testUserNsPodsManager) GetUserNamespacesIDsPerPod() uint32 {
func TestUserNsManagerAllocate(t *testing.T) {
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, pkgfeatures.UserNamespacesSupport, true)
testUserNsPodsManager := &testUserNsPodsManager{}
customUserNsLength := uint32(1048576)
cases := []struct {
name string
userNsLength uint32
mappingFirstID uint32
mappingLen uint32
}{
{
name: "default",
userNsLength: testUserNsLength,
mappingFirstID: minimumMappingUID,
mappingLen: mappingLen,
},
{
name: "custom",
userNsLength: customUserNsLength,
mappingFirstID: customUserNsLength,
mappingLen: customUserNsLength * 2000,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
testUserNsPodsManager := &testUserNsPodsManager{
userNsLength: tc.userNsLength,
mappingFirstID: tc.mappingFirstID,
mappingLen: tc.mappingLen,
}
m, err := MakeUserNsManager(testUserNsPodsManager)
require.NoError(t, err)
allocated, length, err := m.allocateOne("one")
assert.NoError(t, err)
assert.Equal(t, testUserNsLength, length, "m.isSet(%d).length=%v", allocated, length)
require.NoError(t, err)
assert.Equal(t, tc.userNsLength, length, "m.isSet(%d).length=%v", allocated, length)
assert.True(t, m.isSet(allocated), "m.isSet(%d)", allocated)
allocated2, length2, err := m.allocateOne("two")
assert.NoError(t, err)
require.NoError(t, err)
assert.NotEqual(t, allocated, allocated2, "allocated != allocated2")
assert.Equal(t, length, length2, "length == length2")
// verify that re-adding the same pod with the same settings won't fail
err = m.record("two", allocated2, length2)
assert.NoError(t, err)
require.NoError(t, err)
// but it fails if anyting is different
err = m.record("two", allocated2+1, length2)
assert.Error(t, err)
require.Error(t, err)
m.Release("one")
m.Release("two")
@ -131,11 +159,11 @@ func TestUserNsManagerAllocate(t *testing.T) {
var allocs []uint32
for i := 0; i < 1000; i++ {
allocated, length, err = m.allocateOne(types.UID(fmt.Sprintf("%d", i)))
assert.Equal(t, testUserNsLength, length, "length is not the expected. iter: %v", i)
assert.NoError(t, err)
assert.GreaterOrEqual(t, allocated, uint32(minimumMappingUID))
assert.Equal(t, tc.userNsLength, length, "length is not the expected. iter: %v", i)
require.NoError(t, err)
assert.GreaterOrEqual(t, allocated, tc.mappingFirstID)
// The last ID of the userns range (allocated+userNsLength) should be within bounds.
assert.LessOrEqual(t, allocated, uint32(minimumMappingUID+mappingLen-testUserNsLength))
assert.LessOrEqual(t, allocated, tc.mappingFirstID+tc.mappingLen-tc.userNsLength)
allocs = append(allocs, allocated)
}
for i, v := range allocs {
@ -143,11 +171,13 @@ func TestUserNsManagerAllocate(t *testing.T) {
m.Release(types.UID(fmt.Sprintf("%d", i)))
assert.False(t, m.isSet(v), "m.isSet(%d) should be false", v)
err = m.record(types.UID(fmt.Sprintf("%d", i)), v, testUserNsLength)
assert.NoError(t, err)
err = m.record(types.UID(fmt.Sprintf("%d", i)), v, tc.userNsLength)
require.NoError(t, err)
m.Release(types.UID(fmt.Sprintf("%d", i)))
assert.False(t, m.isSet(v), "m.isSet(%d) should be false", v)
}
})
}
}
func TestMakeUserNsManager(t *testing.T) {