User/Timbeale/Replace cwrap with namespaces
As an experiment, I tried to remove selftest's reliance on cwrap and use linux namespaces instead. Mostly I played around with network namespaces, which removes the need for socket-wrapper.
What are namespaces?
Namespaces allow the kernel to segregate its system resources (files, CPU, etc), so different processes only see the set of resources they are allowed to use. There are several different types of namespace: network, user, process, file, IPC, etc.
Some key points are:
- Each type of namespace gets managed separately by the kernel, i.e. process namespaces are managed separately to network namespaces, which are separate to user namespaces. This prototyping gave each testenv its own network namespace, but otherwise they all still share the same user/process/etc namespace. (In future, we may want to give each testenv its own process and user namespace, to better mimic a production DC, but this was beyond the scope of what I had time to do).
- Namespaces are created using the 'unshare' utility. The new selftest namespaces are anonymous/nameless, and so the different namespaces are identified by the PID of the processes running within the namespace (typically samba).
- Linux supports nesting namespaces within namespaces. In this case, each testenv DC has its own network namespace, which is a child of the overarching selftest namespace (which itself is a child of whatever namespace you run 'make test' from - usually this would be the root namespace).
Why would we want to replace cwrap?
The main benefits are:
- We can do real testing of DNS, which has historically been hard to write decent automated tests for. There may be some overlap between this namespace work and dns_hub, which were both done in parallel.
- We can do more realistic testing of the samba codebase. E.g. we noticed that the LMDB backend was 20% slower when run inside a testenv compared to when run outside. The difference was purely due to socket-wrapper.
- It allows developers to do more realistic testing - we're no longer limited to what we can do inside a testenv. They can easily connect to the selftest DC from outside of the testenv, which allows you to potentially do things like join a Windows DC to your testenv DC, or point the Windows RSAT GUI at the testenv DC.
- It allows you to do quite powerful things with the Customdc testenv, as the testenv essentially becomes like a VM that's ridiculously easy to spin up.
- Initially we though that using user namespaces would allow better testing of UIDs and root vs non-root permissions. This may be the case, but it ended up falling outside of what I had time to prototype.
- Also Cwrap is getting pretty old, and was added long before namespaces even existed. We though it'd be interesting to see what we could do with more modern kernel containerization.
Can just ditch cwrap?
Cwrap will still be needed for the foreseeable future. There are a few limitations that mean we can't just drop cwrap completely:
- We can't use namespaces on older ubuntu 14.04 releases (i.e. sn-devel).
- It still needs more work to use namespaces on gitlab. Selftest needs to call unshare, and this syscall is not permitted by docker's default seccomp. You can get around this locally by using --privileged, but this is probably not something we want to do as part of CI. One way forward may be to use a custom seccomp profile that whitelists the unshare/clone syscalls.
- Other systems like FreeBSD won't support it.
- Currently we've only prototyped removal of socket-wrapper and resolv-wrapper. Cwrap is still used for a bunch more things.
- Removing just the socket-wrapper highlighted a bunch of tests where we've relied on the socket-wrapper behaviour (either explicitly or implicitly), and so the tests won't run correctly when socket-wrapper is removed. Some testenvs can pass all their test-cases successfully (e.g. restoredc), but other tests will need to be fixed and made more generic before we can run an entire autobuild using namespaces.
So what was the point of this work?
We wanted to see if it was feasible to replace cwrap. The answer is yes, but not easily and not quickly.
The current plan is to tidy up what we've done, integrate it with master (so that it's disabled by default), and gradually chip away at extending it, i.e. a similar approach to what we took with the python3 work.
We'll get some benefit from having the option of running tests differenty. And it'll be insurance in case we really do need to drop cwrap in the future.
Further down the track, once we've addressed the above issues, we could decide to switchover so that namespaces are the CI default and cwrap is the fallback.
So why didn't we just use docker?
The Gitlab CI runs selftest inside a docker container. Running docker inside another docker container isn't really a practical solution. We want to keep using gitlab CI, therefore the testenvs themselves cannot use docker directly.
Docker is essentially just a convenient wrapper for the underlying kernel namespaces, which is doing the bulk of the containerization work anyway.