def test_return(): # ensure bad returns are thrown try: proc.call(['exit 1'], shell=True) assert False except proc.BadExitCode: pass
def test_encoding(): output = proc.call(['cat', 'happy.txt']) assert output == 'Häppy ☺\n' # 'cat' appends a newline try: bad = proc.call(['cat', 'UTF-8-test.txt']) assert False except UnicodeDecodeError: assert True
def test_example(): # Example from docs with fs.NamedTempFile() as nm: proc.call( "curl http://mortoray.com/ -o {}".format( nm ) ) html = open(nm,'r').read() print( len(html) ) name = nm assert not os.path.exists(name)
def get_next_id(): params = ["crudini", "--get", DB_FILE, "globals", "next_id"] raw_output, exit_code = proc.call(params, shell=False, check_exit_code=False) if exit_code: return 1 return int(raw_output)
def set_next_id(next_cidr_id): params = [ "crudini", "--set", DB_FILE, "globals", "next_id", str(next_cidr_id) ] _raw_output, exit_code = proc.call(params, shell=False, check_exit_code=False) return exit_code
def delete_cidr(cidr_id): params = ["crudini", "--del", DB_FILE, str(cidr_id)] _raw_output, exit_code = proc.call(params, shell=False, check_exit_code=False) if not exit_code: # Purge away all known allocations to this cidr. # Maybe that should prevent cidr from being removed? _raw_output, exit_code = _delete_cidr_allocation(cidr_id) return exit_code
def _update_cidr_allocation(cidr_id, operation, address, value=None): section = CIDR_ALLOC_SECTION.format(cidr_id) params = ['crudini', operation, DB_FILE, section] if address: # encode address in a way where ':' is replaced with '_'. This is to make crudini happy address_encoded = address.replace(':', '_') params.append(address_encoded) if value: params.append(value) output, exit_code = proc.call(params, shell=False, check_exit_code=False) return output, exit_code
def _read_cidr_allocations(cidr_id): section = CIDR_ALLOC_SECTION.format(cidr_id) allocations = set() # Make sure cidr_id exists cidr_dicts = read_cidrs(family_filter=None, cidr_id_filter=cidr_id) if not cidr_dicts: return None, allocations cidr_dict = cidr_dicts[0] params = ['crudini', '--get', '--format=lines', DB_FILE, section] output, exit_code = proc.call(params, shell=False, check_exit_code=False) if exit_code == 0: allocations.update(_parse_allocations_raw(output)) return cidr_dict, allocations
def read_cidrs(family_filter=None, cidr_id_filter=None): cidr_raw = '' params = ['crudini', '--get', '--format=lines', DB_FILE] if cidr_id_filter: params.append(str(cidr_id_filter)) output, exit_code = proc.call(params, shell=False, check_exit_code=False) if output and exit_code == 0: cidr_raw = str(output) cidrs_dict = _parse_cidr_raw(cidr_raw) cidrs = [] od = collections.OrderedDict(sorted(cidrs_dict.items())) for cidr_id, cidr_attrs in od.items(): if family_filter and cidr_attrs.get("family") != family_filter: continue cidr_dict = { "subnet_id": str(cidr_id), "family": cidr_attrs.get("family"), "cidr": cidr_attrs.get("cidr") } cidrs.append(cidr_dict) return cidrs
def test_shell(): out, code = proc.call(['exit 123'], shell=True, check_exit_code=False) assert out == '' assert code == 123
def test_not_timeout(): proc.call('sleep 1', timeout=10)
def test_basic(): out = proc.call('echo 123') assert out == '123\n'
def test_timeout(): try: proc.call('sleep 10', timeout=0.1) assert False except proc.Timeout: pass
def save_cidr(cidr_dict): params_fixed = ["crudini", "--set", DB_FILE, cidr_dict.get("subnet_id")] for k in ["family", "cidr"]: proc.call(params_fixed + [k, cidr_dict.get(k)], shell=False, check_exit_code=True)