1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
// Package testsuite provides many quick-and-dirty integration testing utilities.
package testsuite
import (
"log"
"os"
"os/exec"
"os/signal"
"os/user"
"syscall"
)
func init() {
log.SetFlags(0)
log.SetPrefix("testsuite: ")
if os.Geteuid() != 0 {
log.Fatal("this program must run as root")
}
}
// ReceiveSignals blocks until a termination signal arrives, and terminates.
func ReceiveSignals() {
s := make(chan os.Signal, 3)
signal.Notify(s, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP)
log.Fatalf("terminating on signal %s", <-s)
}
// GetUser returns the [user.User] structure describing the user with uid 1000.
func GetUser() *user.User {
if u, err := user.LookupId("1000"); err != nil {
log.Fatal(err)
return nil
} else {
return u
}
}
// MustRun runs command and terminates the testsuite on error.
func MustRun(command ...string) {
cmd := exec.Command(command[0], command[1:]...)
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
}
// MustRunAs wraps [MustRun] for sudo.
func MustRunAs(username string, command ...string) {
MustRun(append([]string{"sudo", "-u", username}, command...)...)
}
|