Example #1
0
def test_parse_docker_image():
    from Tests.scripts.hook_validations.docker import DockerImageValidator
    assert 'demisto/python', '1.3-alpine' == DockerImageValidator.parse_docker_image(
        'demisto/python:1.3-alpine')
    assert 'demisto/slack', '1.2.3.4' == DockerImageValidator.parse_docker_image(
        'demisto/slack:1.2.3.4')
    assert ('', '') == DockerImageValidator.parse_docker_image(
        'demisto/python/1.2.3.4')
Example #2
0
def test_get_docker_image_from_yml():
    from Tests.scripts.hook_validations.docker import DockerImageValidator
    with patch.object(DockerImageValidator, '__init__',
                      lambda x, y, z, w: None):
        # Test integration case
        docker_validator = DockerImageValidator(None, None, None)
        docker_validator.yml_file = get_yaml(
            "Tests/scripts/hook_validations/tests/tests_data/fake_integration.yml"
        )
        docker_validator.is_integration = True
        docker_image = docker_validator.get_docker_image_from_yml()
        assert docker_image == "demisto/pyjwt:1.0"
        # Test script case
        docker_validator.yml_file = get_yaml(
            "Tests/scripts/hook_validations/tests/tests_data/fake-script.yml")
        docker_validator.is_integration = False
        docker_image = docker_validator.get_docker_image_from_yml()
        assert docker_image == "demisto/stix2:1.0.0.204"
Example #3
0
    def validate_added_files(self, added_files):
        """Validate the added files from your branch.

        In case we encounter an invalid file we set the self._is_valid param to False.

        Args:
            added_files (set): A set of the modified files in the current branch.
        """
        for file_path in added_files:
            is_python_file = FilesValidator.is_py_script_or_integration(file_path)
            print('Validating {}'.format(file_path))

            structure_validator = StructureValidator(file_path, is_added_file=True)
            if not structure_validator.is_file_valid():
                self._is_valid = False

            if not self.id_set_validator.is_file_valid_in_set(file_path):
                self._is_valid = False

            if self.id_set_validator.is_file_has_used_id(file_path):
                self._is_valid = False

            if re.match(TEST_PLAYBOOK_REGEX, file_path, re.IGNORECASE):
                if not self.conf_json_validator.is_test_in_conf_json(collect_ids(file_path)):
                    self._is_valid = False

            elif re.match(INTEGRATION_REGEX, file_path, re.IGNORECASE) or \
                    re.match(INTEGRATION_YML_REGEX, file_path, re.IGNORECASE) or \
                    re.match(IMAGE_REGEX, file_path, re.IGNORECASE):

                image_validator = ImageValidator(file_path)
                if not image_validator.is_valid():
                    self._is_valid = False

                description_validator = DescriptionValidator(file_path)
                if not description_validator.is_valid():
                    self._is_valid = False

                integration_validator = IntegrationValidator(file_path)
                if not integration_validator.is_valid_integration():
                    self._is_valid = False

                if is_python_file:
                    docker_image_validator = DockerImageValidator(file_path, is_modified_file=False)
                    if not docker_image_validator.is_docker_image_valid():
                        self._is_valid = False

            elif re.match(SCRIPT_REGEX, file_path, re.IGNORECASE) or \
                    re.match(SCRIPT_YML_REGEX, file_path, re.IGNORECASE) or \
                    re.match(SCRIPT_PY_REGEX, file_path, re.IGNORECASE):

                if is_python_file:
                    docker_image_validator = DockerImageValidator(file_path, is_modified_file=False)
                    if not docker_image_validator.is_docker_image_valid():
                        self._is_valid = False

            elif re.match(BETA_INTEGRATION_REGEX, file_path, re.IGNORECASE) or \
                    re.match(BETA_INTEGRATION_YML_REGEX, file_path, re.IGNORECASE):
                description_validator = DescriptionValidator(file_path)
                if not description_validator.is_valid_beta_description():
                    self._is_valid = False

                integration_validator = IntegrationValidator(file_path)
                if not integration_validator.is_valid_beta_integration(is_new=True):
                    self._is_valid = False

                if is_python_file:
                    docker_image_validator = DockerImageValidator(file_path, is_modified_file=False)
                    if not docker_image_validator.is_docker_image_valid():
                        self._is_valid = False

            elif re.match(IMAGE_REGEX, file_path, re.IGNORECASE):
                image_validator = ImageValidator(file_path)
                if not image_validator.is_valid():
                    self._is_valid = False

            elif re.match(INCIDENT_FIELD_REGEX, file_path, re.IGNORECASE):
                incident_field_validator = IncidentFieldValidator(file_path)
                if not incident_field_validator.is_valid():
                    self._is_valid = False
Example #4
0
    def validate_modified_files(self, modified_files, is_backward_check=True, old_branch='master'):
        """Validate the modified files from your branch.

        In case we encounter an invalid file we set the self._is_valid param to False.

        Args:
            modified_files (set): A set of the modified files in the current branch.
            is_backward_check (bool): When set to True will run backward compatibility checks
            old_branch (str): Old git branch to compare backward compatibility check to
        """
        for file_path in modified_files:
            old_file_path = None

            if isinstance(file_path, tuple):
                old_file_path, file_path = file_path

            is_python_file = FilesValidator.is_py_script_or_integration(file_path)

            print('Validating {}'.format(file_path))
            if not checked_type(file_path):
                print_warning('- Skipping validation of non-content entity file.')
                continue

            structure_validator = StructureValidator(file_path, is_added_file=not (False or is_backward_check),
                                                     is_renamed=old_file_path is not None)
            if not structure_validator.is_file_valid():
                self._is_valid = False

            if not self.id_set_validator.is_file_valid_in_set(file_path):
                self._is_valid = False

            elif re.match(INTEGRATION_REGEX, file_path, re.IGNORECASE) or \
                    re.match(INTEGRATION_YML_REGEX, file_path, re.IGNORECASE):

                image_validator = ImageValidator(file_path)
                if not image_validator.is_valid():
                    self._is_valid = False

                description_validator = DescriptionValidator(file_path)
                if not description_validator.is_valid():
                    self._is_valid = False

                integration_validator = IntegrationValidator(file_path, old_file_path=old_file_path,
                                                             old_git_branch=old_branch)
                if is_backward_check and not integration_validator.is_backward_compatible():
                    self._is_valid = False
                if not integration_validator.is_valid_integration():
                    self._is_valid = False

                if is_python_file:
                    docker_image_validator = DockerImageValidator(file_path, is_modified_file=True)
                    if not docker_image_validator.is_docker_image_valid():
                        self._is_valid = False

            elif re.match(BETA_INTEGRATION_REGEX, file_path, re.IGNORECASE) or \
                    re.match(BETA_INTEGRATION_YML_REGEX, file_path, re.IGNORECASE):
                description_validator = DescriptionValidator(file_path)
                if not description_validator.is_valid_beta_description():
                    self._is_valid = False
                integration_validator = IntegrationValidator(file_path, old_file_path=old_file_path)
                if not integration_validator.is_valid_beta_integration():
                    self._is_valid = False
                if is_python_file:
                    docker_image_validator = DockerImageValidator(file_path, is_modified_file=True)
                    if not docker_image_validator.is_docker_image_valid():
                        self._is_valid = False

            elif re.match(SCRIPT_REGEX, file_path, re.IGNORECASE):
                script_validator = ScriptValidator(file_path, old_file_path=old_file_path, old_git_branch=old_branch)
                if is_backward_check and not script_validator.is_backward_compatible():
                    self._is_valid = False
                if not script_validator.is_valid_script():
                    self._is_valid = False

                if is_python_file:
                    docker_image_validator = DockerImageValidator(file_path, is_modified_file=True)
                    if not docker_image_validator.is_docker_image_valid():
                        self._is_valid = False

            elif re.match(SCRIPT_YML_REGEX, file_path, re.IGNORECASE) or \
                    re.match(SCRIPT_PY_REGEX, file_path, re.IGNORECASE) or \
                    re.match(SCRIPT_JS_REGEX, file_path, re.IGNORECASE):

                yml_path, _ = get_script_package_data(os.path.dirname(file_path))
                script_validator = ScriptValidator(yml_path, old_file_path=old_file_path, old_git_branch=old_branch)
                if is_backward_check and not script_validator.is_backward_compatible():
                    self._is_valid = False

                if is_python_file:
                    docker_image_validator = DockerImageValidator(file_path, is_modified_file=True)
                    if not docker_image_validator.is_docker_image_valid():
                        self._is_valid = False

            elif re.match(IMAGE_REGEX, file_path, re.IGNORECASE):
                image_validator = ImageValidator(file_path)
                if not image_validator.is_valid():
                    self._is_valid = False

            elif re.match(INCIDENT_FIELD_REGEX, file_path, re.IGNORECASE):
                incident_field_validator = IncidentFieldValidator(file_path, old_file_path=old_file_path,
                                                                  old_git_branch=old_branch)
                if not incident_field_validator.is_valid():
                    self._is_valid = False
                if is_backward_check and not incident_field_validator.is_backward_compatible():
                    self._is_valid = False
Example #5
0
def test_parse_www_auth(www_auth, expected):
    from Tests.scripts.hook_validations.docker import DockerImageValidator
    assert expected == DockerImageValidator.parse_www_auth(www_auth)
Example #6
0
def test_find_latest_tag_by_date():
    from Tests.scripts.hook_validations.docker import DockerImageValidator
    tag = DockerImageValidator.find_latest_tag_by_date(MOCK_TAG_LIST)
    assert tag == "1.0.0.2876"
Example #7
0
def test_lexical_find_latest_tag():
    from Tests.scripts.hook_validations.docker import DockerImageValidator
    tag_list = ["2.0.2000", "2.1.2700", "2.1.373", "latest"]
    tag = DockerImageValidator.lexical_find_latest_tag(tag_list)
    assert tag == "2.1.2700"
Example #8
0
def test_get_docker_image_latest_tag(image):
    from Tests.scripts.hook_validations.docker import DockerImageValidator
    tag = DockerImageValidator.get_docker_image_latest_tag('demisto/' + image)
    # current latest tag is 2.7.16.2728 or 3.7.2.2728 disable-secrets-detection
    assert int(tag.split('.')[3]) >= 2728
Example #9
0
def test_is_docker_image_latest_tag():
    from Tests.scripts.hook_validations.docker import DockerImageValidator
    with patch.object(DockerImageValidator, '__init__', lambda x, y, z: None):
        docker_image_validator = DockerImageValidator(None, None)
        docker_image_validator.yml_file = {}
        docker_image_validator.docker_image_latest_tag = 'latest_tag'
        docker_image_validator.docker_image_name = 'demisto/python'
        docker_image_validator.from_version = '5.0.0'

        # ===== Added File Tests =====
        # default docker image
        docker_image_validator.is_latest_tag = True
        docker_image_validator.is_modified_file = False
        docker_image_validator.docker_image_tag = '1.3-alpine'
        assert docker_image_validator.is_docker_image_latest_tag() is False

        # regular docker image, not latest tag
        docker_image_validator.is_latest_tag = True
        docker_image_validator.docker_image_tag = 'not_latest_tag'
        assert docker_image_validator.is_docker_image_latest_tag() is False

        # regular docker image, latest tag
        docker_image_validator.is_latest_tag = True
        docker_image_validator.docker_image_tag = 'latest_tag'
        assert docker_image_validator.is_docker_image_latest_tag() is True

        # ===== Modified File Tests =====
        # from version 4.1.0
        docker_image_validator.is_latest_tag = True
        docker_image_validator.is_modified_file = True
        docker_image_validator.from_version = '4.1.0'
        assert docker_image_validator.is_docker_image_latest_tag() is True

        # from version 5.0.0 - regular docker image, latest tag
        docker_image_validator.is_latest_tag = True
        docker_image_validator.from_version = '5.0.0'
        assert docker_image_validator.is_docker_image_latest_tag() is True

        # from version 5.0.0 - regular docker image, not latest tag
        docker_image_validator.is_latest_tag = True
        docker_image_validator.from_version = '5.0.0'
        docker_image_validator.docker_image_tag = 'not_latest_tag'
        assert docker_image_validator.is_docker_image_latest_tag() is False

        # from version 5.0.0 - default docker image
        docker_image_validator.is_latest_tag = True
        docker_image_validator.docker_image_tag = '1.3-alpine'
        assert docker_image_validator.is_docker_image_latest_tag() is True
Example #10
0
def test_clear_non_numbered_tags(input_tags, output_tags):
    from Tests.scripts.hook_validations.docker import DockerImageValidator
    assert sorted(output_tags) == sorted(
        DockerImageValidator.clear_non_numbered_tags(input_tags))