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
|
# The docker image
Kcov has a docker image: [kcov/kcov](https://hub.docker.com/r/kcov/kcov/) and tags since v31.
There is two variants (tags):
- `latest` or `vXX` that are Debian based
- `latest-alpine` or `vXX-alpine` that are Alpine based
## Permissions
kcov needs access the following system calls:
- [`ptrace`](https://linux.die.net/man/2/ptrace)
- [`process_vm_readv`](https://linux.die.net/man/2/process_vm_readv)/[`process_vm_writev`](https://linux.die.net/man/2/process_vm_writev)
- [`personality`](https://linux.die.net/man/2/personality)
You may need to use `--security-opt seccomp=unconfined` as a docker run option to attach to processes.
## Copy into your image
You may want to copy kcov into your image to avoid building it.
```Dockerfile
# Copy kcov (use kcov/kcov:latest-alpine for Alpine based images)
COPY --from=kcov/kcov:latest /usr/local/bin/kcov* /usr/local/bin/
# If you need documentation
COPY --from=kcov/kcov:latest /usr/local/share/doc/kcov /usr/local/share/doc/kcov
```
### Python + bats example
```Dockerfile
# The base layer of your image
FROM python:3.11-slim-bookworm
# Install kcov run-time dependencies and bats.
RUN apt-get update && \
apt-get install --yes --no-install-suggests --no-install-recommends \
libbfd-dev \
libcurl4 \
libdw1 \
zlib1g \
bats \
&& \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
# Copy kcov (use kcov/kcov:latest-alpine for Alpine based images)
COPY --from=kcov/kcov:latest /usr/local/bin/kcov* /usr/local/bin/
COPY --from=kcov/kcov:latest /usr/local/share/doc/kcov /usr/local/share/doc/kcov
WORKDIR /code
CMD ["bats"]
```
#### Test it
##### `test.sh`
```sh
#!/usr/bin/env bats
source script.sh
@test "test outputNumber function" {
result="$( outputNumber )"
[ "$result" -eq 7 ]
}
@test "kcov version" {
kcov --version | grep -q -c -F "v"
[ "$?" -eq 0 ]
}
```
##### `script.sh`
```sh
outputNumber() {
echo 7
}
```
##### Run tests
```sh
# build our image
docker build ./ -t py-bats
# test the built image
docker run --rm -it -v $PWD:/code py-bats kcov --include-path=/code --dump-summary ./coverage bats ./test.sh
# See the coverage result in ./coverage
# Browse the file coverage/index.html in your browser
```
|