Skip to content

Commit 017df16

Browse files
authored
allow specifying settings via argparse instead of environment variables (#41)
* allow specifying settings via argparse instead of environment variables * bump version to 0.9.0
1 parent 27a6b56 commit 017df16

File tree

3 files changed

+63
-14
lines changed

3 files changed

+63
-14
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,22 @@
22
Automatically test services/checker using the enochecker API
33

44
# Usage
5-
`enochecker_test` can be used to run tests against a checker, optionally you can specify wich tests to run e.g. `enochecker_test test_getflag[0] test_exploit_per_exploit_id` will run only the first `getflag` test and all `exploit_per_exploit_id` tests.
5+
`enochecker_test` can be used to run tests against a checker, optionally you can specify wich tests to run e.g. `enochecker_test test_getflag[0] test_exploit_per_exploit_id` will run only the first `getflag` test and all `exploit_per_exploit_id` tests.
6+
7+
```
8+
usage: enochecker_test [-h] [-a CHECKER_ADDRESS] [-p {1..65535}] [-A SERVICE_ADDRESS] [testcase ...]
9+
10+
Utility for testing checkers that implement the enochecker API
11+
12+
positional arguments:
13+
testcase Specify the tests that should be run in the syntax expected by pytest, e.g. test_getflag. If no test is specified, all tests will be run.
14+
15+
options:
16+
-h, --help show this help message and exit
17+
-a CHECKER_ADDRESS, --checker-address CHECKER_ADDRESS
18+
The address on which the checker is listening (defaults to the ENOCHECKER_TEST_CHECKER_ADDRESS environment variable)
19+
-p {1..65535}, --checker-port {1..65535}
20+
The port on which the checker is listening (defaults to ENOCHECKER_TEST_CHECKER_PORT environment variable)
21+
-A SERVICE_ADDRESS, --service-address SERVICE_ADDRESS
22+
The address on which the service is listening (defaults to ENOCHECKER_TEST_SERVICE_ADDRESS environment variable)
23+
```

enochecker_test/main.py

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import argparse
12
import logging
23
import os
34
import sys
4-
from typing import cast
55

66
import jsons
77
import pytest
@@ -58,25 +58,56 @@ def run_tests(host, port, service_address, test_methods):
5858

5959

6060
def main():
61-
if not os.getenv("ENOCHECKER_TEST_CHECKER_ADDRESS"):
61+
parser = argparse.ArgumentParser(
62+
prog="enochecker_test",
63+
description="Utility for testing checkers that implement the enochecker API",
64+
)
65+
parser.add_argument(
66+
"-a",
67+
"--checker-address",
68+
help="The address on which the checker is listening (defaults to the ENOCHECKER_TEST_CHECKER_ADDRESS environment variable)",
69+
default=os.environ.get("ENOCHECKER_TEST_CHECKER_ADDRESS"),
70+
)
71+
parser.add_argument(
72+
"-p",
73+
"--checker-port",
74+
help="The port on which the checker is listening (defaults to ENOCHECKER_TEST_CHECKER_PORT environment variable)",
75+
choices=range(1, 65536),
76+
metavar="{1..65535}",
77+
type=int,
78+
default=os.environ.get("ENOCHECKER_TEST_CHECKER_PORT"),
79+
)
80+
parser.add_argument(
81+
"-A",
82+
"--service-address",
83+
help="The address on which the service is listening (defaults to ENOCHECKER_TEST_SERVICE_ADDRESS environment variable)",
84+
default=os.environ.get("ENOCHECKER_TEST_SERVICE_ADDRESS"),
85+
)
86+
parser.add_argument(
87+
"testcase",
88+
help="Specify the tests that should be run in the syntax expected by pytest, e.g. test_getflag. If no test is specified, all tests will be run.",
89+
nargs="*",
90+
)
91+
92+
args = parser.parse_args()
93+
94+
if not args.checker_address:
95+
parser.print_usage()
6296
raise Exception(
6397
"Missing enochecker address, please set the ENOCHECKER_TEST_CHECKER_ADDRESS environment variable"
6498
)
65-
if not os.getenv("ENOCHECKER_TEST_CHECKER_PORT"):
99+
if not args.checker_port:
100+
parser.print_usage()
66101
raise Exception(
67102
"Missing enochecker port, please set the ENOCHECKER_TEST_CHECKER_PORT environment variable"
68103
)
69-
if not os.getenv("ENOCHECKER_TEST_SERVICE_ADDRESS"):
104+
if not args.service_address:
105+
parser.print_usage()
70106
raise Exception(
71107
"Missing service address, please set the ENOCHECKER_TEST_SERVICE_ADDRESS environment variable"
72108
)
73-
host = os.getenv("ENOCHECKER_TEST_CHECKER_ADDRESS")
74-
_service_address = os.getenv("ENOCHECKER_TEST_SERVICE_ADDRESS")
75-
try:
76-
port_str = os.getenv("ENOCHECKER_TEST_CHECKER_PORT")
77-
port = int(cast(str, port_str))
78-
except ValueError:
79-
raise Exception("Invalid number in ENOCHECKER_TEST_PORT")
80109

81110
logging.basicConfig(level=logging.INFO)
82-
run_tests(host, port, _service_address, sys.argv[1:])
111+
run_tests(
112+
args.checker_address, args.checker_port, args.service_address, args.testcase
113+
)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
setuptools.setup(
1111
name="enochecker_test",
12-
version="0.8.1",
12+
version="0.9.0",
1313
author="ldruschk",
1414
author_email="[email protected]",
1515
description="Library to help testing checker scripts based on enochecker",

0 commit comments

Comments
 (0)