Exemple #1
0
def test_convert_contribution_zip_updated_pack(get_content_path_mock, get_python_version_mock, tmp_path, mocker):
    """
    Create a fake contribution zip file and test that it is converted to a Pack correctly.
    The pack already exists, checking the update flow.

    Args:
        get_content_path_mock (MagicMock): Patch of the 'get_content_path' function to return the fake repo directory
            used in the test
        get_python_version_mock (MagicMock): Patch of the 'get_python_version' function to return the "3.7"
        tmp_path (fixture): Temporary Path used for the unit test and cleaned up afterwards

    Scenario: Simulate converting a contribution zip file.

    Given
    - A contribution zip file
    - The zipfile contains a unified integration file
    When
    - Converting the zipfile to a valid Pack structure
    - The contribution is an update to an existing pack
    Then
    - Ensure integration are componentized and in valid directory structure
    - Ensure that readme file has not been changed.

    """
    mocker.patch.object(GitUtil, '__init__', return_value=None)
    mocker.patch.object(GitUtil, 'added_files', return_value=set())
    mocker.patch.object(GitUtil, 'modified_files', return_value=set())
    # Create all Necessary Temporary directories
    # create temp directory for the repo
    repo_dir = tmp_path / 'content_repo'
    repo_dir.mkdir()
    get_content_path_mock.return_value = repo_dir
    get_python_version_mock.return_value = 3.7
    # create temp target dir in which we will create all the TestSuite content items to use in the contribution zip and
    # that will be deleted after
    target_dir = repo_dir / 'target_dir'
    target_dir.mkdir()
    # create temp directory in which the contribution zip will reside
    contribution_zip_dir = tmp_path / 'contrib_zip'
    contribution_zip_dir.mkdir()
    # Create fake content repo and contribution zip
    repo = Repo(repo_dir)
    pack = repo.create_pack('TestPack')
    integration = pack.create_integration('integration0')
    integration.create_default_integration()
    contrib_zip = Contribution(target_dir, 'ContribTestPack', repo)
    contrib_zip.create_zip(contribution_zip_dir)
    # target_dir should have been deleted after creation of the zip file
    assert not target_dir.exists()
    name = 'Test Pack'
    contribution_path = contrib_zip.created_zip_filepath
    description = 'test pack description here'
    author = 'Octocat Smith'
    contrib_converter_inst = ContributionConverter(
        name=name, contribution=contribution_path, description=description, author=author, create_new=False,
        no_pipenv=True)
    contrib_converter_inst.convert_contribution_to_pack()
    converted_pack_path = repo_dir / 'Packs' / 'TestPack'
    assert converted_pack_path.exists()
    integrations_path = converted_pack_path / 'Integrations'
    sample_integration_path = integrations_path / 'integration0'
    integration_yml = sample_integration_path / 'integration0.yml'
    integration_py = sample_integration_path / 'integration0.py'
    integration_description = sample_integration_path / 'integration0_description.md'
    integration_image = sample_integration_path / 'integration0_image.png'
    integration_readme_md = sample_integration_path / 'README.md'
    unified_yml = integrations_path / 'integration-integration0.yml'
    unified_yml_in_sample = sample_integration_path / 'integration-integration0.yml'
    integration_files = [integration_yml, integration_py, integration_description, integration_image,
                         integration_readme_md]
    for integration_file in integration_files:
        assert integration_file.exists()
    # In a new pack that part will exist.

    assert not unified_yml.exists()
    assert not unified_yml_in_sample.exists()
    def test_excluded_items_contain_aliased_field(mocker, repo: Repo):
        """
        Given
            - An xsoar-only incident field.
            - An incident field with alias of the first field.
            - A mapper using the xsoar-only field.

        When
            creating an ID set for marketplacev2

        Then
            the ID set should not filter the mapper.

        """
        host = {
            'id': 'incident_host',
            'name': 'Host',
            'cliName': 'host',
            'marketplaces': ['xsoar'],
        }
        common_types_pack = repo.create_pack('CommonTypes')
        common_types_pack.pack_metadata.write_json({
            'name':
            'CommonTypes',
            'currentVersion':
            '1.0.0',
            'marketplaces': ['xsoar', 'marketplacev2'],
        })
        common_types_pack.create_incident_field('Host', content=host)
        common_types_pack.create_incident_type('IncidentType')
        host_name = {
            'id': 'incident_hostname',
            'name': 'Host Name',
            'cliName': 'hostname',
            'marketplaces': ['xsoar', 'marketplacev2'],
            'Aliases': [{
                'cliName': 'host',
                'type': 'shortText',
                'name': 'Host',
            }]
        }

        core_alert_fields_pack = repo.create_pack('CoreAlertFields')
        core_alert_fields_pack.pack_metadata.write_json({
            'name':
            'CoreAlertFields',
            'currentVersion':
            '1.0.0',
            'marketplaces': ['marketplacev2'],
        })
        core_alert_fields_pack.create_incident_field('HostName',
                                                     content=host_name)
        mapper = {
            'id': 'Mapper',
            'type': 'mapping-incoming',
            'mapping': {
                'IncidentType': {
                    'dontMapEventToLabels': False,
                    'internalMapping': {
                        'Host': {
                            'simple': 'blabla'
                        },
                    }
                }
            }
        }
        pack = repo.create_pack('MapperPack')
        pack.pack_metadata.write_json({
            'name':
            'MapperPack',
            'currentVersion':
            '1.0.0',
            'marketplaces': ['xsoar', 'marketplacev2'],
        })
        pack.create_mapper('Mapper', content=mapper)

        with ChangeCWD(repo.path):
            # Circle froze on 3.7 dut to high usage of processing power.
            # pool = Pool(processes=cpu_count() * 2) is the line that in charge of the multiprocessing initiation,
            # so changing `cpu_count` return value to 1 still gives you multiprocessing but with only 2 processors,
            # and not the maximum amount.
            import demisto_sdk.commands.common.update_id_set as uis
            mocker.patch.object(uis, 'cpu_count', return_value=1)
            runner = CliRunner(mix_stderr=False)
            result = runner.invoke(main, [
                CREATE_ID_SET_CMD, '-o', 'id_set_result.json', '--marketplace',
                'marketplacev2'
            ],
                                   catch_exceptions=False)
            assert result.exit_code == 0

        with open(os.path.join(repo.path, 'id_set_result.json')) as file_:
            id_set = json.load(file_)
        assert len(id_set['Mappers']) == 1