github.com/hpcng/singularity@v3.1.1+incompatible/pkg/util/namespaces/setns_linux_test.go (about)

     1  // Copyright (c) 2018, Sylabs Inc. All rights reserved.
     2  // This software is licensed under a 3-clause BSD license. Please consult the
     3  // LICENSE.md file distributed with the sources of this project regarding your
     4  // rights to use or distribute this software.
     5  
     6  package namespaces
     7  
     8  import (
     9  	"os/exec"
    10  	"syscall"
    11  	"testing"
    12  
    13  	"github.com/sylabs/singularity/internal/pkg/test"
    14  )
    15  
    16  func TestEnter(t *testing.T) {
    17  	test.EnsurePrivilege(t)
    18  
    19  	cmd := exec.Command("/bin/cat")
    20  	cmd.SysProcAttr = &syscall.SysProcAttr{}
    21  	cmd.SysProcAttr.Cloneflags = syscall.CLONE_NEWIPC | syscall.CLONE_NEWNET
    22  
    23  	pipe, err := cmd.StdinPipe()
    24  	if err != nil {
    25  		t.Fatal(err)
    26  	}
    27  
    28  	if err := cmd.Start(); err != nil {
    29  		t.Fatal(err)
    30  	}
    31  
    32  	if err := Enter(cmd.Process.Pid, "ipc"); err != nil {
    33  		t.Error(err)
    34  	}
    35  	if err := Enter(cmd.Process.Pid, "net"); err != nil {
    36  		t.Error(err)
    37  	}
    38  
    39  	pipe.Close()
    40  
    41  	if err := cmd.Wait(); err != nil {
    42  		t.Error(err)
    43  	}
    44  
    45  	if err := Enter(0, "net"); err == nil {
    46  		t.Errorf("should have failed with bad process")
    47  	}
    48  	if err := Enter(cmd.Process.Pid, "user"); err == nil {
    49  		t.Error("should have failed with unsupported namespace")
    50  	}
    51  }