예제 #1
0
def test_job_put(dci_context, job_id):
    new_comment = "foo"
    j = job.get(dci_context, job_id).json()["job"]
    assert j["comment"] is None
    job.update(dci_context, id=job_id, etag=j["etag"], comment=new_comment)
    r = job.get(dci_context, job_id)
    j = r.json()["job"]
    assert j["comment"] == new_comment
예제 #2
0
def test_ua_headers(job_id, dci_context_other_user_agent):
    with mock.patch("requests.sessions.Session.send"):
        job.get(dci_context_other_user_agent, job_id)
        # the request that will be should be send
        prepared_request = dci_context_other_user_agent.session.send.call_args[
            0]
        assert prepared_request[0].headers["User-Agent"] == "myagent-0.1"
        assert prepared_request[0].headers["Client-Version"] == (
            "python-dciclient_%s" % version.__version__)
예제 #3
0
def test_standard_headers(job_id, dci_context):
    with mock.patch("requests.sessions.Session.send"):
        job.get(dci_context, job_id)
        # the request that will be should be send
        prepared_request = dci_context.session.send.call_args[0]
        ua = "python-dciclient_" + version.__version__
        assert prepared_request[0].headers["User-Agent"] == ua
        assert prepared_request[0].headers["Client-Version"] == (
            "python-dciclient_%s" % version.__version__)
예제 #4
0
def test_ua_headers(job_id, dci_context_other_user_agent):
    with mock.patch('requests.sessions.Session.send'):
        job.get(dci_context_other_user_agent, job_id)
        # the request that will be should be send
        prepared_request = (dci_context_other_user_agent.session.send
                            .call_args[0])
        assert prepared_request[0].headers['User-Agent'] == 'myagent-0.1'
        assert prepared_request[0].headers['Client-Version'] == (
            'python-dciclient_%s' % version.__version__
        )
예제 #5
0
def test_standard_headers(job_id, dci_context):
    with mock.patch('requests.sessions.Session.send'):
        job.get(dci_context, job_id)
        # the request that will be should be send
        prepared_request = dci_context.session.send.call_args[0]
        ua = 'python-dciclient_' + version.__version__
        assert prepared_request[0].headers['User-Agent'] == ua
        assert prepared_request[0].headers['Client-Version'] == (
            'python-dciclient_%s' % version.__version__
        )
예제 #6
0
def loltest_get_job_with_feeder_context_fails(feeder,
                                              signature_context_factory,
                                              job_id):
    context = signature_context_factory(client_id="feeder/%s" % feeder["id"],
                                        api_secret=feeder["api_secret"])
    r = job.get(context, job_id)
    assert r.status_code == 404
예제 #7
0
def schedule_job(topic_name, context):
    log.info('scheduling job on topic %s' % topic_name)

    topic_res = dci_topic.list(context, where='name:' + topic_name)
    if topic_res.status_code == 200:
        topics = topic_res.json()['topics']
        log.debug('topics: %s' % topics)
        if len(topics) == 0:
            log.error('topic %s not found' % topic_name)
            sys.exit(1)
        topic_id = topics[0]['id']
        schedule = dci_job.schedule(context, topic_id=topic_id)
        if schedule.status_code == 201:
            scheduled_job_id = schedule.json()['job']['id']
            scheduled_job = dci_job.get(context,
                                        scheduled_job_id,
                                        embed='topic,remoteci,components')
            if scheduled_job.status_code == 200:
                job_id = scheduled_job.json()['job']['id']
                dci_jobstate.create(context,
                                    status='new',
                                    comment='job scheduled',
                                    job_id=job_id)
                return scheduled_job.json()
            else:
                log.error('error getting schedule info: %s' %
                          scheduled_job.text)
        else:
            log.error('error scheduling: %s' % schedule.text)
    else:
        log.error('error getting the list of topics: %s' % topic_res.text)
    return None
예제 #8
0
def test_server_url_with_trailing_slash(remoteci_id, remoteci_api_secret,
                                        signature_context_factory, job_id):
    dci_context = signature_context_factory(
        client_id=remoteci_id,
        api_secret=remoteci_api_secret,
        url="http://dciserver.com/",
    )

    r = job.get(dci_context, job_id)
    assert r.status_code == 200
예제 #9
0
def show(context, id):
    """show(context, id)

    Show a job.

    >>> dcictl job-show [OPTIONS]

    :param string id: ID of the job to show [required]
    """
    result = job.get(context, id=id)
    utils.format_output(result, context.format,
                        job.RESOURCE[:-1], job.TABLE_HEADERS)
예제 #10
0
def test_data(job_id):
    csv_file_name = create_csv_file_name()
    create_csv_file_with_headers(csv_file_name, headers)
    try:
        r = dci_job.get(context,
                        id=job_id,
                        limit=1,
                        offset=0,
                        embed="remoteci,jobstates,analytics")
        job = r.json()["job"]
        first_jobstate_failure = get_first_jobstate_failure(job["jobstates"])
        first_jobstate_failure_id = first_jobstate_failure["id"]
        files = get_files_for_jobstate(first_jobstate_failure_id)
        job = enhance_job(job, first_jobstate_failure, files)
        job_values = get_values(job)
        append_job_to_csv(csv_file_name, job_values)
    except Exception:
        # LOG.error(traceback.format_exc())
        sys.exit(1)
예제 #11
0
def test_data(job_id):
    context = build_dci_context()
    csv_file_name = create_csv_file_name()
    create_csv_file_with_headers(csv_file_name, headers)
    try:
        r = dci_job.get(context,
                        id=job_id,
                        limit=1,
                        offset=0,
                        embed="remoteci,jobstates")
        job = r.json()["job"]
        first_jobstate_failure = get_first_jobstate_failure(job["jobstates"])
        first_jobstate_failure_id = first_jobstate_failure["id"]
        files = get_files_for_jobstate(first_jobstate_failure_id)
        job = enhance_job(job, first_jobstate_failure, files)
        job_values = get_values(job)
        append_job_to_csv(csv_file_name, job_values)
    except Exception:
        return 0, False

    data = pd.read_csv(csv_file_name)
    return data, True
예제 #12
0
def push_stack_details(context, undercloud, stack_name='overcloud'):
    undercloud.yum_install(['git'])
    repo_url = 'https://github.com/goneri/tripleo-stack-dump'
    undercloud.run(
        'test -d tripleo-stack-dump || git clone ' + repo_url,
        user='******')
    undercloud.add_environment_file(
        user='******',
        filename='stackrc')
    undercloud.run(
        './tripleo-stack-dump/list_nodes_status',
        user='******')
    undercloud.run(
        './tripleo-stack-dump/tripleo-stack-dump ' + stack_name,
        user='******')
    with undercloud.open('/home/stack/tripleo-stack-dump.json') as fd:
        j = job.get(
            context,
            id=context.last_job_id).json()['job']
        job.update(
            context,
            id=context.last_job_id,
            etag=j['etag'],
            configuration=json.load(fd))
예제 #13
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            state=dict(default='present',
                       choices=['present', 'absent'],
                       type='str'),
            # Authentication related parameters
            #
            login=dict(required=False, type='str'),
            password=dict(required=False, type='str'),
            url=dict(required=False, type='str'),
            # Resource related parameters
            #
            id=dict(type='str'),
            topic=dict(required=False, type='str'),
            remoteci=dict(type='str'),
            comment=dict(type='str'),
            status=dict(type='str'),
            configuration=dict(type='dict'),
        ), )

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

    topic_list = [module.params['topic'], os.getenv('DCI_TOPIC')]
    topic = next((item for item in topic_list if item is not None), None)

    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: Delete the job matching the job id
    # Endpoint called: /jobs/<job_id> DELETE via dci_job.delete()
    #
    # If the job 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
    if module.params['state'] == 'absent':
        if not module.params['id']:
            module.fail_json(msg='id parameter is required')
        res = dci_job.get(ctx, module.params['id'])
        if res.status_code not in [400, 401, 404, 422]:
            kwargs = {
                'id': module.params['id'],
                'etag': res.json()['job']['etag']
            }
            res = dci_job.delete(ctx, **kwargs)

    # Action required: Retrieve job informations
    # Endpoint called: /jobs/<job_id> GET via dci_job.get()
    #
    # Get job informations
    elif module.params[
            'id'] and not module.params['comment'] and not module.params[
                'status'] and not module.params['configuration']:
        res = dci_job.get(ctx, module.params['id'])

    # Action required: Update an existing job
    # Endpoint called: /jobs/<job_id> PUT via dci_job.update()
    #
    # Update the job with the specified characteristics.
    elif module.params['id']:
        res = dci_job.get(ctx, module.params['id'])
        if res.status_code not in [400, 401, 404, 422]:
            kwargs = {
                'id': module.params['id'],
                'etag': res.json()['job']['etag']
            }
            if module.params['comment']:
                kwargs['comment'] = module.params['comment']
            if module.params['status']:
                kwargs['status'] = module.params['status']
            if module.params['configuration']:
                kwargs['configuration'] = module.params['configuration']
            res = dci_job.update(ctx, **kwargs)

    # Action required: Schedule a new job
    # Endpoint called: /jobs/schedule POST via dci_job.schedule()
    #
    # Schedule a new job against the DCI Control-Server
    else:
        topic_id = dci_topic.get(ctx, topic).json()['topic']['id']
        remoteci = dci_remoteci.get(ctx, module.params['remoteci']).json()
        remoteci_id = remoteci['remoteci']['id']

        res = dci_job.schedule(ctx, remoteci_id, topic_id=topic_id)
        if res.status_code not in [400, 401, 404, 422]:
            res = dci_job.get_full_data(ctx, ctx.last_job_id)

    try:
        result = res.json()
        if res.status_code == 404:
            module.fail_json(msg='The resource does not exist')
        if res.status_code in [400, 401, 422]:
            result['changed'] = False
        else:
            result['changed'] = True
    except AttributeError:
        # Enter here if new job has been schedule, return of get_full_data is already json.
        result = res
        result['changed'] = True
        result['job_id'] = ctx.last_job_id
    except:
        result = {}
        result['changed'] = True

    module.exit_json(**result)
예제 #14
0
def test_get_job_with_remoteci_context_succeeds(dci_context_remoteci, job_id):
    context = dci_context_remoteci
    r = job.get(context, job_id)
    assert r.status_code == 200
예제 #15
0
def show(context, args):
    return job.get(context, id=args.id)
예제 #16
0
def show(context, id):
    result = job.get(context, id=id)
    utils.format_output(result.json(), context.format,
                        job.RESOURCE[:-1], job.TABLE_HEADERS)