Пример #1
0
    def test_timezone_aware_parser(self):
        """
        Test the timezone_aware_parser method with different string
        formats
        """
        # test case 1 string with timezone info
        tz_string = '2009/05/13 19:19:30 -0400'
        result = load_datetime_tz(tz_string)
        assert result.tzinfo == tzoffset(None, -14400)

        # test case 2 string with timezone info with a different format
        tz_string = '2004-04-09T21:39:00-08:00'
        result = load_datetime_tz(tz_string)
        assert result.tzinfo == tzoffset(None, -28800)

        # test case 3 string without timezone info,
        # expecting tzlocal() as timezone
        tz_string = str(datetime.now())
        result = load_datetime_tz(tz_string)
        assert result.tzinfo == tzlocal()

        # test case 4 string with a wrong timezone format,
        # expecting tzlocal() as timezone
        tz_string = '16:08:12 05/08/03 AEST'
        result = load_datetime_tz(tz_string)
        assert result.tzinfo == tzlocal()
Пример #2
0
    def test_timezone_aware_parser(self):
        """
        Test the timezone_aware_parser method with different string
        formats
        """
        # test case 1 string with timezone info
        tz_string = '2009/05/13 19:19:30 -0400'
        result = load_datetime_tz(tz_string)
        assert result.tzinfo == tzoffset(None, -14400)

        # test case 2 string with timezone info with a different format
        tz_string = '2004-04-09T21:39:00-08:00'
        result = load_datetime_tz(tz_string)
        assert result.tzinfo == tzoffset(None, -28800)

        # test case 3 string without timezone info,
        # expecting tzlocal() as timezone
        tz_string = str(datetime.now())
        result = load_datetime_tz(tz_string)
        assert result.tzinfo == tzlocal()

        # test case 4 string with a wrong timezone format,
        # expecting tzlocal() as timezone
        tz_string = '16:08:12 05/08/03 AEST'
        result = load_datetime_tz(tz_string)
        assert result.tzinfo == tzlocal()
Пример #3
0
    def test_timezone_aware_parser(self):
        """
        Test the timezone_aware_parser method with different string
        formats
        """
        # test case 1 string with timezone info
        tz_string = '2009/05/13 19:19:30 -0400'
        result = load_datetime_tz(tz_string)
        assert result.tzinfo == tzoffset(None, -14400)

        # test case 2 string with timezone info with a different format
        tz_string = '2004-04-09T21:39:00-08:00'
        result = load_datetime_tz(tz_string)
        assert result.tzinfo == tzoffset(None, -28800)

        # test case 3 string without timezone info,
        # expecting tzlocal() as timezone
        tz_string = str(datetime.now())
        result = load_datetime_tz(tz_string)
        assert result.tzinfo == tzlocal()

        # test case 4 string with a wrong timezone format,
        # expecting tzlocal() as timezone
        tz_string = '16:08:12 05/08/03 AEST'
        with warnings.catch_warnings():
            warnings.filterwarnings(
                "ignore", message="tzname AEST identified but not understood.")
            result = load_datetime_tz(tz_string)
        assert result.tzinfo == tzlocal()
Пример #4
0
def test_load_datetime_tz():
    """
    Unit test for load_datetime_tz function

    This test covers all load_datetime_tz code with correct parameters
    and checks that a ValueError is raised when called with a bad parameter.
    """
    # try to load a tz-less timestamp
    assert load_datetime_tz("2012-12-15 10:14:51.898000") == datetime(
        2012, 12, 15, 10, 14, 51, 898000, tzinfo=tzlocal())

    # try to load a tz-aware timestamp
    assert load_datetime_tz("2012-12-15 10:14:51.898000 +0100") == datetime(
        2012, 12, 15, 10, 14, 51, 898000, tzinfo=tzoffset("GMT+1", 3600))

    # try to load an incorrect date
    with pytest.raises(ValueError):
        load_datetime_tz("Invalid datetime")
Пример #5
0
def test_load_datetime_tz():
    """
    Unit test for load_datetime_tz function

    This test covers all load_datetime_tz code with correct parameters
    and checks that a ValueError is raised when called with a bad parameter.
    """
    # try to load a tz-less timestamp
    assert load_datetime_tz("2012-12-15 10:14:51.898000") == \
        datetime(2012, 12, 15, 10, 14, 51, 898000,
                 tzinfo=tzlocal())

    # try to load a tz-aware timestamp
    assert load_datetime_tz("2012-12-15 10:14:51.898000 +0100") == \
        datetime(2012, 12, 15, 10, 14, 51, 898000,
                 tzinfo=tzoffset('GMT+1', 3600))

    # try to load an incorrect date
    with pytest.raises(ValueError):
        load_datetime_tz("Invalid datetime")
Пример #6
0
    def _set_pitr_targets(self, recovery_info, backup_info, dest, target_name,
                          target_time, target_tli, target_xid, target_barrier,
                          target_immediate):
        """
        Set PITR targets - as specified by the user

        :param dict recovery_info: Dictionary containing all the recovery
            parameters
        :param barman.infofile.BackupInfo backup_info: representation of a
            backup
        :param str dest: destination directory of the recovery
        :param str|None target_name: recovery target name for PITR
        :param str|None target_time: recovery target time for PITR
        :param str|None target_tli: recovery target timeline for PITR
        :param str|None target_xid: recovery target transaction id for PITR
        :param str|None target_barrier: recovery target barrier id for PITR       
        :param bool|None target_immediate: end recovery as soon as consistency
            is reached
        """
        target_epoch = None
        target_datetime = None
        # Detect PITR
        if (target_time or target_xid or target_barrier
                or (target_tli and target_tli != backup_info.timeline)
                or target_name or recovery_info['get_wal']
                or (backup_info.version >= 90400 and target_immediate)):
            recovery_info['is_pitr'] = True
            targets = {}
            if target_time:
                # noinspection PyBroadException
                try:
                    target_datetime = dateutil.parser.parse(target_time)
                except ValueError as e:
                    output.error(
                        "unable to parse the target time parameter %r: %s",
                        target_time, e)
                    self._teardown(recovery_info)
                    output.close_and_exit()
                except Exception:
                    # this should not happen, but there is a known bug in
                    # dateutil.parser.parse() implementation
                    # ref: https://bugs.launchpad.net/dateutil/+bug/1247643
                    output.error(
                        "unable to parse the target time parameter %r",
                        target_time)
                    output.close_and_exit()

                target_epoch = (time.mktime(target_datetime.timetuple()) +
                                (target_datetime.microsecond / 1000000.))
                targets['time'] = str(target_datetime)
                output.info("target_time is %s", str(target_datetime))
                output.info("backup begin_time is %s",
                            str(backup_info.begin_time))
                if load_datetime_tz(
                        str(target_datetime)) < backup_info.begin_time:
                    output.error(
                        "target_time should be greater than the backup begin time"
                    )
                    output.close_and_exit()
            if target_xid:
                targets['xid'] = str(target_xid)
            if target_barrier:
                targets['barrier'] = str(target_barrier)
            if target_tli and target_tli != backup_info.timeline:
                targets['timeline'] = str(target_tli)
            if target_name:
                targets['name'] = str(target_name)
            if target_immediate:
                targets['immediate'] = target_immediate
            output.info(
                "Doing PITR. Recovery target %s",
                (", ".join(["%s: %r" % (k, v) for k, v in targets.items()])))
            recovery_info['wal_dest'] = os.path.join(dest, 'barman_xlog')

            # With a PostgreSQL version older than 8.4, it is the user's
            # responsibility to delete the "barman_xlog" directory as the
            # restore_command option in recovery.conf is not supported
            if backup_info.version < 80400 and \
                    not recovery_info['get_wal']:
                recovery_info['results']['delete_barman_xlog'] = True
        recovery_info['target_epoch'] = target_epoch
        recovery_info['target_datetime'] = target_datetime