| 1 |
# Integration Tests of Cockpit |
| 2 |
|
| 3 |
This directory contains automated integration tests for Cockpit, and the |
| 4 |
support files for them. The architecture of the automated integration tests is |
| 5 |
described in [ARCHITECTURE](./ARCHITECTURE.md) |
| 6 |
|
| 7 |
To run the tests on Fedora, refer to the [HACKING](../HACKING.md) guide for |
| 8 |
installation of all of the necessary build and test dependencies. There's |
| 9 |
no need to trigger a build manually - the test suite preparation step below |
| 10 |
will handle that. |
| 11 |
|
| 12 |
If test failures are encountered that look like they may be related to problems |
| 13 |
with nested virtualization, refer to |
| 14 |
[this Fedora guide](https://docs.fedoraproject.org/en-US/quick-docs/using-nested-virtualization-in-kvm/index.html) |
| 15 |
for more details and recommendations on ensuring it is enabled correctly. |
| 16 |
|
| 17 |
## Preparation and general invocation |
| 18 |
|
| 19 |
*Warning*: Never run the build, test, or any other command here as root! |
| 20 |
|
| 21 |
You first need to build cockpit, and install it into a VM: |
| 22 |
|
| 23 |
test/image-prepare |
| 24 |
|
| 25 |
This uses the default OS image. See `$TEST_OS` below how to select a different one. |
| 26 |
|
| 27 |
See `test/image-prepare --help` for some special modes, like skipping unit |
| 28 |
tests, building overlays, or preparing an image with the |
| 29 |
[cockpit/ws container](https://quay.io/repository/cockpit/ws) instead of RPMs. |
| 30 |
See our scenarios in [test/run](./run) for how they are being used. |
| 31 |
|
| 32 |
In most cases you want to run an individual test in a suite, for example: |
| 33 |
|
| 34 |
test/verify/check-metrics TestCurrentMetrics.testCPU |
| 35 |
|
| 36 |
You can get a list of tests by inspecting the `def test*` in the source, or by |
| 37 |
running the suite with `-l`/`--list`: |
| 38 |
|
| 39 |
test/verify/check-metrics -l |
| 40 |
|
| 41 |
Sometimes you may also want to run all tests in a test file suite: |
| 42 |
|
| 43 |
test/verify/check-session |
| 44 |
|
| 45 |
To see more verbose output from the test, use the `-v`/`--verbose` and/or `-t`/`--trace` flags: |
| 46 |
|
| 47 |
test/verify/check-session --verbose --trace |
| 48 |
|
| 49 |
If you specify `-s`/`--sit` in addition, then the test will wait on failure and |
| 50 |
allow you to log into cockpit and/or the test instance and diagnose the issue. |
| 51 |
The cockpit and SSH addresses of the test instance will be printed: |
| 52 |
|
| 53 |
test/verify/check-session -st |
| 54 |
|
| 55 |
You can also run *all* the tests, with automatic parallelism: |
| 56 |
|
| 57 |
test/common/run-tests --test-dir test/verify |
| 58 |
|
| 59 |
However, this will take *really* long. You can specify a subset of tests (see |
| 60 |
`--help`); but usually it's better to run individual tests locally, and let the |
| 61 |
CI machinery run all of them in a draft pull request. |
| 62 |
|
| 63 |
The tests will automatically download the VM images they need, so expect |
| 64 |
that the initial run may take a few minutes. |
| 65 |
|
| 66 |
## Interactive browser |
| 67 |
|
| 68 |
Normally each test starts its own chromium headless browser process on a |
| 69 |
separate random port. To interactively follow what a test is doing: |
| 70 |
|
| 71 |
TEST_SHOW_BROWSER=1 test/verify/check-session --trace |
| 72 |
|
| 73 |
You can also run a test against Firefox instead of Chromium: |
| 74 |
|
| 75 |
TEST_BROWSER=firefox test/verify/check-session --trace |
| 76 |
|
| 77 |
See below for details. |
| 78 |
|
| 79 |
## Manual testing |
| 80 |
|
| 81 |
You can conduct manual interactive testing against a test image by starting the |
| 82 |
image like so: |
| 83 |
|
| 84 |
bots/vm-run -s cockpit.socket arch |
| 85 |
|
| 86 |
Once the machine is booted and the cockpit socket has been activated, a |
| 87 |
message will be printed describing how to access the virtual machine, via |
| 88 |
ssh and web. See the "Helpful tips" section below. |
| 89 |
|
| 90 |
To login using the web client you have to use the username and password listed below. If you wish to enable `root` login using the web you can remove `root` from `/etc/cockpit/disallowed-users`. |
| 91 |
|
| 92 |
- Username: `admin` |
| 93 |
- Password: `foobar` |
| 94 |
|
| 95 |
By default, it's only possible to contact the virtual machine from the host |
| 96 |
machine on which it's running. If you want to conduct manual testing from |
| 97 |
other devices on your network, set `TEST_BIND_GLOBAL=1`, for example: |
| 98 |
|
| 99 |
TEST_BIND_GLOBAL=1 bots/vm-run -s cockpit.socket arch |
| 100 |
|
| 101 |
This will bind the Cockpit and SSH ports to all interfaces, making it possible |
| 102 |
to access a URL like http://yourhost.local:9091/ to test Cockpit from another |
| 103 |
machine on your LAN. |
| 104 |
|
| 105 |
## Pixel tests |
| 106 |
|
| 107 |
Pixel tests in Cockpit ensure that updates of our dependencies or code changes |
| 108 |
don't break the UI: for example slight changes of layout, padding, color and |
| 109 |
everything which isn't easily spotted by a human. They also give us confidence |
| 110 |
that an update of our UI Framework doesn't introduce changes in how Cockpit |
| 111 |
looks. |
| 112 |
|
| 113 |
Pixel tests make a screenshot of a selector and compare it to a known good |
| 114 |
reference image. if there is a difference, the test fails and a pixel |
| 115 |
difference is shown. |
| 116 |
|
| 117 |
This works as our tests run in the [cockpit/tasks container](https://ghcr.io/cockpit-project/tasks) |
| 118 |
which pins the browser and font rendering so repeated runs provide the same |
| 119 |
pixels. To generate new pixels, this tasks container must be used; your own |
| 120 |
browser and font rendering software might generate different results. For more |
| 121 |
information read the ["introduction blog post"](https://cockpit-project.org/blog/pixel-testing.html). |
| 122 |
|
| 123 |
The test images are stored in a git submodule in the `test/reference` directory |
| 124 |
and be fetched with: |
| 125 |
|
| 126 |
```sh |
| 127 |
./test/common/pixel-tests update |
| 128 |
``` |
| 129 |
|
| 130 |
As Cockpit tests under multiple distributions and it is not worth the effort to |
| 131 |
run pixel tests on every supported distribution we only run them for the |
| 132 |
image configured in `test/reference-image`. |
| 133 |
|
| 134 |
Our tests call `Browser.assert_pixels` at interesting and strategic places. |
| 135 |
This assertion method requires at least a CSS selector and an image title. |
| 136 |
Pixel tests are generated in five layouts by default: desktop, medium, mobile, |
| 137 |
dark and rtl. |
| 138 |
|
| 139 |
Take a screenshot of the content in `#detail-content`: |
| 140 |
```python |
| 141 |
browser.assert_pixels("#detail-content", "filesystem") |
| 142 |
``` |
| 143 |
|
| 144 |
Take a screenshot of the content in `#detail-content` and ignore all elements |
| 145 |
with a class `disk-stats` as they change per test run: |
| 146 |
```python |
| 147 |
browser.assert_pixels("#detail-content", "filesystem", ignore=[".disks-stats"]) |
| 148 |
``` |
| 149 |
|
| 150 |
Take a screenshot of the content in `#detail-content` and skip it for a |
| 151 |
specific layout as it generates unstable pixels: |
| 152 |
```python |
| 153 |
browser.assert_pixels("#detail-content", "filesystem", skip_layouts=["rtl"]) |
| 154 |
``` |
| 155 |
|
| 156 |
To update pixel tests, locally run the test in the current tasks container, or |
| 157 |
create a draft PR and let the tests run for `test/reference-image` and |
| 158 |
afterwards fetch the new pixels: |
| 159 |
|
| 160 |
``` |
| 161 |
./test/common/pixel-tests fetch "https://cockpit-logs.us-east-1.linodeobjects.com/<snip>/log.html" |
| 162 |
``` |
| 163 |
|
| 164 |
Finally, upload the new pixel tests and commit the newly generated submodule commit: |
| 165 |
``` |
| 166 |
./test/common/pixel-tests push |
| 167 |
``` |
| 168 |
|
| 169 |
**Note** that you have to a part of the [Contributors group](https://github.com/orgs/cockpit-project/teams/contributors) |
| 170 |
to push pixel tests. |
| 171 |
|
| 172 |
## Test Configuration |
| 173 |
|
| 174 |
You can set these environment variables to configure the test suite: |
| 175 |
|
| 176 |
* `TEST_OS`: The OS to run the tests in, like "fedora-coreos" or |
| 177 |
"debian-testing". See the "cockpit-project/cockpit" section in the |
| 178 |
[test map](https://github.com/cockpit-project/bots/blob/main/lib/testmap.py) |
| 179 |
for all supported values. "fedora-44" is the default (`TEST_OS_DEFAULT` in |
| 180 |
bots' [constants.py](https://github.com/cockpit-project/bots/blob/main/lib/constants.py)). |
| 181 |
|
| 182 |
* `TEST_BROWSER`: What browser should be used for testing. Currently supported |
| 183 |
values are "chromium" and "firefox". "chromium" is the default. |
| 184 |
|
| 185 |
* `TEST_SHOW_BROWSER`: Set to run browser interactively. When not specified, |
| 186 |
browser is run in headless mode. When set to "pixels", the browser will be |
| 187 |
resized to the exact dimensions that are used for pixel tests. |
| 188 |
|
| 189 |
* `TEST_TIMEOUT_FACTOR`: Scale normal timeouts by given float value. Useful for |
| 190 |
slow/busy testbeds or architectures. |
| 191 |
|
| 192 |
See the [bots documentation](https://github.com/cockpit-project/bots/blob/main/README.md) |
| 193 |
for details about the tools and configuration for these. |
| 194 |
|
| 195 |
## Convenient test VM SSH access |
| 196 |
|
| 197 |
It is recommended to add a snippet like this to your `~/.ssh/config`. Then |
| 198 |
you can log in to test machines without authentication: |
| 199 |
|
| 200 |
Match final host 127.0.0.2 |
| 201 |
User root |
| 202 |
StrictHostKeyChecking no |
| 203 |
UserKnownHostsFile /dev/null |
| 204 |
CheckHostIp no |
| 205 |
IdentityFile CHECKOUT_DIR/bots/machine/identity |
| 206 |
IdentitiesOnly yes |
| 207 |
|
| 208 |
You need to replace `CHECKOUT_DIR` with the actual directory where you cloned |
| 209 |
`cockpit.git`, or `bots.git` if you have a separate clone for that. |
| 210 |
|
| 211 |
Many cockpit developers take it a step further, and add an alias to |
| 212 |
allow typing `ssh c`: |
| 213 |
|
| 214 |
Host c |
| 215 |
Hostname 127.0.0.2 |
| 216 |
Port 2201 |
| 217 |
|
| 218 |
The `final` keyword in the first rule will cause it to be checked (and matched) |
| 219 |
after the `Hostname` substitution in the `c` rule. |
| 220 |
|
| 221 |
## Fast develop/test iteration |
| 222 |
|
| 223 |
Each `image-prepare` invocation will always start from the pristine image and |
| 224 |
ignore the current overlay in `test/images`. It is thorough, but also rather |
| 225 |
slow. If you want to iterate on changing only JavaScript/HTML code, as opposed |
| 226 |
to the bridge or webserver, the whole build and test cycle can be done much |
| 227 |
faster. |
| 228 |
|
| 229 |
You always need to do at least one initial `test/image-prepare $TEST_OS` run. |
| 230 |
Afterwards it depends on the kind of test you want to run. |
| 231 |
|
| 232 |
### Nondestructive tests |
| 233 |
|
| 234 |
Many test methods or classes are marked as `@nondestructive`, meaning that |
| 235 |
they restore the state of the test VM enough that other tests can run |
| 236 |
afterwards. This is the fastest and most convenient situation for both |
| 237 |
iterating on the code and debugging failing tests. |
| 238 |
|
| 239 |
Start the prepared VM with `bots/vm-run $TEST_OS`. Note the SSH and cockpit |
| 240 |
ports. If this is the only running VM, it will have the ports in the |
| 241 |
examples below, otherwise the port will be different. |
| 242 |
|
| 243 |
Then start building the page you are working on |
| 244 |
[in watch and rsync mode](../HACKING.md#working-on-cockpits-session-pages), e.g. |
| 245 |
|
| 246 |
RSYNC=c ./build.js -w users |
| 247 |
|
| 248 |
(Assuming the `c` SSH alias from the previous section and first running VM). |
| 249 |
|
| 250 |
Then you can run a corresponding test against the running VM, with additional |
| 251 |
debug output: |
| 252 |
|
| 253 |
TEST_OS=... test/verify/check-users -t --machine 127.0.0.2:2201 --browser 127.0.0.2:9091 TestAccounts.testBasic |
| 254 |
|
| 255 |
### Destructive tests |
| 256 |
|
| 257 |
Other tests need one or more fresh VMs. Instead of a full `test/image-prepare` |
| 258 |
run (which is slow), you can update the existing VM overlay with updated |
| 259 |
bundles. Start the build in watch mode, but without rsyncing, e.g. |
| 260 |
|
| 261 |
./build.js -w storaged |
| 262 |
|
| 263 |
and after each iteration, copy the new bundles into the VM overlay: |
| 264 |
|
| 265 |
bots/image-customize -u dist:/usr/share/cockpit/ $TEST_OS |
| 266 |
|
| 267 |
Then run the test as you would normally do, e.g. |
| 268 |
|
| 269 |
TEST_OS=... test/verify/check-storage-stratis -t TestStorageStratis.testBasic |
| 270 |
|
| 271 |
Use `bots/vm-reset` to clean up all prepared overlays in `test/images`. |
| 272 |
|
| 273 |
## Debugging tests |
| 274 |
|
| 275 |
If you pass the `-s` ("sit on failure") option to a test program, it |
| 276 |
will pause when a failure occurs so that you can log into the test |
| 277 |
machine and investigate the problem. |
| 278 |
|
| 279 |
A test will print out the commands to access it when it fails in this |
| 280 |
way. You can log into a running test-machine using ssh. See the |
| 281 |
"Helpful tips" section below. |
| 282 |
|
| 283 |
You can also put calls to `sit()` into the tests themselves to stop them |
| 284 |
at strategic places. |
| 285 |
|
| 286 |
That way, you can run a test cleanly while still being able to make |
| 287 |
quick changes, such as adding debugging output to JavaScript. |
| 288 |
|
| 289 |
## Guidelines for writing tests |
| 290 |
|
| 291 |
If a test is not decorated with `@nondestructive`, it is OK for a test to |
| 292 |
destroy the test machine OS installation, or otherwise modify it without |
| 293 |
cleaning up. For example, it is OK to remove all of `/etc` just to see what |
| 294 |
happens. The next test will get a pristine test machine. |
| 295 |
|
| 296 |
Tests decorated with `@nondestructive` will all run against the same test |
| 297 |
machine. The nondestructive test should clean up after itself and restore the |
| 298 |
state of the machine, such that the next nondestructive test is not impacted. |
| 299 |
|
| 300 |
A fast running test suite is more important than independent, |
| 301 |
small test cases. |
| 302 |
|
| 303 |
Thus, it is OK for tests to be long. Starting the test machine is so slow that |
| 304 |
we should run as many checks within a single session as make sense. Note that |
| 305 |
nondestructive tests do not suffer from this, and are much quicker. |
| 306 |
|
| 307 |
Still, within a long test, try to have independent sections, where |
| 308 |
each section returns the machine to more or less the state that it was |
| 309 |
in before the section. This makes it easier to run these sections |
| 310 |
ad-hoc when doing incremental development. |
| 311 |
|
| 312 |
## Coverage |
| 313 |
|
| 314 |
Every pull request will trigger a `$DEFAULT_OS/devel` scenario which creates a |
| 315 |
coverage report of the JavaScript code executed and writes comments about |
| 316 |
uncovered code in the pull request. The overall coverage percentage is recorded |
| 317 |
in prometheus for a subset of our projects and [visualized in Grafana](https://grafana-cockpit.apps.ocp.cloud.ci.centos.org/d/ci/cockpit-ci?orgId=1). |
| 318 |
|
| 319 |
To generate coverage locally for `TestApps`: |
| 320 |
|
| 321 |
``` |
| 322 |
export NODE_ENV=devel |
| 323 |
./build.js |
| 324 |
./test/image-prepare -q |
| 325 |
./test/common/run-tests --test-dir test/verify --coverage TestApps |
| 326 |
``` |
| 327 |
|
| 328 |
Code which is impossible or very hard to test in our tests can be excluded from |
| 329 |
appearing in a pull request as comment by adding a `not-covered` comment with a |
| 330 |
short justification: |
| 331 |
|
| 332 |
```javascript |
| 333 |
return cockpit.script(data, { superuser: "try", err: "message" }) |
| 334 |
.catch(console.error); // not-covered: OS error |
| 335 |
``` |
| 336 |
|
| 337 |
## Helpful tips |
| 338 |
|
| 339 |
For web access, if you'd like to avoid Chromium (or Chrome) prompting |
| 340 |
about certificate errors while connecting to localhost, you can change |
| 341 |
the following setting: |
| 342 |
|
| 343 |
chrome://flags/#allow-insecure-localhost |