Ejemplo n.º 1
0
 def test_format_email_summary_empty(self):
     """Test building an email body based on no commands being run."""
     obs = format_email_summary([])
     self.assertEqual(obs, '')
Ejemplo n.º 2
0
 def test_format_email_summary_single_suite(self):
     """Test building an email body based on a single test suite."""
     exp = 'foo: Pass\n\n'
     obs = format_email_summary([('foo', 0)])
     self.assertEqual(obs, exp)
Ejemplo n.º 3
0
 def test_format_email_summary_all_fail(self):
     """Test building an email body where all commands failed."""
     exp = 'QIIME: Fail\nPyCogent: Fail\n\n'
     obs = format_email_summary([('QIIME', 1), ('PyCogent', 77)])
     self.assertEqual(obs, exp)
Ejemplo n.º 4
0
 def test_format_email_summary_all_pass(self):
     """Test building an email body where all commands ran okay."""
     exp = 'QIIME: Pass\nPyCogent: Pass\n\n'
     obs = format_email_summary([('QIIME', 0), ('PyCogent', 0)])
     self.assertEqual(obs, exp)
Ejemplo n.º 5
0
def _execute_commands_and_build_email(test_suites, setup_cmds,
                                      test_suites_cmds, teardown_cmds,
                                      setup_timeout, test_suites_timeout,
                                      teardown_timeout, cluster_tag):
    """Executes the test suite commands and builds the body of an email.

    Returns the body of an email containing the summarized results and any
    error message or issues that should be brought to the recipient's
    attention, and a list of attachments, which are the log files from running
    the commands.

    Arguments:
        test_suites - the output of _parse_config_file()
        setup_cmds - the output of _build_test_execution_commands()
        test_suites_cmds - the output of _build_test_execution_commands()
        teardown_cmds - the output of _build_test_execution_commands()
        setup_timeout - same as for run_test_suites()
        test_suites_timeout - same as for run_test_suites()
        teardown_timeout - same as for run_test_suites()
        cluster_tag - same as for run_test_suites()
    """
    email_body = ""
    attachments = []

    # Create a unique temporary file to hold the results of all commands.
    log_f = TemporaryFile(prefix='clout_log', suffix='.txt')
    attachments.append(('complete_log.txt', log_f))

    # Build up the body of the email as we execute the commands. First, execute
    # the setup commands.
    cmd_executor = CommandExecutor(setup_cmds, log_f,
                                   stop_on_first_failure=True)
    setup_cmds_succeeded = cmd_executor(setup_timeout)[0]

    if setup_cmds_succeeded is None:
        email_body += ("The maximum allowable cluster setup time of %s "
                       "minute(s) was exceeded.\n\n" % str(setup_timeout))
    elif not setup_cmds_succeeded:
        email_body += ("There were problems in starting the remote cluster "
                       "while preparing to execute the test suite(s). Please "
                       "check the attached log for more details.\n\n")
    else:
        # Execute each test suite command, keeping track of stdout and stderr
        # in a temporary file. These will be used as attachments when the
        # email is sent. Since the temporary files will have randomly-generated
        # names, we'll also specify what we want the file to be called when it
        # is attached to the email (we don't have to worry about having unique
        # filenames at that point).
        cmd_executor.cmds = test_suites_cmds
        cmd_executor.stop_on_first_failure = False
        cmd_executor.log_individual_cmds = True
        test_suites_cmds_succeeded, test_suites_cmds_status = \
                cmd_executor(test_suites_timeout)

        # It is okay if there are fewer test suites that got executed than
        # there were input test suites (which is possible if we encounter a
        # timeout). Just report the ones that finished.
        label_to_ret_val = []
        for (label, cmd), (test_suite_log_f, ret_val) in \
                zip(test_suites, test_suites_cmds_status):
            label_to_ret_val.append((label, ret_val))
            attachments.append(('%s_results.txt' % label, test_suite_log_f))

        # Build a summary of the test suites that passed and those that didn't.
        email_body += format_email_summary(label_to_ret_val)

        if test_suites_cmds_succeeded is None:
            timeout_test_suite = \
                    test_suites[len(test_suites_cmds_status) - 1][0]
            untested_suites = [label for label, cmd in
                               test_suites[len(test_suites_cmds_status):]]
            email_body += ("The maximum allowable time of %s minute(s) for "
                           "all test suites to run was exceeded. The timeout "
                           "occurred while running the %s test suite." %
                           (str(test_suites_timeout), timeout_test_suite))
            if untested_suites:
                email_body += (" The following test suites were not tested: "
                               "%s\n\n" % ', '.join(untested_suites))

    # Lastly, execute the teardown commands.
    cluster_termination_msg = ("IMPORTANT: You should check that the cluster "
                               "labelled with the tag '%s' was properly "
                               "terminated. If not, you should manually "
                               "terminate it.\n\n" % cluster_tag)

    cmd_executor.cmds = teardown_cmds
    cmd_executor.stop_on_first_failure = False
    cmd_executor.log_individual_cmds = False
    teardown_cmds_succeeded = cmd_executor(teardown_timeout)[0]

    if teardown_cmds_succeeded is None:
        email_body += ("The maximum allowable cluster termination time of "
                       "%s minute(s) was exceeded.\n\n%s" %
                       (str(teardown_timeout), cluster_termination_msg))
    elif not teardown_cmds_succeeded:
        email_body += ("There were problems in terminating the remote "
                       "cluster. Please check the attached log for more "
                       "details.\n\n%s" % cluster_termination_msg)

    # Set our file position to the beginning for all attachments since we are
    # in read/write mode and we need to read from the beginning again. Closing
    # the file will delete it.
    for attachment in attachments:
        attachment[1].seek(0, 0)

    return email_body, attachments
Ejemplo n.º 6
0
def _execute_commands_and_build_email(test_suites, setup_cmds,
                                      test_suites_cmds, teardown_cmds,
                                      setup_timeout, test_suites_timeout,
                                      teardown_timeout, cluster_tag):
    """Executes the test suite commands and builds the body of an email.

    Returns the body of an email containing the summarized results and any
    error message or issues that should be brought to the recipient's
    attention, and a list of attachments, which are the log files from running
    the commands.

    Arguments:
        test_suites - the output of _parse_config_file()
        setup_cmds - the output of _build_test_execution_commands()
        test_suites_cmds - the output of _build_test_execution_commands()
        teardown_cmds - the output of _build_test_execution_commands()
        setup_timeout - same as for run_test_suites()
        test_suites_timeout - same as for run_test_suites()
        teardown_timeout - same as for run_test_suites()
        cluster_tag - same as for run_test_suites()
    """
    email_body = ""
    attachments = []

    # Create a unique temporary file to hold the results of all commands.
    log_f = TemporaryFile(prefix='clout_log', suffix='.txt')
    attachments.append(('complete_log.txt', log_f))

    # Build up the body of the email as we execute the commands. First, execute
    # the setup commands.
    cmd_executor = CommandExecutor(setup_cmds,
                                   log_f,
                                   stop_on_first_failure=True)
    setup_cmds_succeeded = cmd_executor(setup_timeout)[0]

    if setup_cmds_succeeded is None:
        email_body += ("The maximum allowable cluster setup time of %s "
                       "minute(s) was exceeded.\n\n" % str(setup_timeout))
    elif not setup_cmds_succeeded:
        email_body += ("There were problems in starting the remote cluster "
                       "while preparing to execute the test suite(s). Please "
                       "check the attached log for more details.\n\n")
    else:
        # Execute each test suite command, keeping track of stdout and stderr
        # in a temporary file. These will be used as attachments when the
        # email is sent. Since the temporary files will have randomly-generated
        # names, we'll also specify what we want the file to be called when it
        # is attached to the email (we don't have to worry about having unique
        # filenames at that point).
        cmd_executor.cmds = test_suites_cmds
        cmd_executor.stop_on_first_failure = False
        cmd_executor.log_individual_cmds = True
        test_suites_cmds_succeeded, test_suites_cmds_status = \
                cmd_executor(test_suites_timeout)

        # It is okay if there are fewer test suites that got executed than
        # there were input test suites (which is possible if we encounter a
        # timeout). Just report the ones that finished.
        label_to_ret_val = []
        for (label, cmd), (test_suite_log_f, ret_val) in \
                zip(test_suites, test_suites_cmds_status):
            label_to_ret_val.append((label, ret_val))
            attachments.append(('%s_results.txt' % label, test_suite_log_f))

        # Build a summary of the test suites that passed and those that didn't.
        email_body += format_email_summary(label_to_ret_val)

        if test_suites_cmds_succeeded is None:
            timeout_test_suite = \
                    test_suites[len(test_suites_cmds_status) - 1][0]
            untested_suites = [
                label
                for label, cmd in test_suites[len(test_suites_cmds_status):]
            ]
            email_body += ("The maximum allowable time of %s minute(s) for "
                           "all test suites to run was exceeded. The timeout "
                           "occurred while running the %s test suite." %
                           (str(test_suites_timeout), timeout_test_suite))
            if untested_suites:
                email_body += (" The following test suites were not tested: "
                               "%s\n\n" % ', '.join(untested_suites))

    # Lastly, execute the teardown commands.
    cluster_termination_msg = ("IMPORTANT: You should check that the cluster "
                               "labelled with the tag '%s' was properly "
                               "terminated. If not, you should manually "
                               "terminate it.\n\n" % cluster_tag)

    cmd_executor.cmds = teardown_cmds
    cmd_executor.stop_on_first_failure = False
    cmd_executor.log_individual_cmds = False
    teardown_cmds_succeeded = cmd_executor(teardown_timeout)[0]

    if teardown_cmds_succeeded is None:
        email_body += ("The maximum allowable cluster termination time of "
                       "%s minute(s) was exceeded.\n\n%s" %
                       (str(teardown_timeout), cluster_termination_msg))
    elif not teardown_cmds_succeeded:
        email_body += ("There were problems in terminating the remote "
                       "cluster. Please check the attached log for more "
                       "details.\n\n%s" % cluster_termination_msg)

    # Set our file position to the beginning for all attachments since we are
    # in read/write mode and we need to read from the beginning again. Closing
    # the file will delete it.
    for attachment in attachments:
        attachment[1].seek(0, 0)

    return email_body, attachments