def test_update(self): """Update a working hg repository""" os.mkdir('/tmp/remote_pacha') os.mkdir('/tmp/remote_pacha/hosts') os.mkdir('/tmp/remote_pacha/hosts/%s' % host.hostname()) mercurial = hg.Hg(port=22, host=host.hostname(), user=self.username, path='/tmp/test_pacha', test=True, conf=self.dict_conf) mercurial.initialize() mercurial.hg_add() mercurial.commit() mercurial.clone() new_line = open('/tmp/test_pacha/foo', 'w') new_line.write('new line') new_line.close() mercurial.hgrc() mercurial.hg_add() mercurial.commit() mercurial.push() hg.update(hosts_path='/tmp/remote_pacha/hosts') self.assertTrue(os.path.isfile('/tmp/remote_pacha/hosts/%s/test_pacha/foo' % host.hostname())) get_line = open('/tmp/remote_pacha/hosts/%s/test_pacha/foo' % host.hostname()) actual = get_line.readlines()[0] expected = 'new line' self.assertEqual(actual, expected)
def test_push(self): """Push local changes to remote server""" os.mkdir('/tmp/remote_pacha') os.mkdir('/tmp/remote_pacha/hosts/') os.mkdir('/tmp/remote_pacha/hosts/%s' % host.hostname()) mercurial = hg.Hg(port=22, host=host.hostname(), user=self.username, path='/tmp/test_pacha', test=True, conf=self.dict_conf) mercurial.hgrc() mercurial.hg_add() mercurial.commit() mercurial.clone() new_file = open('/tmp/test_pacha/foo', 'w') new_file.write('new line') new_file.close() mercurial.hg_add() mercurial.commit() mercurial.push() hg.update(hosts_path = '/tmp/remote_pacha/hosts') new_line = open('/tmp/remote_pacha/hosts/%s/test_pacha/foo' % host.hostname()) actual = new_line.readlines()[0] expected = 'new line' self.assertEqual(actual, expected)
def setUp(self): test_dir = '/tmp/pacha_test' remote_dir = '/tmp/remote_pacha' pacha_host = '/tmp/pacha_test_host' if os.path.isdir(test_dir): shutil.rmtree(test_dir) if os.path.isdir(remote_dir): shutil.rmtree(remote_dir) if os.path.isdir(pacha_host): shutil.rmtree(pacha_host) pacha.DB_DIR = '/tmp/pacha_test' pacha.DB_FILE ='/tmp/pacha_test/pacha_test.db' pacha.permissions.DB_FILE ='/tmp/pacha_test/pacha_test.db' pacha.sync.DB_FILE ='/tmp/pacha_test/pacha_test.db' pacha.hg.DB_FILE ='/tmp/pacha_test/pacha_test.db' pacha.database.DB_FILE = '/tmp/pacha_test/pacha_test.db' pacha.database.DB_DIR = '/tmp/pacha_test' pacha.daemon.PID_DIR = '/tmp/pacha_test' pacha.daemon.DB_FILE = '/tmp/pacha_test/pacha_test.db' os.makedirs('/tmp/remote_pacha/hosts/%s' % host.hostname()) os.mkdir(test_dir) conf = open('/tmp/pacha_test/pacha.conf', 'w') conf.write('[DEFAULT]\n') conf.write('pacha.ssh.user = %s\n' % self.username) conf.write('pacha.host = %s\n' % host.hostname()) conf.write('pacha.hosts.path = /tmp/remote_pacha/hosts\n') conf.close() conf = ConfigMapper('/tmp/pacha_test/pacha_test.db').stored_config() for k, v in DICT_CONF.items(): conf[k] = v
def test_watch(self): """Watch a directory for changes and watch the db if not watched""" cmd = pacha.PachaCommands(test=True, parse=False, db=ConfigMapper('/tmp/pacha_test/pacha_test.db'), db_file='/tmp/pacha_test/pacha_test.db') cmd.add_config('/tmp/pacha_test/pacha.conf') cmd.check_config() os.mkdir('/tmp/pacha_test/foo') cmd.watch('/tmp/pacha_test/foo') db = pacha.database.Worker(db='/tmp/pacha_test/pacha_test.db') repos = [i for i in db.get_repos()] self.assertEqual(len(repos), 2) self.assertEqual(repos[1][0], 2) self.assertEqual(repos[1][1], u'/tmp/pacha_test') self.assertEqual(repos[1][2], None) self.assertEqual(repos[1][3], u'dir') self.assertEqual(repos[0][0], 1) self.assertEqual(repos[0][1], u'/tmp/pacha_test/foo') self.assertEqual(repos[0][2], None) self.assertEqual(repos[0][3], u'dir') self.assertTrue(os.path.isdir('/tmp/remote_pacha/hosts/%s/pacha_test' % host.hostname())) self.assertTrue(os.path.isdir('/tmp/pacha_test/foo')) self.assertTrue(os.path.isdir('/tmp/remote_pacha/hosts/%s/foo' % host.hostname())) self.assertTrue(os.path.isfile('/tmp/pacha_test/pacha_test.db'))
def setUp(self): # make sure we do not have db file test_dir = '/tmp/pacha_test' remote_dir = '/tmp/remote_pacha' pacha_host = '/tmp/pacha_test_host' if os.path.isdir(test_dir): shutil.rmtree(test_dir) if os.path.isdir(remote_dir): shutil.rmtree(remote_dir) if os.path.isdir(pacha_host): shutil.rmtree(pacha_host) pacha.DB_DIR = '/tmp/pacha_test' pacha.DB_FILE ='/tmp/pacha_test/pacha_test.db' pacha.permissions.DB_FILE ='/tmp/pacha_test/pacha_test.db' pacha.sync.DB_FILE ='/tmp/pacha_test/pacha_test.db' pacha.hg.DB_FILE ='/tmp/pacha_test/pacha_test.db' pacha.database.DB_FILE = '/tmp/pacha_test/pacha_test.db' pacha.database.DB_DIR = '/tmp/pacha_test' pacha.daemon.PID_DIR = '/tmp/pacha_test' os.makedirs('/tmp/remote_pacha/hosts/%s' % host.hostname()) os.mkdir(test_dir) conf = open('/tmp/pacha_test/pacha.conf', 'w') conf.write('[DEFAULT]\n') conf.write('pacha.ssh.user = %s\n' % self.username) conf.write('pacha.host = %s\n' % host.hostname()) conf.write('pacha.hosts.path = /tmp/remote_pacha/hosts\n') conf.close()
def test_retrieve_files_with_pre_hook(self): """Retrieve files and execute the pre_hook if found""" os.makedirs('/tmp/remote_pacha/localhost/etc') os.mkdir('/tmp/remote_pacha/localhost/home') remote_file = open('/tmp/remote_pacha/localhost/etc/etc.conf', 'w') remote_file.write("remote second file") remote_file.close() remote_file = open('/tmp/remote_pacha/localhost/home/home.conf', 'w') remote_file.write("remote file") remote_file.close() os.makedirs('/tmp/remote_pacha/localhost/pacha_pre') touch_script = open('/tmp/remote_pacha/localhost/pacha_pre/foo.sh', 'w') touch_script.write('''touch /tmp/remote_pacha/localhost/pre_got_executed.txt''') touch_script.close() server = "%s@%s" % (self.username, host.hostname()) run = rebuild.Rebuild(server=server, hostname='localhost', source='/tmp/remote_pacha') run.retrieve_files() result_1 = os.path.isfile('/tmp/localhost/etc/etc.conf') result_2 = os.path.isfile('/tmp/localhost/home/home.conf') line = open('/tmp/localhost/etc/etc.conf') remote_line = line.readline() self.assertEqual(remote_line, "remote second file") self.assertTrue(result_2) self.assertTrue(result_1) self.assertTrue(os.path.isfile('/tmp/remote_pacha/localhost/pre_got_executed.txt'))
def test_retrieve_files_move_existing_file(self): """if there is an exisiting file when retrieving - move it""" os.makedirs('/tmp/remote_pacha/localhost/etc') os.mkdir('/tmp/remote_pacha/localhost/home') remote_file = open('/tmp/remote_pacha/localhost/etc/etc.conf', 'w') remote_file.write("remote second file") remote_file.close() remote_file = open('/tmp/remote_pacha/localhost/home/home.conf', 'w') remote_file.write("remote file") remote_file.close() server = "%s@%s" % (self.username, host.hostname()) os.mkdir('/tmp/localhost') run = rebuild.Rebuild(server=server, hostname='localhost', source='/tmp/remote_pacha') run.retrieve_files() result_1 = os.path.isfile('/tmp/localhost/etc/etc.conf') result_2 = os.path.isfile('/tmp/localhost/home/home.conf') result_3 = os.path.isdir('/tmp/localhost.%s' % strftime('%H%M%s')) line = open('/tmp/localhost/etc/etc.conf') remote_line = line.readline() self.assertEqual(remote_line, "remote second file") self.assertTrue(result_3) self.assertTrue(result_2) self.assertTrue(result_1)
def __init__(self, port = 22, host = None, user = None, path = None, log = True, conf = None, test = False, single = False ): self.port = port self.host = host self.user = user self.log = log self.single = single if conf == None: self.conf = ConfigMapper(DB_FILE).stored_config() else: self.conf = conf if os.path.exists(path): snc_log.debug('verified path exists: %s' % path) self.path = os.path.normpath(path) self.dir = os.path.basename(path) else: snc_log.error('%s does not exist' % path) try: self.dest_path = '%s/%s' % (self.conf['hosts_path'], hostname()) except AttributeError, error: snc_log.error('error trying to use config options: %s' % error) sys.stderr.write('error trying to use config options: %s' % error) sys.exit(1)
def test_synchronize_false(self): """Do not synchronize anything if the timestamp is older""" db = Worker('/tmp/pacha_test/pacha_test.db') db.insert('/tmp/pacha_test',None, None, timestamp=9999999999) watch = daemon.SingleRepository('/tmp/pacha_test') watch.synchronize() repo = [i for i in db.get_repo('/tmp/pacha_test')] self.assertFalse(os.path.isdir('/tmp/remote_pacha/hosts/%s/pacha_test' % host.hostname())) self.assertEqual(repo[0][4], u'9999999999')
def test_synchronize_true(self): """When we find a modified file we need to synchronize""" db = Worker('/tmp/pacha_test/pacha_test.db') db.insert('/tmp/pacha_test',None, None, timestamp=1) watch = daemon.SingleRepository('/tmp/pacha_test') watch.synchronize() repo = [i for i in db.get_repo('/tmp/pacha_test')] self.assertTrue(os.path.isdir('/tmp/remote_pacha/hosts/%s/pacha_test' % host.hostname())) self.assertNotEqual(repo[0][4], 1)
def test_initialize(self): """Initializes a directory with Mercurial""" mercurial = hg.Hg(port=22, host=host.hostname(), user=self.username, path='/tmp/test_pacha', test=True, conf=self.dict_conf) mercurial.initialize() expected = os.path.isdir('/tmp/test_pacha/.hg') self.assertTrue(expected)
def test_validate_false(self): """Return False to a non existent hg repository""" mercurial = hg.Hg(port=22, host=host.hostname(), user=self.username, path='/tmp/test_pacha', test=True, conf=self.dict_conf) expected = mercurial.validate() self.assertFalse(expected)
def test_clone(self): """Clones the test repo to localhost""" os.mkdir('/tmp/remote_pacha') os.mkdir('/tmp/remote_pacha/hosts/') os.mkdir('/tmp/remote_pacha/hosts/%s' % host.hostname()) self.assertTrue(os.path.isdir('/tmp/remote_pacha/hosts/%s' % host.hostname())) mercurial = hg.Hg(port=22, host=host.hostname(), user=self.username, path='/tmp/test_pacha', test=True, conf=self.dict_conf) mercurial.initialize() mercurial.hg_add() mercurial.commit() mercurial.clone() result = os.path.isdir('/tmp/remote_pacha/hosts/%s/test_pacha' % host.hostname()) self.assertTrue(result)
def test_hg_add(self): """We create a file and then we add it""" mercurial = hg.Hg(port=22, host=host.hostname(), user=self.username, path='/tmp/test_pacha', test=True, conf=self.dict_conf) mercurial.initialize() mercurial.hg_add() out = Popen('hg st /tmp/test_pacha', shell=True, stdout=PIPE) expected = 'A pacha.conf\n' actual = out.stdout.readline() self.assertEqual(expected, actual)
def test_hgrc_except(self): """Return False for issues if hgrc cannot be written""" mercurial = hg.Hg(port=22, host=host.hostname(), user=self.username, path='/tmp/test_pacha', test=True, conf=self.dict_conf) mercurial.hgrc() self.assertFalse(mercurial.hgrc())
def test_validate_true(self): """Validate a working hg repository by returning True""" mercurial = hg.Hg(port=22, host=host.hostname(), user=self.username, path='/tmp/test_pacha', test=True, conf=self.dict_conf) mercurial.initialize() expected = mercurial.validate() self.assertTrue(expected)
def test_rebuild(self): """Test a simple rebuild with some files in the pacha.db""" pacha.DB_DIR = '/tmp/pacha_test/db' pacha.DB_FILE ='/tmp/pacha_test/db/pacha_test.db' pacha.permissions.DB_FILE ='/tmp/pacha_test/db/pacha_test.db' pacha.sync.DB_FILE ='/tmp/pacha_test/db/pacha_test.db' pacha.hg.DB_FILE ='/tmp/pacha_test/db/pacha_test.db' pacha.database.DB_FILE = '/tmp/pacha_test/db/pacha_test.db' pacha.database.DB_DIR = '/tmp/pacha_test/db' pacha.daemon.PID_DIR = '/tmp/pacha_test' os.makedirs('/tmp/pacha_test/db') os.makedirs('/tmp/remote_pacha/hosts/%s' % host.hostname()) cmd = pacha.PachaCommands(test=True, parse=False, db=ConfigMapper('/tmp/pacha_test/db/pacha_test.db'), db_file='/tmp/pacha_test/db/pacha_test.db') cmd.add_config('/tmp/pacha_test/pacha.conf') cmd.check_config() os.makedirs('/tmp/pacha_test/foo/bar') test_file = open('/tmp/pacha_test/foo/bar/test.txt', 'w') test_file.write('file should be rebuilt') test_file.close() cmd.watch('/tmp/pacha_test/foo/bar') # fake getting the db to the expected location shutil.copy('/tmp/pacha_test/db/pacha_test.db' , '/tmp/remote_pacha/hosts/%s/db/pacha.db' % host.hostname()) shutil.rmtree('/tmp/pacha_test') server = "%s@%s" % (self.username, host.hostname()) run = rebuild.Rebuild(server=server, hostname=host.hostname(), source='/tmp/remote_pacha/hosts') run.retrieve_files() self.assertTrue(os.path.exists('/tmp/%s' % host.hostname())) self.assertFalse(os.path.exists('/tmp/pacha_test')) run.replace_manager() self.assertTrue(os.path.exists('/tmp/pacha_test/foo/bar')) self.assertEqual(open('/tmp/pacha_test/foo/bar/test.txt').readline(), 'file should be rebuilt')
def test_hgrc(self): """Add a line for automated push inside .hg""" mercurial = hg.Hg(port=22, host=host.hostname(), user=self.username, path='/tmp/test_pacha', test=True, conf=self.dict_conf) mercurial.hgrc() actual = open('/tmp/test_pacha/.hg/hgrc').readlines()[1] expected = 'default = ssh://%s@%s//tmp/remote_pacha/hosts/%s/test_pacha' % (self.username,host.hostname(), host.hostname()) self.assertEqual(expected, actual)
def setUp(self): """Will setup just once for all tests""" if os.path.isdir('/tmp/remote_pacha'): shutil.rmtree('/tmp/remote_pacha') if os.path.isdir('/tmp/test_pacha'): shutil.rmtree('/tmp/test_pacha') os.mkdir('/tmp/test_pacha') config = open('/tmp/test_pacha/pacha.conf', 'w') config.write('[DEFAULT]\n') config.write('pacha.ssh.user = %s\n' % self.username) config.write('pacha.host = %s\n' % host.hostname()) config.write('pacha.hosts.path = /tmp/remote_pacha/hosts\n') config.close()
def setUp(self): """Will setup just once for all tests""" if os.path.isdir('/tmp/remote_pacha'): shutil.rmtree('/tmp/remote_pacha') if os.path.isdir('/tmp/localhost'): shutil.rmtree('/tmp/localhost') if os.path.isdir('/tmp/test_pacha'): shutil.rmtree('/tmp/test_pacha') if os.path.isdir('/tmp/%s' % host.hostname()): shutil.rmtree('/tmp/%s' % host.hostname()) if os.path.isdir('/tmp/pacha_bucket'): shutil.rmtree('/tmp/pacha_bucket') if os.path.isdir('/tmp/pacha_test'): shutil.rmtree('/tmp/pacha_test') os.mkdir('/tmp/test_pacha') config = open('/tmp/test_pacha/pacha.conf', 'w') config.write('[DEFAULT]\n') config.write('pacha.ssh.user = %s\n' % self.username) config.write('pacha.host = %s\n' % host.hostname()) config.write('pacha.hosts.path = /tmp/remote_pacha/hosts\n') config.close() # static mappings for file locations pacha.DB_DIR = '/tmp/pacha_test' pacha.DB_FILE ='/tmp/pacha_test/pacha_test.db' pacha.permissions.DB_FILE ='/tmp/pacha_test/pacha_test.db' pacha.hg.DB_FILE ='/tmp/pacha_test/pacha_test.db' pacha.database.DB_FILE = '/tmp/pacha_test/pacha_test.db' pacha.database.DB_DIR = '/tmp/pacha_test' pacha.daemon.PID_DIR = '/tmp/pacha_test' # write a config file os.makedirs('/tmp/pacha_test') conf = open('/tmp/pacha_test/pacha.conf', 'w') conf.write('[DEFAULT]\n') conf.write('pacha.ssh.user = %s\n' % self.username) conf.write('pacha.host = %s\n' % host.hostname()) conf.write('pacha.hosts.path = /tmp/remote_pacha/hosts\n') conf.close()
def test_commit(self): """Builds a mercurial repo and commits""" mercurial = hg.Hg(port=22, host=host.hostname(), user=self.username, path='/tmp/test_pacha', test=True, conf=self.dict_conf) mercurial.initialize() mercurial.hg_add() mercurial.commit() # we need to run hg st to verify we have actually commited stuff out = Popen('hg st /tmp/test_pacha', shell=True, stdout=PIPE) expected = '' actual = out.stdout.readline() self.assertEqual(expected, actual)
def test_retrieve_files_single(self): """Gets a single directory from a remote host""" os.makedirs('/tmp/remote_pacha/localhost/another_dir') os.makedirs('/tmp/remote_pacha/localhost/single_dir') remote_file = open('/tmp/remote_pacha/localhost/single_dir/remote.txt', 'w') remote_file.write("remote file") remote_file.close() self.assertTrue(os.path.isfile('/tmp/remote_pacha/localhost/single_dir/remote.txt')) server = "%s@%s" % (self.username, host.hostname()) run = rebuild.Rebuild(server=server, hostname='localhost', source='/tmp/remote_pacha', directory='single_dir') run.retrieve_files() result = os.path.isfile('/tmp/localhost/single_dir/remote.txt') line = open('/tmp/localhost/single_dir/remote.txt') remote_line = line.readline() self.assertEqual(remote_line, "remote file") self.assertTrue(result)
def hgrc(self): """An option to write the default path in hgrc for pushing via hg""" if self.validate(): machine = hostname() try: hgrc = open(self.path+'/.hg/hgrc', 'w') hgrc.write('[paths]\n') ssh_line = "default = ssh://%s@%s:%s%s" % (self.conf['ssh_user'], self.conf['host'], self.conf.get('ssh_port', 22), self.dest_path) hgrc.write(ssh_line) hgrc.close() hg_log.debug("wrote hgrc in %s" % self.path) hg_log.debug("default is %s" % ssh_line) except Exception, error: hg_log.error(error) return False
def test_rebuild_no_db(self): """Run a full rebuild and fail because no db was found""" os.makedirs('/tmp/remote_pacha/localhost/etc') os.mkdir('/tmp/remote_pacha/localhost/home') remote_file = open('/tmp/remote_pacha/localhost/etc/etc.conf', 'w') remote_file.write("remote second file") remote_file.close() remote_file = open('/tmp/remote_pacha/localhost/home/home.conf', 'w') remote_file.write("remote file") remote_file.close() server = "%s@%s" % (self.username, host.hostname()) run = rebuild.Rebuild(server=server, hostname='localhost', source='/tmp/remote_pacha') sys.stdout = MockSys() sys.exit = MockSys() run.retrieve_files() run.replace_manager() actual = sys.stdout.captured() expected = """Could not find DB at /tmp/localhost/db/pacha.db\n""" self.assertEqual(actual, expected)
def test_retrieve_files_all(self): """Gets all files from a remote host""" os.makedirs('/tmp/remote_pacha/localhost/etc') os.mkdir('/tmp/remote_pacha/localhost/home') remote_file = open('/tmp/remote_pacha/localhost/etc/etc.conf', 'w') remote_file.write("remote second file") remote_file.close() remote_file = open('/tmp/remote_pacha/localhost/home/home.conf', 'w') remote_file.write("remote file") remote_file.close() server = "%s@%s" % (self.username, host.hostname()) run = rebuild.Rebuild(server=server, hostname='localhost', source='/tmp/remote_pacha') run.retrieve_files() result_1 = os.path.isfile('/tmp/localhost/etc/etc.conf') result_2 = os.path.isfile('/tmp/localhost/home/home.conf') line = open('/tmp/localhost/etc/etc.conf') remote_line = line.readline() self.assertEqual(remote_line, "remote second file") self.assertTrue(result_2) self.assertTrue(result_1)
def test_retrieve_files_error_message(self): """If you can't retrieve files let me know""" os.makedirs('/tmp/remote_pacha/localhost/etc') os.mkdir('/tmp/remote_pacha/localhost/home') remote_file = open('/tmp/remote_pacha/localhost/etc/etc.conf', 'w') remote_file.write("remote second file") remote_file.close() remote_file = open('/tmp/remote_pacha/localhost/home/home.conf', 'w') remote_file.write("remote file") remote_file.close() server = "%s@%s" % (self.username, host.hostname()) run = rebuild.Rebuild(server=server, hostname='localhost', source='/tmpp/remote_pacha') sys.stdout = MockSys() sys.exit = MockSys() run.retrieve_files() actual = sys.stdout.captured() expected = """ Pacha was not able to retrieve the files from the SSH server provided. Check your configuration file settings and try again. """ self.assertEqual(actual, expected)
def test_hostname(self): """Get the right hostname""" uname = os.uname()[1] self.assertEquals(uname, host.hostname())
def test_daemon_do_nothing(self): """Since the repos are empty just run once and exit cleanly """ self.assertRaises(SystemExit, daemon.start, foreground=True, run_once=True) self.assertEqual(len(os.listdir('/tmp/remote_pacha/hosts/%s' % host.hostname())), 0)
import getpass import os import unittest import shutil import pacha from guachi import ConfigMapper from pacha import daemon, host from pacha.database import Worker from mock import MockSys DICT_CONF = dict( frequency = 60, master = 'False', host = '%s' % host.hostname(), ssh_user = '******' % getpass.getuser(), ssh_port = 22, hosts_path = '/tmp/remote_pacha/hosts', hg_autocorrect = 'True', log_enable = 'False', log_path = 'False', log_level = 'DEBUG', log_format = '%(asctime)s %(levelname)s %(name)s %(message)s', log_datefmt = '%H=%M=%S' ) class SingleRepository(unittest.TestCase):