Skip to content

Commit

Permalink
Merge d986441 into 2b49f1f
Browse files Browse the repository at this point in the history
  • Loading branch information
DarrinFong committed Apr 28, 2020
2 parents 2b49f1f + d986441 commit aeea2b9
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 30 deletions.
7 changes: 5 additions & 2 deletions tools/python/boutiques/bosh.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,10 @@ def execute(*params):
"requireComplete": results.complete,
"sandbox": results.sandbox})
if not inp:
executor.generateRandomParams(1)
# Add optional inputs with default-value to inputs_dict,
# which is then populated with random params
executor.in_dict = addDefaultValues(executor.desc_dict, {})
executor.generateRandomParams()

if results.json:
sout = [json.dumps(
Expand Down Expand Up @@ -677,7 +680,7 @@ def example(*params):
"skipDataCollect": True,
"requireComplete": results.complete,
"sandbox": results.sandbox})
executor.generateRandomParams(1)
executor.generateRandomParams()
return json.dumps(
customSortInvocationByInput(executor.in_dict, descriptor), indent=4)

Expand Down
51 changes: 25 additions & 26 deletions tools/python/boutiques/localExec.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,8 @@ def checkMutualRequirements(targ):

# Start actual dictionary filling part
# Clear the dictionary
self.in_dict = {}
self.in_dict = self.in_dict if hasattr(self, 'in_dict') and\
self.in_dict is not None else {}
for params in [r for r in self.inputs if not r.get('optional')]:
self.in_dict[params['id']] = makeParam(params)

Expand Down Expand Up @@ -913,38 +914,36 @@ def checkMutualRequirements(targ):
# Function to generate random parameter values
# This fills the in_dict with random values, validates the input,
# and generates the appropriate command line
def generateRandomParams(self, n):
def generateRandomParams(self):

'''
The generateRandomParams method fills the in_dict field
with randomly generated values following the schema.
It then generates command line strings based on these
values (more than 1 if -n was given).
It then generates command line strings based on these values
'''

self.cmd_line = []
for i in range(0, n):
# Set in_dict with random values
self._randomFillInDict()
# Look at generated input, if debugging
if self.debug:
print_info("Input: " + str(self.in_dict))
# Check results (as much as possible)
try:
args = [self.desc_path, "-i", json.dumps(self.in_dict)]
if self.sandbox:
args.append("--sandbox")
boutiques.invocation(*args)
# If an error occurs, print out the problems already
# encountered before blowing up
except Exception as e: # Avoid BaseExceptions like SystemExit
sys.stderr.write("An error occurred in validation\n"
"Previously saved issues\n")
for err in self.errs:
sys.stderr.write("\t" + str(err) + "\n")
raise e # Pass on (throw) the caught exception
# Add new command line
self.cmd_line.append(self._generateCmdLineFromInDict())
# Set in_dict with random values
self._randomFillInDict()
# Look at generated input, if debugging
if self.debug:
print_info("Input: " + str(self.in_dict))
# Check results (as much as possible)
try:
args = [self.desc_path, "-i", json.dumps(self.in_dict)]
if self.sandbox:
args.append("--sandbox")
boutiques.invocation(*args)
# If an error occurs, print out the problems already
# encountered before blowing up
except Exception as e: # Avoid BaseExceptions like SystemExit
sys.stderr.write("An error occurred in validation\n"
"Previously saved issues\n")
for err in self.errs:
sys.stderr.write("\t" + str(err) + "\n")
raise e # Pass on (throw) the caught exception
# Add new command line
self.cmd_line.append(self._generateCmdLineFromInDict())

# Read in parameter input file or string
def readInput(self, infile):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "tool",
"tool-version": "1",
"description": "a tool",
"command-line": "tool_cli [VALUE]",
"schema-version": "0.5",
"inputs": [
{
"id": "value",
"name": "value",
"type": "String",
"value-key": "[VALUE]",
"value-choices": ["yes", "no"],
"default-value": "no",
"optional": true
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "tool",
"tool-version": "1",
"description": "a tool",
"command-line": "tool_cli [CONFIG_FILE]",
"schema-version": "0.5",
"inputs": [
{
"id": "value",
"name": "value",
"type": "String",
"value-key": "[VALUE]",
"value-choices": ["yes", "no"],
"default-value": "no",
"optional": true
}
],
"output-files": [
{
"id": "config_file",
"name": "Configuration file",
"value-key": "[CONFIG_FILE]",
"path-template": "tmpConfig.toml",
"file-template": [
"hi__",
"value = [VALUE]",
"__bye"
]
}
]
}
6 changes: 4 additions & 2 deletions tools/python/boutiques/tests/test_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,7 @@ def test_example_requires_group_complete_x10(self):
# Bosh example is inherently random,
# Couldn't even inject prederemined input to executor.in_dict
# because _randomFillInDict clears it
executor.generateRandomParams(100)
self.assertGreater(len(executor.in_dict), 0)
for _ in range(0, 100):
executor.generateRandomParams()
self.assertGreater(len(executor.in_dict), 0)
executor.in_dict = None
34 changes: 34 additions & 0 deletions tools/python/boutiques/tests/test_simulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,37 @@ def test_collapsing_whitespace_requireds(self):
command = re.search(r"(test[ \da-z]+)", output).group(0)

self.assertNotIn(" ", command)

def test_consistency_withAndWithout_invoc(self):
descriptor = os.path.join(os.path.split(bfile)[0],
'schema/examples/'
'test_simulate_consistency.json')
noInvoc = bosh.execute("simulate", descriptor).stdout
wInvoc = bosh.execute("simulate", descriptor, "-i",
bosh.example(descriptor)).stdout
self.assertEqual(noInvoc, wInvoc)

def test_consistency_withAndWithout_invoc_withConfigFile(self):
descriptor = os.path.join(os.path.split(bfile)[0],
'schema/examples/'
'test_simulate_consistency_configFile.json')
invoc = "tmpInvoc.json"
config = "tmpConfig.toml"
wInvocCommand = ("bosh example {0}" +
" > {1} " +
" && bosh exec simulate {0} -i {1}").format(descriptor,
invoc)
noInvocCommand = "bosh exec simulate {0}".format(descriptor, invoc)

subprocess.call(wInvocCommand, shell=True)
with open(config, "r+") as configFile:
wInvoc = configFile.readlines()
os.remove(config)
os.remove(invoc)

subprocess.call(noInvocCommand, shell=True)
with open(config, "r+") as configFile:
noInvoc = configFile.readlines()
os.remove(config)

self.assertEqual(wInvoc, noInvoc)

0 comments on commit aeea2b9

Please sign in to comment.