test_confserver.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/env python
  2. from __future__ import print_function
  3. import os
  4. import json
  5. import argparse
  6. import tempfile
  7. import pexpect
  8. def parse_testcases():
  9. with open("testcases.txt", "r") as f:
  10. cases = [l for l in f.readlines() if len(l.strip()) > 0]
  11. # Each 3 lines in the file should be formatted as:
  12. # * Description of the test change
  13. # * JSON "changes" to send to the server
  14. # * Result JSON to expect back from the server
  15. if len(cases) % 3 != 0:
  16. print("Warning: testcases.txt has wrong number of non-empty lines (%d). Should be 3 lines per test case, always." % len(cases))
  17. for i in range(0, len(cases), 3):
  18. desc = cases[i]
  19. send = cases[i + 1]
  20. expect = cases[i + 2]
  21. if not desc.startswith("* "):
  22. raise RuntimeError("Unexpected description at line %d: '%s'" % (i + 1, desc))
  23. if not send.startswith("> "):
  24. raise RuntimeError("Unexpected send at line %d: '%s'" % (i + 2, send))
  25. if not expect.startswith("< "):
  26. raise RuntimeError("Unexpected expect at line %d: '%s'" % (i + 3, expect))
  27. desc = desc[2:]
  28. send = json.loads(send[2:])
  29. expect = json.loads(expect[2:])
  30. yield (desc, send, expect)
  31. def main():
  32. parser = argparse.ArgumentParser()
  33. parser.add_argument('--logfile', type=argparse.FileType('w'), help='Optional session log of the interactions with confserver.py')
  34. args = parser.parse_args()
  35. try:
  36. # set up temporary file to use as sdkconfig copy
  37. with tempfile.NamedTemporaryFile(mode="w", delete=False) as temp_sdkconfig:
  38. temp_sdkconfig_path = os.path.join(tempfile.gettempdir(), temp_sdkconfig.name)
  39. with open("sdkconfig") as orig:
  40. temp_sdkconfig.write(orig.read())
  41. cmdline = "../confserver.py --kconfig Kconfig --config %s" % temp_sdkconfig_path
  42. print("Running: %s" % cmdline)
  43. p = pexpect.spawn(cmdline, timeout=0.5)
  44. p.logfile = args.logfile
  45. p.setecho(False)
  46. def expect_json():
  47. # run p.expect() to expect a json object back, and return it as parsed JSON
  48. p.expect("{.+}\r\n")
  49. return json.loads(p.match.group(0).strip().decode())
  50. p.expect("Server running.+\r\n")
  51. initial = expect_json()
  52. print("Initial: %s" % initial)
  53. cases = parse_testcases()
  54. for (desc, send, expected) in cases:
  55. print(desc)
  56. req = {"version": "1", "set": send}
  57. req = json.dumps(req)
  58. print("Sending: %s" % (req))
  59. p.send("%s\n" % req)
  60. readback = expect_json()
  61. print("Read back: %s" % (json.dumps(readback)))
  62. if readback.get("version", None) != 1:
  63. raise RuntimeError('Expected {"version" : 1} in response')
  64. for expect_key in expected.keys():
  65. read_vals = readback[expect_key]
  66. exp_vals = expected[expect_key]
  67. if read_vals != exp_vals:
  68. expect_diff = dict((k,v) for (k,v) in exp_vals.items() if k not in read_vals or v != read_vals[k])
  69. raise RuntimeError("Test failed! Was expecting %s: %s" % (expect_key, json.dumps(expect_diff)))
  70. print("OK")
  71. print("Testing load/save...")
  72. before = os.stat(temp_sdkconfig_path).st_mtime
  73. p.send("%s\n" % json.dumps({"version": "1", "save": temp_sdkconfig_path}))
  74. save_result = expect_json()
  75. print("Save result: %s" % (json.dumps(save_result)))
  76. assert len(save_result["values"]) == 0
  77. assert len(save_result["ranges"]) == 0
  78. after = os.stat(temp_sdkconfig_path).st_mtime
  79. assert after > before
  80. p.send("%s\n" % json.dumps({"version": "1", "load": temp_sdkconfig_path}))
  81. load_result = expect_json()
  82. print("Load result: %s" % (json.dumps(load_result)))
  83. assert len(load_result["values"]) > 0 # loading same file should return all config items
  84. assert len(load_result["ranges"]) > 0
  85. print("Done. All passed.")
  86. finally:
  87. try:
  88. os.remove(temp_sdkconfig_path)
  89. except OSError:
  90. pass
  91. if __name__ == "__main__":
  92. main()