Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from support.test_support import testAssertEqual, testAssertNotEqual
- from support.console import serial_command
- from pylibpigeon import PigeonKernel, PIGEON_RUNFOR_TIMEOUT, ATTACHMENT_TYPE_NONE, ATTACHMENT_TYPE_MEMORY
- 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
- import json, time
- from configure_test_configurations import factory_config, partial_config, bad_config, bad_interface_config
- class ConfigureTestCase:
- def __init__(self):
- self.identified = False
- self.conf_hash = None
- self.conf_reason = None
- def identify_cb(self, session, payload, attach):
- if self.identified:
- self.reset_conf_source = payload['conf-source']
- self.reset_conf_hash = payload['conf-hash']
- if self.store_stages:
- self.dest_file = "after_reboot.json"
- self.next_msg = None
- session.sendMessage("debug::read-file-request", {"filename": "/etc/config.json"})
- else:
- self.p.running = False
- else:
- self.identified = True
- self.addr = session.sock.getsockname()[0]
- session.sendMessage("control::identify-ack", {})
- if self.store_stages:
- self.dest_file = "before.json"
- self.next_msg = "control::configure"
- self.next_payload = self.payload
- session.sendMessage("debug::read-file-request", {"filename": "/etc/config.json"})
- else:
- session.sendMessage("control::configure", self.payload)
- def read_file_cb(self, session, payload, attach):
- if attach.type == ATTACHMENT_TYPE_NONE:
- attach.type = ATTACHMENT_TYPE_MEMORY
- return
- with open(self.dest_file, "wb") as fw:
- fw.write(str(attach))
- if self.next_msg:
- session.sendMessage(self.next_msg, self.next_payload)
- else:
- self.p.running = False
- def read_file_nak_cb(self, session, payload, attach):
- print "Read file failed ({0}): {1}".format(payload['errno'], smp_strerror(payload['errno']))
- self.p.running = False
- def configure_ack_cb(self, session, payload, attach):
- self.conf_hash = payload['hash']
- if self.store_stages:
- self.dest_file = "after.json"
- self.next_msg = "control::check-config"
- self.next_payload = {}
- session.sendMessage("debug::read-file-request", {"filename": "/etc/config.json"})
- else:
- session.sendMessage("control::check-config", {})
- def configure_nak_cb(self, session, payload, attach):
- self.conf_reason = payload['reason']
- session.pendDelete()
- def check_config_response(self, session, payload, attach):
- self.checked_conf_hash = payload["conf-hash"]
- if self.recheck_id:
- time.sleep(5)
- session.sendMessage("control::reset", {"component": 0})
- else:
- session.pendDelete()
- def run(self, syncbox_ip, payload, recheck_id=False, runfor=120, store_stages=False):
- self.payload = payload
- self.recheck_id = recheck_id
- self.store_stages = store_stages
- self.p = PigeonKernel()
- self.p.messageRouter.subscribe("control::identify", self.identify_cb)
- self.p.messageRouter.subscribe("control::configure-ack", self.configure_ack_cb)
- self.p.messageRouter.subscribe("control::configure-nak", self.configure_nak_cb)
- self.p.messageRouter.subscribe("control::check-config-response", self.check_config_response)
- self.p.messageRouter.subscribe("debug::read-file-nak", self.read_file_nak_cb)
- self.p.messageRouter.subscribe("debug::read-file", self.read_file_cb)
- if recheck_id:
- reconnect = True
- discon = True
- else:
- reconnect = False
- discon = True
- self.p.connect((syncbox_ip, 2000), reconnect=reconnect)
- self.rc = self.p.main(runfor=runfor, quit_after_disconnect=discon)
- def test_default_configuration(syncbox_ip=None, serial_port=None):
- """ Send a factory-default configuration and verify it gets applied without error. """
- m = ConfigureTestCase()
- m.run(syncbox_ip, factory_config, recheck_id=True, store_stages=False)
- testAssertEqual(m.identified, True)
- testAssertNotEqual(m.conf_hash, None)
- testAssertEqual(m.conf_reason, None)
- testAssertEqual(m.conf_hash, m.checked_conf_hash)
- testAssertEqual(m.conf_hash, m.reset_conf_hash)
- testAssertEqual(m.addr, m.reset_conf_source)
- def test_partial_configuration(syncbox_ip=None, serial_port=None):
- """ Send a partial configuration and verify that it gets applied successfully. """
- m = ConfigureTestCase()
- m.run(syncbox_ip, partial_config)
- testAssertEqual(m.identified, True)
- testAssertNotEqual(m.conf_hash, None)
- testAssertEqual(m.conf_reason, None)
- def test_invalid_config(syncbox_ip=None, serial_port=None):
- """ Send a deliberately invalid configuration and verify config-nak is returned """
- m = ConfigureTestCase()
- m.run(syncbox_ip, bad_config)
- testAssertEqual(m.identified, True)
- testAssertEqual(m.conf_hash, None)
- testAssertEqual(m.conf_reason, SMP_EINVAL)
- def test_invalid_interface_config(syncbox_ip=None, serial_port=None):
- """ Send a deliberately invalid interface configuration and verify config-nak is returned with
- the EINTERFACE error code. """
- m = ConfigureTestCase()
- m.run(syncbox_ip, bad_interface_config)
- testAssertEqual(m.identified, True)
- testAssertEqual(m.conf_hash, None)
- testAssertEqual(m.conf_reason, SMP_EINTERFACE)
- def tests(args=None):
- return [
- {"function": test_default_configuration},
- {"function": test_partial_configuration},
- {"function": test_invalid_config},
- {"function": test_invalid_interface_config},
- ]
- if __name__ == "__main__":
- import sys
- print "Should be called with test_runner.py [OPTIONS] {0}".format(sys.argv[0])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement