UserNamespace checks that the current test could use user namespace, if user namespaces are not enabled or supported, the current test is skipped with a message.
(t *testing.T)
| 42 | // user namespace, if user namespaces are not enabled or |
| 43 | // supported, the current test is skipped with a message. |
| 44 | func UserNamespace(t *testing.T) { |
| 45 | // not performance critical, just save extra execution |
| 46 | // to get the same result |
| 47 | hasUserNamespaceOnce.Do(func() { |
| 48 | // user namespace is a bit special, as there is no simple |
| 49 | // way to detect if it's supported or enabled via a call |
| 50 | // on /proc/self/ns/user, the easiest and reliable way seems |
| 51 | // to directly execute a command by requesting user namespace |
| 52 | cmd := exec.Command("/bin/true") |
| 53 | cmd.SysProcAttr = &syscall.SysProcAttr{ |
| 54 | Cloneflags: syscall.CLONE_NEWUSER, |
| 55 | } |
| 56 | // no error means user namespaces are enabled |
| 57 | err := cmd.Run() |
| 58 | hasUserNamespace = err == nil |
| 59 | if !hasUserNamespace { |
| 60 | t.Logf("Could not use user namespaces: %s", err) |
| 61 | } |
| 62 | }) |
| 63 | if !hasUserNamespace { |
| 64 | t.Skipf("user namespaces seems not enabled or supported") |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | var ( |
| 69 | supportNetwork bool |
no test coverage detected