<s> import unittest <EOL> from freezer . storage import base <EOL> import mock <EOL> class TestBackup ( unittest . TestCase ) : <EOL> def test_backup_parse ( self ) : <EOL> self . assertRaises ( ValueError , base . Backup . _parse , "<STR_LIT>" ) <EOL> backup = base . Backup . _parse ( "<STR_LIT>" ) <EOL> self . assertEqual ( backup . level , <NUM_LIT:0> ) <EOL> self . assertEqual ( backup . timestamp , <NUM_LIT> ) <EOL> self . assertEqual ( backup . hostname_backup_name , "<STR_LIT>" ) <EOL> def test_backup_creation ( self ) : <EOL> backup = base . Backup ( None , "<STR_LIT:name>" , <NUM_LIT> ) <EOL> self . assertEqual ( backup . hostname_backup_name , "<STR_LIT:name>" ) <EOL> self . assertEqual ( backup . timestamp , <NUM_LIT> ) <EOL> self . assertEqual ( backup . level , <NUM_LIT:0> ) <EOL> self . assertEqual ( backup . latest_update . level , <NUM_LIT:0> ) <EOL> self . assertEqual ( backup . latest_update . timestamp , <NUM_LIT> ) <EOL> self . assertEqual ( backup . latest_update . hostname_backup_name , "<STR_LIT:name>" ) <EOL> self . assertEqual ( len ( backup . increments ) , <NUM_LIT:1> ) <EOL> def test_backup_full_backup ( self ) : <EOL> ok = False <EOL> try : <EOL> base . Backup ( None , "<STR_LIT:name>" , <NUM_LIT> , <NUM_LIT:0> , "<STR_LIT>" ) <EOL> except ValueError : <EOL> ok = True <EOL> if not ok : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> def test_backup_increment ( self ) : <EOL> backup = base . Backup ( None , "<STR_LIT:name>" , <NUM_LIT> ) <EOL> self . assertRaises ( ValueError , backup . add_increment , backup ) <EOL> increment = base . Backup ( None , "<STR_LIT:name>" , <NUM_LIT> , <NUM_LIT:1> , backup ) <EOL> backup . add_increment ( increment ) <EOL> self . assertEqual ( len ( backup . increments ) , <NUM_LIT:2> ) <EOL> def test__find_previous_backup ( self ) : <EOL> backup = base . Backup ( None , "<STR_LIT:name>" , <NUM_LIT> ) <EOL> b = base . Storage . _find_previous_backup ( [ backup ] , False , <NUM_LIT:2> , False , <NUM_LIT:0> ) <EOL> assert b == backup <EOL> def test__find_previous_backup_with_max_level ( self ) : <EOL> backup = base . Backup ( None , "<STR_LIT:name>" , <NUM_LIT> ) <EOL> i1 = base . Backup ( None , "<STR_LIT:name>" , <NUM_LIT> , <NUM_LIT:1> , backup ) <EOL> i2 = base . Backup ( None , "<STR_LIT:name>" , <NUM_LIT> , <NUM_LIT:2> , backup ) <EOL> backup . add_increment ( i1 ) <EOL> backup . add_increment ( i2 ) <EOL> b = base . Storage . _find_previous_backup ( [ backup ] , False , <NUM_LIT:2> , False , <NUM_LIT:0> ) <EOL> assert not b <EOL> def test__find_previous_backup_with_max_level_not_reached ( self ) : <EOL> backup = base . Backup ( None , "<STR_LIT:name>" , <NUM_LIT> ) <EOL> i1 = base . Backup ( None , "<STR_LIT:name>" , <NUM_LIT> , <NUM_LIT:1> , backup ) <EOL> backup . add_increment ( i1 ) <EOL> b = base . Storage . _find_previous_backup ( [ backup ] , False , <NUM_LIT:2> , False , <NUM_LIT:0> ) <EOL> assert b == i1 <EOL> def test__find_previous_backup_with_always_level_reached ( self ) : <EOL> backup = base . Backup ( None , "<STR_LIT:name>" , <NUM_LIT> ) <EOL> i1 = base . Backup ( None , "<STR_LIT:name>" , <NUM_LIT> , <NUM_LIT:1> , backup ) <EOL> i2 = base . Backup ( None , "<STR_LIT:name>" , <NUM_LIT> , <NUM_LIT:2> , backup ) <EOL> backup . add_increment ( i1 ) <EOL> backup . add_increment ( i2 ) <EOL> b = base . Storage . _find_previous_backup ( [ backup ] , False , False , <NUM_LIT:2> , <NUM_LIT:0> ) <EOL> assert b == i1 <EOL> def test__find_previous_backup_with_always_level_reached_2 ( self ) : <EOL> backup = base . Backup ( None , "<STR_LIT:name>" , <NUM_LIT> ) <EOL> i1 = base . Backup ( None , "<STR_LIT:name>" , <NUM_LIT> , <NUM_LIT:1> , backup ) <EOL> i2 = base . Backup ( None , "<STR_LIT:name>" , <NUM_LIT> , <NUM_LIT:2> , backup ) <EOL> backup . add_increment ( i1 ) <EOL> backup . add_increment ( i2 ) <EOL> b = base . Storage . _find_previous_backup ( [ backup ] , False , False , <NUM_LIT:3> , <NUM_LIT:0> ) <EOL> assert b == i2 <EOL> def test_add_increment_raises ( self ) : <EOL> backup = base . Backup ( None , "<STR_LIT:name>" , <NUM_LIT> , level = <NUM_LIT:3> ) <EOL> self . assertRaises ( ValueError , backup . add_increment , None ) <EOL> def test_restore_latest_backup ( self ) : <EOL> t = base . Storage ( "<STR_LIT>" , skip_prepare = True ) <EOL> t . find_all = mock . Mock ( ) <EOL> last = base . Backup ( t , "<STR_LIT>" , <NUM_LIT> ) <EOL> t . find_all . return_value = [ <EOL> base . Backup ( t , "<STR_LIT>" , <NUM_LIT:1000> ) , <EOL> base . Backup ( t , "<STR_LIT>" , <NUM_LIT> ) , <EOL> base . Backup ( t , "<STR_LIT>" , <NUM_LIT> ) , <EOL> base . Backup ( t , "<STR_LIT>" , <NUM_LIT> ) , <EOL> base . Backup ( t , "<STR_LIT>" , <NUM_LIT:1000> ) , <EOL> last <EOL> ] <EOL> assert t . find_one ( "<STR_LIT>" ) == last <EOL> def test_find_latest_backup_respects_increments_timestamp ( self ) : <EOL> test_backup = base . Backup ( None , "<STR_LIT>" , <NUM_LIT> ) <EOL> increment = base . Backup ( None , "<STR_LIT>" , <NUM_LIT> , <NUM_LIT:1> , test_backup ) <EOL> test_backup . add_increment ( increment ) <EOL> t = base . Storage ( None , skip_prepare = True ) <EOL> t . find_all = mock . Mock ( ) <EOL> t . find_all . return_value = [ <EOL> test_backup , <EOL> base . Backup ( None , "<STR_LIT>" , <NUM_LIT> ) , <EOL> base . Backup ( None , "<STR_LIT>" , <NUM_LIT> ) , <EOL> base . Backup ( None , "<STR_LIT>" , <NUM_LIT> ) , <EOL> base . Backup ( None , "<STR_LIT>" , <NUM_LIT:1000> ) , <EOL> base . Backup ( None , "<STR_LIT>" , <NUM_LIT> ) , <EOL> ] <EOL> assert t . find_one ( "<STR_LIT>" ) == increment <EOL> def test_restore_from_date ( self ) : <EOL> t = base . Storage ( None , skip_prepare = True ) <EOL> t . find_all = mock . Mock ( ) <EOL> backup_restore = base . Backup ( None , "<STR_LIT>" , <NUM_LIT> ) <EOL> t . find_all . return_value = [ <EOL> base . Backup ( None , "<STR_LIT>" , <NUM_LIT:1000> ) , <EOL> base . Backup ( None , "<STR_LIT>" , <NUM_LIT> ) , <EOL> backup_restore , <EOL> base . Backup ( None , "<STR_LIT>" , <NUM_LIT> ) , <EOL> base . Backup ( None , "<STR_LIT>" , <NUM_LIT:1000> ) , <EOL> base . Backup ( None , "<STR_LIT>" , <NUM_LIT> ) , <EOL> ] <EOL> assert t . find_one ( "<STR_LIT>" , <NUM_LIT> ) == backup_restore <EOL> def test_restore_from_date_increment ( self ) : <EOL> t = base . Storage ( None , skip_prepare = True ) <EOL> t . find_all = mock . Mock ( ) <EOL> test_backup = base . Backup ( None , "<STR_LIT>" , <NUM_LIT:1000> ) <EOL> increment = base . Backup ( None , "<STR_LIT>" , <NUM_LIT> , <NUM_LIT:1> , test_backup ) <EOL> test_backup . add_increment ( increment ) <EOL> t . find_all . return_value = [ <EOL> test_backup , <EOL> base . Backup ( None , "<STR_LIT>" , <NUM_LIT> ) , <EOL> base . Backup ( None , "<STR_LIT>" , <NUM_LIT:1000> ) , <EOL> base . Backup ( None , "<STR_LIT>" , <NUM_LIT> ) , <EOL> ] <EOL> assert t . find_one ( "<STR_LIT>" , <NUM_LIT> ) == increment <EOL> def test__get_backups_wrong_name ( self ) : <EOL> result = base . Backup . parse_backups ( [ "<STR_LIT>" ] , None ) <EOL> assert len ( result ) == <NUM_LIT:0> <EOL> result = base . Backup . parse_backups ( [ "<STR_LIT>" ] , None ) <EOL> assert len ( result ) == <NUM_LIT:0> <EOL> def test__get_backups_good_name ( self ) : <EOL> result = base . Backup . parse_backups ( [ "<STR_LIT>" ] , None ) <EOL> assert len ( result ) == <NUM_LIT:1> <EOL> result = result [ <NUM_LIT:0> ] <EOL> assert result . hostname_backup_name == "<STR_LIT>" <EOL> assert result . timestamp == <NUM_LIT:100> <EOL> assert result . level == <NUM_LIT:0> <EOL> def test_remove_older_than ( self ) : <EOL> t = base . Storage ( None , skip_prepare = True ) <EOL> t . find_all = mock . Mock ( ) <EOL> r1 = base . Backup ( t , "<STR_LIT>" , <NUM_LIT:1000> ) <EOL> r2 = base . Backup ( t , "<STR_LIT>" , <NUM_LIT> ) <EOL> t . find_all . return_value = [ <EOL> r1 , <EOL> r2 , <EOL> base . Backup ( t , "<STR_LIT>" , <NUM_LIT> ) , <EOL> base . Backup ( t , "<STR_LIT>" , <NUM_LIT> ) , <EOL> base . Backup ( t , "<STR_LIT>" , <NUM_LIT> ) , <EOL> ] <EOL> t . remove_backup = mock . Mock ( ) <EOL> t . remove_older_than ( <NUM_LIT> , "<STR_LIT>" ) <EOL> t . remove_backup . assert_any_call ( r1 ) <EOL> t . remove_backup . assert_any_call ( r2 ) <EOL> assert t . remove_backup . call_count == <NUM_LIT:2> <EOL> def test_create_backup ( self ) : <EOL> t = base . Storage ( None , skip_prepare = True ) <EOL> t . find_all = mock . Mock ( ) <EOL> t . find_all . return_value = [ ] <EOL> t . _find_previous_backup = mock . Mock ( ) <EOL> t . _find_previous_backup . return_value = base . Backup ( None , "<STR_LIT>" , <NUM_LIT> , tar_meta = True ) <EOL> t . create_backup ( "<STR_LIT>" , True , <NUM_LIT:12> , False , False ) <EOL> def test_restart_always_level ( self ) : <EOL> t = base . Storage ( None , skip_prepare = True ) <EOL> t . find_all = mock . Mock ( ) <EOL> t . find_all . return_value = [ ] <EOL> backup = base . Backup ( None , "<STR_LIT>" , <NUM_LIT> , tar_meta = True ) <EOL> t . _find_previous_backup ( [ backup ] , False , None , None , <NUM_LIT:10> ) </s>
