示例#1
0
def run_test():
    test_1 = {'type': 'test',
              'config': {}}
    test_2 = {'type': 'testfeedback',
              'config': {}}
    config = {'type': 'all',
              'config': {'tasks': [test_1, test_2]}}

    sim = usersim.UserSim(True)

    task_id = api.new_task(config)

    print('Cycle 1')
    sim.cycle()

    print(api.status_task(task_id))

    print('Cycle 2')
    sim.cycle()

    # If both tasks go before the all task, it will be stopped here.
    print(api.status_task(task_id))

    print('Cycle 3')
    sim.cycle()

    # Otherwise, it will be stopped here.
    print(api.status_task(task_id))
示例#2
0
def run_test():
    test_1 = {'type': 'test', 'config': {}}
    test_2 = {'type': 'testfeedback', 'config': {}}
    config = {'type': 'sequence', 'config': {'tasks': [test_1, test_2]}}

    sim = usersim.UserSim(True)

    task_id = api.new_task(config)

    print('Cycle 1')
    sim.cycle()

    print(api.status_task(task_id))

    print('Cycle 2')
    sim.cycle()

    print(api.status_task(task_id))

    print('Cycle 3')
    sim.cycle()

    # The sequence task may be stopped here, if it goes after the testfeedback task.
    print(api.status_task(task_id))

    print('Cycle 4')
    sim.cycle()

    # Otherwise it would be stopped here.
    print(api.status_task(task_id))
示例#3
0
def run_test():
    # Calculate the trigger time to be 10 seconds from now.
    trigger_time = datetime.datetime.now() + datetime.timedelta(seconds=5)
    trigger_time = trigger_time.time()
    time = '{}{}'.format(str(trigger_time.hour), str(trigger_time.minute))
    seconds = float(trigger_time.second)
    print('Trigger time is {}'.format(str(trigger_time)))

    config = {'type': 'attime',
              'config': {'time': str(time),
                         'seconds': seconds,
                         'task': {'type': 'test',
                                  'config': {}}}}

    sim = usersim.UserSim(True)

    task_id = api.new_task(config)

    result = sim.cycle()
    print(sim.status_task(task_id)['status'])

    while True:
        result = sim.cycle()

        if len(result) > 1:
            print(result)

        # Only break once we see the nested task has been stopped.
        if api.status_task(2)['state'] == api.States.STOPPED:
            break
示例#4
0
def run_test():
    reps = 10
    config = {
        'type': 'frequency',
        'config': {
            'frequency': 2000,
            'repetitions': reps,
            'task': {
                'type': 'test',
                'config': {}
            }
        }
    }

    sim = usersim.UserSim(True)

    task_id = api.new_task(config)

    while True:
        result = sim.cycle()

        if len(result) > 1:
            print(result)

        # Break once the final task has been stopped.
        if api.status_task(task_id + reps)['state'] == api.States.STOPPED:
            break
示例#5
0
def unpause_task(task_id):
    """ Unpause a single task.

    Arguments:
        task_id (int > 0): The task ID returned by an earlier call to new_task.

    Returns:
        bool: True if the operation succeeded, False otherwise.
    """
    sim = usersim.UserSim()
    return sim.unpause_task(task_id)
示例#6
0
def run_test():
    telnet_config = {'type': 'telnet',
                     'config': {'host': TCP_IP,
                                'username': '******',
                                'password': '******',
                                'commandlist': ['printstuff', 'do other stuff', 'do this thing'],
                                'port': TCP_PORT}}

    sim = usersim.UserSim(True)

    task_id = api.validate_config(telnet_config)
示例#7
0
def add_feedback(task_id, error):
    """ Create an additional feedback message. This will mostly be used from within threads supporting a main task, for
    example, managing interaction with an external program.

    Args:
        task_id (int): The task ID of the calling task. This can be accessed from within a task object by using
            self._task_id. If task_id < 1, no feedback will be generated.
        error (str): A description of the error, or a traceback.format_exc().
    """
    sim = usersim.UserSim()
    sim.add_feedback(task_id, error)
示例#8
0
def test_good_cases(task, good_cases):
    """ Used to test properly formatted configs.

    Args:
        task: A task dictionary mapping 'type' to the task name (e.g. 'ssh')
        good_cases: A list of tuples of the form ('config_name, config'). config should be properly formatted.
    """
    for config_name, config in good_cases:
        task['config'] = config
        sim = usersim.UserSim(True)
        api.validate_config(task)
        print('Correctly accepted %s' % config_name)
示例#9
0
def test_good_cases(task, good_cases):
    """ Used to test properly formatted configs. Prints feedback from the task.

    Args:
        task: A task dictionary mapping 'type' to the task name (e.g. 'ssh')
        good_cases: A list of tuples of the form ('config_name', config). config should be properly formatted.
    """
    sim = usersim.UserSim(True)
    for config_name, config in good_cases:
        task['config'] = config
        api.new_task(task)
        print('Correctly accepted %s' % config_name)
示例#10
0
def status_all():
    """ Get a list of the status of all managed tasks.

    Returns:
        list of dicts: Each dictionary will have the following key:value pairs:
            'id':int
            'type':str
            'state':str
            'status':str
    """
    sim = usersim.UserSim()
    return sim.status_all()
示例#11
0
def test_scheduling_auto_stop():
    test_new_task()
    sim = usersim.UserSim()

    sim.cycle()
    assert api.status_all()
    assert api.status_task(1)['state'] == api.States.TO_STOP

    sim.cycle()
    assert not api.status_all()
    assert api.status_task(1)['state'] == api.States.STOPPED

    assert api.status_task(2)['state'] == api.States.UNKNOWN
示例#12
0
def run_test():
    seconds = 10
    task = {'type': 'test', 'config': {}}

    config = {'type': 'delay', 'config': {'task': task, 'seconds': seconds}}

    sim = usersim.UserSim(True)

    task_id = api.new_task(config)

    while True:
        sim.cycle()
        if api.status_task(2)['state'] == api.States.STOPPED:
            print('Nested delay task complete.')
            break
示例#13
0
def status_task(task_id):
    """ Get the status of a single task.

    Arguments:
        task_id (int > 0): The task ID returned by an earlier call to new_task.

    Returns:
        dict: A dictionary with the following key:value pairs:
            'id':int
            'type':str
            'state':str
            'status':str
    """
    sim = usersim.UserSim()
    return sim.status_task(task_id)
示例#14
0
def test_good_cases(task, good_cases):
    """ Test properly formatted configs.

    Args:
        task (dict): A task config dict. task['config'] will be overwritten.
        good_cases (list(tuple)): List of tuples of the form ('config_name', config), where config
            is a valid config for the task.
    """
    for config_name, config in good_cases:
        task['config'] = config
        sim = usersim.UserSim(True)
        api.new_task(task)
        # Running this one is way too annoying.
        #result = sim.cycle()
        #for item in result:
        #    print(item)
        print('Correctly accepted %s' % config_name)
示例#15
0
def test_scheduling():
    test_new_task_stop()
    sim = usersim.UserSim()

    sim.cycle()
    assert api.status_task(1)['state'] == api.States.SCHEDULED

    sim.cycle()
    assert api.status_task(1)['state'] == api.States.SCHEDULED

    api.pause_all()
    assert api.status_task(1)['state'] == api.States.TO_PAUSE

    # pauses should be idempotent
    api.pause_task(1)
    assert api.status_task(1)['state'] == api.States.TO_PAUSE

    sim.cycle()
    assert api.status_task(1)['state'] == api.States.PAUSED

    api.unpause_all()
    assert api.status_task(1)['state'] == api.States.TO_SCHEDULE

    # unpauses should be idempotent
    api.unpause_task(1)
    assert api.status_task(1)['state'] == api.States.TO_SCHEDULE

    sim.cycle()
    assert api.status_task(1)['state'] == api.States.SCHEDULED

    api.stop_all()
    assert api.status_task(1)['state'] == api.States.TO_STOP

    # stops should be idempotent
    api.stop_task(1)
    assert api.status_task(1)['state'] == api.States.TO_STOP

    sim.cycle()
    assert api.status_task(1)['state'] == api.States.STOPPED
示例#16
0
def main():
    feedback_queue = queue.Queue()

    test_mode = cli.parse_and_initialize(feedback_queue)

    if test_mode:
        tests.run_all_tests()
        return

    sim = usersim.UserSim()

    spinner = itertools.cycle('-/|\\')
    while True:
        result = sim.cycle()
        for feedback in result:
            feedback_queue.put(feedback)

        # Spinner to demonstrate that the simulator is still running without scrolling the terminal.
        for i in range(4):
            sys.stdout.write(next(spinner))
            sys.stdout.flush()
            time.sleep(.25)
            sys.stdout.write('\b')
示例#17
0
def new_task(config, start_paused=False, reset=False):
    """ Inserts a new task into the user simulator.

    Arguments:
        task_config (dict): A dictionary with the following key:value pairs.
            'type':str
            'config':dict
        start_paused (bool): True if the new task should be paused initially, False otherwise.
        reset (bool): True if the simulator should be reset, False otherwise. This option should only be used
            for writing tests.

    Raises:
        KeyError: See validate_config docstring.
        ValueError: See validate_config docstring.

    Returns:
        int: The new task's unique ID.
    """
    sim = usersim.UserSim(reset)
    validated_config = validate_config(config)
    task = tasks.task_dict[config['type']]

    return sim.new_task(task, validated_config, start_paused)
示例#18
0
def pause_all():
    """ Pause all currently scheduled tasks.
    """
    sim = usersim.UserSim()
    sim.pause_all()
示例#19
0
def test_cycle():
    test_new_task()
    sim = usersim.UserSim()
    assert sim.cycle()
示例#20
0
def unpause_all():
    """ Unpause all currently paused tasks.
    """
    sim = usersim.UserSim()
    sim.unpause_all()
示例#21
0
def stop_all():
    """ Stop all tasks that are currently scheduled or paused.
    """
    sim = usersim.UserSim()
    sim.stop_all()