9
9
from struct import pack
10
10
import base64
11
11
12
- from generator_lib import generators
13
-
12
+ # Generator functions
13
+ # Add your new generator function here
14
+ # TODO: with some import syntax or clever moduling, it might be possible not
15
+ # 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
14
19
15
20
class TestSpec (object ):
16
21
"""Class to represent a test specification. Used to convert
@@ -27,39 +32,30 @@ def _traverse(key, element):
27
32
self .__dict__ .update (objd )
28
33
self .basedict = dictionary
29
34
30
- def is_valid (self ):
31
- return True
32
-
33
-
34
35
def read_specs (folder ):
35
36
"""Read test specs from a folder"""
36
37
specfiles = [f for f in listdir (folder ) if isfile (join (folder , f ))]
37
38
specs = []
38
39
for file in specfiles :
39
40
print (f"Parsing spec file: { file } " )
41
+ # Only use the first part of the filename as spec name
42
+ name = file .split ('.' )[0 ]
40
43
with open (os .path .join (folder , file ), 'r' ) as f :
41
44
spec = safe_load (f )
42
45
testspec = TestSpec (spec ["spec" ])
43
- if testspec .is_valid ():
44
- specs .append (testspec )
45
- else :
46
- print (f"Error loading test spec from { file } " )
46
+ specs .append ((name , testspec ))
47
47
return specs
48
48
49
49
50
50
def generate_data (specs , output_folder ):
51
51
"""Generate test data for a list of specs"""
52
- for spec in specs :
53
- if spec .generator in generators :
54
- print (f"Generating test { spec .name } " )
55
- generate_spec_data (output_folder , spec , generators [spec .generator ])
56
- else :
57
- print (f"No generator function found for spec { spec .name } , skipping..." )
58
-
52
+ for (name , spec ) in specs :
53
+ generate_spec_data (output_folder , spec , name )
59
54
60
- def generate_spec_data (output_folder , spec , generator_fn ):
55
+ def generate_spec_data (output_folder , spec , name ):
61
56
"""Generates data for a single spec and outputs it into the specified output folder."""
62
- (operation , result ) = generator_fn ()
57
+ # The generator function has the same name as the test specification
58
+ (operation , result ) = eval (name )()
63
59
64
60
request_auth = create_auth (spec .request .auth )
65
61
request_content_len = spec .request .header .content_length
@@ -76,21 +72,21 @@ def generate_spec_data(output_folder, spec, generator_fn):
76
72
response_content_len = len (result )
77
73
78
74
response_auth_len = spec .response .header .auth_length
79
-
80
75
response_header = pack_header (spec .response .header , response_auth_len , response_content_len )
81
76
82
77
request_buf = request_header + operation + request_auth
83
78
response_buf = response_header + result
84
79
80
+ # The generator appends the base64 data at the end of the spec file
85
81
out_data = {
86
82
"spec" : spec .basedict ,
87
83
"test_data" : {
88
84
"request" : base64 .b64encode (request_buf ).decode ('ascii' ),
89
85
"response" : base64 .b64encode (response_buf ).decode ('ascii' ),
90
86
}
91
87
}
92
- out_path = os .path .join (output_folder , spec . name + ".test.yaml" )
93
- print (f"Writing spec { spec . name } test data to { out_path } " )
88
+ out_path = os .path .join (output_folder , name + ".test.yaml" )
89
+ print (f"Writing spec { name } test data to { out_path } " )
94
90
with open (out_path , 'w' ) as f :
95
91
dump (out_data , f , sort_keys = False )
96
92
@@ -100,6 +96,8 @@ def pack_header(header, auth_len, body_len):
100
96
# pack function converts arguments into binary string, based on format string.
101
97
# < means integers are little endian. Rest of format string is one character per input to indicate
102
98
# packed field interpretation. See struct.pack docs for details.
99
+ # This should map to the Fixed Common Header defined here:
100
+ # https://parallaxsecond.github.io/parsec-book/parsec_client/wire_protocol.html#the-fixed-common-header
103
101
return pack ('<IHBBHBQBBBIHIHH' ,
104
102
header .magic_number ,
105
103
header .header_size ,
@@ -129,15 +127,15 @@ def create_auth(auth_spec):
129
127
130
128
131
129
def main ():
132
- specdir = os .path .abspath (os .path .join (os .path .dirname (__file__ ), 'testspecs' ))
133
- datadir = os .path .abspath (os .path .join (os .path .dirname (__file__ ), '../testdata' ))
134
130
print ("Generating test data." )
131
+
132
+ specdir = os .path .abspath (os .path .join (os .path .dirname (__file__ ), 'test_specs' ))
135
133
print (f"Reading test specs from { specdir } " )
136
- print (f"Generating test data to { datadir } " )
137
134
specs = read_specs (specdir )
138
135
136
+ datadir = os .path .abspath (os .path .join (os .path .dirname (__file__ ), '../generator_output' ))
137
+ print (f"Generating test data to { datadir } " )
139
138
generate_data (specs , datadir )
140
139
141
-
142
140
if __name__ == "__main__" :
143
141
main ()
0 commit comments