def main():
    options = options_handler()
    c = demisto.DemistoClient(options.key, options.server)
    postData = {'sort': [{'field': 'value', 'asc': True}]}
    if options.filter:
        postData['query'] = options.filter
    if options.delta:
        postData['fromDate'] = fromDate(options.delta)
    indicatorsResponse = c.req('POST', 'indicators/search', postData)
    if indicatorsResponse.status_code != 200:
        raise RuntimeError(
            'Error getting indicators data - %d (%s)' %
            (indicatorsResponse.status_code, indicatorsResponse.reason))
    indicators = indicatorsResponse.json()
    with open(options.output, 'w') as csvfile:
        writer = csv.DictWriter(csvfile,
                                fieldnames=[
                                    'Value', 'Type', 'Source', 'FirstSeen',
                                    'LastSeen', 'Score'
                                ])
        writer.writeheader()
        for i in indicators['iocObjects']:
            writer.writerow({
                'Value': i['value'],
                'Type': i['indicator_type'],
                'Source': i['source'],
                'FirstSeen': i['firstSeen'],
                'LastSeen': i['lastSeen'],
                'Score': i['score']
            })
示例#2
0
def main():
    username, password = get_username_password()

    ready_ami_list = []
    with open('./Tests/instance_ips.txt', 'r') as instance_file:
        instance_ips = instance_file.readlines()
        instance_ips = [line.strip('\n').split(":") for line in instance_ips]

    for i in range(MAX_TRIES * SLEEP_TIME):
        if len(instance_ips) > len(ready_ami_list):
            for ami_instance_name, ami_instance_ip in instance_ips:
                if ami_instance_name not in ready_ami_list:
                    c = demisto.DemistoClient(None, "https://{}".format(ami_instance_ip), username, password)
                    res = c.Login()
                    if res.status_code == 200:
                        print "[{}] {} is ready for use".format(datetime.datetime.now(), ami_instance_name)
                        ready_ami_list.append(ami_instance_name)
                    elif i % 30 == 0:  # printing the message every 30 seconds
                        print "{} is not ready yet - waiting for it to start".format(ami_instance_name)

            if len(instance_ips) > len(ready_ami_list):
                sleep(1)

        else:
            break

    if len(ready_ami_list) != len(instance_ips):
        print_error("The server is not ready :(")
        sys.exit(1)
示例#3
0
def main():
    options = options_handler()
    c = demisto.DemistoClient(options.key, options.server)
    incidents = c.SearchIncidents(options.page, 0, options.filter)
    print('using filter %s' % options.filter)
    print('Total #incidents: %d, incidents going to be updated' %
          incidents['total'])
    proceed = raw_input('OK to proceed (type y, yes or leave empty)? ')
    proceed = proceed.lower()
    if proceed == 'y' or proceed == 'yes' or proceed == '':
        if options.action == 'close':
            data = {'closeReason': options.closeReason, 'closeNotes': options.closeNotes, 'filter': {
                'page': options.page, 'size': options.size, 'query': options.filter}}
            data['all'] = True
            if options.customFields:
                data['CustomFields'] = json.loads(options.customFields)
            r = c.req('POST', 'incident/batchClose', data)
            if r.status_code != 200:
                raise RuntimeError(
                    'Error updating incidents - %d (%s)' % (r.status_code, r.reason))
            rj = json.loads(r.content)
            if rj['notUpdated'] > 0:
                print('Updated %d and could not update %d' %
                       (rj['total'], rj['notUpdated']))
            else:
                print('Updated %d incidents' % rj['total'])
        else:
            print('action "%s" is not implemented.' % options.action)
def main():
    options = options_handler()
    c = demisto.DemistoClient(options.key, options.server)
    p('using filter %s' %  options.filter)
    incidents = c.SearchIncidents(0, 10000, options.filter)
    p('Total #incidents: %d' % (incidents['total']))
    mttr = {}
    for i in incidents['data']:
        if not 'closed' in i or not i['closed']:
            continue
        created = parse_dt(i['created'])
        closed = parse_dt(i['closed'])
        d = closed - created
        field = ''
        if options.group == 'owner':
            field = i['owner'] if i['owner'] else 'dbot'
        else:
            field = i[options.group]
        if field in mttr:
            mttr[field]['mttr'] = mttr[field]['mttr'] + d.total_seconds()
            mttr[field]['incidents'] = mttr[field]['incidents'] + 1
        else:
            mttr[field] = {'mttr': d.total_seconds(), 'incidents': 1}
    with open(options.output, 'w') as csvfile:
        writer = csv.DictWriter(csvfile, fieldnames=['Field', 'Incidents', 'MTTR'])
        writer.writeheader()
        for f in mttr:
            writer.writerow({'Field': f, 'Incidents': mttr[f]['incidents'], 'MTTR': int(mttr[f]['mttr'] / mttr[f]['incidents'] / 60)})
def get_demisto_instance_and_login(server, username, password):
    c = demisto.DemistoClient(None, server, username, password)
    res = c.Login()
    if res.status_code != 200:
        print_error("Login has failed with status code " + str(res.status_code))
        sys.exit(1)

    return c
示例#6
0
def main():
    options = options_handler()
    c = demisto.DemistoClient(options.key, options.server)
    with open(options.widget, 'r') as widget_file:
        widget_data = widget_file.read()
        res = c.req('POST', 'widgets', json.loads(widget_data))
        if res.status_code != 200:
            raise RuntimeError('Error uploading widget - %d (%s)' %
                               (res.status_code, res.reason))
        p('Widget successfully uploaded')
示例#7
0
def is_correct_content_installed(username, password, ips, content_version):
    # type: (AnyStr, AnyStr, List[List], AnyStr) -> bool
    """ Checks if specific content version is installed on server list

    Args:
        username: for server connection
        password: for server connection
        ips: list with lists of [instance_name, instance_ip]
        content_version: content version that should be installed

    Returns:
        True: if all tests passed, False if one failure
    """
    method = "post"
    suffix = "/content/installed/"
    for ami_instance_name, ami_instance_ip in ips:
        d = demisto.DemistoClient(None, "https://{}".format(ami_instance_ip),
                                  username, password)
        d.Login()
        resp = d.req(method, suffix, None)
        resp_json = None
        try:
            resp_json = resp.json()
            if not isinstance(resp_json, dict):
                raise ValueError(
                    'Response from server is not a Dict, got [{}].\n'
                    'Text: {}'.format(type(resp_json), resp.text))
            release = resp_json.get("release")
            notes = resp_json.get("releaseNotes")
            installed = resp_json.get("installed")
            if not (release and content_version in release and notes
                    and installed):
                print_error(
                    "Failed install content on instance [{}]\nfound content version [{}], expected [{}]"
                    "".format(ami_instance_name, release, content_version))
                return False
            else:
                print_color(
                    "Instance [{instance_name}] content verified with version [{content_version}]"
                    .format(instance_name=ami_instance_name,
                            content_version=release), LOG_COLORS.GREEN)
        except ValueError as exception:
            err_msg = "Failed to verify content version on server [{}]\n" \
                      "Error: [{}]\n".format(ami_instance_name, str(exception))
            if resp_json is not None:
                err_msg += "Server response: {}".format(resp_json)
            print_error(err_msg)
            return False
    print_color(
        "Content was installed successfully on all of the instances! :)",
        LOG_COLORS.GREEN)
    return True
def main():
    options = options_handler()
    c = demisto.DemistoClient(options.key, options.server)
    labels = None
    if (options.labels is not None) and len(options.labels) > 0:
        labels = json.loads(options.labels)
    fields = None
    if (options.custom_fields is not None) and len(options.custom_fields) > 0:
        fields = json.loads(options.custom_fields)
    r = c.CreateIncident(options.name, options.type,
                         severity_to_number(options.severity), options.owner,
                         labels, options.details, fields)
    print(r)
def main():
    options = options_handler()
    c = demisto.DemistoClient(options.key, options.server)
    integrationsResponse = c.req('GET', 'settings/integration-commands', None)
    if integrationsResponse.status_code != 200:
        raise RuntimeError(
            'Error getting integration data - %d (%s)' %
            (integrationsResponse.status_code, integrationsResponse.reason))
    integrations = sorted(
        integrationsResponse.json(),
        key=lambda m: m['category'].lower() + '---' + m['name'].lower())
    with open(options.output, 'w') as csvfile:
        writer = csv.DictWriter(csvfile,
                                fieldnames=[
                                    'Category', 'Product',
                                    'ProductDescription', 'Command',
                                    'CommandDescription'
                                ])
        writer.writeheader()
        for m in integrations:
            if 'commands' in m:
                for cmd in m['commands']:
                    writer.writerow({
                        'Category':
                        m['category'],
                        'Product':
                        m['display'],
                        'ProductDescription':
                        m['description'].encode('ascii', 'ignore'),
                        'Command':
                        cmd['name'],
                        'CommandDescription':
                        cmd['description'].encode('ascii', 'ignore')
                    })
            else:
                writer.writerow({
                    'Category':
                    m['category'],
                    'Product':
                    m['display'],
                    'ProductDescription':
                    m['description'].encode('ascii', 'ignore')
                })
示例#10
0
def main():
    options = options_handler()
    username = options.user
    password = options.password
    server = options.server
    conf_path = options.conf
    secret_conf_path = options.secret
    is_nightly = options.nightly

    if not (username and password and server):
        print_error('You must provide server user & password arguments')
        sys.exit(1)

    c = demisto.DemistoClient(None, server, username, password)
    res = c.Login()
    if res.status_code is not 200:
        print_error("Login has failed with status code " +
                    str(res.status_code))
        sys.exit(1)

    with open(conf_path) as data_file:
        conf = json.load(data_file)

    secret_conf = None
    if secret_conf_path:
        with open(secret_conf_path) as data_file:
            secret_conf = json.load(data_file)

    tests = conf['tests']

    secret_params = secret_conf['integrations'] if secret_conf else []

    if not tests or len(tests) is 0:
        print('no integrations are configured for test')
        return

    succeed_playbooks = []
    failed_playbooks = []
    for t in tests:
        test_options = {
            'timeout':
            t['timeout'] if 'timeout' in t else conf.get('testTimeout', 30),
            'interval':
            conf.get('testInterval', 10)
        }

        playbook_id = t['playbookID']

        integrations_conf = t.get('integrations', [])

        if not isinstance(integrations_conf, list):
            integrations_conf = [integrations_conf]

        integrations = []
        for integration in integrations_conf:
            if type(integration) is dict:
                # dict description
                integrations.append({
                    'name': integration.get('name'),
                    'byoi': integration.get('byoi', True),
                    'params': {}
                })
            else:
                # string description
                integrations.append({
                    'name': integration,
                    'byoi': True,
                    'params': {}
                })

        for integration in integrations:
            integration_params = [
                item for item in secret_params
                if item["name"] == integration['name']
            ]
            if integration_params:
                integration['params'] = integration_params[0].get('params', {})

        test_message = 'playbook: ' + playbook_id
        if integrations:
            integrations_names = [
                integration['name'] for integration in integrations
            ]
            test_message = test_message + ' with integration(s): ' + ','.join(
                integrations_names)

        print '------ Test %s start ------' % (test_message, )

        nightly_test = t.get('nightly', False)

        skip_test = True if nightly_test and not is_nightly else False

        if skip_test:
            print 'Skip test'
        else:
            # run test
            succeed = test_integration(c, integrations, playbook_id,
                                       test_options)

            # use results
            if succeed:
                print 'PASS: %s succeed' % (test_message, )
                succeed_playbooks.append(playbook_id)
            else:
                print 'Failed: %s failed' % (test_message, )
                failed_playbooks.append(playbook_id)

        print '------ Test %s end ------' % (test_message, )

    print_test_summary(succeed_playbooks, failed_playbooks)
    if len(failed_playbooks):
        sys.exit(1)
示例#11
0
def execute_testing(server,
                    server_ip,
                    server_version,
                    server_numeric_version,
                    is_ami=True):
    print("Executing tests with the server {} - and the server ip {}".format(
        server, server_ip))

    options = options_handler()
    username = options.user
    password = options.password
    conf_path = options.conf
    secret_conf_path = options.secret
    is_nightly = options.nightly
    slack = options.slack
    circle_ci = options.circleci
    build_number = options.buildNumber
    build_name = options.buildName

    if not (username and password and server):
        print_error('You must provide server user & password arguments')
        sys.exit(1)

    c = demisto.DemistoClient(None, server, username, password)
    res = c.Login()
    if res.status_code != 200:
        print_error("Login has failed with status code " +
                    str(res.status_code))
        sys.exit(1)

    demisto_api_key = generate_demisto_api_key(c)

    conf, secret_conf = load_conf_files(conf_path, secret_conf_path)

    default_test_timeout = conf.get('testTimeout', 30)

    tests = conf['tests']
    skipped_tests_conf = conf['skipped_tests']
    nightly_integrations = conf['nigthly_integrations']
    skipped_integrations_conf = conf['skipped_integrations']
    unmockable_integrations = conf['unmockable_integrations']

    secret_params = secret_conf['integrations'] if secret_conf else []

    filtered_tests, is_filter_configured, run_all_tests = extract_filtered_tests(
    )
    if is_filter_configured and not run_all_tests:
        is_nightly = True

    if not tests or len(tests) == 0:
        print('no integrations are configured for test')
        return

    proxy = None
    if is_ami:
        ami = AMIConnection(server_ip)
        ami.clone_mock_data()
        proxy = MITMProxy(c, server_ip)

    failed_playbooks = []
    succeed_playbooks = []
    skipped_tests = set([])
    skipped_integration = set([])

    disable_all_integrations(c)

    if is_ami:
        # move all mock tests to the top of the list
        mock_tests, mockless_tests = organize_tests(tests,
                                                    unmockable_integrations,
                                                    skipped_integrations_conf,
                                                    nightly_integrations)
    else:  # In case of a non AMI run we don't want to use the mocking mechanism
        mockless_tests = tests

    # first run the mock tests to avoid mockless side effects in container
    if is_ami and mock_tests:
        proxy.configure_proxy_in_demisto(proxy.ami.docker_ip + ':' +
                                         proxy.PROXY_PORT)
        for t in mock_tests:
            run_test_scenario(t, c, proxy, default_test_timeout,
                              skipped_tests_conf, nightly_integrations,
                              skipped_integrations_conf, skipped_integration,
                              is_nightly, run_all_tests, is_filter_configured,
                              filtered_tests, skipped_tests, demisto_api_key,
                              secret_params, failed_playbooks,
                              unmockable_integrations, succeed_playbooks,
                              slack, circle_ci, build_number, server,
                              build_name, server_numeric_version)

        print("\nRunning mock-disabled tests")
        proxy.configure_proxy_in_demisto('')
        print("Restarting demisto service")
        restart_demisto_service(ami, c)
        print("Demisto service restarted\n")

    for t in mockless_tests:
        run_test_scenario(t, c, proxy, default_test_timeout,
                          skipped_tests_conf, nightly_integrations,
                          skipped_integrations_conf, skipped_integration,
                          is_nightly, run_all_tests, is_filter_configured,
                          filtered_tests, skipped_tests, demisto_api_key,
                          secret_params, failed_playbooks,
                          unmockable_integrations, succeed_playbooks, slack,
                          circle_ci, build_number, server, build_name,
                          server_numeric_version, is_ami)

    print_test_summary(succeed_playbooks, failed_playbooks, skipped_tests,
                       skipped_integration, unmockable_integrations, proxy,
                       is_ami)

    create_result_files(failed_playbooks, skipped_integration, skipped_tests)

    if is_ami and build_name == 'master':
        print("Pushing new/updated mock files to mock git repo.")
        ami.upload_mock_files(build_name, build_number)

    if len(failed_playbooks):
        file_path = "./Tests/is_build_failed_{}.txt".format(
            server_version.replace(' ', ''))
        with open(file_path, "w") as is_build_failed_file:
            is_build_failed_file.write('Build failed')

        sys.exit(1)
示例#12
0
def main():
    options = options_handler()
    username = options.user
    password = options.password
    server = options.server
    conf_path = options.conf
    secret_conf_path = options.secret
    is_nightly = options.nightly

    if not (username and password and server):
        print_error('You must provide server user & password arguments')
        sys.exit(1)

    c = demisto.DemistoClient(None, server, username, password)
    res = c.Login()
    if res.status_code is not 200:
        print_error("Login has failed with status code " +
                    str(res.status_code))
        sys.exit(1)

    demisto_api_key = ''.join(
        random.choice(string.ascii_letters + string.digits) for _ in range(32))
    apikey_json = {'name': 'test_apikey', 'apikey': demisto_api_key}
    c.req('POST', '/apikeys', apikey_json)

    with open(conf_path) as data_file:
        conf = json.load(data_file)

    secret_conf = None
    if secret_conf_path:
        with open(secret_conf_path) as data_file:
            secret_conf = json.load(data_file)

    tests = conf['tests']
    skipped_tests_conf = conf['skipped_tests']
    skipped_integrations_conf = conf['skipped_integrations']

    secret_params = secret_conf['integrations'] if secret_conf else []

    with open(FILTER_CONF, 'r') as filter_file:
        filterd_tests = filter_file.readlines()
        filterd_tests = [line.strip('\n') for line in filterd_tests]
        is_filter_configured = True if filterd_tests else False

    if not tests or len(tests) is 0:
        print('no integrations are configured for test')
        return

    succeed_playbooks = []
    failed_playbooks = []
    skipped_tests = []
    for t in tests:
        playbook_id = t['playbookID']
        integrations_conf = t.get('integrations', [])

        if playbook_id in skipped_tests_conf:
            skipped_tests.append(playbook_id)
            continue

        if is_filter_configured and not is_nightly and playbook_id not in filterd_tests:
            continue

        test_options = {
            'timeout':
            t['timeout'] if 'timeout' in t else conf.get('testTimeout', 30),
            'interval':
            conf.get('testInterval', 10)
        }

        if not isinstance(integrations_conf, list):
            integrations_conf = [integrations_conf]

        integrations = []
        has_skipped_integration = False
        for integration in integrations_conf:
            if type(integration) is dict:
                if integration.get('name') in skipped_integrations_conf:
                    has_skipped_integration = True
                    break

                # dict description
                integrations.append({
                    'name': integration.get('name'),
                    'byoi': integration.get('byoi', True),
                    'params': {}
                })
            else:
                if integration in skipped_integrations_conf:
                    has_skipped_integration = True
                    break

                # string description
                integrations.append({
                    'name': integration,
                    'byoi': True,
                    'params': {}
                })

        if has_skipped_integration:
            continue

        for integration in integrations:
            integration_params = [
                item for item in secret_params
                if item["name"] == integration['name']
            ]
            if integration_params:
                integration['params'] = integration_params[0].get('params', {})
            elif 'Demisto REST API' == integration['name']:
                integration['params'] = {
                    'url': 'https://localhost',
                    'apikey': demisto_api_key,
                    'insecure': True,
                }

        test_message = 'playbook: ' + playbook_id
        if integrations:
            integrations_names = [
                integration['name'] for integration in integrations
            ]
            test_message = test_message + ' with integration(s): ' + ','.join(
                integrations_names)

        print '------ Test %s start ------' % (test_message, )

        nightly_test = t.get('nightly', False)

        skip_test = True if nightly_test and not is_nightly else False

        if skip_test:
            print 'Skip test'
        else:
            # run test
            succeed = test_integration(c, integrations, playbook_id,
                                       test_options)

            # use results
            if succeed:
                print 'PASS: %s succeed' % (test_message, )
                succeed_playbooks.append(playbook_id)
            else:
                print 'Failed: %s failed' % (test_message, )
                failed_playbooks.append(playbook_id)

        print '------ Test %s end ------' % (test_message, )

    print_test_summary(succeed_playbooks, failed_playbooks, skipped_tests)
    os.remove(FILTER_CONF)
    if len(failed_playbooks):
        sys.exit(1)
示例#13
0
def main():
    options = options_handler()
    username = options.user
    password = options.password
    server = options.server
    conf_path = options.conf
    secret_conf_path = options.secret
    is_nightly = options.nightly
    slack = options.slack
    CircleCI = options.circleci
    buildNumber = options.buildNumber

    if not (username and password and server):
        print_error('You must provide server user & password arguments')
        sys.exit(1)

    c = demisto.DemistoClient(None, server, username, password)
    res = c.Login()
    if res.status_code is not 200:
        print_error("Login has failed with status code " +
                    str(res.status_code))
        sys.exit(1)

    demisto_api_key = generate_demisto_api_key(c)

    conf, secret_conf = load_conf_files(conf_path, secret_conf_path)

    tests = conf['tests']
    skipped_tests_conf = conf['skipped_tests']
    skipped_integrations_conf = conf['skipped_integrations']

    secret_params = secret_conf['integrations'] if secret_conf else []

    filterd_tests, is_filter_configured = extract_filtered_tests()
    if is_filter_configured:
        is_nightly = True

    if not tests or len(tests) is 0:
        print('no integrations are configured for test')
        return

    skipped_tests = []
    failed_playbooks = []
    succeed_playbooks = []
    skipped_integration = []
    for t in tests:
        playbook_id = t['playbookID']
        nightly_test = t.get('nightly', False)
        integrations_conf = t.get('integrations', [])
        skip_test = True if nightly_test and not is_nightly else False

        test_message = 'playbook: ' + playbook_id

        test_options = {
            'timeout':
            t['timeout'] if 'timeout' in t else conf.get('testTimeout', 30),
            'interval':
            conf.get('testInterval', 10)
        }

        if not isinstance(integrations_conf, list):
            integrations_conf = [integrations_conf]

        has_skipped_integration, integrations = collect_integrations(
            integrations_conf, is_filter_configured, skipped_integration,
            skipped_integrations_conf)

        # Skip nightly test
        if skip_test:
            print '------ Test %s start ------' % (test_message, )
            print 'Skip test'
            print '------ Test %s end ------' % (test_message, )

            continue

        # Skip filtered test
        if is_filter_configured and playbook_id not in filterd_tests:
            continue

        # Skip bad test
        if playbook_id in skipped_tests_conf:
            skipped_tests.append(playbook_id)
            continue

        # Skip integration
        if has_skipped_integration:
            continue

        set_integration_params(demisto_api_key, integrations, secret_params)
        test_message = update_test_msg(integrations, test_message)

        run_test(c, failed_playbooks, integrations, playbook_id,
                 succeed_playbooks, test_message, test_options, slack,
                 CircleCI, buildNumber, server)

    print_test_summary(succeed_playbooks, failed_playbooks, skipped_tests,
                       skipped_integration)

    create_result_files(failed_playbooks, skipped_integration, skipped_tests)
    os.remove(FILTER_CONF)

    if len(failed_playbooks):
        sys.exit(1)
示例#14
0
def main():
    options = options_handler()
    username = options.user
    password = options.password
    server = options.server
    conf_path = options.conf
    secret_conf_path = options.secret
    is_nightly = options.nightly
    slack = options.slack
    CircleCI = options.circleci
    buildNumber = options.buildNumber
    build_name = options.buildName

    if not (username and password and server):
        print_error('You must provide server user & password arguments')
        sys.exit(1)

    c = demisto.DemistoClient(None, server, username, password)
    res = c.Login()
    if res.status_code != 200:
        print_error("Login has failed with status code " + str(res.status_code))
        sys.exit(1)

    demisto_api_key = generate_demisto_api_key(c)

    conf, secret_conf = load_conf_files(conf_path, secret_conf_path)

    default_test_timeout = conf.get('testTimeout', 30)

    tests = conf['tests']
    skipped_tests_conf = conf['skipped_tests']
    nightly_integrations = conf['nigthly_integrations']
    skipped_integrations_conf = conf['skipped_integrations']

    secret_params = secret_conf['integrations'] if secret_conf else []

    filterd_tests, is_filter_configured, run_all_tests = extract_filtered_tests()
    if is_filter_configured and not run_all_tests:
        is_nightly = True

    if not tests or len(tests) == 0:
        print('no integrations are configured for test')
        return

    failed_playbooks = []
    succeed_playbooks = []
    skipped_tests = set([])
    skipped_integration = set([])
    for t in tests:
        playbook_id = t['playbookID']
        nightly_test = t.get('nightly', False)
        integrations_conf = t.get('integrations', [])
        instance_names_conf = t.get('instance_names', [])

        test_message = 'playbook: ' + playbook_id

        test_options = {
            'timeout': t.get('timeout', default_test_timeout)
        }

        if not isinstance(integrations_conf, list):
            integrations_conf = [integrations_conf, ]

        if not isinstance(instance_names_conf, list):
            instance_names_conf = [instance_names_conf, ]

        has_skipped_integration, integrations, is_nightly_integration = collect_integrations(
            integrations_conf, skipped_integration, skipped_integrations_conf, nightly_integrations)

        skip_nightly_test = True if (nightly_test or is_nightly_integration) and not is_nightly else False

        # Skip nightly test
        if skip_nightly_test:
            print '------ Test %s start ------' % (test_message,)
            print 'Skip test'
            print '------ Test %s end ------' % (test_message,)

            continue

        if not run_all_tests:
            # Skip filtered test
            if is_filter_configured and playbook_id not in filterd_tests:
                continue

        # Skip bad test
        if playbook_id in skipped_tests_conf.keys():
            skipped_tests.add("{0} - reason: {1}".format(playbook_id, skipped_tests_conf[playbook_id]))
            continue

        # Skip integration
        if has_skipped_integration:
            continue

        are_params_set = set_integration_params(demisto_api_key, integrations,
                                                secret_params, instance_names_conf, playbook_id)
        if not are_params_set:
            failed_playbooks.append(playbook_id)
            continue

        test_message = update_test_msg(integrations, test_message)

        run_test(c, failed_playbooks, integrations, playbook_id,
                 succeed_playbooks, test_message, test_options, slack, CircleCI,
                 buildNumber, server, build_name)

    print_test_summary(succeed_playbooks, failed_playbooks, skipped_tests, skipped_integration)

    create_result_files(failed_playbooks, skipped_integration, skipped_tests)
    os.remove(FILTER_CONF)

    if len(failed_playbooks):
        with open("./Tests/is_build_failed.txt", "w") as is_build_failed_file:
            is_build_failed_file.write('Build failed')

        sys.exit(1)
示例#15
0
def execute_testing(server, server_ip, server_version):
    options = options_handler()
    username = options.user
    password = options.password
    conf_path = options.conf
    secret_conf_path = options.secret
    is_nightly = options.nightly
    slack = options.slack
    circle_ci = options.circleci
    build_number = options.buildNumber
    build_name = options.buildName

    if not (username and password and server):
        print_error('You must provide server user & password arguments')
        sys.exit(1)

    c = demisto.DemistoClient(None, server, username, password)
    res = c.Login()
    if res.status_code != 200:
        print_error("Login has failed with status code " +
                    str(res.status_code))
        sys.exit(1)

    demisto_api_key = generate_demisto_api_key(c)

    conf, secret_conf = load_conf_files(conf_path, secret_conf_path)

    default_test_timeout = conf.get('testTimeout', 30)

    tests = conf['tests']
    skipped_tests_conf = conf['skipped_tests']
    nightly_integrations = conf['nigthly_integrations']
    skipped_integrations_conf = conf['skipped_integrations']
    unmockable_integrations = conf['unmockable_integrations']

    secret_params = secret_conf['integrations'] if secret_conf else []

    filtered_tests, is_filter_configured, run_all_tests = extract_filtered_tests(
    )
    if is_filter_configured and not run_all_tests:
        is_nightly = True

    if not tests or len(tests) == 0:
        print('no integrations are configured for test')
        return

    ami = AMIConnection(server_ip)
    ami.clone_mock_data()
    proxy = MITMProxy(c, server_ip)

    failed_playbooks = []
    succeed_playbooks = []
    skipped_tests = set([])
    skipped_integration = set([])

    # move all mock tests to the top of the list
    tests = organize_tests(tests, unmockable_integrations)

    for t in tests:
        playbook_id = t['playbookID']
        nightly_test = t.get('nightly', False)
        integrations_conf = t.get('integrations', [])
        instance_names_conf = t.get('instance_names', [])

        test_message = 'playbook: ' + playbook_id

        test_options = {'timeout': t.get('timeout', default_test_timeout)}

        if not isinstance(integrations_conf, list):
            integrations_conf = [
                integrations_conf,
            ]

        if not isinstance(instance_names_conf, list):
            instance_names_conf = [
                instance_names_conf,
            ]

        has_skipped_integration, integrations, is_nightly_integration = collect_integrations(
            integrations_conf, skipped_integration, skipped_integrations_conf,
            nightly_integrations)

        skip_nightly_test = True if (nightly_test or is_nightly_integration
                                     ) and not is_nightly else False

        # Skip nightly test
        if skip_nightly_test:
            print '------ Test %s start ------' % (test_message, )
            print 'Skip test'
            print '------ Test %s end ------' % (test_message, )

            continue

        if not run_all_tests:
            # Skip filtered test
            if is_filter_configured and playbook_id not in filtered_tests:
                continue

        # Skip bad test
        if playbook_id in skipped_tests_conf.keys():
            skipped_tests.add("{0} - reason: {1}".format(
                playbook_id, skipped_tests_conf[playbook_id]))
            continue

        # Skip integration
        if has_skipped_integration:
            continue

        are_params_set = set_integration_params(demisto_api_key, integrations,
                                                secret_params,
                                                instance_names_conf,
                                                playbook_id)
        if not are_params_set:
            failed_playbooks.append(playbook_id)
            continue

        test_message = update_test_msg(integrations, test_message)

        run_test(c, proxy, failed_playbooks, integrations,
                 unmockable_integrations, playbook_id, succeed_playbooks,
                 test_message, test_options, slack, circle_ci, build_number,
                 server, build_name)

    print_test_summary(succeed_playbooks, failed_playbooks, skipped_tests,
                       skipped_integration, unmockable_integrations, proxy)

    create_result_files(failed_playbooks, skipped_integration, skipped_tests)

    if build_name == 'master':
        print "Pushing new/updated mock files to mock git repo."
        ami.upload_mock_files(build_name, build_number)

    if len(failed_playbooks):
        file_path = "./Tests/is_build_failed_{}.txt".format(
            server_version.replace(' ', ''))
        with open(file_path, "w") as is_build_failed_file:
            is_build_failed_file.write('Build failed')

        sys.exit(1)
示例#16
0
def main():
    options = options_handler()
    username = options.user
    password = options.password
    server = options.server
    conf_path = options.conf
    secret_conf_path = options.secret
    is_nightly = options.nightly

    if not (username and password and server):
        print_error('You must provide server user & password arguments')
        exit(1)

    c = demisto.DemistoClient(None, server, username, password)
    res = c.Login()
    if res.status_code is not 200:
        print_error("Login has failed with status code " +
                    str(res.status_code))
        exit(1)

    with open(conf_path) as data_file:
        conf = json.load(data_file)

    secret_conf = None
    if secret_conf_path:
        with open(secret_conf_path) as data_file:
            secret_conf = json.load(data_file)

    integrations = conf['integrations']

    secret_integrations = secret_conf['integrations'] if secret_conf else []

    if not integrations or len(integrations) is 0:
        print('no integrations are configured for test')
        return

    succeed_playbooks = []
    failed_playbooks = []
    for integration in integrations:
        test_options = {
            'timeout':
            integration['timeout'] if 'timeout' in integration else conf.get(
                'testTimeout', 30),
            'interval':
            conf.get('testInterval', 10)
        }

        integration_name = integration.get('name', None)
        playbook_id = integration['playbookID']
        if 'params' in integration:
            integration_params = integration['params']
        else:
            # get from secret conf
            secret_integration_match = [
                item for item in secret_integrations
                if item["name"] == integration_name
            ]
            if len(secret_integration_match) > 0:
                integration_params = secret_integration_match[0].get('params')
            else:
                integration_params = {}

        if integration_name:
            test_message = 'integration: ' + integration_name + ' with playbook: ' + playbook_id
        else:
            test_message = 'playbook: ' + playbook_id
        print '------ Test %s start ------' % (test_message, )

        nightly_test = integration.get('nightly', False)

        is_byoi = integration.get('byoi', True)

        skip_test = True if nightly_test and not is_nightly else False

        if skip_test:
            print 'Skip test'
        else:
            # run test
            succeed = test_integration(c, integration_name, integration_params,
                                       playbook_id, is_byoi, test_options)

            # use results
            if succeed:
                print 'PASS: %s succeed' % (test_message, )
                succeed_playbooks.append(playbook_id)
            else:
                print 'Failed: %s failed' % (test_message, )
                failed_playbooks.append(playbook_id)

        print '------ Test %s end ------' % (test_message, )

    print_test_summary(succeed_playbooks, failed_playbooks)
    if len(failed_playbooks):
        sys.exit(1)