#!/usr/bin/env python
# -*- Mode: Python; indent-tabs-mode: nil; tab-width: 2; coding: utf-8 -*-
#
# This file is part of Déjà Dup.
# For copyright information, see AUTHORS.
#
# Déjà Dup is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Déjà Dup is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Déjà Dup.  If not, see <http://www.gnu.org/licenses/>.

# Test whether we correctly switch to full backup mode vs incremental

import sys
sys.path.insert(0, sys.path[0]+'/..')
import base
import ldtp

max_allowed = 24 * 7
scale = 12

def max_inc():
  base.setup()
  base.backup_simple(backend='file', encrypt=False, includes=['data/simple'])
  # Change date to maximum allowed incremental, confirm next is incremental
  base.last_date_change('%d days ago' % (max_allowed))
  base.backup_simple(encrypt=None)
  assert base.last_type() == 'inc', base.list_manifests()

def max_full():
  base.setup()
  base.backup_simple(backend='file', encrypt=False, includes=['data/simple'])
  # Change date to maximum allowed incremental+1, confirm next is full
  base.last_date_change('%d days ago' % (max_allowed+1))
  base.backup_simple(encrypt=None)
  assert base.last_type() == 'full', base.list_manifests()

def half_inc():
  base.setup()
  base.set_settings_value("delete-after", '80')
  base.backup_simple(backend='file', encrypt=False, includes=['data/simple'])
  # Change date to maximum allowed incremental, confirm next is inc
  base.last_date_change('%d days ago' % (40))
  base.backup_simple(encrypt=None)
  assert base.last_type() == 'inc', base.list_manifests()

def half_full():
  base.setup()
  base.set_settings_value("delete-after", '80')
  base.backup_simple(backend='file', encrypt=False, includes=['data/simple'])
  # Change date to maximum allowed incremental+1, confirm next is full
  base.last_date_change('%d days ago' % (41))
  base.backup_simple(encrypt=None)
  assert base.last_type() == 'full', base.list_manifests()

def period_inc():
  base.setup()
  base.set_settings_value("periodic", 'true')
  base.set_settings_value("periodic-period", '7')
  base.backup_simple(backend='file', encrypt=False, includes=['data/simple'])
  # Change date to maximum allowed incremental, confirm next is inc
  base.last_date_change('%d days ago' % (7*scale))
  base.backup_simple(encrypt=None)
  assert base.last_type() == 'inc', base.list_manifests()

def period_full():
  base.setup()
  base.set_settings_value("periodic", 'true')
  base.set_settings_value("periodic-period", '7')
  base.backup_simple(backend='file', encrypt=False, includes=['data/simple'])
  # Change date to maximum allowed incremental+1, confirm next is full
  base.last_date_change('%d days ago' % ((7*scale)+1))
  base.backup_simple(encrypt=None)
  assert base.last_type() == 'full', base.list_manifests()

base.run(max_inc)
base.run(max_full)
base.run(half_inc)
base.run(half_full) # optimism!
base.run(period_inc)
base.run(period_full)
