def test_config_override(self): """ Make sure local config files overrides default values.""" # We don't care about the AMI cache for this test, # but the file has to exist and to contain valid json self._create_local_file( res.DEFAULT_AMI_CACHE_FILENAME + res.EXTENSIONS[0], '{}') # Create a local config file and verify that it overrides the factory default config = self._create_dummy_config() # Change one of the values original_value = config['global']['environment_name'] config['global']['environment_name'] = original_value + 'dummy' with open(res.DEFAULT_CONFIG_FILENAME + res.EXTENSIONS[0], 'w') as f: f.write(yaml.dump(config)) f.flush() fake_cli = self.fake_cli(['create']) base = eb.EnvironmentBase(fake_cli) base.load_config() self.assertNotEqual(base.config['global']['environment_name'], original_value) # 4) Validate local config with non-default name config_filename = 'not_default_name' # existence check with self.assertRaises(Exception): base = eb.EnvironmentBase( self.fake_cli(['create', '--config-file', config_filename])) base.load_config() # remove config.json and create the alternate config file os.remove(res.DEFAULT_CONFIG_FILENAME + res.EXTENSIONS[0]) self.assertFalse( os.path.isfile(res.DEFAULT_CONFIG_FILENAME + res.EXTENSIONS[0])) with open(config_filename, 'w') as f: f.write(yaml.dump(config)) f.flush() base = eb.EnvironmentBase( self.fake_cli(['create', '--config-file', config_filename])) base.load_config() self.assertNotEqual(base.config['global']['environment_name'], original_value)
def test_alternate_view(self): """ More of an example of how to use your own custom view than a test """ actions_called = {'init': 0, 'deploy': 0, 'create': 0, 'delete': 0} class MyView(object): def __init__(self): super(MyView, self).__init__() # Start an api, a web server or a rich client UI for example # Record user request(s), the controller will then call process_request() # so the can relay user requests to the appropriate controller action self.user_actions = ['init', 'create', 'deploy', 'delete'] self.user_config_changes = {'output_filename': 'output.txt'} def update_config(self, config): # Update any config properties you need to # config['global']['print_debug'] = self.user_config_changes['debug'] config['global']['output'] = self.user_config_changes[ 'output_filename'] def process_request(self, controller): for action in self.user_actions: # if action == 'create': # controller.create_action() # elif action == 'deploy': # controller.deploy_action() actions_called[action] += 1 eb.EnvironmentBase(MyView()) self.assertEqual(actions_called['init'], 1) self.assertEqual(actions_called['create'], 1) self.assertEqual(actions_called['deploy'], 1) self.assertEqual(actions_called['delete'], 1)
def test_constructor(self): """Make sure EnvironmentBase passes control to view to process user requests""" fake_cli = self.fake_cli(['init']) env_base = eb.EnvironmentBase(fake_cli) # Check that EnvironmentBase started the CLI fake_cli.process_request.assert_called_once_with(env_base)
def test_template_file_flag(self): # verify that the --template-file flag changes the config value dummy_value = 'dummy' base = eb.EnvironmentBase( self.fake_cli(['create', '--template-file', dummy_value])) base.init_action() base.load_config() self.assertEqual(base.config['global']['output'], dummy_value)
def test_generate_config(self): """ Verify cli flags update config object """ # Verify that debug and output are set to the factory default base = eb.EnvironmentBase(self.fake_cli(['init'])) base.init_action() base.load_config() self.assertEqual(base.config['global']['print_debug'], res.FACTORY_DEFAULT_CONFIG['global']['print_debug']) self.assertEqual(base.config['global']['output'], res.FACTORY_DEFAULT_CONFIG['global']['output'])
def test_factory_default(self): with self.assertRaises(Exception): base = eb.EnvironmentBase(self.fake_cli(['init'])) base.load_config() # Create refs to files that should be created and make sure they don't already exists config_file = os.path.join(self.temp_dir, res.DEFAULT_CONFIG_FILENAME) ami_cache_file = os.path.join(self.temp_dir, res.DEFAULT_AMI_CACHE_FILENAME) self.assertFalse(os.path.isfile(config_file)) self.assertFalse(os.path.isfile(ami_cache_file)) # Verify that create_missing_files works as intended base = eb.EnvironmentBase(self.fake_cli(['init'])) base.init_action() self.assertTrue(os.path.isfile(config_file)) # TODO: After ami_cache is updated change 'create_missing_files' to be singular # self.assertTrue(os.path.isfile(ami_cache_file)) # Verify that the previously created files are loaded up correctly eb.EnvironmentBase(self.fake_cli(['create']))
def test_config_yaml(self): """ Make sure load_config can load yaml files.""" with open("config.yaml", 'w') as f: f.write( yaml.dump(res.FACTORY_DEFAULT_CONFIG, default_flow_style=False)) f.flush() fake_cli = self.fake_cli(['create', '--config-file', 'config.yaml']) base = eb.EnvironmentBase(fake_cli) base.load_config() self.assertEqual(base.config['global']['environment_name'], 'environmentbase')
def test_config_yaml(self): """ Make sure load_config can load yaml files.""" with open("config.yaml", 'w') as f: f.write(""" global: setting: value # We test this setting print_debug: false output: dummy environment_name: test monitor_stack: false network: network_cidr_base: dummy subnet_types: - dummy az_count: 1 private_subnet_size: dummy public_subnet_size: dummy network_cidr_size: dummy public_subnet_count: 1 private_subnet_count: 1 template: ami_map_file: dummy template_upload_acl: dummy utility_bucket: dummy description: dummy template_bucket: dummy ec2_key_default: dummy s3_template_prefix: dummy nat: instance_type: dummy enable_ntp: false """) f.flush() fake_cli = self.fake_cli(['create', '--config-file', 'config.yaml']) base = eb.EnvironmentBase(fake_cli) base.load_config() self.assertEqual(base.config['global']['setting'], 'value')
def test_config_file_flag(self): dummy_value = 'dummy' base = eb.EnvironmentBase( self.fake_cli(['create', '--config-file', dummy_value])) base.init_action() self.assertTrue(os.path.isfile(dummy_value))
def test_config_validation(self): """ environmentbase.TEMPLATE_REQUIREMENTS defines the required sections and keys for a valid input config file This test ensures that EnvironmentBase._validate_config() enforces the TEMPLATE_REQUIREMENTS contract """ cntrl = eb.EnvironmentBase(self.fake_cli(['create'])) valid_config = self._create_dummy_config() cntrl._validate_config(valid_config) # Find a section with at least one required key section = '' keys = {} while True: (section, keys) = valid_config.items()[0] if len(keys) > 0: break assert len(keys) > 0 # Check type error with self.assertRaises(eb.ValidationError): invalid_config = copy.deepcopy(valid_config) invalid_config['global']['print_debug'] = "dfhkjdshf" cntrl._validate_config(invalid_config) # Check missing key validation (key, value) = keys.items()[0] del valid_config[section][key] with self.assertRaises(eb.ValidationError): cntrl._validate_config(valid_config) # Check missing section validation del valid_config[section] with self.assertRaises(eb.ValidationError): cntrl._validate_config(valid_config) # Check wildcard sections extra_reqs = {'*-db': {'host': 'str', 'port': 'int'}} extra_reqs.update(res.CONFIG_REQUIREMENTS) valid_config.update({ 'my-db': { 'host': 'localhost', 'port': 3306 }, 'my-other-db': { 'host': 'localhost', 'port': 3306 } }) # Check deep nested sections extra_reqs = {'lets': {'go': {'deeper': {'key': 'str'}}}} extra_reqs.update(res.CONFIG_REQUIREMENTS) valid_config.update( {'lets': { 'go': { 'deeper': { 'key': 'super_secret_value' } } }})