def test_conditional_miss2(self): serialized_input = { "key": "alias", "title": "Alias", "type": "string", "help": "Name of alias to assign this custom dev stack", "required": True, "conditions": { "kvps": "env:dev,date:today" } } inputs = Input(serialized_input) params = Params({"env": {"value": "dev"}}) # inject an invalid response followed by a valid response sys.stdin = io.StringIO("my-stack") value, user_prompted = inputs.process(params) # test that the user was not prompted and that no value was returned self.assertTrue(not user_prompted) self.assertTrue(not value)
def __init__(self, serialized_input_array, inputs_cacher=None): """ Provides a species of stack params that are input interactively via command line prompts. Inputs are typically used when a service provider wants to create an easy installation experience for their service. Args: serialized_input_array: dictionary satisfying the yac/schema/input.json schema inputs_cacher: An instance of InstanceCacher Returns: None Raises: ValidationError: if the serialized_input_array fails schema validation or if any individual input fails """ self.inputs_cacher = inputs_cacher self.standard = [] self.conditional = [] for serialized_input in serialized_input_array: if 'conditions' in serialized_input: self.conditional.append(Input(serialized_input)) else: self.standard.append(Input(serialized_input))
def test_bool(self): serialized_input = { "key": "rebuild", "title": "Rebuilt", "type": "bool", "help": "Rebuild the stack?", "required": True } inputs = Input(serialized_input) params = Params({}) # inject the correct response to inputs prompt into stdin sys.stdin = io.StringIO('False') value, user_prompted = inputs.process(params) # test that the user was prompted and that dev was returned self.assertTrue(user_prompted) self.assertTrue(not value)
def test_string_no_options(self): serialized_input = { "key": "env", "title": "Environment", "type": "string", "help": "The environment to build stack for", "required": True } inputs = Input(serialized_input) params = Params({}) # inject the correct response to inputs prompt into stdin sys.stdin = io.StringIO('dev') value, user_prompted = inputs.process(params) # test that the user was prompted and that dev was returned self.assertTrue(user_prompted) self.assertTrue(value == 'dev')
def test_int(self): serialized_input = { "key": "scale", "type": "int", "title": "Scale", "help": "Horizontal scale", "required": True } inputs = Input(serialized_input) params = Params({}) # inject the correct response to inputs prompt into stdin sys.stdin = io.StringIO('4') value, user_prompted = inputs.process(params) # test that the user was prompted and that dev was returned self.assertTrue(user_prompted) self.assertTrue(value == 4)
def test_bool_invalid(self): serialized_input = { "key": "rebuild", "title": "Rebuilt", "type": "bool", "help": "Rebuild the stack?", "required": True } inputs = Input(serialized_input) params = Params({}) # inject an invalid response followed by a valid response sys.stdin = io.StringIO('yes\nfalse') value, user_prompted = inputs.process(params) # test that the user was prompted and that dev was returned self.assertTrue(user_prompted) self.assertTrue(not value)
def test_int_invalid(self): serialized_input = { "key": "scale", "type": "int", "title": "Scale", "help": "Horizontal scale", "required": True } inputs = Input(serialized_input) params = Params({}) # inject an invalid response followed by a valid response sys.stdin = io.StringIO("test\n13") value, user_prompted = inputs.process(params) # test that the user was prompted and that dev was returned self.assertTrue(user_prompted) self.assertTrue(value == 13)
def test_optional(self): serialized_input = { "key": "alias", "title": "Alias", "type": "string", "help": "Name of alias to assign this custom dev stack", "required": False, } inputs = Input(serialized_input) params = Params({}) # inject a carriage return sys.stdin = io.StringIO("\n") value, user_prompted = inputs.process(params) # test that the user was prompted and that an empty string was returned self.assertTrue(user_prompted) self.assertTrue(value == "")
def test_required(self): serialized_input = { "key": "alias", "title": "Alias", "type": "string", "help": "Name of alias to assign this custom dev stack", "required": True, } inputs = Input(serialized_input) params = Params({}) # inject a response sys.stdin = io.StringIO("my-stack") value, user_prompted = inputs.process(params) # test that the user was prompted and that 'my-stack' was returned self.assertTrue(user_prompted) self.assertTrue(value == "my-stack")
def test_string_invalid_option(self): serialized_input = { "key": "env", "title": "Environment", "type": "string", "help": "The environment to build stack for", "required": True, "options": ["stage"] } inputs = Input(serialized_input) params = Params({}) # inject an invalid response followed by a valid response sys.stdin = io.StringIO("dev\nstage") value, user_prompted = inputs.process(params) # test that the user was prompted and that dev was returned self.assertTrue(user_prompted) self.assertTrue(value == 'stage')
def test_optional_w_options(self): serialized_input = { "key": "color", "title": "Color", "type": "string", "help": "Your hair color", "required": False, "options": ["red", "brown"] } inputs = Input(serialized_input) params = Params({}) # inject a carriage return sys.stdin = io.StringIO("\n") value, user_prompted = inputs.process(params) # test that the user was prompted and that an empty string was returned self.assertTrue(user_prompted) self.assertTrue(value == "")
def test_schema_good(self): serialized_input = { "key": "alias", "title": "Alias", "type": "string", "help": "Name of alias to assign this custom dev stack", "required": True, "conditions": { "kvps": "env:dev" } } # test that the inputs doesn't throw any schema validation errors validation_success = True try: inputs = Input(serialized_input) except ValidationError as e: validation_success = False print("validation failed") print(e) self.assertTrue(validation_success == True)
def test_schema_bad_kvps(self): serialized_input = { "key": "alias", "title": "Alias", "type": "string", "help": "Name of alias to assign this custom dev stack", "required": True, "conditions": { "kvp": "env:dev" } } # test that the a schema validation errors is raised # do to the kee attribute validation_success = True try: inputs = Input(serialized_input) except ValidationError as e: validation_success = False print("validation failed") print(e) self.assertTrue(validation_success == False)