def test_shub_size(self): '''test the function to return Singularity Hub Image Size ''' print('Testing Singularity Hub Size') from sutils import read_file shub_cont = "shub://vsoch/singularity-hello-world" os.environ['SINGULARITY_CONTAINER'] = shub_cont os.environ['SINGULARITY_CONTENTS'] = self.file script_path = "%s/size.py" % self.here if VERSION == 2: testing_command = ["python2", script_path] else: testing_command = ["python3", script_path] print(' '.join(testing_command)) output = Popen(testing_command, stderr=STDOUT, stdout=PIPE) t = output.communicate()[0], output.returncode result = {'message': t[0], 'return_code': t[1]} if result['return_code'] != 0: print(result['message']) self.assertEqual(result['return_code'], 0) result = read_file(self.file)[0] self.assertEqual('260', result)
def test_write_read_files(self): '''test_write_read_files will test the functions write_file and read_file ''' print("Testing utils.write_file...") from sutils import write_file import json tmpfile = tempfile.mkstemp()[1] os.remove(tmpfile) write_file(tmpfile, "hello!") self.assertTrue(os.path.exists(tmpfile)) print("Testing utils.read_file...") from sutils import read_file content = read_file(tmpfile)[0] self.assertEqual("hello!", content) from sutils import write_json print("Testing utils.write_json...") print("Case 1: Providing bad json") bad_json = {"Wakkawakkawakka'}": [{True}, "2", 3]} tmpfile = tempfile.mkstemp()[1] os.remove(tmpfile) with self.assertRaises(TypeError) as cm: write_json(bad_json, tmpfile) print("Case 2: Providing good json") good_json = {"Wakkawakkawakka": [True, "2", 3]} tmpfile = tempfile.mkstemp()[1] os.remove(tmpfile) write_json(good_json, tmpfile) content = json.load(open(tmpfile, 'r')) self.assertTrue(isinstance(content, dict)) self.assertTrue("Wakkawakkawakka" in content)
def test_docker_size(self): '''test the function to return Docker size ''' print('Testing Docker Size') from sutils import read_file sha256 = "476959f29a17423a24a17716e058352ff" sha256 += "6fbf13d8389e4a561c8ccc758245937" sha256 = "docker://debian@sha256:%s" % sha256 os.environ['SINGULARITY_CONTAINER'] = sha256 os.environ['SINGULARITY_CONTENTS'] = self.file script_path = "%s/size.py" % self.here if VERSION == 2: testing_command = ["python2", script_path] else: testing_command = ["python3", script_path] output = Popen(testing_command, stderr=STDOUT, stdout=PIPE) t = output.communicate()[0], output.returncode result = {'message': t[0], 'return_code': t[1]} self.assertEqual(result['return_code'], 0) result = int(read_file(self.file)[0]) self.assertTrue(250 <= result <= 251)
def configure(args): # Get fullpath to each file, and concurrently check that exists defaultfile = get_fullpath(args.defaults) # ../src/lib/config_defaults.h infile = get_fullpath(args.infile) # singularity.conf.in # Find define statements define_re = re.compile("#define ([A-Z_]+) (.*)") # Read in input and default files defaultfile = read_file(defaultfile) data = "".join(read_file(infile)) # Lookup for values we want replaced lookup = {'0': 'no', '1': 'yes'} defaults = {} # Read in defaults to dictionary for line in defaultfile: match = define_re.match(line) if match: key, value = match.groups() # Maintain the original default set by user defaults[key] = value # Use parsed value for final config new_value = value.replace('"', '') if new_value in lookup: new_value = lookup[new_value] data = data.replace("@" + key + "@", new_value) # Write to output file outfile = "%s.tmp" % args.outfile write_file(outfile, data) os.rename(outfile, args.outfile) bot.info("*** FINISHED PYTHON CONFIGURATION HELPER ****\n")
def INSPECT(inspect_labels=None,inspect_def=None,inspect_runscript=None,inspect_test=None, inspect_env=None,pretty_print=True): '''INSPECT will print a "manifest" for an image, with one or more variables asked for by the user. The default prints a human readable format (text and json) and if pretty_print is turned to False, the entire thing will be returned as json via the JSON API standard. The base is checked for the metadata folder, and if it does not exist, the links are searched for and parsed (to support older versions). :param inspect_runscript: if not None, will include runscript, if it exists :param inspect_labels: if not None, will include labels, if they exist. :param inspect_def: if not None, will include definition file, if it exists :param inspect_test: if not None, will include test, if it exists. :param inspect_env: if not None, will include environment, if exists. :param pretty_print: if False, return all JSON API spec ''' data = dict() errors = dict() # Labels if inspect_labels: bot.verbose2("Inspection of labels selected.") if os.path.exists(LABELFILE): data["labels"] = read_json(LABELFILE) else: data["labels"] = None errors["labels"] = generate_error(404, detail="This container does not have labels", title="Labels Undefined") # Definition File if inspect_def: bot.verbose2("Inspection of deffile selected.") if os.path.exists(DEFFILE): data["deffile"] = read_file(DEFFILE,readlines=False) else: data["deffile"] = None errors["deffile"] = generate_error(404,title="Definition File Undefined", detail="This container does not include the bootstrap definition") # Runscript if inspect_runscript: bot.verbose2("Inspection of runscript selected.") if os.path.exists(RUNSCRIPT): data["runscript"] = read_file(RUNSCRIPT,readlines=False) else: data["runscript"] = None errors["runscript"] = generate_error(404,title="Runscript Undefined", detail="This container does not have any runscript defined") # Test if inspect_test: bot.verbose2("Inspection of test selected.") if os.path.exists(TESTFILE): data["test"] = read_file(TESTFILE,readlines=False) else: data["test"] = None errors["test"] = generate_error(404,title="Tests Undefined", detail="This container does not have any tests defined") # Environment if inspect_env: bot.verbose2("Inspection of environment selected.") if os.path.exists(ENVIRONMENT): data["environment"] = read_file(ENVIRONMENT,readlines=False) else: data["environment"] = None errors["environment"] = generate_error(404,title="Tests Undefined", detail="This container does not have any custom environment defined") if pretty_print: bot.verbose2("Structured printing specified.") for dtype,content in data.items(): if content is not None: if isinstance(content,dict): print_json(content,print_console=True) else: bot.info(content) else: print_json(errors[dtype],print_console=True) else: bot.verbose2("Unstructed printing specified") # Only define a type if there is data to return, else return errors result = dict() if len(data) > 0: result["data"] = {"attributes": data, "type": "container" } else: result["errors"] = [] for dtype,content in errors.items(): result["errors"].append(content) print_json(result,print_console=True)