def test_validate_command(self):
        test_validate_command_dir = f'{os.path.dirname(__file__)}/resources/test_validate_command'

        test_cases = [
            {
                'dir': 'missing_replication_key_incremental',
                'exception': True
            },
            {
                'dir': 'missing_replication_key',
                'exception': False
            },
            {
                'dir': 'invalid_target',
                'exception': True
            }
        ]

        for test_case in test_cases:
            args = CliArgs(dir=f'{test_validate_command_dir}/{test_case["dir"]}')
            pipelinewise = PipelineWise(args, CONFIG_DIR, VIRTUALENVS_DIR)

            if test_case['exception']:
                with pytest.raises(SystemExit):
                    pipelinewise.validate()
            else:
                pipelinewise.validate()
Example #2
0
    def test_command_sync_tables(self, capsys):
        """Test run tap command"""
        args = CliArgs(target="target_one", tap="tap_one")
        pipelinewise = PipelineWise(args, CONFIG_DIR, VIRTUALENVS_DIR)

        # Running sync_tables should detect the tap type and path to the connector
        # Since the executable is not available in this test then it should fail
        # TODO: sync discover_tap and run_tap behaviour. run_tap sys.exit but discover_tap does not.
        with pytest.raises(SystemExit) as pytest_wrapped_e:
            pipelinewise.sync_tables()
        assert pytest_wrapped_e.type == SystemExit
        assert pytest_wrapped_e.value.code == 1
    def test_command_discover_tap(self, capsys):
        """Test discover tap command"""
        args = CliArgs(target="target_one", tap="tap_one")
        pipelinewise = PipelineWise(args, CONFIG_DIR, VIRTUALENVS_DIR)

        # Running discovery mode should detect the tap type and path to the connector
        # Since the executable is not available in this test then it should fail
        pipelinewise.discover_tap()
        stdout, stderr = capsys.readouterr()

        exp_err_pattern = os.path.join(VIRTUALENVS_DIR, "/tap-mysql/bin/tap-mysql: No such file or directory")
        assert exp_err_pattern in stdout or exp_err_pattern in stderr
    def test_command_encrypt_string(self, capsys):
        """Test vault encryption command output"""
        secret_path = "{}/resources/vault-secret.txt".format(os.path.dirname(__file__))

        args = CliArgs(string="plain text", secret=secret_path)
        pipelinewise = PipelineWise(args, CONFIG_DIR, VIRTUALENVS_DIR)

        # Encrypted string should be printed to stdout
        pipelinewise.encrypt_string()
        stdout, stderr = capsys.readouterr()
        assert not stderr.strip()
        assert stdout.startswith("!vault |") and "$ANSIBLE_VAULT;" in stdout
    def test_command_init(self):
        """Test init command"""
        args = CliArgs(name=TEST_PROJECT_NAME)
        pipelinewise = PipelineWise(args, CONFIG_DIR, VIRTUALENVS_DIR)

        # Init new project
        pipelinewise.init()

        # The test project should contain every sample YAML file
        for s in os.listdir("{}/../../../pipelinewise/cli/samples".format(os.path.dirname(__file__))):
            assert os.path.isfile(os.path.join(TEST_PROJECT_DIR, s))

        # Re-creating project should reaise exception of directory not empty
        with pytest.raises(SystemExit) as pytest_wrapped_e:
            pipelinewise.init()
        assert pytest_wrapped_e.type == SystemExit
        assert pytest_wrapped_e.value.code == 1
    def test_exit_gracefully(self):
        """Gracefully shoudl run tap command"""
        args = CliArgs(target="target_one", tap="tap_one")
        pipelinewise = PipelineWise(args, CONFIG_DIR, VIRTUALENVS_DIR)

        # Create a test log file, simulating a running tap
        pipelinewise.tap_run_log_file = "test-tap-run-dummy.log"
        Path("{}.running".format(pipelinewise.tap_run_log_file)).touch()

        # Graceful exit should return 1 by default
        with pytest.raises(SystemExit) as pytest_wrapped_e:
            pipelinewise._exit_gracefully(signal.SIGINT, frame=None)
        assert pytest_wrapped_e.type == SystemExit
        assert pytest_wrapped_e.value.code == 1

        # Graceful exit should rename log file from running status to terminated
        assert os.path.isfile("{}.terminated".format(pipelinewise.tap_run_log_file))

        # Delete test log file
        os.remove("{}.terminated".format(pipelinewise.tap_run_log_file))
    def test_post_import_checks(self):
        args = CliArgs()
        pipelinewise = PipelineWise(args, CONFIG_DIR, VIRTUALENVS_DIR)
        test_files_dir = "{}/resources/test_post_import_checks".format(os.path.dirname(__file__))

        target_pk_required = cli.utils.load_json("{}/target_config_pk_required.json".format(test_files_dir))
        target_pk_not_required = cli.utils.load_json("{}/target_config_pk_not_required.json".format(test_files_dir))
        tap_with_pk = cli.utils.load_json("{}//tap_properties_with_pk.json".format(test_files_dir))
        tap_with_no_pk_full_table = cli.utils.load_json("{}//tap_properties_with_no_pk_full_table.json".format(test_files_dir))
        tap_with_no_pk_incremental = cli.utils.load_json("{}//tap_properties_with_no_pk_incremental.json".format(test_files_dir))
        tap_with_no_pk_log_based = cli.utils.load_json("{}//tap_properties_with_no_pk_log_based.json".format(test_files_dir))
        tap_with_no_pk_not_selected = cli.utils.load_json("{}//tap_properties_with_no_pk_not_selected.json".format(test_files_dir))

        # Test scenarios when post import checks should pass
        assert pipelinewise._run_post_import_tap_checks(tap_with_pk, target_pk_required) == []
        assert pipelinewise._run_post_import_tap_checks(tap_with_pk, target_pk_not_required) == []
        assert pipelinewise._run_post_import_tap_checks(tap_with_no_pk_full_table, target_pk_required) == []
        assert pipelinewise._run_post_import_tap_checks(tap_with_no_pk_incremental, target_pk_not_required) == []
        assert pipelinewise._run_post_import_tap_checks(tap_with_no_pk_log_based, target_pk_not_required) == []
        assert pipelinewise._run_post_import_tap_checks(tap_with_no_pk_not_selected, target_pk_required) == []

        # Test scenarios when post import checks should fail due to primary keys not exists
        assert len(pipelinewise._run_post_import_tap_checks(tap_with_no_pk_incremental, target_pk_required)) == 1
        assert len(pipelinewise._run_post_import_tap_checks(tap_with_no_pk_log_based, target_pk_required)) == 1
Example #8
0
 def setup_method(self):
     # Create CLI arguments
     self.args = CliArgs(log="coverage.log")
     self.pipelinewise = PipelineWise(self.args, CONFIG_DIR,
                                      VIRTUALENVS_DIR)