summaryrefslogtreecommitdiff
path: root/test/e2e/rootless_test.go
blob: f162d5ddf4fe853317a3030aa234bef7a28f54d6 (plain)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package integration

import (
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"syscall"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

func canExec() bool {
	const nsGetParent = 0xb702

	u, err := os.Open("/proc/self/ns/user")
	if err != nil {
		return false
	}
	defer u.Close()

	_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, u.Fd(), uintptr(nsGetParent), 0)
	return errno != syscall.ENOTTY
}

var _ = Describe("Podman rootless", func() {
	var (
		tempdir    string
		err        error
		podmanTest PodmanTest
	)

	BeforeEach(func() {
		tempdir, err = CreateTempDirInTempDir()
		if err != nil {
			os.Exit(1)
		}
		podmanTest = PodmanCreate(tempdir)
		podmanTest.RestoreAllArtifacts()
	})

	AfterEach(func() {
		podmanTest.Cleanup()
		f := CurrentGinkgoTestDescription()
		timedResult := fmt.Sprintf("Test: %s completed in %f seconds", f.TestText, f.Duration.Seconds())
		GinkgoWriter.Write([]byte(timedResult))
	})

	It("podman rootless help|version", func() {
		commands := []string{"help", "version"}
		for _, v := range commands {
			env := os.Environ()
			cmd := podmanTest.PodmanAsUser([]string{v}, 1000, 1000, env)
			cmd.WaitWithDefaultTimeout()
			Expect(cmd.ExitCode()).To(Equal(0))
		}
	})

	runRootless := func(args []string) {
		// Check if we can create an user namespace
		err := exec.Command("unshare", "-r", "echo", "hello").Run()
		if err != nil {
			Skip("User namespaces not supported.")
		}

		setup := podmanTest.Podman([]string{"create", ALPINE, "ls"})
		setup.WaitWithDefaultTimeout()
		Expect(setup.ExitCode()).To(Equal(0))
		cid := setup.OutputToString()

		mount := podmanTest.Podman([]string{"mount", cid})
		mount.WaitWithDefaultTimeout()
		Expect(mount.ExitCode()).To(Equal(0))
		mountPath := mount.OutputToString()

		chownFunc := func(p string, info os.FileInfo, err error) error {
			if err != nil {
				return err
			}
			return os.Lchown(p, 1000, 1000)
		}

		err = filepath.Walk(tempdir, chownFunc)
		if err != nil {
			fmt.Printf("cannot chown the directory: %q\n", err)
			os.Exit(1)
		}

		runRootless := func(mountPath string) {
			tempdir, err := CreateTempDirInTempDir()
			Expect(err).To(BeNil())
			podmanTest := PodmanCreate(tempdir)
			err = filepath.Walk(tempdir, chownFunc)
			Expect(err).To(BeNil())

			xdgRuntimeDir, err := ioutil.TempDir("/run", "")
			Expect(err).To(BeNil())
			defer os.RemoveAll(xdgRuntimeDir)
			err = filepath.Walk(xdgRuntimeDir, chownFunc)
			Expect(err).To(BeNil())

			home, err := CreateTempDirInTempDir()
			Expect(err).To(BeNil())
			err = filepath.Walk(xdgRuntimeDir, chownFunc)
			Expect(err).To(BeNil())

			env := os.Environ()
			env = append(env, fmt.Sprintf("XDG_RUNTIME_DIR=%s", xdgRuntimeDir))
			env = append(env, fmt.Sprintf("HOME=%s", home))
			env = append(env, "PODMAN_ALLOW_SINGLE_ID_MAPPING_IN_USERNS=1")
			allArgs := append([]string{"run"}, args...)
			allArgs = append(allArgs, "--rootfs", mountPath, "echo", "hello")
			cmd := podmanTest.PodmanAsUser(allArgs, 1000, 1000, env)
			cmd.WaitWithDefaultTimeout()
			Expect(cmd.ExitCode()).To(Equal(0))
			Expect(cmd.LineInOutputContains("hello")).To(BeTrue())

			cmd = podmanTest.PodmanAsUser([]string{"rm", "-l", "-f"}, 1000, 1000, env)
			cmd.WaitWithDefaultTimeout()
			Expect(cmd.ExitCode()).To(Equal(0))

			allArgs = append([]string{"run", "-d"}, args...)
			allArgs = append(allArgs, "--security-opt", "seccomp=unconfined", "--rootfs", mountPath, "unshare", "-r", "unshare", "-r", "top")
			cmd = podmanTest.PodmanAsUser(allArgs, 1000, 1000, env)
			cmd.WaitWithDefaultTimeout()
			Expect(cmd.ExitCode()).To(Equal(0))

			cmd = podmanTest.PodmanAsUser([]string{"stop", "-l", "-t", "0"}, 1000, 1000, env)
			cmd.WaitWithDefaultTimeout()
			Expect(cmd.ExitCode()).To(Equal(0))

			cmd = podmanTest.PodmanAsUser([]string{"start", "-l"}, 1000, 1000, env)
			cmd.WaitWithDefaultTimeout()
			Expect(cmd.ExitCode()).To(Equal(0))

			if !canExec() {
				Skip("ioctl(NS_GET_PARENT) not supported.")
			}

			cmd = podmanTest.PodmanAsUser([]string{"exec", "-l", "echo", "hello"}, 1000, 1000, env)
			cmd.WaitWithDefaultTimeout()
			Expect(cmd.ExitCode()).To(Equal(0))
			Expect(cmd.LineInOutputContains("hello")).To(BeTrue())
		}

		runRootless(mountPath)

		umount := podmanTest.Podman([]string{"umount", cid})
		umount.WaitWithDefaultTimeout()
		Expect(umount.ExitCode()).To(Equal(0))
	}

	It("podman rootless rootfs", func() {
		runRootless([]string{})
	})

	It("podman rootless rootfs --net host", func() {
		runRootless([]string{"--net", "host"})
	})

	It("podman rootless rootfs --privileged", func() {
		runRootless([]string{"--privileged"})
	})

	It("podman rootless rootfs --net host --privileged", func() {
		runRootless([]string{"--net", "host", "--privileged"})
	})

	It("podman rootless rootfs --uts host", func() {
		runRootless([]string{"--uts", "host"})
	})

	It("podman rootless rootfs --ipc host", func() {
		runRootless([]string{"--ipc", "host"})
	})
})