Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1e7e68c

Browse files
committedSep 30, 2021
Fix CI
Also formats the code Signed-off-by: Hugues de Valon <[email protected]>
1 parent 07b9b57 commit 1e7e68c

File tree

8 files changed

+98
-72
lines changed

8 files changed

+98
-72
lines changed
 

‎.github/workflows/ci.yml

+12-15
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,28 @@ jobs:
1616
run: exit ${{ steps.lc.outputs.exit_code }}
1717

1818
build:
19+
name: CI tests
1920
runs-on: ubuntu-latest
20-
strategy:
21-
matrix:
22-
# pyyaml version we use only supporting python 3.6 upwards
23-
python-version: [ 3.6, 3.7, 3.8]
24-
2521
steps:
2622
- uses: actions/checkout@v2
27-
- name: Set up Python ${{ matrix.python-version }}
28-
uses: actions/setup-python@v2
23+
- name: Set up Python 3.7
24+
uses: actions/setup-python@v1
2925
with:
30-
python-version: ${{ matrix.python-version }}
26+
python-version: 3.7
3127
- name: Install dependencies
3228
run: |
3329
python -m pip install --upgrade pip
34-
pip install flake8 pytest
35-
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
30+
pip install -r requirements.txt
31+
- name: Check formatting with blake
32+
run: |
33+
black --check . --exclude generator/generators/protobuf
3634
- name: Lint with flake8
3735
run: |
38-
# stop the build if there are Python syntax errors or undefined names
39-
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics --exclude generator/protobuf
40-
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
41-
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics --exclude generator/generators/protobuf
36+
# The GitHub editor is 127 chars wide
37+
flake8 . --count --max-line-length=127 --statistics --exclude generator/generators/protobuf
4238
- name: Execute smoke test
4339
run: |
44-
python parsec_mock/parsec_mock.py
40+
python parsec_mock/parsec_mock.py &
4541
sleep 5
4642
python tests/mock_test.py
43+
pkill python

‎generator/generator.py

+52-38
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@
1313
# Add your new generator function here
1414
# TODO: with some import syntax or clever moduling, it might be possible not
1515
# to have to do anything here?
16-
from generators.ping_noauth import gen as ping_noauth
17-
from generators.list_opcodes_bad1 import gen as list_opcodes_bad1
18-
from generators.list_opcodes_directauth import gen as list_opcodes_directauth
16+
# F401 ignored for flake8 as those methods are called through eval
17+
from generators.ping_noauth import gen as ping_noauth # noqa: F401
18+
from generators.list_opcodes_bad1 import gen as list_opcodes_bad1 # noqa: F401
19+
from generators.list_opcodes_directauth import ( # noqa: F401
20+
gen as list_opcodes_directauth,
21+
)
22+
1923

2024
class TestSpec(object):
2125
"""Class to represent a test specification. Used to convert
@@ -32,15 +36,16 @@ def _traverse(key, element):
3236
self.__dict__.update(objd)
3337
self.basedict = dictionary
3438

39+
3540
def read_specs(folder):
3641
"""Read test specs from a folder"""
3742
specfiles = [f for f in listdir(folder) if isfile(join(folder, f))]
3843
specs = []
3944
for file in specfiles:
4045
print(f"Parsing spec file: {file}")
4146
# Only use the first part of the filename as spec name
42-
name = file.split('.')[0]
43-
with open(os.path.join(folder, file), 'r') as f:
47+
name = file.split(".")[0]
48+
with open(os.path.join(folder, file), "r") as f:
4449
spec = safe_load(f)
4550
testspec = TestSpec(spec["spec"])
4651
specs.append((name, testspec))
@@ -52,27 +57,32 @@ def generate_data(specs, output_folder):
5257
for (name, spec) in specs:
5358
generate_spec_data(output_folder, spec, name)
5459

60+
5561
def generate_spec_data(output_folder, spec, name):
5662
"""Generates data for a single spec and outputs it into the specified output folder."""
5763
# The generator function has the same name as the test specification
5864
(operation, result) = eval(name)()
5965

6066
request_auth = create_auth(spec.request.auth)
6167
request_content_len = spec.request.header.content_length
62-
if request_content_len == 'auto':
68+
if request_content_len == "auto":
6369
request_content_len = len(operation)
6470

6571
request_auth_len = spec.request.header.auth_length
66-
if request_auth_len == 'auto':
72+
if request_auth_len == "auto":
6773
request_auth_len = len(request_auth)
68-
request_header = pack_header(spec.request.header, request_auth_len, request_content_len)
74+
request_header = pack_header(
75+
spec.request.header, request_auth_len, request_content_len
76+
)
6977

7078
response_content_len = spec.response.header.content_length
71-
if response_content_len == 'auto':
79+
if response_content_len == "auto":
7280
response_content_len = len(result)
7381

7482
response_auth_len = spec.response.header.auth_length
75-
response_header = pack_header(spec.response.header, response_auth_len, response_content_len)
83+
response_header = pack_header(
84+
spec.response.header, response_auth_len, response_content_len
85+
)
7686

7787
request_buf = request_header + operation + request_auth
7888
response_buf = response_header + result
@@ -81,13 +91,13 @@ def generate_spec_data(output_folder, spec, name):
8191
out_data = {
8292
"spec": spec.basedict,
8393
"test_data": {
84-
"request": base64.b64encode(request_buf).decode('ascii'),
85-
"response": base64.b64encode(response_buf).decode('ascii'),
86-
}
94+
"request": base64.b64encode(request_buf).decode("ascii"),
95+
"response": base64.b64encode(response_buf).decode("ascii"),
96+
},
8797
}
8898
out_path = os.path.join(output_folder, name + ".test.yaml")
8999
print(f"Writing spec {name} test data to {out_path}")
90-
with open(out_path, 'w') as f:
100+
with open(out_path, "w") as f:
91101
dump(out_data, f, sort_keys=False)
92102

93103

@@ -98,44 +108,48 @@ def pack_header(header, auth_len, body_len):
98108
# packed field interpretation. See struct.pack docs for details.
99109
# This should map to the Fixed Common Header defined here:
100110
# https://parallaxsecond.github.io/parsec-book/parsec_client/wire_protocol.html#the-fixed-common-header
101-
return pack('<IHBBHBQBBBIHIHH',
102-
header.magic_number,
103-
header.header_size,
104-
header.major_version_number,
105-
header.minor_version_number,
106-
header.flags,
107-
header.provider,
108-
header.session_handle,
109-
header.content_type,
110-
header.accept_type,
111-
header.auth_type,
112-
body_len,
113-
auth_len,
114-
header.opcode,
115-
header.status,
116-
0
117-
)
111+
return pack(
112+
"<IHBBHBQBBBIHIHH",
113+
header.magic_number,
114+
header.header_size,
115+
header.major_version_number,
116+
header.minor_version_number,
117+
header.flags,
118+
header.provider,
119+
header.session_handle,
120+
header.content_type,
121+
header.accept_type,
122+
header.auth_type,
123+
body_len,
124+
auth_len,
125+
header.opcode,
126+
header.status,
127+
0,
128+
)
118129

119130

120131
def create_auth(auth_spec):
121132
"""Creates auth body of message"""
122-
if auth_spec.type == 'none':
123-
return b''
124-
if auth_spec.type == 'direct':
125-
return auth_spec.app_name.encode('utf-8')
126-
return b''
133+
if auth_spec.type == "none":
134+
return b""
135+
if auth_spec.type == "direct":
136+
return auth_spec.app_name.encode("utf-8")
137+
return b""
127138

128139

129140
def main():
130141
print("Generating test data.")
131142

132-
specdir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'test_specs'))
143+
specdir = os.path.abspath(os.path.join(os.path.dirname(__file__), "test_specs"))
133144
print(f"Reading test specs from {specdir}")
134145
specs = read_specs(specdir)
135146

136-
datadir = os.path.abspath(os.path.join(os.path.dirname(__file__), '../generator_output'))
147+
datadir = os.path.abspath(
148+
os.path.join(os.path.dirname(__file__), "../generator_output")
149+
)
137150
print(f"Generating test data to {datadir}")
138151
generate_data(specs, datadir)
139152

153+
140154
if __name__ == "__main__":
141155
main()

‎generator/generators/list_opcodes_bad1.py

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# SPDX-License-Identifier: Apache-2.0
33
from .protobuf import list_opcodes_pb2
44

5+
56
def gen():
67
operation = list_opcodes_pb2.Operation()
78
operation.provider_id = 1

‎generator/generators/list_opcodes_directauth.py

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# SPDX-License-Identifier: Apache-2.0
33
from .protobuf import list_opcodes_pb2
44

5+
56
def gen():
67
operation = list_opcodes_pb2.Operation()
78
operation.provider_id = 1

‎generator/generators/ping_noauth.py

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# SPDX-License-Identifier: Apache-2.0
33
from .protobuf import ping_pb2
44

5+
56
def gen():
67
op = ping_pb2.Operation()
78
result = ping_pb2.Result()

‎parsec_mock/parsec_mock.py

+16-11
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,25 @@
1313

1414

1515
@click.command()
16-
@click.option('--test-folder', default='./generator_output', help='Load all tests from folder')
17-
@click.option('--parsec-socket', default='./parsec.sock', help='Path to parsec unix socket')
16+
@click.option(
17+
"--test-folder", default="./generator_output", help="Load all tests from folder"
18+
)
19+
@click.option(
20+
"--parsec-socket", default="./parsec.sock", help="Path to parsec unix socket"
21+
)
1822
def run_test(test_folder, parsec_socket):
19-
print('Mock parsec service listening on unix://{}.'.format(parsec_socket))
23+
print("Mock parsec service listening on unix://{}.".format(parsec_socket))
2024

2125
test_cases = load_tests_from_folder(test_folder)
2226

23-
print('Serving all {} tests in folder {}'.format(len(test_cases),test_folder))
27+
print("Serving all {} tests in folder {}".format(len(test_cases), test_folder))
2428

2529
# Make sure socket doesn't already exist
2630
try:
2731
os.unlink(parsec_socket)
2832
except OSError:
2933
if os.path.exists(parsec_socket):
30-
print('Error removing old parsec socket, exiting')
34+
print("Error removing old parsec socket, exiting")
3135
return
3236

3337
# Create a unix socket
@@ -39,16 +43,16 @@ def run_test(test_folder, parsec_socket):
3943
while True:
4044
connection, client_addr = sock.accept()
4145
try:
42-
print('Connection received from {}'.format(client_addr))
46+
print("Connection received from {}".format(client_addr))
4347
received_data = connection.recv(4096)
44-
b64_received_data = base64.b64encode(received_data).decode('ascii')
48+
b64_received_data = base64.b64encode(received_data).decode("ascii")
4549
if b64_received_data in test_cases:
4650
(name, test_case) = test_cases[b64_received_data]
47-
print('Received expected request for test case {}'.format(name))
51+
print("Received expected request for test case {}".format(name))
4852
bin_response = base64.b64decode(test_case.test_data.response)
4953
connection.sendall(bin_response)
5054
else:
51-
print('Received unexpected request {}'.format(b64_received_data))
55+
print("Received unexpected request {}".format(b64_received_data))
5256
finally:
5357
connection.close()
5458

@@ -68,15 +72,16 @@ def _traverse(key, element):
6872
self.__dict__.update(objd)
6973
self.basedict = dictionary
7074

75+
7176
def load_tests_from_folder(test_folder):
7277
tests = {}
7378
"""Read test specs from a folder"""
7479
specfiles = [f for f in listdir(test_folder) if isfile(join(test_folder, f))]
7580

7681
for file in specfiles:
7782
print(f"Parsing spec file: {file}")
78-
name = file.split('.')[0]
79-
with open(os.path.join(test_folder, file), 'r') as f:
83+
name = file.split(".")[0]
84+
with open(os.path.join(test_folder, file), "r") as f:
8085
spec = safe_load(f)
8186
testspec = TestSpec(spec)
8287
tests[testspec.test_data.request] = (name, testspec)

‎requirements.txt

+7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
attrs==20.3.0
2+
black==21.9b0
23
click==7.1.2
34
flake8==3.9.0
45
iniconfig==1.1.1
56
mccabe==0.6.1
7+
mypy-extensions==0.4.3
68
packaging==20.9
9+
pathspec==0.9.0
10+
platformdirs==2.4.0
711
pluggy==0.13.1
812
protobuf==3.15.6
913
py==1.10.0
@@ -13,6 +17,9 @@ pyflakes==2.3.1
1317
pyparsing==2.4.7
1418
pytest==6.2.2
1519
PyYAML==5.4.1
20+
regex==2021.9.24
1621
rope==0.18.0
1722
six==1.15.0
1823
toml==0.10.2
24+
tomli==1.2.1
25+
typing-extensions==3.10.0.2

‎tests/mock_test.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,31 @@
66
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
77

88
# Connect the socket to the port where the server is listening
9-
server_address = './parsec.sock'
10-
print('connecting to %s' % server_address)
9+
server_address = "./parsec.sock"
10+
print("connecting to %s" % server_address)
1111

1212
sock.connect(server_address)
1313
exit_code = 0
1414

1515
try:
1616
# Send data
17-
message = 'EKfAXh4AAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAA'
17+
message = "EKfAXh4AAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAA"
1818
bin_message = base64.b64decode(message)
1919

20-
expected_response = 'EKfAXh4AAQAAAAAAAAAAAAAAAAAAAAIAAAAAAAEAAAAAAAAACAE='
20+
expected_response = "EKfAXh4AAQAAAAAAAAAAAAAAAAAAAAIAAAAAAAEAAAAAAAAACAE="
2121
bin_expected = base64.b64decode(expected_response)
2222

2323
print('sending "%s"' % message)
2424
sock.sendall(bin_message)
2525

2626
received_data = sock.recv(4096)
27-
print('received {}'.format(base64.b64encode(received_data).decode('ascii')))
27+
print("received {}".format(base64.b64encode(received_data).decode("ascii")))
2828
if bin_expected == received_data:
29-
print('Received expected response')
29+
print("Received expected response")
3030
else:
31-
print('Did not get expected response')
31+
print("Did not get expected response")
3232
exit_code = 1
3333
finally:
34-
print('closing socket')
34+
print("closing socket")
3535
sock.close()
3636
exit(exit_code)

0 commit comments

Comments
 (0)
Please sign in to comment.