def test_init_db(self): from os import remove from os.path import exists if exists(utils.path_file_db()): remove(utils.path_file_db()) utils.init_db() self.assertTrue(exists(utils.path_file_db())) remove(utils.path_file_db())
def test_normal_flow_update_mode(self): utils.init_ignore() db = utils.DB(None, [ utils.Job(tag='test_normal_flow', hosts=[remote_host], using_host=remote_host, remote_path='~/Projects/test-remotedocker/normal-flow', command=['cat', 'supplementary/hello.py'], step=None), utils.Job(tag='another_task', hosts=[remote_host], using_host=remote_host, remote_path='~/Projects/test-remotedocker/normal-flow', command=['cat', 'supplementary/hello.py'], step='running') ]) log = run.run(db.jobs[0], db, run.NormalFlow) print('run output:', log) self.assertEqual(log, 'print(\'hello world!\')') _db = utils.DB.load() print(_db.dict()) self.assertDictEqual(_db.latest_job.dict(), db.jobs[0].dict()) from os import remove remove(utils.path_file_ignore()) remove(utils.path_file_db())
def test_run_existing(self): src.utils.init_ignore() import arrow a = arrow.utcnow() d = { 'latest_host': 'a', 'jobs': [{ 'tag': 'test_run_existing', 'hosts': [remote_host], 'using_host': remote_host, 'step': None, 'docker': 'docker', 'remote_path': '~/Projects/test-remotedocker/run_existing', 'command': ['echo', 'test'], 'start_time': str(a), 'container': 'container', 'oth': dict(a=10), }] } from src import utils db = utils.DB.parse(d) db.save() from os.path import join file = join(src.utils.path_src(), 'remotedocker.py') output = test.utils.run_python(file, 'run', '--tag=test_run_existing') print('run output:', output) from os import remove remove(utils.path_file_db()) remove(utils.path_file_ignore())
def test_DB_get_latest(self): from os import remove from os.path import exists if exists(utils.path_file_db()): remove(utils.path_file_db()) db = utils.DB.load() self.assertEqual(db.get_latest('tag'), None) import arrow a = arrow.utcnow() d = { 'latest_job': { 'tag': 'tag', 'hosts': ['host1', 'host2'], 'using_host': 'host1', 'step': 'step', 'docker': 'docker', 'remote_path': 'remote_path', 'command': 'command', 'start_time': str(a), 'container': 'container', 'oth': dict(a=10), }, 'jobs': [{ 'tag': 'tag', 'hosts': ['host1', 'host2'], 'using_host': 'host1', 'step': 'step', 'docker': 'docker', 'remote_path': 'remote_path', 'command': 'command', 'start_time': str(a), 'container': 'container', 'oth': dict(a=10), }] } db = utils.DB.parse(d) self.assertEqual(db.get_latest('tag'), 'tag') self.assertEqual(db.get_latest('somethingnotthere'), None)
def test_run(self): from src import utils utils.init_ignore() from os.path import join file = join(src.utils.path_src(), 'remotedocker.py') output = test.utils.run_python( file, 'run', '--tag=test_run', '--host={}'.format(remote_host), '--path=~/Projects/test-remotedocker/run-plain', 'echo', 'test') print('run output:', output) from os import remove remove(utils.path_file_db()) remove(utils.path_file_ignore())
def test_normal_flow_err(self): utils.init_ignore() db = utils.DB(None, [ utils.Job( tag='test_normal_flow_err', hosts=[remote_host], using_host=remote_host, remote_path='~/Projects/test-remotedocker/normal-flow-err', command=['aoeu'], step=None) ]) self.assertRaises(utils.errors.WrongExitCode, run.run, db.jobs[0], db, run.NormalFlow) _db = utils.DB.load() self.assertDictEqual(_db.latest_job.dict(), db.jobs[0].dict()) from os import remove remove(utils.path_file_ignore()) remove(utils.path_file_db())
def test_full(self): from src import utils from os.path import join file = join(src.utils.path_src(), 'remotedocker.py') utils.init_ignore() utils.init_db() ''' Init run $ remotedocker run --tag=tag --host=host --path=path echo test ''' print('=====INIT RUN=====') test.utils.run_python(file, 'run', '--tag=test_full_first', '--host', remote_host, '--path', '~/Projects/test-remotedocker/full', 'echo', 'test') ''' Sync ''' print('=====SYNC DOWN=====') test.utils.run_python(file, 'sync') ''' Run again $ remotedocker run --tag=tag ''' print('=====RUN AGAIN=====') test.utils.run_python(file, 'run', '--tag=test_full_first') ''' Add a host to the same run $ remotedocker run --tag=tag --host=newhost ''' print('=====ADD HOST TO THE EXISTING RUN=====') db = utils.DB.load() db.jobs[0].host = 'somehost' db = utils.DB.parse(db.dict()) db.save() db = utils.DB.load() print('current host:', db.jobs[0].using_host) another_host = remote_host test.utils.run_python(file, 'run', '--tag=test_full_first', '--host={}'.format(another_host)) ''' Start another run with the same host (latest) (same path) $ remotedocker run --tag=newtag echo test2 ''' print('=====START ANOTHER RUN WITH THE SAME HOST=====') test.utils.run_python(file, 'run', '--tag=test_full_second', 'echo', 'test2') ''' Restart the succeeded run ''' print('=====RESTART=====') test.utils.run_python(file, 'restart') ''' Stop the succeeded run ''' print('=====STOP THE SUCCEEDED RUN=====') self.assertRaises(test.utils.run_python, file, 'stop') ''' Remove the first run ''' print('=====REMOVE THE FIRST RUN=====') self.assertRaises(Exception, test.utils.run_python, file, 'rm') test.utils.run_python(file, 'rm', 'test_full_first') db = utils.DB.load() self.assertEqual(len(db.jobs), 1) self.assertEqual(db.jobs[0].tag, 'test_full_second') from os import remove remove(utils.path_file_ignore()) remove(utils.path_file_db())