2018-05-16 06:54:22 +00:00
|
|
|
#!/usr/bin/env python
|
2018-09-18 12:43:23 +00:00
|
|
|
from __future__ import print_function
|
2018-05-16 06:54:22 +00:00
|
|
|
import os
|
|
|
|
import json
|
|
|
|
import argparse
|
2018-09-18 12:43:23 +00:00
|
|
|
import tempfile
|
2018-05-16 06:54:22 +00:00
|
|
|
|
|
|
|
import pexpect
|
|
|
|
|
2019-01-25 04:16:51 +00:00
|
|
|
# Each protocol version to be tested needs a 'testcases_vX.txt' file
|
|
|
|
PROTOCOL_VERSIONS = [1, 2]
|
2018-05-16 06:54:22 +00:00
|
|
|
|
2019-01-25 04:16:51 +00:00
|
|
|
|
|
|
|
def parse_testcases(version):
|
|
|
|
with open("testcases_v%d.txt" % version, "r") as f:
|
2018-12-04 12:46:48 +00:00
|
|
|
cases = [l for l in f.readlines() if len(l.strip()) > 0]
|
2018-05-16 06:54:22 +00:00
|
|
|
# Each 3 lines in the file should be formatted as:
|
|
|
|
# * Description of the test change
|
|
|
|
# * JSON "changes" to send to the server
|
|
|
|
# * Result JSON to expect back from the server
|
|
|
|
if len(cases) % 3 != 0:
|
|
|
|
print("Warning: testcases.txt has wrong number of non-empty lines (%d). Should be 3 lines per test case, always." % len(cases))
|
|
|
|
|
|
|
|
for i in range(0, len(cases), 3):
|
|
|
|
desc = cases[i]
|
2018-12-04 12:46:48 +00:00
|
|
|
send = cases[i + 1]
|
|
|
|
expect = cases[i + 2]
|
2018-05-16 06:54:22 +00:00
|
|
|
if not desc.startswith("* "):
|
2018-12-04 12:46:48 +00:00
|
|
|
raise RuntimeError("Unexpected description at line %d: '%s'" % (i + 1, desc))
|
2018-05-16 06:54:22 +00:00
|
|
|
if not send.startswith("> "):
|
2018-12-04 12:46:48 +00:00
|
|
|
raise RuntimeError("Unexpected send at line %d: '%s'" % (i + 2, send))
|
2018-05-16 06:54:22 +00:00
|
|
|
if not expect.startswith("< "):
|
2018-12-04 12:46:48 +00:00
|
|
|
raise RuntimeError("Unexpected expect at line %d: '%s'" % (i + 3, expect))
|
2018-05-16 06:54:22 +00:00
|
|
|
desc = desc[2:]
|
|
|
|
send = json.loads(send[2:])
|
|
|
|
expect = json.loads(expect[2:])
|
|
|
|
yield (desc, send, expect)
|
|
|
|
|
2018-12-04 12:46:48 +00:00
|
|
|
|
2018-05-16 06:54:22 +00:00
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('--logfile', type=argparse.FileType('w'), help='Optional session log of the interactions with confserver.py')
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
try:
|
2018-09-18 12:43:23 +00:00
|
|
|
# set up temporary file to use as sdkconfig copy
|
|
|
|
with tempfile.NamedTemporaryFile(mode="w", delete=False) as temp_sdkconfig:
|
|
|
|
temp_sdkconfig_path = os.path.join(tempfile.gettempdir(), temp_sdkconfig.name)
|
2018-05-16 06:54:22 +00:00
|
|
|
with open("sdkconfig") as orig:
|
|
|
|
temp_sdkconfig.write(orig.read())
|
|
|
|
|
|
|
|
cmdline = "../confserver.py --kconfig Kconfig --config %s" % temp_sdkconfig_path
|
|
|
|
print("Running: %s" % cmdline)
|
2019-06-11 00:15:42 +00:00
|
|
|
p = pexpect.spawn(cmdline, timeout=2)
|
2018-05-16 06:54:22 +00:00
|
|
|
p.logfile = args.logfile
|
|
|
|
p.setecho(False)
|
|
|
|
|
|
|
|
p.expect("Server running.+\r\n")
|
2019-01-25 04:16:51 +00:00
|
|
|
initial = expect_json(p)
|
2018-05-16 06:54:22 +00:00
|
|
|
print("Initial: %s" % initial)
|
2019-01-25 04:16:51 +00:00
|
|
|
|
|
|
|
for version in PROTOCOL_VERSIONS:
|
|
|
|
test_protocol_version(p, version)
|
|
|
|
|
|
|
|
test_load_save(p, temp_sdkconfig_path)
|
|
|
|
|
2019-03-15 01:17:14 +00:00
|
|
|
test_invalid_json(p)
|
|
|
|
|
2018-05-16 06:54:22 +00:00
|
|
|
print("Done. All passed.")
|
|
|
|
|
|
|
|
finally:
|
|
|
|
try:
|
|
|
|
os.remove(temp_sdkconfig_path)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
2018-12-04 12:46:48 +00:00
|
|
|
|
2019-01-25 04:16:51 +00:00
|
|
|
def expect_json(p):
|
|
|
|
# run p.expect() to expect a json object back, and return it as parsed JSON
|
|
|
|
p.expect("{.+}\r\n")
|
|
|
|
result = p.match.group(0).strip().decode()
|
|
|
|
print("Read raw data from server: %s" % result)
|
|
|
|
return json.loads(result)
|
|
|
|
|
|
|
|
|
|
|
|
def send_request(p, req):
|
|
|
|
req = json.dumps(req)
|
|
|
|
print("Sending: %s" % (req))
|
|
|
|
p.send("%s\n" % req)
|
|
|
|
readback = expect_json(p)
|
|
|
|
print("Read back: %s" % (json.dumps(readback)))
|
|
|
|
return readback
|
|
|
|
|
|
|
|
|
|
|
|
def test_protocol_version(p, version):
|
|
|
|
print("*****")
|
|
|
|
print("Testing version %d..." % version)
|
|
|
|
|
|
|
|
# reload the config from the sdkconfig file
|
|
|
|
req = {"version": version, "load": None}
|
|
|
|
readback = send_request(p, req)
|
|
|
|
print("Reset response: %s" % (json.dumps(readback)))
|
|
|
|
|
|
|
|
# run through each test case
|
|
|
|
cases = parse_testcases(version)
|
|
|
|
for (desc, send, expected) in cases:
|
|
|
|
print(desc)
|
|
|
|
req = {"version": version, "set": send}
|
|
|
|
readback = send_request(p, req)
|
|
|
|
if readback.get("version", None) != version:
|
|
|
|
raise RuntimeError('Expected {"version" : %d} in response' % version)
|
|
|
|
for expect_key in expected.keys():
|
|
|
|
read_vals = readback[expect_key]
|
|
|
|
exp_vals = expected[expect_key]
|
|
|
|
if read_vals != exp_vals:
|
|
|
|
expect_diff = dict((k,v) for (k,v) in exp_vals.items() if k not in read_vals or v != read_vals[k])
|
|
|
|
raise RuntimeError("Test failed! Was expecting %s: %s" % (expect_key, json.dumps(expect_diff)))
|
|
|
|
print("OK")
|
|
|
|
print("Version %d OK" % version)
|
|
|
|
|
|
|
|
|
|
|
|
def test_load_save(p, temp_sdkconfig_path):
|
|
|
|
print("Testing load/save...")
|
|
|
|
before = os.stat(temp_sdkconfig_path).st_mtime
|
|
|
|
save_result = send_request(p, {"version": 2, "save": temp_sdkconfig_path})
|
|
|
|
print("Save result: %s" % (json.dumps(save_result)))
|
|
|
|
assert "error" not in save_result
|
|
|
|
assert len(save_result["values"]) == 0 # nothing changes when we save
|
|
|
|
assert len(save_result["ranges"]) == 0
|
|
|
|
after = os.stat(temp_sdkconfig_path).st_mtime
|
|
|
|
assert after > before # something got written to disk
|
|
|
|
|
2019-03-14 06:47:08 +00:00
|
|
|
# Do a V2 load
|
|
|
|
load_result = send_request(p, {"version": 2, "load": temp_sdkconfig_path})
|
|
|
|
print("V2 Load result: %s" % (json.dumps(load_result)))
|
|
|
|
assert "error" not in load_result
|
|
|
|
assert len(load_result["values"]) == 0 # in V2, loading same file should return no config items
|
|
|
|
assert len(load_result["ranges"]) == 0
|
|
|
|
|
2019-01-25 04:16:51 +00:00
|
|
|
# Do a V1 load
|
|
|
|
load_result = send_request(p, {"version": 1, "load": temp_sdkconfig_path})
|
|
|
|
print("V1 Load result: %s" % (json.dumps(load_result)))
|
|
|
|
assert "error" not in load_result
|
|
|
|
assert len(load_result["values"]) > 0 # in V1, loading same file should return all config items
|
|
|
|
assert len(load_result["ranges"]) > 0
|
|
|
|
|
|
|
|
|
2019-03-15 01:17:14 +00:00
|
|
|
def test_invalid_json(p):
|
|
|
|
print("Testing invalid JSON formatting...")
|
|
|
|
|
|
|
|
bad_escaping = r'{ "version" : 2, "load" : "c:\some\path\not\escaped\as\json" }'
|
|
|
|
p.send("%s\n" % bad_escaping)
|
|
|
|
readback = expect_json(p)
|
|
|
|
print(readback)
|
|
|
|
assert "json" in readback["error"][0].lower()
|
|
|
|
|
|
|
|
not_really_json = 'Hello world!!'
|
|
|
|
p.send("%s\n" % not_really_json)
|
|
|
|
readback = expect_json(p)
|
|
|
|
print(readback)
|
|
|
|
assert "json" in readback["error"][0].lower()
|
|
|
|
|
|
|
|
|
2018-05-16 06:54:22 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|