This document describes the Docker Compose testing environment for the Shock repository, including setup instructions, available commands, and common testing scenarios.
The Shock testing environment uses Docker Compose to create a consistent and isolated environment for running tests. It includes:
- A service for running Go tests
- A MongoDB instance for test data
- Volume mappings to persist test results and coverage reports
- Environment variables for test configuration
- Dedicated output directory for test results and coverage reports
- Docker and Docker Compose installed on your system
- Git (to clone the repository)
-
Clone the Shock repository (if you haven't already):
git clone https://github.com/MG-RAST/Shock.git cd Shock -
Make the test script executable (if it's not already):
chmod +x run-tests.sh
The run-tests.sh script provides a simple interface for running tests:
./run-tests.sh [command] [options]all: Run all tests (default)package [path]: Run tests for a specific package (e.g.,./shock-server/node)coverage: Generate test coverage reportoutput-dir [path]: Set custom directory for test outputs (default:./test-output)clean: Clean up test environmenthelp: Show help message
-v, --verbose: Enable verbose output-h, --help: Show help message
To run all tests in the repository:
./run-tests.sh allTo run tests for a specific package:
./run-tests.sh package ./shock-server/nodeTo generate a test coverage report:
./run-tests.sh coverageThis will create an HTML coverage report (test-output/coverage.html) in the project's test output directory and attempt to open it in your default browser. The test results will also be saved to test-output/test-results.log.
To specify a custom directory for test outputs:
./run-tests.sh output-dir ./my-test-resultsThis will save all test outputs (results log and coverage reports) to the specified directory.
To clean up the test environment (remove containers, volumes, and coverage files):
./run-tests.sh cleanThe test environment is defined in docker-compose.test.yml and includes:
-
shock-test: Service for running Go tests
- Built from
Dockerfile.test - Mounts the project directory for live testing
- Configurable via environment variables
- Built from
-
shock-mongo-test: MongoDB instance for testing
- Uses MongoDB 3.6
- Includes health checks to ensure it's ready before tests run
The test environment uses the following environment variables:
GO_TEST_ARGS: Arguments to pass togo test(default:./...)GO_TEST_COVERAGE: Whether to generate coverage reports (default:false)GO_TEST_COVERAGE_FILE: Path to coverage output file (default:/test-output/coverage.out)GO_TEST_COVERAGE_HTML: Path to HTML coverage report (default:/test-output/coverage.html)GO_TEST_RESULTS_FILE: Path to test results log file (default:/test-output/test-results.log)
These can be set in the environment or passed via the run-tests.sh script.
If your tests require additional services (e.g., a mock API server), you can add them to the docker-compose.test.yml file:
services:
# Existing services...
mock-api:
image: mockserver/mockserver
ports:
- "1080"
environment:
- MOCKSERVER_INITIALIZATION_JSON_PATH=/config/mockserver.json
volumes:
- ./test/mockserver.json:/config/mockserver.jsonThe test configuration is defined in Dockerfile.test and can be customized as needed. For example, to add additional test dependencies:
# Install additional dependencies
RUN go get -u github.com/some/packageIf tests fail with connection errors to MongoDB, ensure that:
-
The MongoDB service is running and healthy:
docker-compose -f docker-compose.test.yml ps
-
The MongoDB connection string in the test configuration is correct:
mongodb://shock-mongo-test:27017/shock_test
If you encounter permission issues with test files:
-
Check the ownership of files created by Docker:
ls -la
-
If necessary, adjust permissions:
sudo chown -R $(id -u):$(id -g) .
If the coverage report is not generated:
-
Ensure the test run completed successfully
-
Check for errors in the test output
-
Verify that the test output directory exists and has the correct permissions:
ls -la test-output/
-
Verify that the coverage file was created:
ls -la test-output/coverage.out
-
Manually generate the HTML report:
go tool cover -html=test-output/coverage.out -o test-output/coverage.html
Test results are saved to a log file in the test output directory. To view the test results:
cat test-output/test-results.logFor a quick summary of test results:
grep -E "^(PASS|FAIL)" test-output/test-results.log | sort | uniq -c- Use Mocks: Use the provided mock implementations for database and file operations
- Isolate Tests: Each test should be independent and not rely on the state from other tests
- Clean Up: Always clean up resources created during tests
- Use Test Helpers: Utilize the test utilities in
shock-server/test/util - Test Edge Cases: Include tests for error conditions and edge cases