@@ -129,6 +129,10 @@ func TestRuntimeWriteProcessPid(t *testing.T) {
129129}
130130
131131func TestRuntimeProxyInheritsCorrectNamespace (t * testing.T ) {
132+ if os .Geteuid () != 0 {
133+ t .Skip ("requires root privileges to create new namespaces" )
134+ }
135+
132136 rootDir := t .TempDir ()
133137 remoteprocName := "a-lovely-blue-device"
134138 sim := remoteproc .NewSimulator (rootDir ).WithName (remoteprocName )
@@ -167,6 +171,48 @@ func TestRuntimeProxyInheritsCorrectNamespace(t *testing.T) {
167171 require .NoError (t , err )
168172}
169173
174+ func TestRuntimeProxyKeepsHostNamespaceWhenNotRoot (t * testing.T ) {
175+ if os .Geteuid () == 0 {
176+ t .Skip ("this test must be run as non-root" )
177+ }
178+ rootDir := t .TempDir ()
179+ remoteprocName := "a-lovely-blue-device"
180+ sim := remoteproc .NewSimulator (rootDir ).WithName (remoteprocName )
181+ if err := sim .Start (); err != nil {
182+ t .Fatalf ("failed to run simulator: %s" , err )
183+ }
184+ defer func () { _ = sim .Stop () }()
185+
186+ bin , err := repo .BuildRuntimeBin (t .TempDir (), rootDir , nil )
187+ require .NoError (t , err )
188+
189+ const containerName = "good-looking-container"
190+ bundlePath := t .TempDir ()
191+
192+ require .NoError (t , generateBundle (
193+ bundlePath ,
194+ remoteprocName ,
195+ specs.LinuxNamespace {Type : specs .MountNamespace },
196+ ))
197+
198+ _ , err = invokeRuntime (bin , "create" , "--bundle" , bundlePath , containerName )
199+ require .NoError (t , err )
200+ defer func () {
201+ _ , _ = invokeRuntime (bin , "delete" , containerName )
202+ }()
203+
204+ state , err := getContainerState (bin , containerName )
205+ require .NoError (t , err )
206+ require .Greater (t , state .Pid , 0 )
207+
208+ hostMountNS , err := os .Readlink ("/proc/self/ns/mnt" )
209+ require .NoError (t , err )
210+ proxyMountNS , err := os .Readlink (fmt .Sprintf ("/proc/%d/ns/mnt" , state .Pid ))
211+ require .NoError (t , err )
212+
213+ assert .Equal (t , hostMountNS , proxyMountNS )
214+ }
215+
170216func assertContainerStatus (t testing.TB , bin repo.RuntimeBin , containerName string , wantStatus specs.ContainerState ) {
171217 t .Helper ()
172218 state , err := getContainerState (bin , containerName )
@@ -183,6 +229,10 @@ func getContainerPid(bin repo.RuntimeBin, containerName string) (int, error) {
183229}
184230
185231func getContainerState (bin repo.RuntimeBin , containerName string ) (specs.State , error ) {
232+ return getContainerStateWithEnv (bin , containerName , nil )
233+ }
234+
235+ func getContainerStateWithEnv (bin repo.RuntimeBin , containerName string , extraEnv []string ) (specs.State , error ) {
186236 var state specs.State
187237 out , err := invokeRuntime (bin , "state" , containerName )
188238 if err != nil {
0 commit comments