Advertisement
Guest User

Untitled

a guest
Dec 12th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.29 KB | None | 0 0
  1. from support.test_support import testAssertEqual, testAssertNotEqual
  2. from support.console import serial_command
  3. from pylibpigeon import PigeonKernel, PIGEON_RUNFOR_TIMEOUT, ATTACHMENT_TYPE_NONE, ATTACHMENT_TYPE_MEMORY
  4. from libsmp import SMP_TOD_DIR_OFF, SMP_TOD_DIR_IN, SMP_TOD_DIR_OUT, SMP_TS_CLOCK_PLL1, SMP_TS_NODE_PLL1, SMP_EINVAL, SMP_EINTERFACE, smp_strerror
  5. import json, time
  6. from configure_test_configurations import factory_config, partial_config, bad_config, bad_interface_config
  7.  
  8. class ConfigureTestCase:
  9. def __init__(self):
  10. self.identified = False
  11. self.conf_hash = None
  12. self.conf_reason = None
  13.  
  14. def identify_cb(self, session, payload, attach):
  15. if self.identified:
  16. self.reset_conf_source = payload['conf-source']
  17. self.reset_conf_hash = payload['conf-hash']
  18.  
  19. if self.store_stages:
  20. self.dest_file = "after_reboot.json"
  21. self.next_msg = None
  22. session.sendMessage("debug::read-file-request", {"filename": "/etc/config.json"})
  23. else:
  24. self.p.running = False
  25. else:
  26. self.identified = True
  27.  
  28. self.addr = session.sock.getsockname()[0]
  29.  
  30. session.sendMessage("control::identify-ack", {})
  31.  
  32. if self.store_stages:
  33. self.dest_file = "before.json"
  34. self.next_msg = "control::configure"
  35. self.next_payload = self.payload
  36. session.sendMessage("debug::read-file-request", {"filename": "/etc/config.json"})
  37. else:
  38. session.sendMessage("control::configure", self.payload)
  39.  
  40. def read_file_cb(self, session, payload, attach):
  41. if attach.type == ATTACHMENT_TYPE_NONE:
  42. attach.type = ATTACHMENT_TYPE_MEMORY
  43. return
  44.  
  45. with open(self.dest_file, "wb") as fw:
  46. fw.write(str(attach))
  47.  
  48. if self.next_msg:
  49. session.sendMessage(self.next_msg, self.next_payload)
  50. else:
  51. self.p.running = False
  52.  
  53. def read_file_nak_cb(self, session, payload, attach):
  54. print "Read file failed ({0}): {1}".format(payload['errno'], smp_strerror(payload['errno']))
  55. self.p.running = False
  56.  
  57. def configure_ack_cb(self, session, payload, attach):
  58. self.conf_hash = payload['hash']
  59.  
  60. if self.store_stages:
  61. self.dest_file = "after.json"
  62. self.next_msg = "control::check-config"
  63. self.next_payload = {}
  64. session.sendMessage("debug::read-file-request", {"filename": "/etc/config.json"})
  65. else:
  66. session.sendMessage("control::check-config", {})
  67.  
  68. def configure_nak_cb(self, session, payload, attach):
  69. self.conf_reason = payload['reason']
  70.  
  71. session.pendDelete()
  72.  
  73. def check_config_response(self, session, payload, attach):
  74. self.checked_conf_hash = payload["conf-hash"]
  75.  
  76. if self.recheck_id:
  77. time.sleep(5)
  78. session.sendMessage("control::reset", {"component": 0})
  79. else:
  80. session.pendDelete()
  81.  
  82. def run(self, syncbox_ip, payload, recheck_id=False, runfor=120, store_stages=False):
  83. self.payload = payload
  84. self.recheck_id = recheck_id
  85. self.store_stages = store_stages
  86.  
  87. self.p = PigeonKernel()
  88. self.p.messageRouter.subscribe("control::identify", self.identify_cb)
  89. self.p.messageRouter.subscribe("control::configure-ack", self.configure_ack_cb)
  90. self.p.messageRouter.subscribe("control::configure-nak", self.configure_nak_cb)
  91. self.p.messageRouter.subscribe("control::check-config-response", self.check_config_response)
  92. self.p.messageRouter.subscribe("debug::read-file-nak", self.read_file_nak_cb)
  93. self.p.messageRouter.subscribe("debug::read-file", self.read_file_cb)
  94.  
  95. if recheck_id:
  96. reconnect = True
  97. discon = True
  98. else:
  99. reconnect = False
  100. discon = True
  101. self.p.connect((syncbox_ip, 2000), reconnect=reconnect)
  102. self.rc = self.p.main(runfor=runfor, quit_after_disconnect=discon)
  103.  
  104. def test_default_configuration(syncbox_ip=None, serial_port=None):
  105. """ Send a factory-default configuration and verify it gets applied without error. """
  106.  
  107. m = ConfigureTestCase()
  108. m.run(syncbox_ip, factory_config, recheck_id=True, store_stages=False)
  109.  
  110. testAssertEqual(m.identified, True)
  111. testAssertNotEqual(m.conf_hash, None)
  112. testAssertEqual(m.conf_reason, None)
  113. testAssertEqual(m.conf_hash, m.checked_conf_hash)
  114. testAssertEqual(m.conf_hash, m.reset_conf_hash)
  115. testAssertEqual(m.addr, m.reset_conf_source)
  116.  
  117. def test_partial_configuration(syncbox_ip=None, serial_port=None):
  118. """ Send a partial configuration and verify that it gets applied successfully. """
  119.  
  120. m = ConfigureTestCase()
  121. m.run(syncbox_ip, partial_config)
  122.  
  123. testAssertEqual(m.identified, True)
  124. testAssertNotEqual(m.conf_hash, None)
  125. testAssertEqual(m.conf_reason, None)
  126.  
  127. def test_invalid_config(syncbox_ip=None, serial_port=None):
  128. """ Send a deliberately invalid configuration and verify config-nak is returned """
  129.  
  130. m = ConfigureTestCase()
  131. m.run(syncbox_ip, bad_config)
  132.  
  133. testAssertEqual(m.identified, True)
  134. testAssertEqual(m.conf_hash, None)
  135. testAssertEqual(m.conf_reason, SMP_EINVAL)
  136.  
  137. def test_invalid_interface_config(syncbox_ip=None, serial_port=None):
  138. """ Send a deliberately invalid interface configuration and verify config-nak is returned with
  139. the EINTERFACE error code. """
  140.  
  141. m = ConfigureTestCase()
  142. m.run(syncbox_ip, bad_interface_config)
  143.  
  144. testAssertEqual(m.identified, True)
  145. testAssertEqual(m.conf_hash, None)
  146. testAssertEqual(m.conf_reason, SMP_EINTERFACE)
  147.  
  148. def tests(args=None):
  149. return [
  150. {"function": test_default_configuration},
  151. {"function": test_partial_configuration},
  152. {"function": test_invalid_config},
  153. {"function": test_invalid_interface_config},
  154. ]
  155.  
  156. if __name__ == "__main__":
  157. import sys
  158. print "Should be called with test_runner.py [OPTIONS] {0}".format(sys.argv[0])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement