| 1 |
# Architecture |
| 2 |
|
| 3 |
This document describes the architecture of Cockpit's browser integration |
| 4 |
tests. The tests should automate how a normal user interacts with Cockpit. |
| 5 |
This requires a test machine which can add multiple disks or interfaces, |
| 6 |
reboot, interact with multiple machines on the same network and run potentially |
| 7 |
"destructive" test scenarios (e.g. installing/updating packages, formatting |
| 8 |
disks). |
| 9 |
|
| 10 |
For these reasons, Cockpit tests run inside a virtual machine (VM). The virtual |
| 11 |
machine uses Cockpit specific virtual machine images maintained and created in |
| 12 |
the [bots](https://github.com/cockpit-project/bots) repository. The images are |
| 13 |
usually based on a distribution's cloud image customized with: |
| 14 |
|
| 15 |
* A well-known password and SSH key for the admin and root users |
| 16 |
* Packages required to test Cockpit |
| 17 |
* A build chroot with Cockpit's build dependencies to build the to be tested |
| 18 |
Cockpit source inside the virtual machine offline. This allows a developer on |
| 19 |
Fedora to easily prepare a Debian test image without having to install Debian |
| 20 |
build tools. |
| 21 |
* Disabling system services which interfere with testing |
| 22 |
|
| 23 |
To automate user actions on the web page, Cockpit is tested in a browser |
| 24 |
controlled using the [Webdriver BiDi](https://w3c.github.io/webdriver-bidi/) |
| 25 |
protocol which is supported by all modern browsers. |
| 26 |
|
| 27 |
The Python test framework in [test/common](./common) is responsible for setting up the |
| 28 |
test environment, running tests and reporting of the test output. |
| 29 |
|
| 30 |
Diagram of the interaction of Browser/Machine/Webdriver/Test Framework. |
| 31 |
```mermaid |
| 32 |
graph TD; |
| 33 |
id[Test Framework] <-->|Webdriver| Browser; |
| 34 |
Browser <-->|HTTPS/WebSocket| Cockpit; |
| 35 |
id1[Virtual Machine] <-->|HTTP/DBus/OS APIs| Cockpit; |
| 36 |
id[Test Framework] <-->|SSH| id1[Virtual Machine]; |
| 37 |
``` |
| 38 |
|
| 39 |
## Integration Tests |
| 40 |
|
| 41 |
Cockpit's tests can be run via three different entry points: |
| 42 |
|
| 43 |
* `test/verify/check-$page` - run a single or multiple test(s) |
| 44 |
* `test/common/run-tests` - run tests through our test scheduler (retries, tracks naughties, parallelism) |
| 45 |
* `test/run` or `.cockpit-ci/run` - CI entry point |
| 46 |
|
| 47 |
We will start with how a single integration test is run and then explore the test |
| 48 |
scheduler and CI setup. |
| 49 |
|
| 50 |
### Test runtime |
| 51 |
|
| 52 |
The base of a Cockpit integration test looks as following: |
| 53 |
|
| 54 |
```python |
| 55 |
class TestSystem(testlib.MachineCase): |
| 56 |
def testBasic(self): |
| 57 |
self.login_and_go("/system") |
| 58 |
self.machine.execute("echo 'test' > /etc/motd") |
| 59 |
self.browser.wait_text("#motd", 'test') |
| 60 |
|
| 61 |
if __name__ == '__main__': |
| 62 |
testlib.test_main() |
| 63 |
``` |
| 64 |
|
| 65 |
In Cockpit there are two types of tests, "destructive" and "non-destructive" |
| 66 |
tests. Destructive tests do something to the virtual machine which makes it |
| 67 |
unable to run another test afterwards or requires another virtual machine for |
| 68 |
testing. The test above is a "destructive" test which is the default, a non |
| 69 |
"destructive" test makes sure any destructive action is restored after the test |
| 70 |
has run as can be seen below. "Non-destructive" tests were introduced to be able |
| 71 |
to run them on a single running machine, such as the Testing Farm or a custom VM. |
| 72 |
They also speed up testing, as booting a machine for every tests incurs a |
| 73 |
significant penalty of ~ 10-30 seconds per test. |
| 74 |
|
| 75 |
```python |
| 76 |
@testlib.nondestructive |
| 77 |
class TestSystem(testlib.MachineCase): |
| 78 |
def testBasic(self): |
| 79 |
self.login_and_go("/system") |
| 80 |
self.write_file("/etc/motd", "test") |
| 81 |
self.browser.wait_text("#motd", 'test') |
| 82 |
|
| 83 |
if __name__ == '__main__': |
| 84 |
testlib.test_main() |
| 85 |
``` |
| 86 |
|
| 87 |
The test above would be invoked via `./test/verify/check-apps TestSystem.testBasic` |
| 88 |
and would execute as can be seen in the diagram below: |
| 89 |
```mermaid |
| 90 |
sequenceDiagram |
| 91 |
participant test |
| 92 |
participant machine |
| 93 |
participant browser |
| 94 |
|
| 95 |
test->>test: test_main() |
| 96 |
test->>test: setUp() |
| 97 |
test->>machine: boot() |
| 98 |
test->>browser: __init__() |
| 99 |
test->>test: setup non-destructive setup |
| 100 |
test->>test: run test |
| 101 |
test->>browser: start() |
| 102 |
test->>test: tearDown() |
| 103 |
test->>browser: kill() |
| 104 |
test->>machine: kill() |
| 105 |
``` |
| 106 |
|
| 107 |
A test starts by calling `test_main`, this provides common command line |
| 108 |
arguments for debugging and to optionally run a test on a different |
| 109 |
machine/browser. These arguments are available in the `MachineCase` class as |
| 110 |
`opts`. `test_main` also takes care of instantiating a `TapRunner` which runs |
| 111 |
all the specified tests sequentially. |
| 112 |
|
| 113 |
Once a test is started it runs `MachineCase.setUp` which has the responsibility |
| 114 |
to start a virtual machine(s) depending on if it is a "non-destructive" or |
| 115 |
"destructive" test. If we run a "non-destructive" test a global machine is |
| 116 |
created, and re-used for other "non-destructive" tests which might run. For |
| 117 |
"destructive" tests a machine is created on-demand, possible multiple machines |
| 118 |
depending on the test class `provision` variable. |
| 119 |
|
| 120 |
For "non-destructive" tests cleanup handlers are installed to restore files in |
| 121 |
`/etc`, cleans up home directories etc. |
| 122 |
|
| 123 |
Lastly a `Browser` class is instantiated, this does not start the Browser |
| 124 |
directly but builds the required command for the `TEST_BROWSER` to start |
| 125 |
either Chromium or Firefox. When a test calls any method on the browser object |
| 126 |
the browser will be started, so tests which require no browser don't start a |
| 127 |
browser needlessly. |
| 128 |
|
| 129 |
The `WebdriverBidi` class is responsible for spawning the browser and talking |
| 130 |
to it using the [Webdriver BiDi](https://w3c.github.io/webdriver-bidi/) |
| 131 |
protocol (which we implement directly for ourselves, using aiohttp). |
| 132 |
|
| 133 |
On `tearDown` the test status is inspected, if it failed test logs are |
| 134 |
collected and if the user has passed `--sit` the test pauses execution until |
| 135 |
the user presses enter so that the machine/browser state can be inspected. The |
| 136 |
test browser is killed after the `tearDown` function completed. |
| 137 |
|
| 138 |
Virtual machines are killed by the `TapRunner` once all tests have finished or |
| 139 |
in `setUp` if it's a "destructive" test as "non-destructive" tests re-use the |
| 140 |
existing global machine. |
| 141 |
|
| 142 |
### Test runner |
| 143 |
|
| 144 |
Cockpit uses a custom test runner to run the tests, spread the load across |
| 145 |
parallel instances and special handling of test failures. The test runner is |
| 146 |
implemented in Python in [test/common/run-tests](./common/run-tests) and expects |
| 147 |
a list of tests to be provided. |
| 148 |
|
| 149 |
The provided tests are collected and split up in "destructive" and |
| 150 |
"non-destructive" tests, every test calculates its RAM requirements based on the |
| 151 |
amount of virtual machines and browser memory needed to run the test. A default |
| 152 |
timeout is added to every test so hanging tests get killed over time, tests |
| 153 |
which take longer can set a custom timeout using a test decorator. |
| 154 |
|
| 155 |
To make sure our tests aren't flaky we retry tests three times when they are |
| 156 |
affected. An affected test is for example `test/verify/check-apps` if |
| 157 |
`pkg/apps` is changed, or if the changeset changes less then four tests they |
| 158 |
will be marked as affected as well. The changed tests are limited so big |
| 159 |
refactors won't retry a lot of tests. |
| 160 |
|
| 161 |
Having collected the "destructive", "non-destructive" and affected tests a |
| 162 |
scheduling loop is started, if a machine was provided it is used for the |
| 163 |
"non-destructive" tests, "destructive" tests will always spawn a new machine. |
| 164 |
If no machine is provided a pool of global machines for "non-destructive" tests |
| 165 |
depending on available CPUs and RAM. The test runner will first try to assign all |
| 166 |
"non-destructive" tests on the available global machines and start the tests. |
| 167 |
|
| 168 |
The scheduling loop periodically inspects all running tests and polls if the |
| 169 |
test has ended. Depending on the exit code of the test process the test runner |
| 170 |
makes a decision on what to do next as described in the diagram below. |
| 171 |
```mermaid |
| 172 |
graph TD; |
| 173 |
finished[Test finished] |
| 174 |
succeeded{Test succeeded?} |
| 175 |
affected{"Test affected?"} |
| 176 |
affecteddone["Retry three times"] |
| 177 |
skipped{Test skipped?} |
| 178 |
todosuccess{Test todo?} |
| 179 |
todonosuccess{Test todo?} |
| 180 |
todosucceeded["Unexpected success |
| 181 |
show as failure"] |
| 182 |
todofail[Expected failure] |
| 183 |
pixel_journal_failure{"Pixel test or |
| 184 |
unexpected journal |
| 185 |
message?"} |
| 186 |
retry["Retry three times |
| 187 |
to be robust against |
| 188 |
test failures"] |
| 189 |
testfailed["Test failed"] |
| 190 |
failure_policy{"Known issue?"} |
| 191 |
|
| 192 |
finished --> skipped |
| 193 |
skipped --> |No| succeeded |
| 194 |
skipped --> |Yes| skiptest[Show as skipped test] |
| 195 |
succeeded --> |No| todonosuccess |
| 196 |
succeeded --> |yes| affected |
| 197 |
|
| 198 |
affected --> |Yes| affecteddone |
| 199 |
affected --> |No| todosuccess |
| 200 |
|
| 201 |
todosuccess --> |Yes| todosucceeded |
| 202 |
todosuccess --> |No| done[Test succeeded] |
| 203 |
|
| 204 |
todonosuccess --> |Yes| todofail |
| 205 |
todonosuccess --> |No| failure_policy |
| 206 |
|
| 207 |
failure_policy --> |Yes| known_issue[Show as known issue] |
| 208 |
failure_policy --> |No| pixel_journal_failure |
| 209 |
|
| 210 |
pixel_journal_failure --> |Yes| testfailed |
| 211 |
pixel_journal_failure --> |No| retry |
| 212 |
``` |
| 213 |
|
| 214 |
* `Skipped` - tests can be skipped because the test can not run on the given `TEST_OS`. |
| 215 |
* `Affected` - tests to be retried to make sure any changes affecting them do not lead to flaky tests. |
| 216 |
* `Todo` - tests which are incomplete and expected to fail. |
| 217 |
* `Known issue` - Naughties are expected test failures due to known issues in software we test, |
| 218 |
we still run the test but if the test error output matches a known naughty it |
| 219 |
is skipped. The [bots](https://github.com/cockpit-project/bots) repository keeps track |
| 220 |
of all our known naughties per distro. (The bots repository has automation |
| 221 |
setup to see if a naughty is still affected and if not open a pull request to |
| 222 |
drop it). |
| 223 |
* `Failed` - tests can fail due to our test shared infrastructure, instead of |
| 224 |
letting the whole test run fail, we re-try them unless `--no-retry-fail` is |
| 225 |
passed. |
| 226 |
|
| 227 |
### Continuous Integration (CI) |
| 228 |
|
| 229 |
In CI we have two entry points, one for our tests which runs on our own |
| 230 |
managed infrastructure by [cockpituous](https://github.com/cockpit-project/cockpituous/) |
| 231 |
and one for tests which run on the [testing farm (TF)](https://docs.testing-farm.io/). |
| 232 |
|
| 233 |
For our own managed infrastructure the entry point of the Cockpit tests is |
| 234 |
`test/run`, or alternatively `.cockpit-ci/run`. This bash script expects a |
| 235 |
`TEST_OS` environment variable to be set to determine what distribution to run |
| 236 |
the tests under, and a `TEST_SCENARIO` environment variable to determine the |
| 237 |
type of test. A list of currently supported scenarios can be found in |
| 238 |
[test/run](./run). |
| 239 |
|
| 240 |
Cockpit's tests are split up in scenarios to heavily parallelize our testing and |
| 241 |
allow for faster retrying. |
| 242 |
|
| 243 |
[test/run](./run) prepares an Virtual machine image for the given `TEST_OS` and then |
| 244 |
runs the tests by calling `test/common/run-tests` with the provided tests. |
| 245 |
|
| 246 |
The [tmt](https://tmt.readthedocs.io) test is defined in [test/browser/main.fmf](./browser/main.fmf). These |
| 247 |
run on the Testing Farm (TF), triggered by [Packit](https://packit.dev/) for upstream PRs. On TF we |
| 248 |
get a single virtual machine without a hypervisor so tests run on the virtual |
| 249 |
machine directly. This also implies that only "non-destructive" tests can be run. |
| 250 |
The `test/browser/browser.sh` script sets up the virtual machine and calls |
| 251 |
`test/browser/run-tests.sh` which selects a subset of all the "non-destructive" |
| 252 |
tests to run using `test/common/run-tests`. |
| 253 |
|
| 254 |
The tmt test is also split up into three plans (basic, networking, optional) to |
| 255 |
run faster in parallel. |