-
Notifications
You must be signed in to change notification settings - Fork 0
/
tester.py
executable file
·221 lines (171 loc) · 7.03 KB
/
tester.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env python3
from transitions import Machine
import subprocess
import sys
import time
import argparse
# Test line number
current_line = 0
# Parse command line arguments
parser = argparse.ArgumentParser(description = "MaxScale + replication-manager tester", conflict_handler="resolve")
parser.add_argument("-a", "--maxadmin", dest="maxadmin", help="Path to MaxAdmin binary", default="/usr/bin/maxadmin")
parser.add_argument("-h", "--host", dest="host", help="MaxAdmin network address", default="localhost")
parser.add_argument("-u", "--user", dest="user", help="MaxAdmin user", default="admin")
parser.add_argument("-p", "--password", dest="password", help="MaxAdmin password", default="mariadb")
parser.add_argument("-P", "--port", dest="port", help="MaxAdmin listener port", default="6603")
parser.add_argument("-i", "--interactive", action="store_true", help="Run test in interactive mode", default=False)
parser.add_argument("-b", "--bootstrap", help=
"""Script used to bootstrap the test environment. Tests can also define the
bootstrap script in a comment by adding '# bootstrap: <path-to-script>' as the
first line.""")
parser.add_argument("-c", "--client", help="Client script used to add load to the test environment")
parser.add_argument("-v", "--verbose", action="store_true", help="Verbose output", default=False)
parser.add_argument("-z", "--sleep", help="How long to sleep between state changes", default=10)
parser.add_argument("TEST", help="Test to run", nargs="+")
options = parser.parse_args(sys.argv[1:])
global test
failed_tests = dict()
class Test():
def on_enter_MX(self):
self.start(0)
self.kill(1)
time.sleep(int(options.sleep))
def on_enter_XM(self):
self.kill(0)
self.start(1)
time.sleep(int(options.sleep))
def on_enter_MS(self):
self.start(1)
self.start(0)
time.sleep(int(options.sleep))
def on_enter_SM(self):
self.start(1)
self.start(0)
time.sleep(int(options.sleep))
def on_enter_XX(self):
self.kill(1)
self.kill(0)
time.sleep(int(options.sleep))
def start(self, node):
subprocess.run(["start.sh", str(node)])
def kill(self, node):
subprocess.run(["stop.sh", str(node)])
def get_output():
output = subprocess.run([options.maxadmin, "-u", options.user,
"-p" + options.password, "-h", options.host,
"-P", options.port, "list", "servers"],
stdout=subprocess.PIPE).stdout.decode().split('\n')
return output[4:len(output) - 2]
def check_status(name, status):
global current_line
output = get_output()
i = [z.strip() for a in output for z in a.split('|') if name == a.split('|')[0].strip()]
if len(i) == 0:
print("No matching rows found for server", name)
return False
for x in status:
states = [st.strip() for st in i[4].split(',')]
if x not in states:
print("In test '" + test_name + "' at line " + str(current_line) + ":")
print("Expected state:", test.state)
print("Server:", name)
print("Expected", status, "got", i[4])
for l in get_output():
print([a.strip() for a in l.split('|')])
failed_tests[test_name] = True
return False
return True
def check_no_status(name, status):
global current_line
output = get_output()
i = [z.strip() for a in output for z in a.split('|') if name == a.split('|')[0].strip()]
for x in status:
states = [st.strip() for st in i[4].split(',')]
if x in states:
print("In test '" + test_name + "' at line " + str(current_line) + ":")
print("Expected state:", test.state)
print("Server:", name)
print("Unexpected", status)
for l in get_output():
print([a.strip() for a in l.split('|')])
failed_tests[test_name] = True
return False
return True
def get_statelist():
states = [ "M", "S", "X" ]
invalid_states = ["MM", "SS", "SX", "XS"]
return [x + y for x in states for y in states if x + y not in invalid_states]
def do_bootstrap(script):
subprocess.run([script], shell=True)
def run_test(tname):
global current_line
current_line = 0
with open(tname) as f:
print()
print("Running test:", tname)
for x in f:
current_line += 1
if '#' in x:
continue
x = x.strip()
if options.interactive == True:
input("Press Enter to transition from " + test.state + " to " + x)
if x == "MS":
test.ms()
check_status("server1", ["Master", "Running"])
check_status("server2", ["Slave", "Running"])
elif x == "MX":
test.mx()
check_status("server1", ["Master", "Running"])
check_no_status("server2", ["Running"])
elif x == "SM":
test.sm()
check_status("server1", ["Slave", "Running"])
check_status("server2", ["Master", "Running"])
elif x == "XM":
test.xm()
check_no_status("server1", ["Running"])
check_status("server2", ["Master", "Running"])
elif x == "XX":
test.xx()
check_no_status("server1", ["Running"])
check_no_status("server2", ["Running"])
else:
print("ERROR:", x)
if options.verbose == True:
print("Expecting:", x)
for l in get_output():
print([a.strip() for a in l.split('|')])
def read_line(test_name, i):
with open(test_name) as f:
for n in range(0, i):
f.readline()
return f.readline().strip()
# Create list of all allowed permutations of the three node states
statelist = get_statelist()
transitions = [
{"trigger": "mx", "source": ["MS", "SM", "XX"], "dest": "MX"},
{"trigger": "xm", "source": ["MS", "SM", "XX"], "dest": "XM"},
{"trigger": "xx", "source": ["MX", "XM", "XX"], "dest": "XX"},
{"trigger": "ms", "source": ["MX", "XM", "MS"], "dest": "MS"},
{"trigger": "sm", "source": ["MX", "XM"], "dest": "SM"},
]
client = None
for test_name in options.TEST:
test = Test()
initial_state = read_line(test_name, 0)
if "bootstrap:" in initial_state:
do_bootstrap(initial_state.strip("#").replace("bootstrap:", "").strip())
initial_state = read_line(test_name, 1);
elif options.bootstrap != None:
do_bootstrap(options.bootstrap)
if options.client != None:
client = subprocess.Popen([options.client], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
machine = Machine(model=test, states=statelist, initial=initial_state, transitions=transitions)
run_test(test_name)
if client != None:
client.kill()
client.communicate()
print("Failed tests:")
for k in failed_tests:
print(k)