from fabric.api import task, env from fabric.operations import run import os import modules.utils as utils from modules.conf import create_dir_top from modules.conf import exists_dir_top from modules.conf import exists_dir_sub from modules.conf import exists_file # import modules.conf as conf @task def test(*args, **kwargs): """ Test functions in conf Keyword Arguments: funcname -- name of testing function to run """ # configuration = env.config # dictionary of legitimate functions that can be tested # when given the param name test_values = { 'conf_top': test_conf_top, 'conf_sub': test_conf_sub, 'conf_file': test_conf_file, } funcname = kwargs.get('funcname') if not funcname: if len(args) > 0: funcname = args[0] args = args[1:] if funcname in test_values.keys(): test_values[funcname](*args, **kwargs) else: print "\nTest functions in this module, acceptable values include:" for val in test_values: print val def test_conf_file(*args, **kwargs): SPACING = "\n" utils.print_console("testing exist_conf_file", prepend=SPACING, append=SPACING) confargument = kwargs.get('conf') if not confargument: confargument = args[0] if len(args) > 0 else None exists_file(confargument) def test_conf_sub(*args, **kwargs): SPACING = "\n" utils.print_console("testing exist_conf_sub", prepend=SPACING, append=SPACING) confargument = kwargs.get('conf') if not confargument: confargument = args[0] if len(args) > 0 else None exists_dir_sub(confargument) def test_conf_top(*args, **kwargs): configuration = env.config SPACING = "\n" utils.print_console("testing exists_conf", prepend=SPACING, append=SPACING) utils.printvar("exists_dir_top", exists_dir_top()) utils.print_console("testing create_dir_top", prepend=SPACING, append=SPACING) if exists_dir_top(): msg = "conf directory already exists, move conf to a temporary " \ "directory, and test out the create_dir_top function." utils.print_console(msg, prepend=SPACING, append=SPACING, sep=None) # # command to create a temporary directory and echo it's name # back to stdout, so we can store that name for use cmd_mktmp = "mytmpdir=`mktemp -d 2>/dev/null ||" \ " mktemp -d -t 'mytmpdir'`" cmd_mktmp = cmd_mktmp + "; echo $mytmpdir" # # create a temporary diretory to store old conf files tmpdir = run(cmd_mktmp) # # make sure we are working with a legit path # otherwise, just kick out. with utils.virtualenv(): cmd_py_isdir = "python -c \"import os; "\ "print os.path.isdir('%s')\"" % \ configuration.paths.conf.remote # # take the output from this command and booleanize it output = run(cmd_py_isdir) is_dir = utils.booleanize(output) utils.printvar("is_dir", is_dir) if is_dir: lastpart = os.path.basename(configuration.paths.conf.remote) path_conf_tmp = os.path.join(tmpdir, lastpart) else: utils.printvar("configuration.paths.conf.remote", configuration.paths.conf.remote) msg = "the original configuration path is NOT a path." \ "Continue? y/N" utils.prompt_continue(message=msg, default="N") # # now move the original configuration directory to the temporary # location, and run test running create_dir_top on an empty msg = "moving original conf directory." utils.print_console(msg, prepend=SPACING, append=SPACING, sep=None) cmd_mvtmp = "mv %s %s" % \ (configuration.paths.conf.remote, path_conf_tmp) run(cmd_mvtmp) # # create the new conf directory msg = "creating new conf directory." utils.print_console(msg, prepend=SPACING, append=SPACING, sep=None) create_dir_top() # # testing on empty location completed, remove the current directory # and move back the original msg = "removing created directory." utils.print_console(msg, prepend=SPACING, append=SPACING, sep=None) cmd_rm_created = "rm -Rf %s" % configuration.paths.conf.remote run(cmd_rm_created) # # returning original directory msg = "Moving back original directory." utils.print_console(msg, prepend=SPACING, append=SPACING, sep=None) cmd_return_orig = "mv %s %s" % \ (path_conf_tmp, configuration.paths.conf.remote) run(cmd_return_orig) cmd_rmtmp = "rm -Rf %s" % tmpdir run(cmd_rmtmp) else: msg = "conf directory does not exist, test out create_dir_top" utils.print_console(msg, prepend=SPACING, append=SPACING, sep=None) create_dir_top()