def test_authentication_failure():
    responses.add(
        method=responses.GET,
        url=
        'https://acme.saas.appdynamics.com:443/controller/auth?action=login',
        json={},
        status=401)
    controllerService = AppDService('acme.saas.appdynamics.com', 443, True,
                                    'foo', 'bar', 'baz')
    result = controllerService.login_to_controller()

    assert result.error.msg == 'Controller login failed with 401.'
def test_failure():
    responses.add(
        method=responses.GET,
        url=
        'https://acme.saas.appdynamics.com:443/controller/rest/applications?output=json',
        json={},
        status=500)
    controllerService = AppDService('acme.saas.appdynamics.com', 443, True,
                                    'acme', 'bar', 'hunter1')
    result = controllerService.get_applications()

    assert result.error.msg == 500
Exemple #3
0
def test_login_failure():
    responses.add(
        method=responses.GET,
        url=
        'https://acme.saas.appdynamics.com:443/controller/auth?action=login',
        json={},
        status=401)
    controllerService = AppDService('acme.saas.appdynamics.com', 443, True,
                                    'acme', 'bar', 'hunter1')
    result = controllerService.get_synthetic_jobs('foo')

    assert result.error.msg == 'Controller login failed with 401.'
def test_success():
    responses.add(
        method=responses.GET,
        url=
        'https://acme.saas.appdynamics.com:443/controller/rest/applications?output=json',
        json={'applicationName': 'foo'},
        status=200)
    controllerService = AppDService('acme.saas.appdynamics.com', 443, True,
                                    'acme', 'bar', 'hunter1')
    result = controllerService.get_applications()

    assert result.error is None
    assert result.data['applicationName'] == 'foo'
def test_success():
    responses.add(
        method=responses.GET,
        url=
        'https://acme.saas.appdynamics.com:443/controller/auth?action=login',
        json={},
        headers={'Set-Cookie': 'JSESSIONID=foo; X-CSRF-TOKEN=bar;'},
        status=200)
    controllerService = AppDService('acme.saas.appdynamics.com', 443, True,
                                    'acme', 'bar', 'hunter1')
    result = controllerService.login_to_controller()

    assert result.error is None
    assert controllerService.controller.jsessionid == 'foo'
    assert controllerService.controller.xcsrftoken == 'bar'
def test_no_xcsrftoken():
    responses.add(
        method=responses.GET,
        url=
        'https://acme.saas.appdynamics.com:443/controller/auth?action=login',
        json={},
        headers={'Set-Cookie': 'JSESSIONID=foo;'},
        status=200)
    controllerService = AppDService('acme.saas.appdynamics.com', 443, True,
                                    'foo', 'bar', 'baz')
    result = controllerService.login_to_controller()

    assert result.error.msg == 'Valid authentication headers not cached from previous login call. Please verify credentials.'
    assert controllerService.controller.jsessionid == 'foo'
    assert controllerService.controller.xcsrftoken is None
Exemple #7
0
def test_failure():
    responses.add(
        method=responses.GET,
        url=
        'https://acme.saas.appdynamics.com:443/controller/auth?action=login',
        json={},
        headers={'Set-Cookie': 'JSESSIONID=foo; X-CSRF-TOKEN=bar;'},
        status=200)
    responses.add(
        method=responses.POST,
        url=
        'https://acme.saas.appdynamics.com:443/controller/restui/synthetic/schedule/getJobList/foo?output=json',
        json={},
        status=500)
    controllerService = AppDService('acme.saas.appdynamics.com', 443, True,
                                    'acme', 'bar', 'hunter1')
    result = controllerService.get_synthetic_jobs('foo')

    assert result.error.msg == 500
Exemple #8
0
def test_success():
    responses.add(
        method=responses.GET,
        url=
        'https://acme.saas.appdynamics.com:443/controller/auth?action=login',
        json={},
        headers={'Set-Cookie': 'JSESSIONID=foo; X-CSRF-TOKEN=bar;'},
        status=200)
    responses.add(
        method=responses.POST,
        url=
        'https://acme.saas.appdynamics.com:443/controller/restui/synthetic/schedule/getJobList/foo?output=json',
        json={
            'config': {
                'script': 'foo',
                'browserCodes': 'bar',
                'locationCodes': 'baz',
                'timeoutSeconds': 'foo',
                'rate': {
                    'value': 'bar',
                    'unit': 'baz',
                }
            }
        },
        status=200)
    controllerService = AppDService('acme.saas.appdynamics.com', 443, True,
                                    'acme', 'bar', 'hunter1')
    result = controllerService.get_synthetic_jobs('foo')

    assert result.error is None
    assert result.data['config']['script'] == 'foo'
    assert result.data['config']['browserCodes'] == 'bar'
    assert result.data['config']['locationCodes'] == 'baz'
    assert result.data['config']['timeoutSeconds'] == 'foo'
    assert result.data['config']['rate']['value'] == 'bar'
    assert result.data['config']['rate']['unit'] == 'baz'
Exemple #9
0
def upload(host: str, port: int, ssl: bool, account: str, username: str,
           password: str, only_successful_scripts: bool, overwrite: bool):
    logging.info(f'-----Launching upload step-----')

    controllerService = AppDService(host, port, ssl, account, username,
                                    password)
    if controllerService.login_to_controller().error is not None:
        return

    with open('input/mapping.csv') as f:
        allMappings = [{k: str(v)
                        for k, v in row.items()}
                       for row in csv.DictReader(f, skipinitialspace=True)]

    # validate mapping file
    for file in glob.iglob('output/*.py'):
        filename = Path(file).stem
        if not any(entry['jobName'] == filename for entry in allMappings):
            logging.error(f'{filename} not found in mapping file, exiting')
            return

    # pre-load our report
    if only_successful_scripts:
        with open('output/report.csv') as f:
            report = [{k: str(v)
                       for k, v in row.items()}
                      for row in csv.DictReader(f, skipinitialspace=True)]

    for file in glob.iglob('output/*.py'):
        filename = Path(file).stem

        # only upload scripts which ranSuccessfully
        shouldUpload = True
        if only_successful_scripts:
            reportMapping = next(entry for entry in report
                                 if entry['jobName'] == filename)
            if not json.loads(reportMapping['ranSuccessfully'].lower()):
                shouldUpload = False
        if shouldUpload:
            code = open(file).read()
            jobMap = next(mapping for mapping in allMappings
                          if mapping['jobName'] == filename)
            if overwrite:
                response = controllerService.get_synthetic_jobs(
                    jobMap['eumApplicationId'])
                if response.error is not None:
                    logging.error(response.error)
                    return
                uploadedJobs = response.data['jobListDatas']

                uploadedJob = next(
                    (job for job in uploadedJobs
                     if job['config']['description'] == filename), None)
                if uploadedJob is not None:
                    response = controllerService.overwrite_synthetic_job(
                        jobMap, filename, code, uploadedJob)
                else:
                    response = controllerService.create_synthetic_job(
                        jobMap, filename, code)
            else:
                response = controllerService.create_synthetic_job(
                    jobMap, filename, code)
            if response.error is None:
                logging.info(
                    f'Successfully uploaded synthetic script {filename}')
            elif response.error.msg == 'Already Exists':
                logging.info(
                    f'Synthetic script {filename} already exists. You can optionally run with --overwrite.'
                )
            else:
                logging.error(
                    f'Failed to upload synthetic script {filename} with error: {response.error}'
                )
        else:
            logging.info(
                f'Only uploading validated scripts, skipping {filename}')