Ejemplo n.º 1
0
    def test_create_mapper_from_old_classifier(self, tmpdir):
        """
        Given:
        - 'old_classifier': Old classifier to convert into 6_0_0 classifier.

        When:
        - Creating a 6_0_0 mapper convention from 5_9_9 and below classifier

        Then:
        - Ensure expected mapper is created in the expected path with the expected data.

        """
        fake_pack_name = 'FakeTestPack'
        repo = Repo(tmpdir)
        repo_path = Path(repo.path)
        fake_pack = MockPack(repo_path / 'Packs', fake_pack_name, repo)
        fake_pack.create_classifier('Cymulate_5_9_9',
                                    util_load_json(self.OLD_CLASSIFIER_PATH))
        fake_pack_path = fake_pack.path
        converter = ClassifierSixConverter(Pack(fake_pack_path))
        old_classifier = Classifier(self.OLD_CLASSIFIER_PATH)
        converter.create_mapper_from_old_classifier(old_classifier)
        mapper_path = f'{tmpdir}/Packs/FakeTestPack/Classifiers/classifier-mapper-incoming-Cymulate.json'
        self.assert_expected_file_output(
            mapper_path, 'classifier-mapper-incoming-Cymulate')
Ejemplo n.º 2
0
def test_is_docker_image_same_as_yml(category, release_notes_content,
                                     yml_content, filled_expected_result,
                                     pack: Pack):
    """
    Given
    - Case 1: RN containing a docker update, integration YML containing a docker update, where docker image equal in both.
    - Case 2: RN containing a docker update, script YML containing a docker update, where docker image equal in both.
    - Case 3: RN containing a docker update, script YML containing a docker update, where docker image ins't equal in both.
    - Case 4: Release notes without a docker update, YML without a docker update.
    When
    - Running validation on release notes.
    Then
    - Ensure validation correctly identifies valid release notes.
    - Case 1: Should print nothing and return True.
    - Case 2: Should print nothing and return True.
    - Case 3: Should return the prompt "Release notes dockerimage version does not match yml dockerimage version." and
            return False.
    - Case 4: Should print nothing and return True.
    """
    rn = pack.create_release_notes(version='1_0_1',
                                   content=release_notes_content)
    if category == "integration":
        category = pack.create_integration()
    else:
        category = pack.create_script()
    category.yml.update(yml_content)
    validator = ReleaseNotesValidator(rn.path,
                                      modified_files=[category.yml.path],
                                      pack_name=os.path.basename(pack.path))
    assert validator.is_docker_image_same_as_yml() == filled_expected_result
Ejemplo n.º 3
0
    def test_create_classifier_from_old_classifier(self, tmpdir):
        """
        Given:
        - 'old_classifier': Old classifier to convert into 6_0_0 classifier.
        - 'intersection_fields': List of the intersecting fields of classifiers 5_9_9 and below to classifiers 6_0_0.

        When:
        - Creating a 6_0_0 classifier convention from 5_9_9 and below classifier

        Then:
        - Ensure expected classifier is created in the expected path with the expected data.

        """
        fake_pack_name = 'FakeTestPack'
        repo = Repo(tmpdir)
        repo_path = Path(repo.path)
        fake_pack = MockPack(repo_path / 'Packs', fake_pack_name, repo)
        fake_pack.create_classifier('Cymulate_5_9_9',
                                    util_load_json(self.OLD_CLASSIFIER_PATH))
        fake_pack_path = fake_pack.path
        converter = ClassifierSixConverter(Pack(fake_pack_path))
        old_classifier = Classifier(self.OLD_CLASSIFIER_PATH)
        intersecting_fields = converter.get_classifiers_schema_intersection_fields(
        )
        converter.create_classifier_from_old_classifier(
            old_classifier, intersecting_fields)
        classifier_expected_path = f'{tmpdir}/Packs/FakeTestPack/Classifiers/classifier-Cymulate.json'
        self.assert_expected_file_output(classifier_expected_path,
                                         'classifier-Cymulate')
    def test_convert_dir(self, tmpdir):
        """
        Given:
        - Pack.

        When:
        - Converting every layout of version 6.0.0 and above to version 5.9.9 and below.

        Then:
        - Ensure expected layouts are created with expected values.
        """
        fake_pack_name = 'FakeTestPack'
        repo = Repo(tmpdir)
        repo_path = Path(repo.path)
        fake_pack = MockPack(repo_path / 'Packs', fake_pack_name, repo)
        fake_pack.create_incident_type('ExtraHop_Detection',
                                       util_load_json(self.INCIDENT_TYPE_ONE))
        fake_pack.create_layoutcontainer('ExtraHop Detection',
                                         util_load_json(self.LAYOUT_CONTAINER))
        fake_pack_path = fake_pack.path
        layout_converter = LayoutBelowSixConverter(Pack(fake_pack_path))
        layout_converter.convert_dir()
        test_data_json = util_load_json(
            os.path.join(
                __file__,
                f'{git_path()}/demisto_sdk/commands/convert/converters/layout/'
                'tests/test_data'
                '/layout_up_to_5_9_9_expected_convert_dir_test_file_output.json'
            ))
        for layout_field_name, layout_data in test_data_json.items():
            expected_new_layout_path = f'{str(layout_converter.pack.path)}/Layouts/layout-{layout_field_name}-' \
                                       'ExtraHop_Detection.json'
            assert os.path.exists(expected_new_layout_path)
            assert util_load_json(expected_new_layout_path) == layout_data
            os.remove(expected_new_layout_path)
    def test_layout_to_incidents_dict(self, tmpdir):
        """
        Given:
        - Incident types of the pack.

        When:
        - Creating a dict of key as layout ID and value as list of incident type IDs whom layout field equals to layout
          ID.

        Then:
        - Ensure expected dict is returned.

        """
        fake_pack_name = 'FakeTestPack'
        repo = Repo(tmpdir)
        repo_path = Path(repo.path)
        fake_pack = MockPack(repo_path / 'Packs', fake_pack_name, repo)
        fake_pack.create_incident_type('ExtraHop_Detection',
                                       util_load_json(self.INCIDENT_TYPE_ONE))
        fake_pack.create_incident_type('ExtraHop_Detection_2',
                                       util_load_json(self.INCIDENT_TYPE_TWO))
        fake_pack_path = fake_pack.path
        layout_converter = LayoutBelowSixConverter(Pack(fake_pack_path))
        result = LayoutBelowSixConverter.layout_to_indicators_or_incidents_dict(
            layout_converter.pack.incident_types)
        assert result == {
            'ExtraHop Detection':
            ['ExtraHop Detection', 'ExtraHop Detection 2']
        }
Ejemplo n.º 6
0
 def test_convert_dir(self, tmpdir):
     fake_pack_name = 'FakeTestPack'
     repo = Repo(tmpdir)
     repo_path = Path(repo.path)
     fake_pack = MockPack(repo_path / 'Packs', fake_pack_name, repo)
     fake_pack.create_classifier('Cymulate_5_9_9',
                                 util_load_json(self.OLD_CLASSIFIER_PATH))
     fake_pack_path = fake_pack.path
     classifier_converter = ClassifierSixConverter(Pack(fake_pack_path))
     assert classifier_converter.convert_dir() == 0
     expected_new_classifier_path = f'{tmpdir}/Packs/FakeTestPack/Classifiers/classifier-Cymulate.json'
     expected_new_mapper_path = f'{tmpdir}/Packs/FakeTestPack/Classifiers/classifier-mapper-incoming-Cymulate.json'
     self.assert_expected_file_output(expected_new_classifier_path,
                                      'classifier-Cymulate')
     self.assert_expected_file_output(
         expected_new_mapper_path, 'classifier-mapper-incoming-Cymulate')
Ejemplo n.º 7
0
def test_get_ignore_pack_tests__ignore_missing_test(tmpdir, mocker):
    """
    Given
    - Pack have .pack-ignore file
    - There are skipped tests in .pack-ignore
    - The tests are missing from the content pack
    When
    - Collecting packs' ignored tests - running `get_ignore_pack_tests()`
    Then:
    - returns a list with the skipped tests
    """
    fake_pack_name = 'FakeTestPack'
    fake_test_name = 'FakeTestPlaybook.yml'

    # prepare repo
    repo = Repo(tmpdir)
    packs_path = Path(repo.path) / PACKS_DIR
    pack = Pack(packs_path, fake_pack_name, repo)
    test_playbook_path = packs_path / fake_pack_name / TEST_PLAYBOOKS_DIR
    pack_ignore_path = os.path.join(pack.path, PACKS_PACK_IGNORE_FILE_NAME)

    # prepare .pack-ignore
    with open(pack_ignore_path, 'a') as pack_ignore_f:
        pack_ignore_f.write("[file:TestIntegration.yml]\nignore=IN126\n\n"
                            f"[file:{fake_test_name}]\nignore=auto-test")

    # prepare mocks
    mocker.patch.object(tools, "get_pack_ignore_file_path", return_value=pack_ignore_path)
    mocker.patch.object(os.path, "join", return_value=str(test_playbook_path / fake_test_name))

    ignore_test_set = get_ignore_pack_skipped_tests(fake_pack_name)
    assert len(ignore_test_set) == 0
Ejemplo n.º 8
0
 def test_validate_field_with_aliases__invalid_type(self, pack: Pack):
     """
     Given
         - Incident field with a Aliases field that has an entry with an invalid type.
     When
         - Validating the item.
     Then
         - Ensures the schema is invalid.
     """
     field_content = {
         'cliName': 'mainfield',
         'name': 'main field',
         'id': 'incident',
         'content': True,
         'type': 'longText',
         'Aliases': [{
             "cliName": "alias field",
             "type": "UNKNOWN"
         }]
     }
     incident_field: JSONBased = pack.create_incident_field(
         'incident-field-test',
         content=field_content
     )
     structure = StructureValidator(incident_field.path)
     assert not structure.is_valid_scheme()
Ejemplo n.º 9
0
 def test_validate_field_with_aliases__valid(self, pack: Pack):
     """
     Given
         Incident field with a valid Aliases field.
     When
         Validating the item.
     Then
         Ensures the schema is valid.
     """
     field_content = {
         'cliName':
         'mainfield',
         'name':
         'main field',
         'id':
         'incident',
         'content':
         True,
         'type':
         'longText',
         'Aliases': [{
             "cliName": "aliasfield",
             "type": "shortText",
             "name": "Alias Field",
         }],
     }
     incident_field: JSONBased = pack.create_incident_field(
         'incident-field-test',
         content=field_content,
     )
     structure = StructureValidator(incident_field.path)
     assert structure.is_valid_scheme()
Ejemplo n.º 10
0
    def test_calculate_new_layout_group(self, tmpdir, old_layout_path: str, expected: str):
        """
        Given:
        - 'old_layouts': List of old layout objects.

        When:
        - Calculating the group field value for the layout above 6.0.0 version to be created.

        Then:
        - Ensure the expected group value is returned.
        """
        fake_pack_name = 'FakeTestPack'
        repo = Repo(tmpdir)
        repo_path = Path(repo.path)
        fake_pack = MockPack(repo_path / 'Packs', fake_pack_name, repo)
        fake_pack.create_layout('test', util_load_json(old_layout_path))
        fake_pack_path = fake_pack.path
        layout_converter = LayoutSixConverter(Pack(fake_pack_path))
        assert layout_converter.calculate_new_layout_group(
            [layout for layout in layout_converter.pack.layouts]) == expected
Ejemplo n.º 11
0
 def test_is_indicator_with_open_ended(self, pack: Pack):
     field_content = {
         'cliName': 'sanityname',
         'name': 'sanity name',
         'id': 'incident',
         'content': True,
         'type': 'multiSelect',
         'openEnded': True
     }
     incident_field: JSONBased = pack.create_indicator_field(
         'incident-field-test', content=field_content)
     structure = StructureValidator(incident_field.path)
     assert structure.is_valid_scheme()
    def test_get_layouts_by_layout_container_type(self, tmpdir):
        """
        Given:
        - Layout container FileType.

        When:
        - Wanting to retrieve all layout-containers from the current pack.

        Then:
        - Ensure only layout-containers in the pack are returned.
        """
        fake_pack_name = 'FakeTestPack'
        repo = Repo(tmpdir)
        repo_path = Path(repo.path)
        fake_pack = MockPack(repo_path / 'Packs', fake_pack_name, repo)
        fake_pack.create_layoutcontainer('ExtraHop Detection',
                                         util_load_json(self.LAYOUT_CONTAINER))
        fake_pack_path = fake_pack.path
        layouts = BaseConverter.get_entities_by_entity_type(
            Pack(fake_pack_path).layouts, FileType.LAYOUTS_CONTAINER)
        assert all(layout.type() == FileType.LAYOUTS_CONTAINER
                   for layout in layouts)
        assert [layout.layout_id()
                for layout in layouts] == ['ExtraHop Detection']
Ejemplo n.º 13
0
def test_get_ignore_pack_tests__test_not_ignored(tmpdir):
    """
    Given
    - Pack have .pack-ignore file
    - There are no skipped tests in .pack-ignore
    When
    - Collecting packs' ignored tests - running `get_ignore_pack_tests()`
    Then:
    - returns an empty set
    """
    fake_pack_name = 'FakeTestPack'

    # prepare repo
    repo = Repo(tmpdir)
    repo_path = Path(repo.path)
    pack = Pack(repo_path / PACKS_DIR, fake_pack_name, repo)
    pack_ignore_path = os.path.join(pack.path, PACKS_PACK_IGNORE_FILE_NAME)

    # prepare .pack-ignore
    open(pack_ignore_path, 'a').close()

    ignore_test_set = get_ignore_pack_skipped_tests(fake_pack_name)
    assert len(ignore_test_set) == 0
Ejemplo n.º 14
0
def test_get_ignore_pack_tests__no_ignore_pack(tmpdir):
    """
    Given
    - Pack doesn't have .pack-ignore file
    When
    - Collecting packs' ignored tests - running `get_ignore_pack_tests()`
    Then:
    - returns an empty set
    """
    fake_pack_name = 'FakeTestPack'

    # prepare repo
    repo = Repo(tmpdir)
    repo_path = Path(repo.path)
    pack = Pack(repo_path / PACKS_DIR, fake_pack_name, repo)
    pack_ignore_path = os.path.join(pack.path, PACKS_PACK_IGNORE_FILE_NAME)

    # remove .pack-ignore if exists
    if os.path.exists(pack_ignore_path):
        os.remove(pack_ignore_path)

    ignore_test_set = get_ignore_pack_skipped_tests(fake_pack_name)
    assert len(ignore_test_set) == 0
Ejemplo n.º 15
0
 def create_pack(self, name: Optional[str] = None):
     if name is None:
         name = f'pack_{len(self.packs)}'
     pack = Pack(self._packs_path, name, repo=self)
     self.packs.append(pack)
     return pack
Ejemplo n.º 16
0
    def test_group_layouts_needing_conversion_by_layout_id(self, tmpdir):
        """
        Given:
        -

        When:
        - Grouping layouts needing conversion by their layout ID.

        Then:
        - Ensure expected dict object is returned.
        """
        fake_pack_name = 'FakeTestPack'
        repo = Repo(tmpdir)
        repo_path = Path(repo.path)
        fake_pack = MockPack(repo_path / 'Packs', fake_pack_name, repo)
        fake_pack.create_layout('close-ExtraHop_Detection', util_load_json(self.LAYOUT_CLOSE_PATH))
        fake_pack.create_layout('details-ExtraHop_Detection', util_load_json(self.LAYOUT_DETAILS_PATH))
        fake_pack.create_layout('edit-ExtraHop_Detection', util_load_json(self.LAYOUT_EDIT_PATH))
        fake_pack.create_layout('quickView-ExtraHop_Detection',
                                util_load_json(self.LAYOUT_QUICK_VIEW_PATH))
        fake_pack.create_layout('mobile-ExtraHop_Detection', util_load_json(self.LAYOUT_MOBILE_PATH))
        fake_pack_path = fake_pack.path
        layout_converter = LayoutSixConverter(Pack(fake_pack_path))
        result = layout_converter.group_layouts_needing_conversion_by_layout_id()
        assert len(result) == 1 and 'ExtraHop Detection' in result
        layout_kinds = {layout['kind'] for layout in result['ExtraHop Detection']}
        assert all(layout.layout_id() == 'ExtraHop Detection' for layout in result['ExtraHop Detection'])
        assert layout_kinds == {'close', 'details', 'edit', 'mobile', 'quickView'}
    def test_get_layouts_by_layout_type(self, tmpdir):
        """
        Given:
        - Layout FileType.

        When:
        - Wanting to retrieve all layout below 6.0.0 version from the current pack.

        Then:
        - Ensure only layouts below 6.0.0 version in the pack are returned.
        """
        fake_pack_name = 'FakeTestPack'
        repo = Repo(tmpdir)
        repo_path = Path(repo.path)
        fake_pack = MockPack(repo_path / 'Packs', fake_pack_name, repo)
        fake_pack.create_layout('close-ExtraHop_Detection',
                                util_load_json(self.LAYOUT_CLOSE_PATH))
        fake_pack.create_layout('details-ExtraHop_Detection',
                                util_load_json(self.LAYOUT_DETAILS_PATH))
        fake_pack.create_layout('edit-ExtraHop_Detection',
                                util_load_json(self.LAYOUT_EDIT_PATH))
        fake_pack.create_layout('quickView-ExtraHop_Detection',
                                util_load_json(self.LAYOUT_QUICK_VIEW_PATH))
        fake_pack.create_layout('mobile-ExtraHop_Detection',
                                util_load_json(self.LAYOUT_MOBILE_PATH))
        fake_pack_path = fake_pack.path
        layouts = BaseConverter.get_entities_by_entity_type(
            Pack(fake_pack_path).layouts, FileType.LAYOUT)
        assert len(layouts) == 5
        assert all(layout.type() == FileType.LAYOUT for layout in layouts)
        assert {layout.get('kind')
                for layout in layouts
                } == {'close', 'details', 'edit', 'mobile', 'quickView'}
Ejemplo n.º 18
0
    def test_convert_dir(self, tmpdir):
        """
        Given:
        - Pack.

        When:
        - Converting every layout of version 6.0.0 and above to version 5.9.9 and below.

        Then:
        - Ensure expected layouts are created with expected values.
        """
        fake_pack_name = 'FakeTestPack'
        repo = Repo(tmpdir)
        repo_path = Path(repo.path)
        fake_pack = MockPack(repo_path / 'Packs', fake_pack_name, repo)
        fake_pack.create_layout('close-ExtraHop_Detection', util_load_json(self.LAYOUT_CLOSE_PATH))
        fake_pack.create_layout('details-ExtraHop_Detection', util_load_json(self.LAYOUT_DETAILS_PATH))
        fake_pack.create_layout('edit-ExtraHop_Detection', util_load_json(self.LAYOUT_EDIT_PATH))
        fake_pack.create_layout('quickView-ExtraHop_Detection',
                                util_load_json(self.LAYOUT_QUICK_VIEW_PATH))
        fake_pack.create_layout('mobile-ExtraHop_Detection', util_load_json(self.LAYOUT_MOBILE_PATH))
        fake_pack_path = fake_pack.path
        layout_converter = LayoutSixConverter(Pack(fake_pack_path))
        layout_converter.convert_dir()
        expected_new_layout_path = f'{str(layout_converter.pack.path)}/Layouts/layoutscontainer-ExtraHop_Detection.json'
        assert os.path.exists(expected_new_layout_path)
        with open(expected_new_layout_path, 'r') as f:
            layout_data = json.loads(f.read())
        test_data_json = util_load_json(os.path.join(__file__,
                                                     f'{git_path()}/demisto_sdk/commands/convert/converters/layout/'
                                                     'tests/test_data'
                                                     '/layoutscontainer-ExtraHop_Detection.json'))
        assert layout_data == test_data_json
        os.remove(expected_new_layout_path)