Ejemplo n.º 1
0
def test_extract_code():
    from demisto_sdk.yaml_tools.extractor import Extractor
    configuration = Configuration()
    with patch.object(Extractor, '__init__', lambda a, b, c, d, e, f, g: None):
        extractor = Extractor('', '', False, False, '', configuration)
        extractor.yml_type = ''
        extractor.common_server = True
        extractor.demisto_mock = True
        extractor.yml_path = 'tests/test_files/integration-Zoom.yml'
        extractor.extract_code('tests/test_files/temp_file.txt')
        with open('tests/test_files/temp_file.txt', 'rb') as temp_file:
            file_data = temp_file.read().decode('utf-8')
            assert 'import demistomock as demisto\n' in file_data
            assert 'from CommonServerPython import *\n' in file_data
            assert file_data[-1] == '\n'
        os.remove('tests/test_files/temp_file.txt')
        extractor.common_server = False
        extractor.demisto_mock = False
        extractor.extract_code('tests/test_files/temp_file.txt')
        with open('tests/test_files/temp_file.txt', 'rb') as temp_file:
            file_data = temp_file.read().decode('utf-8')
            assert 'import demistomock as demisto\n' not in file_data
            assert 'from CommonServerPython import *\n' not in file_data
            assert file_data[-1] == '\n'
        os.remove('tests/test_files/temp_file.txt')
Ejemplo n.º 2
0
 def __init__(self,
              configuration=Configuration(),
              is_circle=False,
              white_list_path=''):
     self.configuration = configuration
     self.is_circle = is_circle
     self.white_list_path = white_list_path
Ejemplo n.º 3
0
def main(config, version, env_dir):
    config.configuration = Configuration()
    if version:
        version = get_distribution('demisto-sdk').version
        print(version)

    if env_dir:
        config.configuration.env_dir = env_dir
Ejemplo n.º 4
0
 def __init__(self, is_test_run=False, is_circle=False, configuration=Configuration()):
     self.is_circle = is_circle
     self.configuration = configuration
     if not is_test_run and self.is_circle:
         self.id_set = self.load_id_set()
         self.id_set_path = os.path.join(self.configuration.env_dir, 'configs', 'id_set.json')
         self.script_set = self.id_set[self.SCRIPTS_SECTION]
         self.playbook_set = self.id_set[self.PLAYBOOK_SECTION]
         self.integration_set = self.id_set[self.INTEGRATION_SECTION]
         self.test_playbook_set = self.id_set[self.TEST_PLAYBOOK_SECTION]
Ejemplo n.º 5
0
    def __init__(self,
                 project_dir: str,
                 no_test: bool = False,
                 no_pylint: bool = False,
                 no_flake8: bool = False,
                 no_mypy: bool = False,
                 verbose: bool = False,
                 root: bool = False,
                 keep_container: bool = False,
                 cpu_num: int = 0,
                 configuration: Configuration = Configuration(),
                 lock: threading.Lock = threading.Lock(),
                 no_bandit: bool = False,
                 requirements_3: str = '',
                 requirements_2: str = ''):

        if no_test and no_pylint and no_flake8 and no_mypy and no_bandit:
            raise ValueError("Nothing to run as all --no-* options specified.")

        self.configuration = configuration
        dev_scripts_dir = os.path.join(self.configuration.sdk_env_dir,
                                       'common', 'scripts', 'dev_scripts')
        self.run_dev_tasks_script_name = 'run_dev_tasks.sh'
        self.run_mypy_script_name = 'run_mypy.sh'
        self.container_setup_script_name = 'pkg_dev_container_setup.sh'
        self.run_dev_tasks_script = os.path.join(
            dev_scripts_dir, self.run_dev_tasks_script_name)
        self.container_setup_script = os.path.join(
            dev_scripts_dir, self.container_setup_script_name)
        self.run_mypy_script = os.path.join(dev_scripts_dir,
                                            self.run_mypy_script_name)
        self.docker_login_completed = False
        self.project_dir = os.path.abspath(
            os.path.join(self.configuration.env_dir, project_dir))
        if self.project_dir[-1] != os.sep:
            self.project_dir = os.path.join(self.project_dir, '')

        self.log_verbose = verbose
        self.root = root
        self.keep_container = keep_container
        self.cpu_num = cpu_num
        self.common_server_created = False
        self.run_args = {
            'pylint': not no_pylint,
            'flake8': not no_flake8,
            'mypy': not no_mypy,
            'bandit': not no_bandit,
            'tests': not no_test
        }
        self.lock = lock
        self.requirements_3 = requirements_3
        self.requirements_2 = requirements_2
Ejemplo n.º 6
0
    def __init__(self,
                 project_dir_list: str,
                 no_test: bool = False,
                 no_pylint: bool = False,
                 no_flake8: bool = False,
                 no_mypy: bool = False,
                 verbose: bool = False,
                 root: bool = False,
                 keep_container: bool = False,
                 cpu_num: int = 0,
                 parallel: bool = False,
                 max_workers: int = 10,
                 no_bandit: bool = False,
                 git: bool = False,
                 run_all_tests: bool = False,
                 outfile: str = '',
                 configuration: Configuration = Configuration()):

        if no_test and no_pylint and no_flake8 and no_mypy and no_bandit:
            raise ValueError("Nothing to run as all --no-* options specified.")

        self.parallel = parallel
        self.log_verbose = verbose
        self.root = root
        self.max_workers = 10 if max_workers is None else int(max_workers)
        self.keep_container = keep_container
        self.cpu_num = cpu_num
        self.common_server_created = False
        self.run_args = {
            'pylint': not no_pylint,
            'flake8': not no_flake8,
            'mypy': not no_mypy,
            'tests': not no_test,
            'bandit': not no_bandit
        }

        if run_all_tests or (not project_dir_list and git):
            self.pkgs = self.get_all_directories()

        else:
            self.pkgs = project_dir_list.split(',')

        if git:
            self.pkgs = self._get_packages_to_run()

        self.configuration = configuration
        self.requirements_for_python3 = get_dev_requirements(
            3.7, self.configuration.envs_dirs_base, self.log_verbose)
        self.requirements_for_python2 = get_dev_requirements(
            2.7, self.configuration.envs_dirs_base, self.log_verbose)
        self.outfile = outfile
Ejemplo n.º 7
0
 def __init__(self,
              file_path,
              old_file_path=None,
              predefined_scheme=None,
              configuration=Configuration()):
     # type: (str, Optional[str], Configuration) -> None
     self.is_valid = None  # type: Optional[bool]
     self.file_path = file_path
     self.scheme_name = predefined_scheme or self.scheme_of_file_by_path()
     self.file_type = self.get_file_type()
     self.current_file = self.load_data_from_file()
     self.old_file = get_remote_file(
         old_file_path) if old_file_path else None
     self.configuration = configuration
Ejemplo n.º 8
0
def test_extract_long_description():
    from demisto_sdk.yaml_tools.extractor import Extractor
    configuration = Configuration()
    with patch.object(Extractor, '__init__', lambda a, b, c, d, e, f, g: None):
        # Test yml_type is script
        extractor = Extractor('', '', False, False, '', configuration)
        extractor.yml_type = ''
        extractor.yml_path = 'script'
        assert extractor.extract_long_description('output_path') == 0
        # Test opening the file and writing to it
        extractor.yml_path = 'tests/test_files/integration-Zoom.yml'
        extractor.extract_long_description('tests/test_files/temp_file.txt')
        with open('tests/test_files/temp_file.txt', 'rb') as temp_file:
            file_data = temp_file.read().decode('utf-8')
            assert file_data == 'detaileddescription'
        os.remove('tests/test_files/temp_file.txt')
Ejemplo n.º 9
0
def test_extract_image():
    from demisto_sdk.yaml_tools.extractor import Extractor
    configuration = Configuration()
    with patch.object(Extractor, '__init__', lambda a, b, c, d, e, f, g: None):
        # Test yml_type is script
        extractor = Extractor('', '', False, False, '', configuration)
        extractor.yml_type = ''
        extractor.yml_path = 'script'
        assert extractor.extract_image('output_path') == 0
        # Test opening the file and writing to it
        extractor.yml_path = 'tests/test_files/integration-Zoom.yml'
        extractor.extract_image('tests/test_files/temp_image.png')
        with open('tests/test_files/temp_image.png', 'rb') as temp_image:
            image_data = temp_image.read()
            image = base64.b64encode(image_data).decode('utf-8')
            assert image == DEFAULT_IMAGE_BASE64
        os.remove('tests/test_files/temp_image.png')
Ejemplo n.º 10
0
def test_get_yml_type(yml_path, expected):
    from demisto_sdk.yaml_tools.extractor import Extractor
    configuration = Configuration()
    with patch.object(Extractor, '__init__', lambda a, b, c, d, e, f, g: None):
        extractor = Extractor('', '', False, False, '', configuration)
        # Test script/integration case
        extractor.yml_type = ''
        extractor.yml_path = yml_path
        assert extractor.get_yml_type() == expected
        # Test when type is set
        extractor.yml_type = expected
        assert extractor.get_yml_type() == expected
        # Test error
        extractor.yml_type = ''
        extractor.yml_path = 'path'
        with pytest.raises(ValueError):
            extractor.get_yml_type()
Ejemplo n.º 11
0
    def __init__(self,
                 is_backward_check=True,
                 prev_ver='origin/master',
                 use_git=False,
                 is_circle=False,
                 print_ignored_files=False,
                 validate_conf_json=True,
                 validate_id_set=False,
                 file_path=None,
                 configuration=Configuration()):
        self.branch_name = ''
        self.use_git = use_git
        if self.use_git:
            print('Using git')
            self.branch_name = self.get_current_working_branch()
            print(f'Running validation on branch {self.branch_name}')

        self.prev_ver = prev_ver
        if not self.prev_ver:
            # validate against master if no version was provided
            self.prev_ver = 'origin/master'

        self._is_valid = True
        self.configuration = configuration
        self.is_backward_check = is_backward_check
        self.is_circle = is_circle
        self.print_ignored_files = print_ignored_files
        self.validate_conf_json = validate_conf_json
        self.validate_id_set = validate_id_set
        self.file_path = file_path

        if self.validate_conf_json:
            self.conf_json_validator = ConfJsonValidator()
        if self.validate_id_set:
            self.id_set_validator = IDSetValidator(
                is_circle=self.is_circle, configuration=self.configuration)
Ejemplo n.º 12
0
    def __init__(self,
                 is_backward_check=True,
                 prev_ver='origin/master',
                 use_git=False,
                 is_circle=False,
                 print_ignored_files=False,
                 validate_conf_json=True,
                 validate_id_set=False,
                 configuration=Configuration()):
        self.branch_name = ''
        self.use_git = use_git
        if self.use_git:
            print('Using git')
            branches = run_command('git branch')
            branch_name_reg = re.search(r'\* (.*)', branches)
            self.branch_name = branch_name_reg.group(1)
            print(f'Running validation on branch {self.branch_name}')

        self.prev_ver = prev_ver
        if not self.prev_ver:
            # validate against master if no version was provided
            self.prev_ver = 'origin/master'

        self._is_valid = True
        self.configuration = configuration
        self.is_backward_check = is_backward_check
        self.is_circle = is_circle
        self.print_ignored_files = print_ignored_files
        self.validate_conf_json = validate_conf_json
        self.validate_id_set = validate_id_set

        if self.validate_conf_json:
            self.conf_json_validator = ConfJsonValidator()
        if self.validate_id_set:
            self.id_set_validator = IDSetValidator(
                is_circle=self.is_circle, configuration=self.configuration)
Ejemplo n.º 13
0
from demisto_sdk.common.configuration import Configuration
from demisto_sdk.common.hook_validations.id import IDSetValidator
CONFIG = Configuration()


def test_validness_in_set():
    validator = IDSetValidator(is_circle=False, is_test_run=True, configuration=CONFIG)

    obj_data = {
        "test": {
            "name": "test"
        }
    }
    obj_set = [
        obj_data,
    ]

    assert validator.is_valid_in_id_set(file_path="test", obj_data=obj_data, obj_set=obj_set), \
        "The id validator couldn't find id as valid one"


def test_obj_not_found_in_set():
    validator = IDSetValidator(is_circle=False, is_test_run=True, configuration=CONFIG)

    obj_data = {
        "test": {
            "name": "test"
        }
    }
    actual_obj_set = {
        "test": {