Skip to content

Commit db09060

Browse files
committed
userns: Fix running tests inside a userns
containerd creates a userns and inside there, it runs the critest tool. However, in that setup, the length of containerd's userns is not the whole UID space. Let's verify that the length of the userns inside the pod, when we created it with NamespaceMode_NODE (IOW, when not using a new userns for the pod) is the same as outside the pod. This works fine when contained itsel runs inside a userns. Signed-off-by: Rodrigo Campos <rodrigoca@microsoft.com>
1 parent 316d6d3 commit db09060

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

pkg/validate/security_context_linux.go

+17-3
Original file line numberDiff line numberDiff line change
@@ -936,13 +936,18 @@ var _ = framework.KubeDescribe("Security Context", func() {
936936
podID, podConfig = createNamespacePodSandbox(rc, namespaceOption, podName, podLogPath)
937937
containerName := runUserNamespaceContainer(rc, ic, podID, podConfig)
938938

939-
// 4294967295 means that the entire range is available
940939
expectedOutput := hostUsernsContent()
941940
if expectedOutput == "" {
942941
Fail("failed to get host userns content")
943942
}
944-
// 4294967295 means that the entire range is available
945-
matchContainerOutputRe(podConfig, containerName, `\s+0\s+0\s+4294967295\n`)
943+
// We need to see that the same mapping than from outside the
944+
// container is used.
945+
// This is because critest can run inside a userns (containerd
946+
// does that with critest) and then the mapping we see here is not
947+
// the mapping of the initial user namespace.
948+
for _, line := range strings.Split(expectedOutput, "\n") {
949+
matchContainerOutput(podConfig, containerName, line)
950+
}
946951
})
947952

948953
It("runtime should fail if more than one mapping provided", func() {
@@ -1564,3 +1569,12 @@ func rootfsPath(info map[string]string) string {
15641569
// always exist.
15651570
return filepath.Join(cfg.StateDir, "../")
15661571
}
1572+
1573+
func hostUsernsContent() string {
1574+
uidMapPath := "/proc/self/uid_map"
1575+
uidMapContent, err := os.ReadFile(uidMapPath)
1576+
if err != nil {
1577+
return ""
1578+
}
1579+
return string(uidMapContent)
1580+
}

0 commit comments

Comments
 (0)