Main loop refactor
The code relative to the various actions is moved into their respective job objects. A factory function creates the correct type of job object according to the action requested. This replaces the long "if" chain in the main loop The main loop calls the Job object's execute() method. Change-Id: Iec1acfe62575ba062f9b56171784d1d74dc86556 Implements: blueprint freezer-main-refactor
This commit is contained in:
parent
14775ffc89
commit
b06be1bdfd
156
freezer/job.py
Normal file
156
freezer/job.py
Normal file
@ -0,0 +1,156 @@
|
||||
"""
|
||||
Copyright 2014 Hewlett-Packard
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
This product includes cryptographic software written by Eric Young
|
||||
(eay@cryptsoft.com). This product includes software written by Tim
|
||||
Hudson (tjh@cryptsoft.com).
|
||||
========================================================================
|
||||
"""
|
||||
|
||||
from freezer import swift
|
||||
from freezer import utils
|
||||
from freezer import backup
|
||||
from freezer import restore
|
||||
|
||||
import logging
|
||||
|
||||
|
||||
class Job:
|
||||
def __init__(self, conf_dict):
|
||||
self.conf = conf_dict
|
||||
|
||||
def execute(self):
|
||||
logging.info('[*] Action not implemented')
|
||||
|
||||
@staticmethod
|
||||
def executemethod(func):
|
||||
def wrapper(self):
|
||||
self.time_stamp = utils.TimeStamp.now()
|
||||
self.conf.time_stamp = self.time_stamp
|
||||
logging.info('[*] Job execution Started at: {0}'.
|
||||
format(self.time_stamp.strtime()))
|
||||
|
||||
self.conf = swift.get_client(self.conf)
|
||||
self.conf = swift.get_containers_list(self.conf)
|
||||
retval = func(self)
|
||||
|
||||
end_time = utils.TimeStamp.now()
|
||||
logging.info('[*] Job execution Finished, at: {0}'.
|
||||
format(end_time.strtime()))
|
||||
logging.info('[*] Job time Elapsed: {0}'.
|
||||
format(end_time - self.time_stamp))
|
||||
return retval
|
||||
return wrapper
|
||||
|
||||
|
||||
class InfoJob(Job):
|
||||
@Job.executemethod
|
||||
def execute(self):
|
||||
if self.conf.list_container:
|
||||
swift.show_containers(self.conf)
|
||||
elif self.conf.list_objects:
|
||||
containers = swift.check_container_existance(self.conf)
|
||||
if containers['main_container'] is not True:
|
||||
logging.critical(
|
||||
'[*] Container {0} not available'.format(
|
||||
self.conf.container))
|
||||
return False
|
||||
self.conf = swift.get_container_content(self.conf)
|
||||
swift.show_objects(self.conf)
|
||||
else:
|
||||
logging.warning(
|
||||
'[*] No retrieving info options were set. Exiting.')
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
class BackupJob(Job):
|
||||
@Job.executemethod
|
||||
def execute(self):
|
||||
containers = swift.check_container_existance(self.conf)
|
||||
|
||||
if containers['main_container'] is not True:
|
||||
swift.create_containers(self.conf)
|
||||
|
||||
if self.conf.no_incremental:
|
||||
if self.conf.max_backup_level or \
|
||||
self.conf.always_backup_level:
|
||||
raise Exception(
|
||||
'no-incremental option is not compatible '
|
||||
'with backup level options')
|
||||
manifest_meta_dict = {}
|
||||
else:
|
||||
# Get the object list of the remote containers
|
||||
# and store it in self.conf.remote_obj_list
|
||||
self.conf = swift.get_container_content(self.conf)
|
||||
|
||||
# Check if a backup exist in swift with same name.
|
||||
# If not, set backup level to 0
|
||||
manifest_meta_dict =\
|
||||
utils.check_backup_and_tar_meta_existence(self.conf)
|
||||
|
||||
(self.conf, manifest_meta_dict) = utils.set_backup_level(
|
||||
self.conf, manifest_meta_dict)
|
||||
|
||||
self.conf.manifest_meta_dict = manifest_meta_dict
|
||||
|
||||
if self.conf.mode == 'fs':
|
||||
backup.backup_mode_fs(
|
||||
self.conf, self.time_stamp, manifest_meta_dict)
|
||||
elif self.conf.mode == 'mongo':
|
||||
backup.backup_mode_mongo(
|
||||
self.conf, self.time_stamp, manifest_meta_dict)
|
||||
elif self.conf.mode == 'mysql':
|
||||
backup.backup_mode_mysql(
|
||||
self.conf, self.time_stamp, manifest_meta_dict)
|
||||
else:
|
||||
raise ValueError('Please provide a valid backup mode')
|
||||
|
||||
|
||||
class RestoreJob(Job):
|
||||
@Job.executemethod
|
||||
def execute(self):
|
||||
logging.info('[*] Executing FS restore...')
|
||||
|
||||
# Check if the provided container already exists in swift.
|
||||
containers = swift.check_container_existance(self.conf)
|
||||
if containers['main_container'] is not True:
|
||||
raise ValueError('Container: {0} not found. Please provide an '
|
||||
'existing container.'
|
||||
.format(self.conf.container))
|
||||
|
||||
# Get the object list of the remote containers and store it in the
|
||||
# same dict passes as argument under the dict.remote_obj_list namespace
|
||||
self.conf = swift.get_container_content(self.conf)
|
||||
restore.restore_fs(self.conf)
|
||||
|
||||
|
||||
class AdminJob(Job):
|
||||
@Job.executemethod
|
||||
def execute(self):
|
||||
self.conf = swift.get_container_content(self.conf)
|
||||
swift.remove_obj_older_than(self.conf)
|
||||
|
||||
|
||||
def create_job(conf):
|
||||
if conf.action == 'backup':
|
||||
return BackupJob(conf)
|
||||
if conf.action == 'restore':
|
||||
return RestoreJob(conf)
|
||||
if conf.action == 'info':
|
||||
return InfoJob(conf)
|
||||
if conf.action == 'admin':
|
||||
return AdminJob(conf)
|
||||
raise Exception('Action "{0}" not supported'.format(conf.action))
|
112
freezer/main.py
112
freezer/main.py
@ -21,116 +21,12 @@ Hudson (tjh@cryptsoft.com).
|
||||
Freezer main execution function
|
||||
"""
|
||||
|
||||
from freezer import swift
|
||||
from freezer import utils
|
||||
from freezer import backup
|
||||
from freezer import restore
|
||||
|
||||
import logging
|
||||
from freezer import job
|
||||
|
||||
|
||||
def freezer_main(backup_args):
|
||||
|
||||
"""Freezer Main execution function.
|
||||
This main function is a wrapper for most
|
||||
of the other functions. By calling main() the program execution start
|
||||
and the respective actions are taken. If you want only use the single
|
||||
function is probably better to not import main()
|
||||
"""
|
||||
freezer_job = job.create_job(backup_args)
|
||||
freezer_job.execute()
|
||||
|
||||
# Computing execution start datetime and Timestamp
|
||||
(time_stamp, today_start) = utils.start_time()
|
||||
# Add timestamp to the arguments namespace
|
||||
backup_args.__dict__['time_stamp'] = time_stamp
|
||||
|
||||
# Initialize the swift connector and store it in the same dict passed
|
||||
# as argument under the dict.sw_connector namespace. This is helpful
|
||||
# so the swift client object doesn't need to be initialized every time
|
||||
backup_args = swift.get_client(backup_args)
|
||||
|
||||
# Get the list of the containers
|
||||
backup_args = swift.get_containers_list(backup_args)
|
||||
if backup_args.action == 'info' or backup_args.list_container or \
|
||||
backup_args.list_objects:
|
||||
if backup_args.list_container:
|
||||
swift.show_containers(backup_args)
|
||||
elif backup_args.list_objects:
|
||||
containers = swift.check_container_existance(backup_args)
|
||||
if containers['main_container'] is not True:
|
||||
logging.critical(
|
||||
'[*] Container {0} not available'.format(
|
||||
backup_args.container))
|
||||
return False
|
||||
backup_args = swift.get_container_content(backup_args)
|
||||
swift.show_objects(backup_args)
|
||||
else:
|
||||
logging.warning(
|
||||
'[*] No retrieving info options were set. Exiting.')
|
||||
utils.elapsed_time(today_start)
|
||||
return False
|
||||
utils.elapsed_time(today_start)
|
||||
return True
|
||||
|
||||
if backup_args.action == 'restore':
|
||||
logging.info('[*] Executing FS restore...')
|
||||
|
||||
# Check if the provided container already exists in swift.
|
||||
containers = swift.check_container_existance(backup_args)
|
||||
if containers['main_container'] is not True:
|
||||
raise ValueError('Container: {0} not found. Please provide an '
|
||||
'existing container.'
|
||||
.format(backup_args.container))
|
||||
|
||||
# Get the object list of the remote containers and store it in the
|
||||
# same dict passes as argument under the dict.remote_obj_list namespace
|
||||
backup_args = swift.get_container_content(backup_args)
|
||||
restore.restore_fs(backup_args)
|
||||
|
||||
if backup_args.action == 'backup':
|
||||
# Check if the provided container already exists in swift.
|
||||
containers = swift.check_container_existance(backup_args)
|
||||
if containers['main_container'] is not True:
|
||||
swift.create_containers(backup_args)
|
||||
|
||||
if backup_args.no_incremental:
|
||||
if backup_args.max_backup_level or \
|
||||
backup_args.always_backup_level:
|
||||
raise Exception(
|
||||
'no-incremental option not compatible '
|
||||
'with backup level options')
|
||||
manifest_meta_dict = {}
|
||||
else:
|
||||
# Get the object list of the remote containers and store it in
|
||||
# backup_args.remote_obj_list
|
||||
backup_args = swift.get_container_content(backup_args)
|
||||
|
||||
# Check if a backup exist in swift with same name. If not, set
|
||||
# backup level to 0
|
||||
manifest_meta_dict =\
|
||||
utils.check_backup_and_tar_meta_existence(backup_args)
|
||||
|
||||
(backup_args, manifest_meta_dict) = utils.set_backup_level(
|
||||
backup_args, manifest_meta_dict)
|
||||
|
||||
backup_args.manifest_meta_dict = manifest_meta_dict
|
||||
|
||||
if backup_args.mode == 'fs':
|
||||
backup.backup_mode_fs(
|
||||
backup_args, time_stamp, manifest_meta_dict)
|
||||
elif backup_args.mode == 'mongo':
|
||||
backup.backup_mode_mongo(
|
||||
backup_args, time_stamp, manifest_meta_dict)
|
||||
elif backup_args.mode == 'mysql':
|
||||
backup.backup_mode_mysql(
|
||||
backup_args, time_stamp, manifest_meta_dict)
|
||||
else:
|
||||
raise ValueError('Please provide a valid backup mode')
|
||||
|
||||
# Admin tasks code should go here, before moving it on a dedicated module
|
||||
if backup_args.action == 'admin':
|
||||
# Remove backups older if set.
|
||||
backup_args = swift.get_container_content(backup_args)
|
||||
swift.remove_obj_older_than(backup_args)
|
||||
|
||||
# Compute elapsed time
|
||||
utils.elapsed_time(today_start)
|
||||
return
|
||||
|
@ -368,32 +368,23 @@ def eval_restart_backup(backup_opt_dict):
|
||||
return False
|
||||
|
||||
|
||||
def start_time():
|
||||
'''
|
||||
Compute start execution time, write it in the logs and return timestamp
|
||||
'''
|
||||
class TimeStamp(object):
|
||||
def __init__(self, date_time):
|
||||
self.date_time = date_time
|
||||
self.seconds_since_epoch = int(time.mktime(self.date_time.timetuple()))
|
||||
|
||||
fmt = '%Y-%m-%d %H:%M:%S'
|
||||
today_start = datetime.datetime.now()
|
||||
time_stamp = int(time.mktime(today_start.timetuple()))
|
||||
fmt_date_start = today_start.strftime(fmt)
|
||||
logging.info('[*] Execution Started at: {0}'.format(fmt_date_start))
|
||||
return time_stamp, today_start
|
||||
def strtime(self):
|
||||
return self.date_time.strftime('%Y-%m-%d %H:%M:%S')
|
||||
|
||||
def __repr__(self):
|
||||
return u'{}'.format(self.seconds_since_epoch)
|
||||
|
||||
def elapsed_time(today_start):
|
||||
'''
|
||||
Compute elapsed time from today_start and write basic stats
|
||||
in the log file
|
||||
'''
|
||||
def __sub__(self, other):
|
||||
return self.date_time - other.date_time
|
||||
|
||||
fmt = '%Y-%m-%d %H:%M:%S'
|
||||
today_finish = datetime.datetime.now()
|
||||
fmt_date_finish = today_finish.strftime(fmt)
|
||||
time_elapsed = today_finish - today_start
|
||||
# Logging end execution information
|
||||
logging.info('[*] Execution Finished, at: {0}'.format(fmt_date_finish))
|
||||
logging.info('[*] Time Elapsed: {0}'.format(time_elapsed))
|
||||
@staticmethod
|
||||
def now():
|
||||
return TimeStamp(datetime.datetime.now())
|
||||
|
||||
|
||||
def set_backup_level(backup_opt_dict, manifest_meta_dict):
|
||||
|
@ -901,6 +901,8 @@ class FakeSwift:
|
||||
def fake_get_container_content(self, backup_opt):
|
||||
return backup_opt
|
||||
|
||||
def remove_obj_older_than(self, backup_opt):
|
||||
return backup_opt
|
||||
|
||||
class FakeRestore:
|
||||
|
||||
@ -916,6 +918,9 @@ class FakeUtils:
|
||||
def __init__(self):
|
||||
return None
|
||||
|
||||
def fake_set_backup_level(self,backup_opt, manifest_meta):
|
||||
return backup_opt, manifest_meta
|
||||
|
||||
def fake_set_backup_level_fs(self, backup_opt, manifest_meta):
|
||||
#backup_opt = BackupOpt1()
|
||||
manifest_meta = {}
|
||||
@ -939,3 +944,14 @@ class FakeUtils:
|
||||
manifest_meta = {}
|
||||
backup_opt.mode = None
|
||||
return backup_opt, manifest_meta
|
||||
|
||||
|
||||
class FakeJob:
|
||||
def __init__(self, conf_dict):
|
||||
self.conf = conf_dict
|
||||
|
||||
def execute(self):
|
||||
return
|
||||
|
||||
def fake_create_job(conf):
|
||||
return FakeJob(conf)
|
||||
|
196
tests/test_job.py
Normal file
196
tests/test_job.py
Normal file
@ -0,0 +1,196 @@
|
||||
"""Freezer main.py related tests
|
||||
|
||||
Copyright 2014 Hewlett-Packard
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
This product includes cryptographic software written by Eric Young
|
||||
(eay@cryptsoft.com). This product includes software written by Tim
|
||||
Hudson (tjh@cryptsoft.com).
|
||||
========================================================================
|
||||
|
||||
"""
|
||||
|
||||
from commons import *
|
||||
from freezer import (
|
||||
swift, restore, utils, backup)
|
||||
|
||||
from freezer.job import Job, InfoJob, AdminJob, BackupJob, RestoreJob, create_job
|
||||
import logging
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
class TestJob:
|
||||
|
||||
def do_monkeypatch(self, monkeypatch):
|
||||
fakelogging = FakeLogging()
|
||||
self.fakeswift = fakeswift = FakeSwift()
|
||||
self.fakeutils = FakeUtils()
|
||||
self.fakebackup = FakeBackup()
|
||||
monkeypatch.setattr(logging, 'critical', fakelogging.critical)
|
||||
monkeypatch.setattr(logging, 'warning', fakelogging.warning)
|
||||
monkeypatch.setattr(logging, 'exception', fakelogging.exception)
|
||||
monkeypatch.setattr(logging, 'error', fakelogging.error)
|
||||
monkeypatch.setattr(swift, 'get_client', fakeswift.fake_get_client)
|
||||
monkeypatch.setattr(swift, 'get_containers_list', fakeswift.fake_get_containers_list1)
|
||||
|
||||
def test_execute(self, monkeypatch):
|
||||
self.do_monkeypatch(monkeypatch)
|
||||
job = Job({})
|
||||
assert job.execute() is None
|
||||
|
||||
|
||||
class TestInfoJob(TestJob):
|
||||
|
||||
def test_execute_nothing_to_do(self, monkeypatch):
|
||||
self.do_monkeypatch(monkeypatch)
|
||||
backup_opt = BackupOpt1()
|
||||
job = InfoJob(backup_opt)
|
||||
assert job.execute() is False
|
||||
|
||||
def test_execute_list_container(self, monkeypatch):
|
||||
self.do_monkeypatch(monkeypatch)
|
||||
backup_opt = BackupOpt1()
|
||||
backup_opt.list_container = True
|
||||
job = InfoJob(backup_opt)
|
||||
assert job.execute() is True
|
||||
|
||||
def test_execute_list_objects(self, monkeypatch):
|
||||
self.do_monkeypatch(monkeypatch)
|
||||
monkeypatch.setattr(swift, 'show_containers', self.fakeswift.fake_show_containers)
|
||||
monkeypatch.setattr(swift, 'show_objects', self.fakeswift.fake_show_objects)
|
||||
backup_opt = BackupOpt1()
|
||||
backup_opt.list_objects = True
|
||||
job = InfoJob(backup_opt)
|
||||
assert job.execute() is True
|
||||
|
||||
def test_execute_container_not_exist(self, monkeypatch):
|
||||
self.do_monkeypatch(monkeypatch)
|
||||
backup_opt = BackupOpt1()
|
||||
backup_opt.list_objects = True
|
||||
monkeypatch.setattr(swift, 'check_container_existance', self.fakeswift.fake_check_container_existance1)
|
||||
job = InfoJob(backup_opt)
|
||||
assert job.execute() is False
|
||||
|
||||
|
||||
class TestBackupJob(TestJob):
|
||||
|
||||
def test_execute_backup_fs_incremental(self, monkeypatch):
|
||||
self.do_monkeypatch(monkeypatch)
|
||||
monkeypatch.setattr(swift, 'check_container_existance', self.fakeswift.fake_check_container_existance1)
|
||||
monkeypatch.setattr(swift, 'get_containers_list', self.fakeswift.fake_get_containers_list4)
|
||||
monkeypatch.setattr(utils, 'set_backup_level', self.fakeutils.fake_set_backup_level)
|
||||
monkeypatch.setattr(backup, 'backup_mode_fs', self.fakebackup.fake_backup_mode_fs)
|
||||
monkeypatch.setattr(swift, 'get_container_content', self.fakeswift.fake_get_container_content)
|
||||
backup_opt = BackupOpt1()
|
||||
backup_opt.mode = 'fs'
|
||||
backup_opt.no_incremental = False
|
||||
job = BackupJob(backup_opt)
|
||||
assert job.execute() is None
|
||||
|
||||
def test_execute_backup_fs_no_incremental_and_backup_level_raise(self, monkeypatch):
|
||||
self.do_monkeypatch(monkeypatch)
|
||||
backup_opt = BackupOpt1()
|
||||
backup_opt.mode = 'fs'
|
||||
backup_opt.no_incremental = True
|
||||
job = BackupJob(backup_opt)
|
||||
pytest.raises(Exception, job.execute)
|
||||
|
||||
def test_execute_backup_mongo(self, monkeypatch):
|
||||
self.do_monkeypatch(monkeypatch)
|
||||
monkeypatch.setattr(utils, 'set_backup_level', self.fakeutils.fake_set_backup_level)
|
||||
monkeypatch.setattr(swift, 'get_container_content', self.fakeswift.fake_get_container_content)
|
||||
monkeypatch.setattr(backup, 'backup_mode_mongo', self.fakebackup.fake_backup_mode_mongo)
|
||||
backup_opt = BackupOpt1()
|
||||
backup_opt.no_incremental = False
|
||||
backup_opt.mode = 'mongo'
|
||||
job = BackupJob(backup_opt)
|
||||
assert job.execute() is None
|
||||
|
||||
def test_execute_backup_mysql(self, monkeypatch):
|
||||
self.do_monkeypatch(monkeypatch)
|
||||
monkeypatch.setattr(utils, 'set_backup_level', self.fakeutils.fake_set_backup_level)
|
||||
monkeypatch.setattr(swift, 'get_container_content', self.fakeswift.fake_get_container_content)
|
||||
monkeypatch.setattr(backup, 'backup_mode_mysql', self.fakebackup.fake_backup_mode_mysql)
|
||||
backup_opt = BackupOpt1()
|
||||
backup_opt.no_incremental = False
|
||||
backup_opt.mode = 'mysql'
|
||||
job = BackupJob(backup_opt)
|
||||
assert job.execute() is None
|
||||
|
||||
def test_execute_raise(self, monkeypatch):
|
||||
self.do_monkeypatch(monkeypatch)
|
||||
monkeypatch.setattr(utils, 'set_backup_level', self.fakeutils.fake_set_backup_level)
|
||||
monkeypatch.setattr(swift, 'get_container_content', self.fakeswift.fake_get_container_content)
|
||||
backup_opt = BackupOpt1()
|
||||
backup_opt.no_incremental = False
|
||||
backup_opt.mode = None
|
||||
job = BackupJob(backup_opt)
|
||||
pytest.raises(ValueError, job.execute)
|
||||
|
||||
|
||||
class TestRestoreJob(TestJob):
|
||||
|
||||
def test_execute_raise(self, monkeypatch):
|
||||
self.do_monkeypatch(monkeypatch)
|
||||
monkeypatch.setattr(swift, 'check_container_existance', self.fakeswift.fake_check_container_existance1)
|
||||
monkeypatch.setattr(swift, 'get_containers_list', self.fakeswift.fake_get_containers_list3)
|
||||
backup_opt = BackupOpt1()
|
||||
job = RestoreJob(backup_opt)
|
||||
#assert job.execute() is None
|
||||
pytest.raises(Exception, job.execute)
|
||||
|
||||
|
||||
def test_execute(self, monkeypatch):
|
||||
self.do_monkeypatch(monkeypatch)
|
||||
fakerestore = FakeRestore()
|
||||
monkeypatch.setattr(swift, 'check_container_existance', self.fakeswift.fake_check_container_existance)
|
||||
monkeypatch.setattr(swift, 'get_containers_list', self.fakeswift.fake_get_containers_list3)
|
||||
monkeypatch.setattr(restore, 'restore_fs', fakerestore.fake_restore_fs)
|
||||
monkeypatch.setattr(swift, 'get_container_content', self.fakeswift.fake_get_container_content)
|
||||
backup_opt = BackupOpt1()
|
||||
job = RestoreJob(backup_opt)
|
||||
assert job.execute() is None
|
||||
|
||||
|
||||
class TestAdminJob(TestJob):
|
||||
def test_execute(self, monkeypatch):
|
||||
self.do_monkeypatch(monkeypatch)
|
||||
monkeypatch.setattr(swift, 'remove_obj_older_than', self.fakeswift.remove_obj_older_than)
|
||||
backup_opt = BackupOpt1()
|
||||
job = AdminJob(backup_opt)
|
||||
assert job.execute() is None
|
||||
|
||||
|
||||
def test_create_job():
|
||||
backup_opt = BackupOpt1()
|
||||
backup_opt.action = None
|
||||
pytest.raises(Exception, create_job, backup_opt)
|
||||
|
||||
backup_opt.action = 'backup'
|
||||
job = create_job(backup_opt)
|
||||
assert isinstance(job, BackupJob)
|
||||
|
||||
backup_opt.action = 'restore'
|
||||
job = create_job(backup_opt)
|
||||
assert isinstance(job, RestoreJob)
|
||||
|
||||
backup_opt.action = 'info'
|
||||
job = create_job(backup_opt)
|
||||
assert isinstance(job, InfoJob)
|
||||
|
||||
backup_opt.action = 'admin'
|
||||
job = create_job(backup_opt)
|
||||
assert isinstance(job, AdminJob)
|
||||
|
@ -21,128 +21,13 @@ Hudson (tjh@cryptsoft.com).
|
||||
|
||||
"""
|
||||
|
||||
from commons import *
|
||||
from commons import fake_create_job, BackupOpt1
|
||||
|
||||
from freezer.main import freezer_main
|
||||
from freezer import (
|
||||
swift, restore, utils, backup)
|
||||
|
||||
import logging
|
||||
import pytest
|
||||
from freezer import job
|
||||
|
||||
|
||||
class TestMain:
|
||||
|
||||
def test_freezer_main(self, monkeypatch):
|
||||
|
||||
fakelogging = FakeLogging()
|
||||
fakeswift = FakeSwift()
|
||||
|
||||
monkeypatch.setattr(logging, 'critical', fakelogging.critical)
|
||||
monkeypatch.setattr(logging, 'warning', fakelogging.warning)
|
||||
monkeypatch.setattr(logging, 'exception', fakelogging.exception)
|
||||
monkeypatch.setattr(logging, 'error', fakelogging.error)
|
||||
|
||||
monkeypatch.setattr(swift, 'get_client', fakeswift.fake_get_client)
|
||||
monkeypatch.setattr(swift, 'show_containers', fakeswift.fake_show_containers)
|
||||
monkeypatch.setattr(swift, 'show_objects', fakeswift.fake_show_objects)
|
||||
monkeypatch.setattr(swift, 'get_containers_list', fakeswift.fake_get_containers_list1)
|
||||
def test_freezer_main(monkeypatch):
|
||||
monkeypatch.setattr(job, 'create_job', fake_create_job)
|
||||
backup_opt = BackupOpt1()
|
||||
backup_opt.action = 'info'
|
||||
assert freezer_main(backup_opt) is False
|
||||
|
||||
backup_opt.list_container = True
|
||||
assert freezer_main(backup_opt) is True
|
||||
|
||||
backup_opt.list_container = False
|
||||
backup_opt.list_objects = True
|
||||
assert freezer_main(backup_opt) is True
|
||||
|
||||
monkeypatch.setattr(swift, 'check_container_existance', fakeswift.fake_check_container_existance1)
|
||||
assert freezer_main(backup_opt) is False
|
||||
|
||||
fakeswift = FakeSwift()
|
||||
monkeypatch.setattr(swift, 'check_container_existance', fakeswift.fake_check_container_existance1)
|
||||
monkeypatch.setattr(swift, 'get_containers_list', fakeswift.fake_get_containers_list3)
|
||||
backup_opt = BackupOpt1()
|
||||
backup_opt.action = 'restore'
|
||||
pytest.raises(Exception, freezer_main, backup_opt)
|
||||
|
||||
fakeswift = FakeSwift()
|
||||
fakerestore = FakeRestore()
|
||||
monkeypatch.setattr(swift, 'check_container_existance', fakeswift.fake_check_container_existance)
|
||||
monkeypatch.setattr(swift, 'get_containers_list', fakeswift.fake_get_containers_list3)
|
||||
monkeypatch.setattr(restore, 'restore_fs', fakerestore.fake_restore_fs)
|
||||
monkeypatch.setattr(swift, 'get_container_content', fakeswift.fake_get_container_content)
|
||||
backup_opt = BackupOpt1()
|
||||
backup_opt.action = 'restore'
|
||||
assert freezer_main(backup_opt) is None
|
||||
|
||||
fakeswift = FakeSwift()
|
||||
fakeutils = FakeUtils()
|
||||
fakebackup = FakeBackup()
|
||||
monkeypatch.setattr(swift, 'check_container_existance', fakeswift.fake_check_container_existance1)
|
||||
monkeypatch.setattr(swift, 'get_containers_list', fakeswift.fake_get_containers_list4)
|
||||
monkeypatch.setattr(utils, 'set_backup_level', fakeutils.fake_set_backup_level_fs)
|
||||
monkeypatch.setattr(backup, 'backup_mode_fs', fakebackup.fake_backup_mode_fs)
|
||||
|
||||
backup_opt = BackupOpt1()
|
||||
backup_opt.action = 'backup'
|
||||
backup_opt.no_incremental = False
|
||||
assert freezer_main(backup_opt) is None
|
||||
|
||||
fakeswift = FakeSwift()
|
||||
fakeutils = FakeUtils()
|
||||
fakebackup = FakeBackup()
|
||||
monkeypatch.setattr(swift, 'check_container_existance', fakeswift.fake_check_container_existance1)
|
||||
monkeypatch.setattr(swift, 'get_containers_list', fakeswift.fake_get_containers_list4)
|
||||
monkeypatch.setattr(utils, 'set_backup_level', fakeutils.fake_set_backup_level_mongo)
|
||||
monkeypatch.setattr(backup, 'backup_mode_mongo', fakebackup.fake_backup_mode_mongo)
|
||||
|
||||
backup_opt = BackupOpt1()
|
||||
backup_opt.action = 'backup'
|
||||
backup_opt.no_incremental = False
|
||||
assert freezer_main(backup_opt) is None
|
||||
|
||||
fakeswift = FakeSwift()
|
||||
fakeutils = FakeUtils()
|
||||
fakebackup = FakeBackup()
|
||||
monkeypatch.setattr(swift, 'check_container_existance', fakeswift.fake_check_container_existance1)
|
||||
monkeypatch.setattr(swift, 'get_containers_list', fakeswift.fake_get_containers_list4)
|
||||
monkeypatch.setattr(utils, 'set_backup_level', fakeutils.fake_set_backup_level_mysql)
|
||||
monkeypatch.setattr(backup, 'backup_mode_mysql', fakebackup.fake_backup_mode_mysql)
|
||||
|
||||
backup_opt = BackupOpt1()
|
||||
backup_opt.action = 'backup'
|
||||
backup_opt.no_incremental = False
|
||||
assert freezer_main(backup_opt) is None
|
||||
|
||||
fakeswift = FakeSwift()
|
||||
fakeutils = FakeUtils()
|
||||
monkeypatch.setattr(swift, 'check_container_existance', fakeswift.fake_check_container_existance1)
|
||||
monkeypatch.setattr(swift, 'get_containers_list', fakeswift.fake_get_containers_list4)
|
||||
monkeypatch.setattr(utils, 'set_backup_level', fakeutils.fake_set_backup_level_none)
|
||||
|
||||
backup_opt = BackupOpt1()
|
||||
backup_opt.action = 'backup'
|
||||
backup_opt.no_incremental = False
|
||||
pytest.raises(ValueError, freezer_main, backup_opt)
|
||||
|
||||
fakeswift = FakeSwift()
|
||||
fakeutils = FakeUtils()
|
||||
fakebackup = FakeBackup()
|
||||
monkeypatch.setattr(swift, 'check_container_existance', fakeswift.fake_check_container_existance)
|
||||
monkeypatch.setattr(swift, 'get_containers_list', fakeswift.fake_get_containers_list4)
|
||||
monkeypatch.setattr(utils, 'set_backup_level', fakeutils.fake_set_backup_level_fs)
|
||||
monkeypatch.setattr(backup, 'backup_mode_fs', fakebackup.fake_backup_mode_fs)
|
||||
|
||||
backup_opt = BackupOpt1()
|
||||
backup_opt.action = 'backup'
|
||||
backup_opt.no_incremental = True
|
||||
backup_opt.max_backup_level = True
|
||||
|
||||
pytest.raises(Exception, freezer_main, backup_opt)
|
||||
|
||||
backup_opt.max_backup_level = False
|
||||
backup_opt.always_backup_level = False
|
||||
assert freezer_main(backup_opt) is None
|
||||
|
||||
|
@ -4,9 +4,9 @@ from freezer.utils import (
|
||||
gen_manifest_meta, validate_all_args, validate_any_args,
|
||||
sort_backup_list, create_dir, get_match_backup,
|
||||
get_newest_backup, get_rel_oldest_backup, get_abs_oldest_backup,
|
||||
eval_restart_backup, start_time, elapsed_time, set_backup_level,
|
||||
eval_restart_backup, set_backup_level,
|
||||
get_vol_fs_type, check_backup_and_tar_meta_existence, add_host_name_ts_level,
|
||||
get_mount_from_path)
|
||||
get_mount_from_path, TimeStamp)
|
||||
|
||||
from freezer import utils
|
||||
import pytest
|
||||
@ -155,17 +155,6 @@ class TestUtils:
|
||||
#pytest.raises(Exception, eval_restart_backup, backup_opt)
|
||||
|
||||
|
||||
def test_start_time(self):
|
||||
|
||||
(time_stamp, day_time) = start_time()
|
||||
assert type(day_time) is datetime.datetime
|
||||
assert type(time_stamp) is int
|
||||
|
||||
def test_elapsed_time(self):
|
||||
|
||||
(time_stamp, day_time) = start_time()
|
||||
assert elapsed_time(day_time) is None
|
||||
|
||||
def test_set_backup_level(self):
|
||||
|
||||
manifest_meta = dict()
|
||||
@ -255,3 +244,24 @@ class TestUtils:
|
||||
dir2 = '/tmp/nonexistentpathasdf'
|
||||
assert type(get_mount_from_path(dir1)) is str
|
||||
pytest.raises(Exception, get_mount_from_path, dir2)
|
||||
|
||||
|
||||
class TestTimeStamp:
|
||||
def setup(self):
|
||||
d = datetime.datetime(2015, 3, 7, 17, 47, 44, 716799)
|
||||
self.timestamp = TimeStamp(d)
|
||||
|
||||
def test_factory(self):
|
||||
new_timestamp = TimeStamp.now()
|
||||
assert isinstance(new_timestamp, TimeStamp)
|
||||
|
||||
def test_strtime(self):
|
||||
assert '2015-03-07 17:47:44' == self.timestamp.strtime()
|
||||
|
||||
def test_repr(self):
|
||||
assert '1425750464' == '{}'.format(self.timestamp)
|
||||
|
||||
def test_sub(self):
|
||||
d2 = datetime.datetime(2015, 3, 7, 18, 18, 38, 508411)
|
||||
ts2 = TimeStamp(d2)
|
||||
assert '0:30:53.791612' == '{}'.format(ts2 - self.timestamp)
|
||||
|
Loading…
Reference in New Issue
Block a user