github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/cmds/exp/rush/attr_linux_test.go (about)

     1  // Copyright 2022 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"os"
     9  	"os/exec"
    10  	"syscall"
    11  	"testing"
    12  )
    13  
    14  func TestAttr(t *testing.T) {
    15  	var err error
    16  	ttyf, err = os.OpenFile("/dev/tty", os.O_RDWR, 0)
    17  	if err != nil {
    18  		t.Skipf("can not open /dev/tty, skipping test")
    19  		return
    20  	}
    21  
    22  	c := &Command{Cmd: exec.Command("true")}
    23  
    24  	forkAttr(c)
    25  
    26  	if !c.Cmd.SysProcAttr.Foreground {
    27  		t.Errorf("forkAttr(&c): c.Cmd.SysProcAttr.Foreground is false")
    28  	}
    29  
    30  	fd := int(ttyf.Fd())
    31  	if c.Cmd.SysProcAttr.Ctty != fd {
    32  		t.Errorf("forkAttr(&c): c.Cmd.SysProcAttr.Ctty %d != %d", c.Cmd.SysProcAttr.Ctty, fd)
    33  	}
    34  
    35  	c.BG = true
    36  	forkAttr(c)
    37  	if !c.Cmd.SysProcAttr.Setpgid {
    38  		t.Errorf("forkAttr(&c): c.Cmd.SysProcAttr.Setpgid is not true although BG (BackGround) was set")
    39  	}
    40  
    41  	builtinAttr(c)
    42  	// make sure the right struct member is set.
    43  	if c.Cmd.SysProcAttr.Cloneflags&syscall.CLONE_NEWNS != syscall.CLONE_NEWNS {
    44  		t.Errorf("builtinAttr(&c): c.Cmd.SysProcAttr.Cloneflags did not have syscall.CLONE_NEWNS set")
    45  	}
    46  
    47  }