Skip to content

Commit 0a1af4c

Browse files
Test Automation Performance Improvements + Streamlining (#489)
* works for amqp * updated makefile to use docker compose * make clean now tears everything down * ipp and http working * ntp and pop * postgres * tests run and python script works * Use python instead of shell test script and fail a test immediately if it's failing * cleanup unused setup/cleanup scripts * build runner and service_base in parallel * make integration tests manually triggerable * remove comment * integration test manual run * fix caps * push an intermediate build image so ipp builds faster * allow user to select a subset of modules to test * Allow user to select only a few test modules * update testing README * remove extraneous scripts * Revert "push an intermediate build image so ipp builds faster" This reverts commit fc20f3b. * comment makefile and remove Python2 instructions in README * handle todo and typo fix * test.py cleanup * docker-compose cleanup * add black to make lint * lint python
1 parent 78d5c30 commit 0a1af4c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+739
-1123
lines changed

.github/workflows/integration-test.yml

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ on:
44
branches:
55
- master
66
pull_request:
7+
workflow_dispatch:
78

89
jobs:
910
go-test:

Dockerfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Build image ##
22
ARG GO_VERSION=1.23
3-
FROM golang:${GO_VERSION}-alpine3.21 as build
3+
FROM golang:${GO_VERSION}-alpine3.21 AS build
44

55
# System dependencies
66
RUN apk add --no-cache make
@@ -16,7 +16,7 @@ COPY . .
1616
RUN make all
1717

1818
## Runtime image ##
19-
FROM alpine:3.21 as run
19+
FROM alpine:3.21 AS run
2020

2121
COPY --from=build /usr/src/zgrab2/cmd/zgrab2/zgrab2 /usr/bin/zgrab2
2222

Makefile

+11-8
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ TEST_MODULES ?=
99

1010
all: zgrab2
1111

12-
.PHONY: all clean integration-test integration-test-clean docker-runner gofmt test
12+
.PHONY: all clean integration-test integration-test-clean gofmt test
1313

1414
# Test currently only runs on the modules folder because some of the
1515
# third-party libraries in lib (e.g. http) are failing.
@@ -19,23 +19,26 @@ test:
1919

2020
lint:
2121
gofmt -s -w $(shell find . -type f -name '*.go'| grep -v "/.template/")
22+
black .
2223

2324
zgrab2: $(GO_FILES)
2425
cd cmd/zgrab2 && go build && cd ../..
2526
rm -f zgrab2
2627
ln -s cmd/zgrab2/zgrab2$(EXECUTABLE_EXTENSION) zgrab2
2728

28-
docker-runner: clean
29-
make -C docker-runner
30-
31-
integration-test: docker-runner
29+
integration-test:
3230
rm -rf zgrab-output
33-
TEST_MODULES=$(TEST_MODULES) ./integration_tests/test.sh
31+
docker compose -p zgrab -f integration_tests/docker-compose.yml build --no-cache service_base # ensure the apt cache is up to date
32+
docker compose -p zgrab -f integration_tests/docker-compose.yml build $(TEST_MODULES)
33+
docker compose -p zgrab -f integration_tests/docker-compose.yml up -d $(TEST_MODULES)
34+
sleep 10 # Wait for services to start
35+
TEST_MODULES="$(TEST_MODULES)" python3 integration_tests/test.py
36+
# Shut off the services
37+
docker compose -p zgrab -f integration_tests/docker-compose.yml down
3438

3539
integration-test-clean:
3640
rm -rf zgrab-output
37-
./integration_tests/cleanup.sh
38-
make -C docker-runner clean
41+
docker compose -p zgrab -f integration_tests/docker-compose.yml down
3942

4043
clean:
4144
cd cmd/zgrab2 && go clean

README.md

+19-8
Original file line numberDiff line numberDiff line change
@@ -168,21 +168,25 @@ To add a schema for the new module, add a module under schemas, and update [`zgr
168168
See [zgrab2_schemas/README.md](zgrab2_schemas/README.md) for details.
169169

170170
### Integration tests
171-
To add integration tests for the new module, run `integration_tests/new.sh [your_new_protocol_name]`.
172-
This will add stub shell scripts in `integration_tests/your_new_protocol_name`; update these as needed.
171+
To add integration tests for the new module, you'll need to add a test service to scan against to `integration_tests/docker-compose.yml` and a `test.sh` in a folder named after your module.
172+
Follow the examples in `integration_tests/.template` to create the necessary files.
173173
See [integration_tests/mysql/*](integration_tests/mysql) for an example.
174174
The only hard requirement is that the `test.sh` script drops its output in `$ZGRAB_OUTPUT/[your-module]/*.json`, so that it can be validated against the schema.
175175

176176
#### How to Run Integration Tests
177177

178-
To run integration tests, you must have [Docker](https://www.docker.com/) and **Python 2** on host installed. Then, you can follow the following steps to run integration tests:
178+
To run integration tests, you must have [Docker](https://www.docker.com/) and **Python 3** on host installed. Then, you can follow the following steps to run integration tests:
179179

180180
```shell
181-
go get github.com/jmespath/jp && go build github.com/jmespath/jp
182-
# or, sudo wget https://github.com/jmespath/jp/releases/download/0.2.1/jp-linux-amd64 -O /usr/local/bin/jp && sudo chmod +x /usr/local/bin/jp
183-
pip2 install --user zschema
184-
pip2 install --user -r requirements.txt
185-
make integration-test
181+
# Install Python dependencies
182+
sudo apt update
183+
sudo apt install -y python3 jp python3-pip
184+
python3 -m venv venv
185+
source venv/bin/activate
186+
# Install Python dependencies
187+
pip install zschema
188+
pip install -r requirements.txt
189+
make integration-test-clean; make integration-test
186190
```
187191

188192
Running the integration tests will generate quite a bit of debug output. To ensure that tests completed successfully, you can check for a successful exit code after the tests complete:
@@ -192,6 +196,13 @@ echo $?
192196
0
193197
```
194198

199+
To just run a single/few module's integration tests, you can use the `TEST_MODULES` env. var.:
200+
201+
```shell
202+
make integration-test-clean; TEST_MODULES="http" make integration-test
203+
make integration-test-clean; TEST_MODULES="http ssh" make integration-test
204+
```
205+
195206
Refer to our [Github Actions workflow](.github/workflows/integration-test.yml) for an example of how to prepare environment for integration tests.
196207

197208
## License

docker-runner/Makefile

-26
This file was deleted.

docker-runner/base-apt-update.sh

-14
This file was deleted.

docker-runner/docker-run.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
: "${CONTAINER_NAME:?}"
88

99
set -e
10-
echo 'target' | docker run --rm -i --link $CONTAINER_NAME:target zgrab2_runner $@
10+
echo 'target' | docker run --rm -i --network container:$CONTAINER_NAME zgrab2_runner $@

integration_tests/.template/README.md

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
1-
Template integration test scripts, used by `new.sh`.
1+
Template integration test scripts
2+
3+
Specify your module's service in the `docker-compose.yml` file.
24

35
- All scripts start with the default `CONTAINER_NAME` variable matching
46
the standard format (`zgrab_<module name>`).
57

6-
- `setup.sh` tries to launch the container; if it fails, then it tries
7-
to build the docker image from the `Dockerfile` in
8-
`integration_tests/<module_name>/container`; then it tries to launch
9-
the container again. **This must be modified by all new modules**.
10-
118
- `test.sh` runs the container using the `docker-runner.sh` script and
129
dumps the output in `zgrab-output/<module name>/<module_name>.json`.
1310
Afterwards it dumps the container logs to stdout. This can be used
1411
as-is, it only provides a single test case with no arguments.
1512

16-
- `cleanup.sh` runs `docker stop $CONTAINER_NAME`. This should tear
17-
down everything started in `setup.sh`. In the case where there is
18-
only a single container, it can be used as-is.
19-
2013
- `schema.py` registers the `scan_response_type` and provides a
2114
skeleton that can be filled out. **This must be modified for all new
2215
modules**.

integration_tests/.template/cleanup.sh

-9
This file was deleted.

integration_tests/.template/setup.sh

-25
This file was deleted.

integration_tests/amqp091/cleanup.sh

-15
This file was deleted.

integration_tests/amqp091/setup.sh

-37
This file was deleted.

integration_tests/cleanup.sh

-30
This file was deleted.

0 commit comments

Comments
 (0)