1 package dtest 2 3 import ( 4 "fmt" 5 "os" 6 "os/exec" 7 ) 8 9 // Sudo is intended for use in a TestMain. It will relaunch the test 10 // executable via sudo if it isn't already running with an effective 11 // userid of root. 12 func Sudo() { 13 /* #nosec */ 14 if os.Geteuid() != 0 { 15 cmd := exec.Command("sudo", append([]string{"-E"}, os.Args...)...) 16 cmd.Stdout = os.Stdout 17 cmd.Stderr = os.Stderr 18 err := cmd.Run() 19 if err != nil { 20 fmt.Printf("error re-invoking tests with sudo: %v\n", err) 21 } 22 os.Exit(cmd.ProcessState.ExitCode()) 23 } 24 } 25