示例#1
0
def test_dci_agent_failure(monkeypatch, dci_context, job_id):
    def return_context(**a):
        return dci_context

    def raise_exception(*a, **b):
        raise Exception('booom')
    mock_run_commands = mock.Mock()
    mock_run_tests = raise_exception
    monkeypatch.setattr(agent, 'get_dci_context', return_context)
    monkeypatch.setattr(dciclient.v1.helper, 'run_command',
                        mock_run_commands)
    monkeypatch.setattr(tripleohelper.undercloud, 'Undercloud', mock.Mock())
    monkeypatch.setattr(dciclient.v1.tripleo_helper, 'run_tests',
                        mock_run_tests)
    with pytest.raises(SystemExit):
        agent.main(['--topic', 'topic_name', '--config',
                    os.path.dirname(__file__) + '/dci_agent.yaml'])

    js = dci_jobstate.list(dci_context).json()['jobstates']
    assert js[-1]['status'] == 'failure'
    assert js[-1]['comment'] == 'booom'

    # the where filter does not work yet:
    #   I1f0df01f813efae75f6e0e75a3861d2d4ba5694a
    files = dci_file.list(dci_context).json()['files']
    content = dci_file.content(dci_context, files[-1]['id'])
    assert 'most recent call last' in content.text
示例#2
0
def output(context, id):
    """output(context, id)

    Show a job output.

    >>> dcictl job-output [OPTIONS]

    :param string id: ID of the job to show [required]
    """

    colors = {
        'pre-run': '\x1b[6;30;44m',
        'running': '\x1b[6;30;42m',
        'post-run': '\x1b[6;30;44m',
        'failure': '\x1b[6;30;41m'}
    result = job.list_jobstates(context, id=id)
    jobstates = result.json()['jobstates']

    for js in jobstates:
        color = colors.get(js['status'], '')
        click.echo('%s[%s]\x1b[0m %s' % (
            color,
            js['status'],
            js['comment']))
        f_l = dci_file.list(
            context,
            where='jobstate_id:' + js['id'])
        for f in f_l.json()['files']:
            click.echo(dci_file.content(context, id=f['id']).text)
def test_run_command(dci_context, jobstate_id):
    dci_helper.run_command(
        dci_context,
        ['echo', 'bob'],
        jobstate_id=jobstate_id)
    new_file = dci_file.list(dci_context).json()['files'][-1]
    assert new_file['size'] == 4
    assert 'bob' in new_file['name']
def test_run_command_shell(dci_context, jobstate_id):
    dci_helper.run_command(
        dci_context,
        'echo foo bar',
        shell=True)
    files = dci_file.list(dci_context).json()['files']
    assert files[-1]['name'] == 'echo foo bar'
    f = dci_file.content(dci_context, files[-1]['id'])
    assert f.content.decode(encoding='UTF-8') == 'foo bar\n'
示例#5
0
def list(context, job_id):
    """list(context)

    List all files.

    >>> dcictl file-list
    """
    result = file.list(context, where='job_id:' + job_id)
    utils.format_output(result, context.format,
                        file.RESOURCE, file.TABLE_HEADERS)
示例#6
0
def list(context):
    result = file.list(context)
    utils.format_output(result.json(), context.format,
                        file.RESOURCE, file.TABLE_HEADERS)
示例#7
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            state=dict(default='present', choices=['present', 'absent'], type='str'),
            # Authentication related parameters
            #
            dci_login=dict(required=False, type='str'),
            dci_password=dict(required=False, type='str'),
            dci_cs_url=dict(required=False, type='str'),
            # Resource related parameters
            #
            id=dict(type='str'),
            content=dict(type='str'),
            path=dict(type='str'),
            name=dict(type='str'),
            job_id=dict(type='str'),
            jobstate_id=dict(type='str'),
            mime=dict(default='text/plain', type='str'),
        ),
    )

    if not dciclient_found:
        module.fail_json(msg='The python dciclient module is required')

    login, password, url = get_details(module)
    if not login or not password:
        module.fail_json(msg='login and/or password have not been specified')

    ctx = dci_context.build_dci_context(url, login, password, 'Ansible')

    # Action required: List all files
    # Endpoint called: /files GET via dci_file.list()
    #
    # List all files
    if module_params_empty(module.params):
        res = dci_file.list(ctx)

    # Action required: Delete the file matchin file id
    # Endpoint called: /files/<file_id> DELETE via dci_file.delete()
    #
    # If the file exist and it has been succesfully deleted the changed is
    # set to true, else if the file does not exist changed is set to False
    elif module.params['state'] == 'absent':
        if not module.params['id']:
            module.fail_json(msg='id parameter is required')
        res = dci_file.delete(ctx, module.params['id'])

    # Action required: Retrieve file informations
    # Endpoint called: /files/<file_id> GET via dci_job.file()
    #
    # Get file informations
    elif module.params['id']:
        res = dci_file.get(ctx, module.params['id'])

    # Action required: Creat a file with the specified content
    # Endpoint called: /files POST via dci_file.create()
    #
    # Create the file and attach it where it belongs (either jobstate or job)
    # with the specified content/content of path provided.
    #
    # NOTE: /files endpoint does not support PUT method, hence no update can
    #       be accomplished.
    else:
        if not module.params['job_id'] and not module.params['jobstate_id']:
            module.fail_json(msg='Either job_id or jobstate_id must be specified')

        if (not module.params['content'] and not module.params['path']) or \
            (module.params['content'] and module.params['path']):
            module.fail_json(msg='Either content or path must be specified')

        if module.params['content'] and not module.params['name']:
            module.fail_json(msg='name parameter must be specified when content has been specified')

        if module.params['path'] and not module.params['name']:
            name = module.params['path']
        else:
            name = module.params['name']

        if module.params['path']:
            try:
                content = open(module.params['path'], 'r').read()
            except IOError as e:
                module.fail_json(msg='The path specified cannot be read')
        else:
            content = module.params['content']

        kwargs = {'name': name, 'content': content, 'mime': module.params['mime']}

        if module.params['job_id']:
            kwargs['job_id'] = module.params['job_id']
        if module.params['jobstate_id']:
            kwargs['jobstate_id'] = module.params['jobstate_id']

        res = dci_file.create(ctx, **kwargs)

    try:
        result = res.json()
        if res.status_code == 404:
            module.fail_json(msg='The resource does not exist')
        if res.status_code == 409:
            result['changed'] = False
        else:
            result['changed'] = True
    except:
        result = {}
        result['changed'] = True

    module.exit_json(**result)