2018-05-16 02:54:22 -04:00
|
|
|
#!/usr/bin/env python
|
2018-09-18 08:43:23 -04:00
|
|
|
from __future__ import print_function
|
2018-05-16 02:54:22 -04:00
|
|
|
import os
|
|
|
|
import json
|
|
|
|
import argparse
|
2018-09-18 08:43:23 -04:00
|
|
|
import tempfile
|
2018-05-16 02:54:22 -04:00
|
|
|
|
|
|
|
import pexpect
|
|
|
|
|
2019-01-24 23:16:51 -05:00
|
|
|
# Each protocol version to be tested needs a 'testcases_vX.txt' file
|
|
|
|
PROTOCOL_VERSIONS = [1, 2]
|
2018-05-16 02:54:22 -04:00
|
|
|
|
2019-01-24 23:16:51 -05:00
|
|
|
|
|
|
|
def parse_testcases(version):
|
|
|
|
with open("testcases_v%d.txt" % version, "r") as f:
|
2018-12-04 07:46:48 -05:00
|
|
|
cases = [l for l in f.readlines() if len(l.strip()) > 0]
|
2018-05-16 02:54:22 -04: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 07:46:48 -05:00
|
|
|
send = cases[i + 1]
|
|
|
|
expect = cases[i + 2]
|
2018-05-16 02:54:22 -04:00
|
|
|
if not desc.startswith("* "):
|
2018-12-04 07:46:48 -05:00
|
|
|
raise RuntimeError("Unexpected description at line %d: '%s'" % (i + 1, desc))
|
2018-05-16 02:54:22 -04:00
|
|
|
if not send.startswith("> "):
|
2018-12-04 07:46:48 -05:00
|
|
|
raise RuntimeError("Unexpected send at line %d: '%s'" % (i + 2, send))
|
2018-05-16 02:54:22 -04:00
|
|
|
if not expect.startswith("< "):
|
2018-12-04 07:46:48 -05:00
|
|
|
raise RuntimeError("Unexpected expect at line %d: '%s'" % (i + 3, expect))
|
2018-05-16 02:54:22 -04:00
|
|
|
desc = desc[2:]
|
|
|
|
send = json.loads(send[2:])
|
|
|
|
expect = json.loads(expect[2:])
|
|
|
|
yield (desc, send, expect)
|
|
|
|
|
2018-12-04 07:46:48 -05:00
|
|
|
|
2018-05-16 02:54:22 -04: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 08:43:23 -04: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 02:54:22 -04: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)
|
|
|
|
p = pexpect.spawn(cmdline, timeout=0.5)
|
|
|
|
p.logfile = args.logfile
|
|
|
|
p.setecho(False)
|
|
|
|
|
|
|
|
p.expect("Server running.+\r\n")
|
2019-01-24 23:16:51 -05:00
|
|
|
initial = expect_json(p)
|
2018-05-16 02:54:22 -04:00
|
|
|
print("Initial: %s" % initial)
|
2019-01-24 23:16:51 -05:00
|
|
|
|
|
|
|
for version in PROTOCOL_VERSIONS:
|
|
|
|
test_protocol_version(p, version)
|
|
|
|
|
|
|
|
test_load_save(p, temp_sdkconfig_path)
|
|
|
|
|
|
|
|
p.send("%s\n" % json.dumps({"version": 2, "load": temp_sdkconfig_path}))
|
|
|
|
load_result = expect_json(p)
|
2018-05-16 02:54:22 -04:00
|
|
|
print("Load result: %s" % (json.dumps(load_result)))
|
|
|
|
assert len(load_result["values"]) > 0 # loading same file should return all config items
|
|
|
|
assert len(load_result["ranges"]) > 0
|
|
|
|
print("Done. All passed.")
|
|
|
|
|
|
|
|
finally:
|
|
|
|
try:
|
|
|
|
os.remove(temp_sdkconfig_path)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
2018-12-04 07:46:48 -05:00
|
|
|
|
2019-01-24 23:16:51 -05: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
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
|
2018-05-16 02:54:22 -04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|