def compare(self, got, exp, log, strict_type=True): """ Common function for comparing two transactions. Can be re-implemented by a subclass. """ # Compare the types if strict_type and type(got) != type(exp): self.errors += 1 log.error("Received transaction is a different type to expected " "transaction") log.info("Got: %s but expected %s" % (str(type(got)), str(type(exp)))) if self._imm: raise TestFailure("Received transaction of wrong type") return # Or convert to a string before comparison elif not strict_type: got, exp = str(got), str(exp) # Compare directly if got != exp: self.errors += 1 # Try our best to print out something useful strgot, strexp = str(got), str(exp) log.error("Received transaction differed from expected output") log.info("Expected:\n" + hexdump(strexp)) if not isinstance(exp, str): try: for word in exp: log.info(str(word)) except: pass log.info("Received:\n" + hexdump(strgot)) if not isinstance(got, str): try: for word in got: log.info(str(word)) except: pass log.warning("Difference:\n%s" % hexdiffs(strexp, strgot)) if self._imm: raise TestFailure("Received transaction differed from expected" "transaction") else: # Don't want to fail the test # if we're passed something without __len__ try: log.debug("Received expected transaction %d bytes" % (len(got))) log.debug(repr(got)) except: pass
def check_received_transaction(transaction): """Called back by the monitor when a new transaction has been received""" log = logging.getLogger(self.log.name + '.' + monitor.name) if callable(expected_output): exp = expected_output(transaction) elif len(expected_output): exp = expected_output.pop(0) else: self.errors += 1 log.error("Received a transaction but wasn't expecting anything") log.info("Got: %s" % (hexdump(str(transaction)))) if self._imm: raise TestFailure("Received a transaction but wasn't expecting anything") return if type(transaction) != type(exp): self.errors += 1 log.error("Received transaction is a different type to expected transaction") log.info("Got: %s but expected %s" % (str(type(transaction)), str(type(exp)))) if self._imm: raise TestFailure("Received transaction of wrong type") return if transaction != exp: self.errors += 1 log.error("Received transaction differed from expected output") log.info("Expected:\n" + hexdump(exp)) if not isinstance(exp, str): try: for word in exp: self.log.info(str(word)) except: pass log.info("Received:\n" + hexdump(transaction)) if not isinstance(transaction, str): try: for word in transaction: self.log.info(str(word)) except: pass log.warning("Difference:\n%s" % hexdiffs(exp, transaction)) if self._imm: raise TestFailure("Received transaction differed from expected transaction") else: # Don't want to fail the test if we're passed something without __len__ try: log.debug("Received expected transaction %d bytes" % (len(transaction))) log.debug(repr(transaction)) except: pass
def compare(self, got, exp, log, strict_type=True): """Common function for comparing two transactions. Can be re-implemented by a sub-class. Args: got: The received transaction. exp: The expected transaction. log: The logger for reporting messages. strict_type (bool, optional): Require transaction type to match exactly if ``True``, otherwise compare its string representation. Raises: :any:`TestFailure`: If received transaction differed from expected transaction when :attr:`fail_immediately` is ``True``. If *strict_type* is ``True``, also the transaction type must match. """ # Compare the types if strict_type and type(got) != type(exp): self.errors += 1 log.error("Received transaction type is different than expected") log.info("Received: %s but expected %s" % (str(type(got)), str(type(exp)))) if self._imm: raise TestFailure("Received transaction of wrong type. " "Set strict_type=False to avoid this.") return # Or convert to a string before comparison elif not strict_type: got, exp = str(got), str(exp) # Compare directly if got != exp: self.errors += 1 # Try our best to print out something useful strgot, strexp = str(got), str(exp) log.error("Received transaction differed from expected output") if not strict_type: log.info("Expected:\n" + hexdump(strexp)) else: log.info("Expected:\n" + repr(exp)) if not isinstance(exp, str): try: for word in exp: log.info(str(word)) except Exception: pass if not strict_type: log.info("Received:\n" + hexdump(strgot)) else: log.info("Received:\n" + repr(got)) if not isinstance(got, str): try: for word in got: log.info(str(word)) except Exception: pass log.warning("Difference:\n%s" % hexdiffs(strexp, strgot)) if self._imm: raise TestFailure( "Received transaction differed from expected " "transaction") else: # Don't want to fail the test # if we're passed something without __len__ try: log.debug("Received expected transaction %d bytes" % (len(got))) log.debug(repr(got)) except Exception: pass