<s> from __future__ import unicode_literals <EOL> from datetime import timedelta <EOL> from mock import patch <EOL> from django . conf import settings <EOL> from django . test import TestCase <EOL> from django . utils import timezone <EOL> from nodeconductor . backup . tests import factories <EOL> from nodeconductor . backup import models <EOL> from nodeconductor . iaas . models import Instance <EOL> from nodeconductor . iaas . tests . factories import InstanceFactory <EOL> class BackupScheduleTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . backup_source = InstanceFactory ( state = Instance . States . OFFLINE ) <EOL> def test_update_next_trigger_at ( self ) : <EOL> now = timezone . now ( ) <EOL> schedule = factories . BackupScheduleFactory ( ) <EOL> schedule . schedule = '<STR_LIT>' <EOL> schedule . _update_next_trigger_at ( ) <EOL> self . assertTrue ( schedule . next_trigger_at ) <EOL> self . assertGreater ( schedule . next_trigger_at , now ) <EOL> def test_update_next_trigger_at_with_provided_timezone ( self ) : <EOL> schedule = factories . BackupScheduleFactory ( timezone = '<STR_LIT>' ) <EOL> schedule . _update_next_trigger_at ( ) <EOL> self . assertEqual ( schedule . timezone , schedule . next_trigger_at . tzinfo . zone ) <EOL> def test_update_next_trigger_at_with_default_timezone ( self ) : <EOL> schedule = factories . BackupScheduleFactory ( ) <EOL> schedule . _update_next_trigger_at ( ) <EOL> self . assertEqual ( settings . TIME_ZONE , schedule . timezone ) <EOL> def test_create_backup ( self ) : <EOL> now = timezone . now ( ) <EOL> schedule = factories . BackupScheduleFactory ( retention_time = <NUM_LIT:3> , backup_source = self . backup_source ) <EOL> schedule . _create_backup ( ) <EOL> backup = models . Backup . objects . get ( backup_schedule = schedule ) <EOL> self . assertFalse ( backup . kept_until is None ) <EOL> self . assertGreater ( backup . kept_until , now - timedelta ( days = schedule . retention_time ) ) <EOL> def test_execute ( self ) : <EOL> schedule = factories . BackupScheduleFactory ( maximal_number_of_backups = <NUM_LIT:1> , backup_source = self . backup_source ) <EOL> old_backup1 = factories . BackupFactory ( backup_schedule = schedule ) <EOL> old_backup2 = factories . BackupFactory ( backup_schedule = schedule ) <EOL> deleted_backup = factories . BackupFactory ( backup_schedule = schedule , state = models . Backup . States . DELETED ) <EOL> schedule . execute ( ) <EOL> old_backup1 = models . Backup . objects . get ( pk = old_backup1 . pk ) <EOL> self . assertEqual ( old_backup1 . state , models . Backup . States . DELETING ) <EOL> old_backup2 = models . Backup . objects . get ( pk = old_backup2 . pk ) <EOL> self . assertEqual ( old_backup2 . state , models . Backup . States . DELETING ) <EOL> self . assertEqual ( deleted_backup . state , models . Backup . States . DELETED ) <EOL> self . assertTrue ( models . Backup . objects . filter ( <EOL> backup_schedule = schedule , state = models . Backup . States . BACKING_UP ) . exists ( ) ) <EOL> self . assertGreater ( schedule . next_trigger_at , timezone . now ( ) ) <EOL> def test_save ( self ) : <EOL> schedule = factories . BackupScheduleFactory ( next_trigger_at = None ) <EOL> self . assertGreater ( schedule . next_trigger_at , timezone . now ( ) ) <EOL> schedule . is_active = False <EOL> schedule . next_trigger_at = None <EOL> schedule . save ( ) <EOL> schedule . is_active = True <EOL> schedule . save ( ) <EOL> self . assertGreater ( schedule . next_trigger_at , timezone . now ( ) ) <EOL> schedule . next_trigger_at = None <EOL> schedule . schedule = '<STR_LIT>' <EOL> schedule . save ( ) <EOL> schedule = models . BackupSchedule . objects . get ( id = schedule . id ) <EOL> self . assertGreater ( schedule . next_trigger_at , timezone . now ( ) ) <EOL> class BackupTest ( TestCase ) : <EOL> class MockedAsyncResult ( object ) : <EOL> def __call__ ( self , * args ) : <EOL> return self if not self . _is_none else None <EOL> def __init__ ( self , ready , is_none = False ) : <EOL> self . _ready = ready <EOL> self . _is_none = is_none <EOL> def ready ( self ) : <EOL> return self . _ready <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_start_backup ( self , mocked_task ) : <EOL> backup = factories . BackupFactory ( ) <EOL> backup . start_backup ( ) <EOL> mocked_task . assert_called_with ( backup . uuid . hex ) <EOL> self . assertEqual ( backup . state , models . Backup . States . BACKING_UP ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_start_restoration ( self , mocked_task ) : <EOL> backup = factories . BackupFactory ( ) <EOL> from nodeconductor . iaas . tests import factories as iaas_factories <EOL> instance = iaas_factories . InstanceFactory ( ) <EOL> user_input = { } <EOL> snapshot_ids = [ ] <EOL> backup . start_restoration ( instance . uuid , user_input , snapshot_ids ) <EOL> mocked_task . assert_called_with ( backup . uuid . hex , instance . uuid . hex , user_input , snapshot_ids ) <EOL> self . assertEqual ( backup . state , models . Backup . States . RESTORING ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_start_deletion ( self , mocked_task ) : <EOL> backup = factories . BackupFactory ( ) <EOL> backup . start_deletion ( ) <EOL> mocked_task . assert_called_with ( backup . uuid . hex ) <EOL> self . assertEqual ( backup . state , models . Backup . States . DELETING ) </s>
