-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
46 lines (43 loc) · 1.28 KB
/
main_test.go
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
package main
import (
"strings"
"os"
"os/exec"
"testing"
"syscall"
)
func TestHost(t *testing.T) {
oldHostname, err := os.Hostname()
if err != nil {
t.Fatal(err)
}
var hostname string = "test"
cmd := exec.Command("go", "run", "main.go", "test-Host(hostname)", hostname)
cmd.SysProcAttr = &syscall.SysProcAttr{
Cloneflags: syscall.CLONE_NEWUSER |
syscall.CLONE_NEWUTS,
UidMappings: []syscall.SysProcIDMap{
{
ContainerID: 0,
HostID: os.Getuid(),
Size: 1,
},
},
GidMappings: []syscall.SysProcIDMap{
{
ContainerID: 0,
HostID: os.Getgid(),
Size: 1,
},
},
}
output, err := cmd.Output()
if err != nil {
t.Fatal(err)
}
outputString := strings.Trim(string(output), "\n")
if outputString != hostname {
t.Fatalf("Expected hostname to be '%s' after Host(%s), got '%s'", hostname, hostname, output)
}
t.Logf("Hostname was '%s', successfuly changed it to '%s' in new UTS namespace", oldHostname, outputString)
}