def test_environment(tmpdir): tmpdir = str(tmpdir) assert initialize.init(tmpdir, None, force=True) assert initialize.init_service(tmpdir, 'api', '0.0.1') assert initialize.init_environment(tmpdir, 'dev', None) config_dir = os.path.join(tmpdir, 'config') items = os.listdir(config_dir) assert len(items) == 1 assert 'dev' in items dev_dir = os.path.join(config_dir, 'dev') subitems = os.listdir(dev_dir) assert len(subitems) == 1 assert 'api' in subitems # Check that config was put in with open(os.path.join(dev_dir, subitems[0])) as jsonfile: config = json.loads(jsonfile.read()) assert 'version' in config assert config['version'] == '0.0.1' assert config['config'] == {} assert 'base_dir' in config # Try copy from with open(os.path.join(dev_dir, 'fake.json'), 'w') as fd: fd.write(json.dumps({'testing':True})) assert initialize.init_environment(tmpdir, 'staging', 'dev') assert are_dir_trees_equal(dev_dir, os.path.join(config_dir, 'staging'))
def test_environment(tmpdir): tmpdir = str(tmpdir) assert initialize.init(tmpdir, None, force=True) assert initialize.init_service(tmpdir, 'api', '0.0.1') assert initialize.init_environment(tmpdir, 'dev', None) config_dir = os.path.join(tmpdir, 'config') items = os.listdir(config_dir) assert len(items) == 1 assert 'dev' in items dev_dir = os.path.join(config_dir, 'dev') subitems = os.listdir(dev_dir) assert len(subitems) == 1 assert 'api' in subitems # Check that config was put in with open(os.path.join(dev_dir, subitems[0])) as jsonfile: config = json.loads(jsonfile.read()) assert 'version' in config assert config['version'] == '0.0.1' assert config['config'] == {} assert 'base_dir' in config # Try copy from with open(os.path.join(dev_dir, 'fake.json'), 'w') as fd: fd.write(json.dumps({'testing': True})) assert initialize.init_environment(tmpdir, 'staging', 'dev') assert are_dir_trees_equal(dev_dir, os.path.join(config_dir, 'staging'))
def test_git_repo(tmpdir): dirname = str(tmpdir) test = template.DirectoryRender(os.path.join(test_path, 'test-git')) test.render(dirname, {}, {}) assert are_dir_trees_equal(dirname, os.path.join(test_path, 'test-git-result'))
def test_autotest(tmpdir): dirname = str(tmpdir) test = template.DirectoryRender(os.path.join(test_path, 'test-autotest')) test.render(dirname, {'name': 'user'}, {}) assert are_dir_trees_equal(dirname, os.path.join(test_path, 'test-autotest-result'))
def test_template_chown_problem(tmpdir): dirname = str(tmpdir) test = template.DirectoryRender(os.path.join(test_path, 'test1-notpermitted')) with pytest.raises(OSError) as ex: test.render(dirname, {'name':'user'}, {}, False) test.render(dirname, {'name':'user'}, {}, True) assert are_dir_trees_equal(dirname, os.path.join(test_path, 'test1result'))
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_commit_deep(tmpdir): tmpdir = str(tmpdir) base_dir = os.path.join(tmpdir, 'base_dir') serv_dir = os.path.join(tmpdir, 'serv_dir') atom_base = 'atomic-no1' atom_yes = os.path.join(atom_base, 'atomic') atom_no = 'atomic-no2' atom_dir = os.path.join(serv_dir, atom_yes) atom_base_dir = os.path.join(serv_dir, atom_base) atom_no_dir = os.path.join(serv_dir, atom_no) dir_info = { 'api': { 'base_dir': base_dir, 'dir': serv_dir, 'atomic': { atom_yes: True, atom_no: False } } } # Atomic file os.makedirs(atom_dir) with open(os.path.join(atom_dir, 'atom.txt'), 'w') as wfile: wfile.write('atomic update') with open(os.path.join(atom_base_dir, 'atom-no.txt'), 'w') as wfile: wfile.write('not an atomic update') # Non-atomic directory os.makedirs(atom_no_dir) with open(os.path.join(atom_no_dir, 'atom-no.txt'), 'w') as wfile: wfile.write('non-atomic update') # Non-atomic file with open(os.path.join(serv_dir, 'non-atom.txt'), 'w') as wfile: wfile.write('non-atomic update') result = commit.commit(dir_info) assert are_dir_trees_equal(base_dir, serv_dir) assert os.path.islink(os.path.join(base_dir, atom_yes)) == True assert os.path.islink(os.path.join(base_dir, atom_base)) == False assert os.path.islink(os.path.join(base_dir, atom_no)) == False assert len(result) == 1 assert 'api' in result assert len(result['api']) == 4 assert 'atomic-no1/atomic/atom.txt' in result['api'] assert 'atomic-no1/atom-no.txt' in result['api'] assert 'atomic-no2/atom-no.txt' in result['api'] assert 'non-atom.txt' in result['api']
def test_git_repo_chmod(tmpdir): dirname = str(tmpdir) test = template.DirectoryRender(os.path.join(test_path, 'test-git-chmod')) test.render(dirname, {}, {}) result_dir = os.path.join(test_path, 'test-git-result') assert are_dir_trees_equal(dirname, result_dir) st = os.stat(os.path.join(dirname, 'test', 'install')) # Chose some weird mode that wouldn't normally be set assert stat.S_IMODE(st.st_mode) == 0604
def test_template_chown_problem(tmpdir): dirname = str(tmpdir) test = template.DirectoryRender( os.path.join(test_path, 'test1-notpermitted')) with pytest.raises(OSError) as ex: test.render(dirname, {'name': 'user'}, {}, False) test.render(dirname, {'name': 'user'}, {}, True) assert are_dir_trees_equal(dirname, os.path.join(test_path, 'test1result'))
def test_git_repo_chmod(tmpdir): dirname = str(tmpdir) test = template.DirectoryRender(os.path.join(test_path,'test-git-chmod')) test.render(dirname, {}, {}) result_dir = os.path.join(test_path,'test-git-result') assert are_dir_trees_equal(dirname, result_dir) st = os.stat(os.path.join(dirname, 'test', 'install')) # Chose some weird mode that wouldn't normally be set assert stat.S_IMODE(st.st_mode) == 0604
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_commit_deep(tmpdir): tmpdir = str(tmpdir) base_dir = os.path.join(tmpdir, 'base_dir') serv_dir = os.path.join(tmpdir, 'serv_dir') atom_base = 'atomic-no1' atom_yes = os.path.join(atom_base, 'atomic') atom_no = 'atomic-no2' atom_dir = os.path.join(serv_dir, atom_yes) atom_base_dir = os.path.join(serv_dir, atom_base) atom_no_dir = os.path.join(serv_dir, atom_no) dir_info = {'api': {'base_dir' : base_dir, 'dir' : serv_dir, 'atomic': {atom_yes : True, atom_no : False}}} # Atomic file os.makedirs(atom_dir) with open(os.path.join(atom_dir, 'atom.txt'), 'w') as wfile: wfile.write('atomic update') with open(os.path.join(atom_base_dir, 'atom-no.txt'), 'w') as wfile: wfile.write('not an atomic update') # Non-atomic directory os.makedirs(atom_no_dir) with open(os.path.join(atom_no_dir, 'atom-no.txt'), 'w') as wfile: wfile.write('non-atomic update') # Non-atomic file with open(os.path.join(serv_dir, 'non-atom.txt'), 'w') as wfile: wfile.write('non-atomic update') result = commit.commit(dir_info) assert are_dir_trees_equal(base_dir, serv_dir) assert os.path.islink(os.path.join(base_dir, atom_yes)) == True assert os.path.islink(os.path.join(base_dir, atom_base)) == False assert os.path.islink(os.path.join(base_dir, atom_no)) == False assert len(result) == 1 assert 'api' in result assert len(result['api']) == 4 assert 'atomic-no1/atomic/atom.txt' in result['api'] assert 'atomic-no1/atom-no.txt' in result['api'] assert 'atomic-no2/atom-no.txt' in result['api'] assert 'non-atom.txt' in result['api']
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_template_with_config(tmpdir): dirname = str(tmpdir) test = template.DirectoryRender(os.path.join(test_path, 'test2')) result = test.render(dirname, {'name': 'user'}, {}) assert are_dir_trees_equal(dirname, os.path.join(test_path, 'test2result')) assert len(result) == 3 assert result['test2/'] == True assert result['test3/'] == False assert result['test3/atomic/'] == True st_file = os.stat(os.path.join(dirname, 'file')) assert stat.S_IMODE(st_file.st_mode) == 0642 st_test3 = os.stat(os.path.join(dirname, 'test3')) assert stat.S_IMODE(st_test3.st_mode) == 0775 st_test3_file = os.stat(os.path.join(dirname, 'test3', 'hello.txt')) assert stat.S_IMODE(st_test3_file.st_mode) != 0775 # not recursive
def test_template_with_config(tmpdir): dirname = str(tmpdir) test = template.DirectoryRender(os.path.join(test_path,'test2')) result = test.render(dirname, {'name':'user'}, {}) assert are_dir_trees_equal(dirname, os.path.join(test_path,'test2result')) assert len(result) == 3 assert result['test2/'] == True assert result['test3/'] == False assert result['test3/atomic/'] == True st_file = os.stat(os.path.join(dirname, 'file')) assert stat.S_IMODE(st_file.st_mode) == 0642 st_test3 = os.stat(os.path.join(dirname, 'test3')) assert stat.S_IMODE(st_test3.st_mode) == 0775 st_test3_file = os.stat(os.path.join(dirname, 'test3', 'hello.txt')) assert stat.S_IMODE(st_test3_file.st_mode) != 0775 # not recursive
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_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_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_template_basic(tmpdir): dirname = str(tmpdir) test = template.DirectoryRender(os.path.join(test_path, 'test1')) test.render(dirname, {'name': 'user', 'variable': 'test3'}, {}) assert are_dir_trees_equal(dirname, os.path.join(test_path, 'test1result'))
def test_autotest(tmpdir): dirname = str(tmpdir) test = template.DirectoryRender(os.path.join(test_path,'test-autotest')) test.render(dirname, {'name':'user'}, {}) assert are_dir_trees_equal(dirname, os.path.join(test_path,'test-autotest-result'))
def test_template_basic(tmpdir): dirname = str(tmpdir) test = template.DirectoryRender(os.path.join(test_path, 'test1')) test.render(dirname, {'name':'user', 'variable': 'test3'}, {}) assert are_dir_trees_equal(dirname, os.path.join(test_path, 'test1result'))
def test_git_repo(tmpdir): dirname = str(tmpdir) test = template.DirectoryRender(os.path.join(test_path,'test-git')) test.render(dirname, {}, {}) assert are_dir_trees_equal(dirname, os.path.join(test_path,'test-git-result'))