<commit_before>#!/usr/bin/env python2

# Copyright (C) 2014 Red Hat, Inc.
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
# USA
#
# Author: tasleson

# Takes a csv file of hardware array information and runs the plugin test
# on each of them dumping the results to the specified directory

import test_hardware
import sys
from subprocess import Popen, PIPE
from multiprocessing import Process
import yaml
import time


def call(command):
    """
    Call an executable and return a tuple of exitcode, stdout, stderr
    """
    process = Popen(command, stdout=PIPE, stderr=PIPE)
    out = process.communicate()
    return process.returncode, out[0], out[1]


def run_test(cmdline, output_dir, sys_id, uri, password ):

    exec_array = [cmdline, '-q', '--uri', uri,
                                     '--password', password]

    (ec, out, error) = call(exec_array)

    # Save the output to a temp dir
    sys_id = sys_id.replace('/', '-')
    sys_id = sys_id.replace(' ', '_')
    fn = "%s/%s" % (output_dir, sys_id)

    with open(fn + ".out", 'w') as so:
        so.write(out)
        so.flush()

    with open(fn + ".error", 'w') as se:
        se.write(error)
        se.flush()

    # We should probably put more information in here
    with open(fn + ".ec", 'w') as error_file:
        error_file.write(yaml.dump(dict(ec=str(ec),
                                        error_file=fn + ".error",
                                        uri=uri)))
        error_file.flush()


if __name__ == '__main__':
    if len(sys.argv) != 4:
        print('Syntax: %s <cimon_file> <plugin unit test> <output directory>'
              % (sys.argv[0]))
        sys.exit(1)
    else:
        run = True
        process_list = []
        results = []
        array_cimons = test_hardware.TestArrays().providers(sys.argv[1])

        for system in array_cimons:
            (uri, password) = test_hardware.TestArrays.uri_password_get(system)
            name = system['COMPANY']
            ip = system['IP']
            system_id = "%s-%s" % (name, ip)

            p = Process(target=run_test, args=(sys.argv[2], sys.argv[3],
                                               system_id, uri, password))
            p.name = system_id
            p.start()
            process_list.append(p)

        start = time.time()
        print 'Test run started at: %s' % time.strftime("%c")
        sys.stdout.flush()

        while len(process_list) > 0:
            for p in process_list:
                p.join(1)
                if not p.is_alive():
                    print '%s exited with %s' % (p.name, str(p.exitcode))
                    sys.stdout.flush()
                    process_list.remove(p)
                    break

            current = time.time()
            if current - start > 3600:
                print 'Test taking too long...'
                sys.stdout.flush()
                for p in process_list:
                    print 'Terminating process %s, name %s' % \
                          (str(p.pid), p.name)
                    sys.stdout.flush()
                    p.terminate()
                break

        print 'Test run exiting at: %s' % time.strftime("%c")
        sys.stdout.flush()<commit_msg>test_automated.py:  Add configurable time limit<commit_after>#!/usr/bin/env python2

# Copyright (C) 2014 Red Hat, Inc.
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
# USA
#
# Author: tasleson

# Takes a csv file of hardware array information and runs the plugin test
# on each of them dumping the results to the specified directory

import test_hardware
import sys
from subprocess import Popen, PIPE
from multiprocessing import Process
import yaml
import time
import os


def call(command):
    """
    Call an executable and return a tuple of exitcode, stdout, stderr
    """
    process = Popen(command, stdout=PIPE, stderr=PIPE)
    out = process.communicate()
    return process.returncode, out[0], out[1]


def run_test(cmdline, output_dir, sys_id, uri, password ):

    exec_array = [cmdline, '-q', '--uri', uri,
                                     '--password', password]

    (ec, out, error) = call(exec_array)

    # Save the output to a temp dir
    sys_id = sys_id.replace('/', '-')
    sys_id = sys_id.replace(' ', '_')
    fn = "%s/%s" % (output_dir, sys_id)

    with open(fn + ".out", 'w') as so:
        so.write(out)
        so.flush()

    with open(fn + ".error", 'w') as se:
        se.write(error)
        se.flush()

    # We should probably put more information in here
    with open(fn + ".ec", 'w') as error_file:
        error_file.write(yaml.dump(dict(ec=str(ec),
                                        error_file=fn + ".error",
                                        uri=uri)))
        error_file.flush()


if __name__ == '__main__':
    time_limit_seconds = long(os.getenv('LSM_TEST_TMO_SECS', 90 * 60))  # 90 minutes

    if len(sys.argv) != 4:
        print('Syntax: %s <cimon_file> <plugin unit test> <output directory>'
              % (sys.argv[0]))
        sys.exit(1)
    else:
        run = True
        process_list = []
        results = []
        array_cimons = test_hardware.TestArrays().providers(sys.argv[1])

        for system in array_cimons:
            (uri, password) = test_hardware.TestArrays.uri_password_get(system)
            name = system['COMPANY']
            ip = system['IP']
            system_id = "%s-%s" % (name, ip)

            p = Process(target=run_test, args=(sys.argv[2], sys.argv[3],
                                               system_id, uri, password))
            p.name = system_id
            p.start()
            process_list.append(p)

        start = time.time()
        print 'Test run started at: %s, time limit is %s minutes' % \
              (time.strftime("%c"), str(time_limit_seconds / 60.0))
        sys.stdout.flush()

        while len(process_list) > 0:
            for p in process_list:
                p.join(1)
                if not p.is_alive():
                    print '%s exited with %s at %s (runtime %s seconds)' % \
                          (p.name, str(p.exitcode), time.strftime("%c"),
                           str(time.time() - start))
                    sys.stdout.flush()
                    process_list.remove(p)
                    break

            current = time.time()
            if (current - start) >= time_limit_seconds:
                print 'Test taking too long...'
                sys.stdout.flush()
                for p in process_list:
                    print 'Terminating process %s, name %s' % \
                          (str(p.pid), p.name)
                    sys.stdout.flush()
                    p.terminate()
                break

        print 'Test run exiting at: %s' % time.strftime("%c")
        sys.stdout.flush()
{'fraction_non_alphanumeric': 0.10860453648915187, 'fraction_numerical': 0.008505917159763314, 'mean_word_length': 1.9267676767676767, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6250696', 'n_tokens_mistral': 2396, 'n_tokens_neox': 2278, 'n_words': 764}