def setUp(self):
        self.mock_logger = Mock(
            spec=['log', 'info', 'debug', 'error', 'warn', 'exception'])
        self.apply_patches([
            patch('recovery_base.gplog.setup_tool_logging',
                  return_value=self.mock_logger),
            patch('recovery_base.gplog.enable_verbose_logging'),
        ])

        self.mock_cmd1 = Mock(spec=['run'])
        self.mock_cmd2 = Mock(spec=['run'])
        self.mock_get_cmd_list = Mock(
            return_value=[self.mock_cmd1, self.mock_cmd2])

        self.file_name = '/path/to/test_file.py'

        self.mock_setup_tool_logging = self.get_mock_from_apply_patch(
            'setup_tool_logging')
        self.mock_enable_verbose_logging = self.get_mock_from_apply_patch(
            'enable_verbose_logging')

        self.full_r1 = RecoveryInfo('target_data_dir1', 5001, 1,
                                    'source_hostname1', 6001, True,
                                    '/tmp/progress_file1')
        self.incr_r2 = RecoveryInfo('target_data_dir2', 5002, 2,
                                    'source_hostname2', 6002, False,
                                    '/tmp/progress_file2')
        self.confinfo = gppylib.recoveryinfo.serialize_recovery_info_list(
            [self.full_r1, self.incr_r2])
 def setUp(self):
     self.mock_logger = Mock(spec=['log', 'info', 'debug', 'error', 'warn', 'exception'])
     self.full_r1 = RecoveryInfo('target_data_dir1', 5001, 1, 'source_hostname1',
                                 6001, True, '/tmp/progress_file1')
     self.incr_r1 = RecoveryInfo('target_data_dir2', 5002, 2, 'source_hostname2',
                                 6002, False, '/tmp/progress_file2')
     self.full_r2 = RecoveryInfo('target_data_dir3', 5003, 3, 'source_hostname3',
                                 6003, True, '/tmp/progress_file3')
     self.incr_r2 = RecoveryInfo('target_data_dir4', 5004, 4, 'source_hostname4',
                                 6004, False, '/tmp/progress_file4')
    def setUp(self):
        self.maxDiff = None
        self.mock_logger = Mock()
        self.apply_patches([
            patch('gpsegrecovery.start_segment', return_value=Mock()),
            patch('gppylib.commands.pg.PgRewind.__init__', return_value=None),
            patch('gppylib.commands.pg.PgRewind.run')
        ])
        self.mock_pgrewind_run = self.get_mock_from_apply_patch('run')
        self.mock_pgrewind_init = self.get_mock_from_apply_patch('__init__')

        p = Segment.initFromString(
            "1|0|p|p|s|u|sdw1|sdw1|40000|/data/primary0")
        m = Segment.initFromString("2|0|m|m|s|u|sdw2|sdw2|50000|/data/mirror0")
        self.seg_recovery_info = RecoveryInfo(m.getSegmentDataDirectory(),
                                              m.getSegmentPort(),
                                              m.getSegmentDbId(),
                                              p.getSegmentHostName(),
                                              p.getSegmentPort(), False,
                                              '/tmp/test_progress_file')
        self.era = '1234_20211110'

        self.incremental_recovery_cmd = gpsegrecovery.IncrementalRecovery(
            name='test incremental recovery',
            recovery_info=self.seg_recovery_info,
            logger=self.mock_logger,
            era=self.era)
    def setUp(self):
        # TODO should we mock the set_recovery_cmd_results decorator and not worry about
        # testing the command results in this test class
        self.maxDiff = None
        self.mock_logger = Mock(
            spec=['log', 'info', 'debug', 'error', 'warn', 'exception'])
        self.apply_patches([
            patch('gpsegrecovery.ModifyConfSetting', return_value=Mock()),
            patch('gpsegrecovery.start_segment', return_value=Mock()),
            patch('gpsegrecovery.PgBaseBackup.__init__', return_value=None),
            patch('gpsegrecovery.PgBaseBackup.run')
        ])
        self.mock_pgbasebackup_run = self.get_mock_from_apply_patch('run')
        self.mock_pgbasebackup_init = self.get_mock_from_apply_patch(
            '__init__')
        self.mock_pgbasebackup_modifyconfsetting = self.get_mock_from_apply_patch(
            'ModifyConfSetting')

        p = Segment.initFromString(
            "1|0|p|p|s|u|sdw1|sdw1|40000|/data/primary0")
        m = Segment.initFromString("2|0|m|m|s|u|sdw2|sdw2|50000|/data/mirror0")
        self.seg_recovery_info = RecoveryInfo(m.getSegmentDataDirectory(),
                                              m.getSegmentPort(),
                                              m.getSegmentDbId(),
                                              p.getSegmentHostName(),
                                              p.getSegmentPort(), True,
                                              '/tmp/test_progress_file')
        self.era = '1234_20211110'
        self.full_recovery_cmd = gpsegrecovery.FullRecovery(
            name='test full recovery',
            recovery_info=self.seg_recovery_info,
            forceoverwrite=True,
            logger=self.mock_logger,
            era=self.era)
 def test_complete_workflow(self, mock_execsql, mock_dburl, mock_connect,
                            mock_validate_datadir):
     mock_connect.return_value = Mock()
     mock_dburl.return_value = Mock()
     buf = io.StringIO()
     with tempfile.TemporaryDirectory() as d:
         with redirect_stderr(buf):
             with self.assertRaises(SystemExit) as ex:
                 full_ri = RecoveryInfo('target_data_dir1', 5001, 1,
                                        'source_hostname1', 6001, True, d)
                 mix_confinfo = gppylib.recoveryinfo.serialize_recovery_info_list(
                     [full_ri, self.incr_r2])
                 sys.argv = [
                     'gpsegsetuprecovery', '-c {}'.format(mix_confinfo)
                 ]
                 seg_setup_recovery = SegSetupRecovery()
                 seg_setup_recovery.main()
     self.assertEqual('', buf.getvalue())
     self.assertEqual(0, ex.exception.code)
     mock_validate_datadir.assert_called_once()
     mock_dburl.assert_called_once()
     mock_connect.assert_called_once()
     mock_execsql.assert_called_once()
     #TODO use regex pattern
     self.assertRegex(gplog.get_logfile(),
                      '/gpsegsetuprecovery.py_\d+\.log')
    def setUp(self):
        self.mock_logger = Mock(
            spec=['log', 'info', 'debug', 'error', 'warn', 'exception'])
        self.mock_conn_val = Mock()
        self.mock_dburl_val = Mock()
        self.apply_patches([
            patch('gpsegsetuprecovery.dbconn.connect',
                  return_value=self.mock_conn_val),
            patch('gpsegsetuprecovery.dbconn.DbURL',
                  return_value=self.mock_dburl_val),
            patch('gpsegsetuprecovery.dbconn.execSQL')
        ])
        p = Segment.initFromString(
            "1|0|p|p|s|u|sdw1|sdw1|40000|/data/primary0")
        m = Segment.initFromString("2|0|m|m|s|u|sdw2|sdw2|50000|/data/mirror0")
        self.seg_recovery_info = RecoveryInfo(m.getSegmentDataDirectory(),
                                              m.getSegmentPort(),
                                              m.getSegmentDbId(),
                                              p.getSegmentHostName(),
                                              p.getSegmentPort(), True,
                                              '/test_progress_file')

        self.setup_for_incremental_recovery_cmd = gpsegsetuprecovery.SetupForIncrementalRecovery(
            name='setup for incremental recovery',
            recovery_info=self.seg_recovery_info,
            logger=self.mock_logger)
Beispiel #7
0
    def test_set_recovery_cmd_results_catch_exception_none_error_type(self):
        @set_recovery_cmd_results
        def test_decorator(cmd):
            cmd.name = 'new name'
            cmd.error_type = None
            raise Exception('running the cmd failed')
        recovery_info = RecoveryInfo('/tmp/datadir2', 7002, 2, None, None, None, '/tmp/progress_file2')
        test_cmd = FullRecovery('original name', recovery_info, True,None,None)
        test_decorator(test_cmd)
        self.assertEqual('new name', test_cmd.name)

        expected_stderr = '{"error_type": "default", "error_msg": "running the cmd failed", "dbid": 2, ' \
                          '"datadir": "/tmp/datadir2", "port": 7002, "progress_file": "/tmp/progress_file2"}'
        self._assert_cmd_failed(test_cmd, expected_stderr)
    def setUp(self):
        self.maxDiff = None
        self.mock_logger = Mock(
            spec=['log', 'info', 'debug', 'error', 'warn', 'exception'])
        self.full_r1 = RecoveryInfo('target_data_dir1', 5001, 1,
                                    'source_hostname1', 6001, True,
                                    '/tmp/progress_file1')
        self.incr_r1 = RecoveryInfo('target_data_dir2', 5002, 2,
                                    'source_hostname2', 6002, False,
                                    '/tmp/progress_file2')
        self.full_r2 = RecoveryInfo('target_data_dir3', 5003, 3,
                                    'source_hostname3', 6003, True,
                                    '/tmp/progress_file3')
        self.incr_r2 = RecoveryInfo('target_data_dir4', 5004, 4,
                                    'source_hostname4', 6004, False,
                                    '/tmp/progress_file4')
        self.era = '1234_2021110'

        self.apply_patches([
            patch('gpsegrecovery.SegmentStart.__init__', return_value=None),
            patch('gpsegrecovery.SegmentStart.run'),
        ])
        self.mock_segment_start_init = self.get_mock_from_apply_patch(
            '__init__')
    def setUp(self):
        self.maxDiff = None
        self.mock_logger = Mock(spec=['log', 'info', 'debug', 'error', 'warn', 'exception'])
        p = Segment.initFromString("1|0|p|p|s|u|sdw1|sdw1|40000|/data/primary0")
        m = Segment.initFromString("2|0|m|m|s|u|sdw2|sdw2|50000|/data/mirror0")
        self.seg_recovery_info = RecoveryInfo(m.getSegmentDataDirectory(),
                                              m.getSegmentPort(),
                                              m.getSegmentDbId(),
                                              p.getSegmentHostName(),
                                              p.getSegmentPort(),
                                              True, '/tmp/test_progress_file')

        self.validation_recovery_cmd = gpsegsetuprecovery.ValidationForFullRecovery(
            name='test validation for full recovery', recovery_info=self.seg_recovery_info,
            forceoverwrite=True, logger=self.mock_logger)
Beispiel #10
0
 def test_build_recovery_info_passes(self):
     # The expected dictionary within each test has target_host as the key.
     # Each recoveryInfo object holds source_host (live segment), but not the target_host.
     tests = [
         {
             "name":
             "single_target_host_suggest_full_and_incr",
             "mirrors_to_build": [
                 GpMirrorToBuild(self.m3, self.p3, None, True),
                 GpMirrorToBuild(self.m4, self.p4, None, False)
             ],
             "expected": {
                 'sdw3': [
                     RecoveryInfo(
                         '/data/mirror3', 7000, 7, 'sdw2', 3000, True,
                         '/tmp/logdir/pg_basebackup.111.dbid7.out'),
                     RecoveryInfo('/data/mirror4', 8000, 8, 'sdw3', 4000,
                                  False,
                                  '/tmp/logdir/pg_rewind.111.dbid8.out')
                 ]
             }
         },
         {
             "name":
             "single_target_hosts_suggest_full_and_incr_with_failover",
             "mirrors_to_build": [
                 GpMirrorToBuild(self.m1, self.p1, self.m5, True),
                 GpMirrorToBuild(self.m2, self.p2, self.m6, False)
             ],
             "expected": {
                 'sdw4': [
                     RecoveryInfo(
                         '/data/mirror5', 9000, 5, 'sdw1', 1000, True,
                         '/tmp/logdir/pg_basebackup.111.dbid5.out'),
                     RecoveryInfo(
                         '/data/mirror6', 10000, 6, 'sdw2', 2000, True,
                         '/tmp/logdir/pg_basebackup.111.dbid6.out')
                 ]
             }
         },
         {
             "name":
             "multiple_target_hosts_suggest_full",
             "mirrors_to_build": [
                 GpMirrorToBuild(self.m1, self.p1, None, True),
                 GpMirrorToBuild(self.m2, self.p2, None, True)
             ],
             "expected": {
                 'sdw2': [
                     RecoveryInfo(
                         '/data/mirror1', 5000, 5, 'sdw1', 1000, True,
                         '/tmp/logdir/pg_basebackup.111.dbid5.out')
                 ],
                 'sdw1': [
                     RecoveryInfo(
                         '/data/mirror2', 6000, 6, 'sdw2', 2000, True,
                         '/tmp/logdir/pg_basebackup.111.dbid6.out')
                 ]
             }
         },
         {
             "name":
             "multiple_target_hosts_suggest_full_and_incr",
             "mirrors_to_build": [
                 GpMirrorToBuild(self.m1, self.p1, None, True),
                 GpMirrorToBuild(self.m3, self.p3, None, False),
                 GpMirrorToBuild(self.m4, self.p4, None, True)
             ],
             "expected": {
                 'sdw2': [
                     RecoveryInfo(
                         '/data/mirror1', 5000, 5, 'sdw1', 1000, True,
                         '/tmp/logdir/pg_basebackup.111.dbid5.out')
                 ],
                 'sdw3': [
                     RecoveryInfo('/data/mirror3', 7000, 7, 'sdw2', 3000,
                                  False,
                                  '/tmp/logdir/pg_rewind.111.dbid7.out'),
                     RecoveryInfo(
                         '/data/mirror4', 8000, 8, 'sdw3', 4000, True,
                         '/tmp/logdir/pg_basebackup.111.dbid8.out')
                 ]
             }
         },
         {
             "name":
             "multiple_target_hosts_suggest_incr_failover_same_as_failed",
             "mirrors_to_build": [
                 GpMirrorToBuild(self.m1, self.p1, self.m1, False),
                 GpMirrorToBuild(self.m2, self.p2, self.m2, False)
             ],
             "expected": {
                 'sdw2': [
                     RecoveryInfo(
                         '/data/mirror1', 5000, 5, 'sdw1', 1000, True,
                         '/tmp/logdir/pg_basebackup.111.dbid5.out')
                 ],
                 'sdw1': [
                     RecoveryInfo(
                         '/data/mirror2', 6000, 6, 'sdw2', 2000, True,
                         '/tmp/logdir/pg_basebackup.111.dbid6.out')
                 ]
             }
         },
         {
             "name":
             "multiple_target_hosts_suggest_full_failover_same_as_failed",
             "mirrors_to_build": [
                 GpMirrorToBuild(self.m1, self.p1, self.m1, True),
                 GpMirrorToBuild(self.m3, self.p3, self.m3, True),
                 GpMirrorToBuild(self.m4, self.p4, None, True)
             ],
             "expected": {
                 'sdw2': [
                     RecoveryInfo(
                         '/data/mirror1', 5000, 5, 'sdw1', 1000, True,
                         '/tmp/logdir/pg_basebackup.111.dbid5.out')
                 ],
                 'sdw3': [
                     RecoveryInfo(
                         '/data/mirror3', 7000, 7, 'sdw2', 3000, True,
                         '/tmp/logdir/pg_basebackup.111.dbid7.out'),
                     RecoveryInfo(
                         '/data/mirror4', 8000, 8, 'sdw3', 4000, True,
                         '/tmp/logdir/pg_basebackup.111.dbid8.out')
                 ]
             }
         },
         {
             "name":
             "multiple_target_hosts_suggest_full_and_incr",
             "mirrors_to_build": [
                 GpMirrorToBuild(self.m1, self.p1, self.m5, True),
                 GpMirrorToBuild(self.m2, self.p2, None, False),
                 GpMirrorToBuild(self.m3, self.p3, self.m3, False),
                 GpMirrorToBuild(self.m4, self.p4, self.m8, True)
             ],
             "expected": {
                 'sdw4': [
                     RecoveryInfo(
                         '/data/mirror5', 9000, 5, 'sdw1', 1000, True,
                         '/tmp/logdir/pg_basebackup.111.dbid5.out'),
                 ],
                 'sdw1': [
                     RecoveryInfo('/data/mirror2', 6000, 6, 'sdw2', 2000,
                                  False,
                                  '/tmp/logdir/pg_rewind.111.dbid6.out'),
                     RecoveryInfo(
                         '/data/mirror8', 12000, 8, 'sdw3', 4000, True,
                         '/tmp/logdir/pg_basebackup.111.dbid8.out')
                 ],
                 'sdw3': [
                     RecoveryInfo(
                         '/data/mirror3', 7000, 7, 'sdw2', 3000, True,
                         '/tmp/logdir/pg_basebackup.111.dbid7.out')
                 ]
             }
         },
     ]
     self.run_tests(tests)