def test_force(tmpdir, use_the_force): tmpdir = str(tmpdir) force_test = os.path.join(test_path, 'force1') force_dir = os.path.join(tmpdir, 'force') shutil.copytree(force_test, force_dir) action_file = os.path.join(force_dir, 'services', 'api', '0.0.1', 'actions.json') result_file = os.path.join(tmpdir,'result') with open(action_file, 'w') as afile: afile.write(json.dumps({'test':{ 'commands':['touch {}'.format(result_file)] }})) squadron_state_dir = os.path.join(tmpdir, 'state') makedirsp(squadron_state_dir) create_blank_infojson(squadron_state_dir) main.go(force_dir, squadron_state_dir, os.path.join(test_path, 'main1.config'), 'dev', None, False, use_the_force, False) assert os.path.exists(result_file) os.remove(result_file) main.go(force_dir, squadron_state_dir, os.path.join(test_path, 'main1.config'), 'dev', None, False, use_the_force, False) if use_the_force: assert os.path.exists(result_file) else: assert not os.path.exists(result_file)
def test_config_with_bad_dir(tmpdir): tmpdir = str(tmpdir) squadron_state_dir = os.path.join(tmpdir, 'state') makedirsp(squadron_state_dir) create_blank_infojson(squadron_state_dir) try: main.go(None, squadron_state_dir, os.path.join(test_path,'bad_dir.config'), None, None, False, False, False) assert False == "Should have thrown" # Shouldn't get here except OSError as e: assert e.filename.startswith('/non/existant/dir')
def test_config_with_bad_dir(tmpdir): tmpdir = str(tmpdir) squadron_state_dir = os.path.join(tmpdir, 'state') makedirsp(squadron_state_dir) create_blank_infojson(squadron_state_dir) try: main.go(None, squadron_state_dir, os.path.join(test_path, 'bad_dir.config'), None, None, False, False, False) assert False == "Should have thrown" # Shouldn't get here except OSError as e: assert e.filename.startswith('/non/existant/dir')
def test_main_dryrun(tmpdir): tmpdir = str(tmpdir) squadron_state_dir = os.path.join(tmpdir, 'state') makedirsp(squadron_state_dir) create_blank_infojson(squadron_state_dir) squadron_dir = os.path.join(test_path, 'main1') main.go(squadron_dir, squadron_state_dir, os.path.join(test_path, 'main1.config'), 'dev', None, False, False, True) with open(os.path.join(squadron_state_dir, 'info.json')) as infojson: assert "" == infojson.read()
def test_apply(tmpdir, source_dir, dest_dir, do_commit, do_copy): tmpdir = str(tmpdir) commit_tmp = os.path.join(tmpdir, 'tmp') makedirsp(commit_tmp) apply_dir = os.path.join(tmpdir, 'apply') # apply_dir must not yet exist shutil.copytree(os.path.join(test_path, source_dir), apply_dir) # Write out config state_tmp = os.path.join(tmpdir, 'state') makedirsp(state_tmp) config_dir = os.path.join(apply_dir, 'config', 'dev') makedirsp(config_dir) config = { 'version': '0.0.1', 'config': { 'state1': os.path.join(state_tmp, 'one'), 'state2': os.path.join(state_tmp, 'two'), 'dbhostname': 'example.com' }, 'base_dir': os.path.join(tmpdir, 'basedir') } with open(os.path.join(config_dir, 'api.json'), 'w') as cfile: cfile.write(json.dumps(config)) previous_dir = None if do_copy: previous_dir = os.path.join(tmpdir, 'previous', 'example', 'path') makedirsp(previous_dir) with open(os.path.join(previous_dir, 'test.txt'), 'w') as copy_file: copy_file.write('You did it!\n') results = commit.apply(apply_dir, 'node', commit_tmp, {}, previous_dir) assert len(results) == 1 assert are_dir_trees_equal(results['api']['dir'], os.path.join(test_path, dest_dir)) checkfile(config['config']['state1'], '55') checkfile(config['config']['state2'], '0') if do_commit: makedirsp(results['api']['base_dir']) commit.commit(results)
def test_apply(tmpdir, source_dir, dest_dir, do_commit, do_copy): tmpdir = str(tmpdir) commit_tmp = os.path.join(tmpdir, 'tmp') makedirsp(commit_tmp) apply_dir = os.path.join(tmpdir, 'apply') # apply_dir must not yet exist shutil.copytree(os.path.join(test_path, source_dir), apply_dir) # Write out config state_tmp = os.path.join(tmpdir,'state') makedirsp(state_tmp) config_dir = os.path.join(apply_dir, 'config', 'dev') makedirsp(config_dir) config = { 'version':'0.0.1', 'config':{ 'state1':os.path.join(state_tmp,'one'), 'state2':os.path.join(state_tmp,'two'), 'dbhostname':'example.com' }, 'base_dir':os.path.join(tmpdir,'basedir') } with open(os.path.join(config_dir, 'api.json'), 'w') as cfile: cfile.write(json.dumps(config)) previous_dir = None if do_copy: previous_dir = os.path.join(tmpdir, 'previous', 'example', 'path') makedirsp(previous_dir) with open(os.path.join(previous_dir, 'test.txt'), 'w') as copy_file: copy_file.write('You did it!\n') results = commit.apply(apply_dir, 'node', commit_tmp, {}, previous_dir) assert len(results) == 1 assert are_dir_trees_equal(results['api']['dir'], os.path.join(test_path, dest_dir)) checkfile(config['config']['state1'], '55') checkfile(config['config']['state2'], '0') if do_commit: makedirsp(results['api']['base_dir']) commit.commit(results)
def test_rollback(tmpdir, dry_run, dont_rollback): tmpdir = str(tmpdir) rollback_test = os.path.join(test_path, 'rollback1') old_dir = os.path.join(rollback_test, 'temp', 'sq-59', 'old_result') # Make the real info.json with open(os.path.join(rollback_test, 'state', 'info.json.input')) as inputjson: with open(os.path.join(tmpdir, 'info.json'), 'w+') as outputjson: info = json.loads(inputjson.read()) info['dir'] = old_dir info['commit']['gitolite']['dir'] = os.path.join( info['dir'], info['commit']['gitolite']['dir']) for k, v in info['checksum'].items(): del info['checksum'][k] info['checksum'][os.path.join(info['dir'], k)] = v outputjson.write(json.dumps(info)) # Now let's get back to testing squadron_dir = os.path.join(rollback_test, 'squadron_dir') if dry_run: # Should not raise main.go(squadron_dir, tmpdir, os.path.join(test_path, 'main1.config'), 'dev', None, dont_rollback, False, dry_run) else: # This should raise every time for i in range(3): with pytest.raises(squadron.exceptions.TestException) as ex: makedirsp('/tmp/main2test/') main.go(squadron_dir, tmpdir, os.path.join(test_path, 'main1.config'), 'dev', None, dont_rollback, False, dry_run) assert ex is not None with open(os.path.join(tmpdir, 'info.json')) as infojson: assert info == json.loads(infojson.read()) assert 'dir' in info assert os.path.isdir(info['dir']) == True remove_lock_file(info['dir']) assert are_dir_trees_equal(old_dir, info['dir']) shutil.rmtree('/tmp/main2test/')
def test_rollback(tmpdir, dry_run, dont_rollback): tmpdir = str(tmpdir) rollback_test = os.path.join(test_path, 'rollback1') old_dir = os.path.join(rollback_test,'temp','sq-59','old_result') # Make the real info.json with open(os.path.join(rollback_test, 'state', 'info.json.input')) as inputjson: with open(os.path.join(tmpdir, 'info.json'), 'w+') as outputjson: info = json.loads(inputjson.read()) info['dir'] = old_dir info['commit']['gitolite']['dir'] = os.path.join(info['dir'], info['commit']['gitolite']['dir']) for k,v in info['checksum'].items(): del info['checksum'][k] info['checksum'][os.path.join(info['dir'], k)] = v outputjson.write(json.dumps(info)) # Now let's get back to testing squadron_dir = os.path.join(rollback_test, 'squadron_dir') if dry_run: # Should not raise main.go(squadron_dir, tmpdir, os.path.join(test_path,'main1.config'), 'dev', None, dont_rollback, False, dry_run) else: # This should raise every time for i in range(3): with pytest.raises(squadron.exceptions.TestException) as ex: makedirsp('/tmp/main2test/') main.go(squadron_dir, tmpdir, os.path.join(test_path,'main1.config'), 'dev', None, dont_rollback, False, dry_run) assert ex is not None with open(os.path.join(tmpdir, 'info.json')) as infojson: assert info == json.loads(infojson.read()) assert 'dir' in info assert os.path.isdir(info['dir']) == True remove_lock_file(info['dir']) assert are_dir_trees_equal(old_dir, info['dir']) shutil.rmtree('/tmp/main2test/')
def test_copy(tmpdir, filename, persist): tmpdir = str(tmpdir) port = get_port() http_thread = BackgroundHTTPServer(get_test_path(), port) http_thread.start() try: if persist: directory = 'dir1' else: directory = os.path.join(tmpdir, 'dir1') copy = [{'from':'*.txt', 'to': directory + os.path.sep}] cfg = json.dumps(get_config('http://localhost:{}/data/{}'.format(port, filename), persist=persist, copy=copy)) abs_source = os.path.join(tmpdir, 'input_config') with open(abs_source, 'w') as f: f.write(cfg) dest = os.path.join(tmpdir, 'dest') if persist: dest_subdir = os.path.join(dest, directory) else: dest_subdir = directory makedirsp(dest_subdir) result = extract.ext_extract(abs_source, dest, {}, get_loader()) assert os.path.exists(os.path.join(dest_subdir, 'file.txt')) == True if persist: assert os.path.exists(dest) == True assert result is not None else: assert os.path.exists(dest) == False assert result is None finally: http_thread.httpd.shutdown() http_thread.join()
def test_main_basic(tmpdir): tmpdir = str(tmpdir) squadron_state_dir = os.path.join(tmpdir, 'state') makedirsp(squadron_state_dir) create_blank_infojson(squadron_state_dir) makedirsp('/tmp/applytest1/') squadron_dir = os.path.join(test_path, 'main1') main.go(squadron_dir, squadron_state_dir, os.path.join(test_path, 'main1.config'), 'dev', None, False, False, False) with open(os.path.join(squadron_state_dir, 'info.json')) as infojson: info = json.loads(infojson.read()) assert 'dir' in info assert os.path.isdir(info['dir']) == True remove_lock_file(info['dir']) assert are_dir_trees_equal(os.path.join(test_path, 'main1result'), info['dir']) == True old_dir = info['dir'] main.go(squadron_dir, squadron_state_dir, os.path.join(test_path, 'main1.config'), 'dev', None, False, False, False) with open(os.path.join(squadron_state_dir, 'info.json')) as infojson: info = json.loads(infojson.read()) assert 'dir' in info assert os.path.isdir(info['dir']) == True new_dir = info['dir'] assert old_dir == new_dir remove_lock_file(info['dir']) assert are_dir_trees_equal(os.path.join(test_path, 'main1result'), info['dir']) == True
def test_main_git(tmpdir): tmpdir = str(tmpdir) squadron_state_dir = os.path.join(tmpdir, 'state') makedirsp(squadron_state_dir) create_blank_infojson(squadron_state_dir) squadron_dir = os.path.join(test_path, 'main2') main.go(squadron_dir, squadron_state_dir, os.path.join(test_path,'main1.config'), 'dev', None, False, False, False) with open(os.path.join(squadron_state_dir, 'info.json')) as infojson: info = json.loads(infojson.read()) print "info: {}".format(json.dumps(info)) assert 'dir' in info assert os.path.isdir(info['dir']) == True remove_lock_file(info['dir']) assert are_dir_trees_equal(os.path.join(test_path,'main2result'), info['dir']) == True
def ignore_test_copy(tmpdir, filename, persist): tmpdir = str(tmpdir) port = get_port() http_thread = BackgroundHTTPServer(get_test_path(), port) http_thread.start() try: if persist: directory = 'dir1' else: directory = os.path.join(tmpdir, 'dir1') copy = [{'from': '*.txt', 'to': directory + os.path.sep}] cfg = json.dumps( get_config('http://localhost:{}/data/{}'.format(port, filename), persist=persist, copy=copy)) abs_source = os.path.join(tmpdir, 'input_config') with open(abs_source, 'w') as f: f.write(cfg) dest = os.path.join(tmpdir, 'dest') if persist: dest_subdir = os.path.join(dest, directory) else: dest_subdir = directory makedirsp(dest_subdir) result = extract.ext_extract(abs_source, dest, {}, get_loader()) assert os.path.exists(os.path.join(dest_subdir, 'file.txt')) == True assert os.path.exists(dest) == True assert result is not None finally: http_thread.httpd.shutdown() http_thread.join()
def test_main_with_config(tmpdir): tmpdir = str(tmpdir) squadron_state_dir = os.path.join(tmpdir, 'state') makedirsp(squadron_state_dir) create_blank_infojson(squadron_state_dir) makedirsp('/tmp/applytest1/') squadron_dir = os.path.join(test_path, 'main1') # Gets the node name from the config file main.go(squadron_dir, squadron_state_dir, os.path.join(test_path,'main1.config'), None, None, False, False, False) with open(os.path.join(squadron_state_dir, 'info.json')) as infojson: info = json.loads(infojson.read()) assert 'dir' in info assert os.path.isdir(info['dir']) == True remove_lock_file(info['dir']) assert are_dir_trees_equal(os.path.join(test_path,'main1result'), info['dir']) == True
def test_main_git(tmpdir): tmpdir = str(tmpdir) squadron_state_dir = os.path.join(tmpdir, 'state') makedirsp(squadron_state_dir) create_blank_infojson(squadron_state_dir) squadron_dir = os.path.join(test_path, 'main2') main.go(squadron_dir, squadron_state_dir, os.path.join(test_path, 'main1.config'), 'dev', None, False, False, False) with open(os.path.join(squadron_state_dir, 'info.json')) as infojson: info = json.loads(infojson.read()) print "info: {}".format(json.dumps(info)) assert 'dir' in info assert os.path.isdir(info['dir']) == True remove_lock_file(info['dir']) assert are_dir_trees_equal(os.path.join(test_path, 'main2result'), info['dir']) == True
def test_main_with_config(tmpdir): tmpdir = str(tmpdir) squadron_state_dir = os.path.join(tmpdir, 'state') makedirsp(squadron_state_dir) create_blank_infojson(squadron_state_dir) makedirsp('/tmp/applytest1/') squadron_dir = os.path.join(test_path, 'main1') # Gets the node name from the config file main.go(squadron_dir, squadron_state_dir, os.path.join(test_path, 'main1.config'), None, None, False, False, False) with open(os.path.join(squadron_state_dir, 'info.json')) as infojson: info = json.loads(infojson.read()) assert 'dir' in info assert os.path.isdir(info['dir']) == True remove_lock_file(info['dir']) assert are_dir_trees_equal(os.path.join(test_path, 'main1result'), info['dir']) == True
def test_force(tmpdir, use_the_force): tmpdir = str(tmpdir) force_test = os.path.join(test_path, 'force1') force_dir = os.path.join(tmpdir, 'force') shutil.copytree(force_test, force_dir) action_file = os.path.join(force_dir, 'services', 'api', '0.0.1', 'actions.json') result_file = os.path.join(tmpdir, 'result') with open(action_file, 'w') as afile: afile.write( json.dumps( {'test': { 'commands': ['touch {}'.format(result_file)] }})) squadron_state_dir = os.path.join(tmpdir, 'state') makedirsp(squadron_state_dir) create_blank_infojson(squadron_state_dir) main.go(force_dir, squadron_state_dir, os.path.join(test_path, 'main1.config'), 'dev', None, False, use_the_force, False) assert os.path.exists(result_file) os.remove(result_file) main.go(force_dir, squadron_state_dir, os.path.join(test_path, 'main1.config'), 'dev', None, False, use_the_force, False) if use_the_force: assert os.path.exists(result_file) else: assert not os.path.exists(result_file)
def make_react_tmp(): makedirsp('/tmp/service1') makedirsp('/tmp/apache2')