def test_register_configs_for_all_packs(self): # Verify DB is empty pack_dbs = Pack.get_all() config_dbs = Config.get_all() self.assertEqual(len(pack_dbs), 0) self.assertEqual(len(config_dbs), 0) registrar = ConfigsRegistrar(use_pack_cache=False) registrar._pack_loader.get_packs = mock.Mock() registrar._pack_loader.get_packs.return_value = {"dummy_pack_1": PACK_PATH} packs_base_paths = content_utils.get_packs_base_paths() registrar.register_configs_for_all_packs(base_dirs=packs_base_paths) # Verify pack and schema have been registered pack_dbs = Pack.get_all() config_dbs = Config.get_all() self.assertEqual(len(pack_dbs), 1) self.assertEqual(len(config_dbs), 1) config_db = config_dbs[0] self.assertEqual(config_db.values["api_key"], "{{user.api_key}}") self.assertEqual(config_db.values["api_secret"], "{{user.api_secret}}") self.assertEqual(config_db.values["region"], "us-west-1")
def test_register_configs_for_all_packs(self): # Verify DB is empty pack_dbs = Pack.get_all() config_dbs = Config.get_all() self.assertEqual(len(pack_dbs), 0) self.assertEqual(len(config_dbs), 0) registrar = ConfigsRegistrar(use_pack_cache=False) registrar._pack_loader.get_packs = mock.Mock() registrar._pack_loader.get_packs.return_value = {'dummy_pack_1': PACK_1_PATH} packs_base_paths = content_utils.get_packs_base_paths() registrar.register_from_packs(base_dirs=packs_base_paths) # Verify pack and schema have been registered pack_dbs = Pack.get_all() config_dbs = Config.get_all() self.assertEqual(len(pack_dbs), 1) self.assertEqual(len(config_dbs), 1) config_db = config_dbs[0] self.assertEqual(config_db.values['api_key'], '{{st2kv.user.api_key}}') self.assertEqual(config_db.values['api_secret'], SUPER_SECRET_PARAMETER) self.assertEqual(config_db.values['region'], 'us-west-1')
def test_register_packs(self): # Verify DB is empty pack_dbs = Pack.get_all() config_schema_dbs = ConfigSchema.get_all() self.assertEqual(len(pack_dbs), 0) self.assertEqual(len(config_schema_dbs), 0) registrar = ResourceRegistrar(use_pack_cache=False) registrar._pack_loader.get_packs = mock.Mock() registrar._pack_loader.get_packs.return_value = {'dummy_pack_1': PACK_PATH_1} packs_base_paths = content_utils.get_packs_base_paths() registrar.register_packs(base_dirs=packs_base_paths) # Verify pack and schema have been registered pack_dbs = Pack.get_all() config_schema_dbs = ConfigSchema.get_all() self.assertEqual(len(pack_dbs), 1) self.assertEqual(len(config_schema_dbs), 1) self.assertEqual(pack_dbs[0].name, 'dummy_pack_1') self.assertEqual(len(pack_dbs[0].contributors), 2) self.assertEqual(pack_dbs[0].contributors[0], 'John Doe1 <*****@*****.**>') self.assertEqual(pack_dbs[0].contributors[1], 'John Doe2 <*****@*****.**>') self.assertTrue('api_key' in config_schema_dbs[0].attributes) self.assertTrue('api_secret' in config_schema_dbs[0].attributes)
def test_register_pack_pack_stackstorm_version_and_future_parameters(self): # Verify DB is empty pack_dbs = Pack.get_all() self.assertEqual(len(pack_dbs), 0) registrar = ResourceRegistrar(use_pack_cache=False) registrar._pack_loader.get_packs = mock.Mock() registrar._pack_loader.get_packs.return_value = {'dummy_pack_9': PACK_PATH_9} packs_base_paths = content_utils.get_packs_base_paths() registrar.register_packs(base_dirs=packs_base_paths) # Dependencies, stackstorm_version and future values pack_db = Pack.get_by_name('dummy_pack_9_deps') self.assertEqual(pack_db.dependencies, ['core=0.2.0']) self.assertEqual(pack_db.stackstorm_version, '>=1.6.0, <2.2.0') self.assertEqual(pack_db.system, {'centos': {'foo': '>= 1.0'}}) self.assertEqual(pack_db.python_versions, ['2', '3']) # Note: We only store parameters which are defined in the schema, all other custom user # defined attributes are ignored self.assertTrue(not hasattr(pack_db, 'future')) self.assertTrue(not hasattr(pack_db, 'this')) # Wrong characters in the required st2 version expected_msg = "'wrongstackstormversion' does not match" self.assertRaisesRegexp(ValidationError, expected_msg, registrar._register_pack_db, pack_name=None, pack_dir=PACK_PATH_10)
def test_register_all_configs_with_config_schema_validation_validation_failure( self): # Verify DB is empty pack_dbs = Pack.get_all() config_dbs = Config.get_all() self.assertEqual(len(pack_dbs), 0) self.assertEqual(len(config_dbs), 0) registrar = ConfigsRegistrar(use_pack_cache=False, fail_on_failure=True, validate_configs=True) registrar._pack_loader.get_packs = mock.Mock() registrar._pack_loader.get_packs.return_value = { 'dummy_pack_6': PACK_6_PATH } # Register ConfigSchema for pack registrar._register_pack_db = mock.Mock() registrar._register_pack(pack_name='dummy_pack_5', pack_dir=PACK_6_PATH) packs_base_paths = content_utils.get_packs_base_paths() expected_msg = ( 'Failed validating attribute "regions" in config for pack "dummy_pack_6" ' '(.*?): 1000 is not of type u\'array\'') self.assertRaisesRegexp(jsonschema.ValidationError, expected_msg, registrar.register_from_packs, base_dirs=packs_base_paths)
def test_register_pack_pack_stackstorm_version_and_future_parameters(self): # Verify DB is empty pack_dbs = Pack.get_all() self.assertEqual(len(pack_dbs), 0) registrar = ResourceRegistrar(use_pack_cache=False) registrar._pack_loader.get_packs = mock.Mock() registrar._pack_loader.get_packs.return_value = { 'dummy_pack_9': PACK_PATH_9 } packs_base_paths = content_utils.get_packs_base_paths() registrar.register_packs(base_dirs=packs_base_paths) # Dependencies, stackstorm_version and future values pack_db = Pack.get_by_name('dummy_pack_9_deps') self.assertEqual(pack_db.dependencies, ['core=0.2.0']) self.assertEqual(pack_db.stackstorm_version, '>=1.6.0, <2.2.0') self.assertEqual(pack_db.system, {'centos': {'foo': '>= 1.0'}}) self.assertEqual(pack_db.python_versions, ['2', '3']) # Note: We only store parameters which are defined in the schema, all other custom user # defined attributes are ignored self.assertTrue(not hasattr(pack_db, 'future')) self.assertTrue(not hasattr(pack_db, 'this')) # Wrong characters in the required st2 version expected_msg = "'wrongstackstormversion' does not match" self.assertRaisesRegexp(ValidationError, expected_msg, registrar._register_pack_db, pack_name=None, pack_dir=PACK_PATH_10)
def test_register_all_configs_with_config_schema_validation_validation_failure_1(self): # Verify DB is empty pack_dbs = Pack.get_all() config_dbs = Config.get_all() self.assertEqual(len(pack_dbs), 0) self.assertEqual(len(config_dbs), 0) registrar = ConfigsRegistrar(use_pack_cache=False, fail_on_failure=True, validate_configs=True) registrar._pack_loader.get_packs = mock.Mock() registrar._pack_loader.get_packs.return_value = {'dummy_pack_6': PACK_6_PATH} # Register ConfigSchema for pack registrar._register_pack_db = mock.Mock() registrar._register_pack(pack_name='dummy_pack_5', pack_dir=PACK_6_PATH) packs_base_paths = content_utils.get_packs_base_paths() if six.PY3: expected_msg = ('Failed validating attribute "regions" in config for pack ' '"dummy_pack_6" (.*?): 1000 is not of type \'array\'') else: expected_msg = ('Failed validating attribute "regions" in config for pack ' '"dummy_pack_6" (.*?): 1000 is not of type u\'array\'') self.assertRaisesRegexp(ValueError, expected_msg, registrar.register_from_packs, base_dirs=packs_base_paths)
def test_register_pack_pack_ref(self): # Verify DB is empty pack_dbs = Pack.get_all() self.assertEqual(len(pack_dbs), 0) registrar = ResourceRegistrar(use_pack_cache=False) registrar._pack_loader.get_packs = mock.Mock() registrar._pack_loader.get_packs.return_value = { 'dummy_pack_1': PACK_PATH_1, 'dummy_pack_6': PACK_PATH_6 } packs_base_paths = content_utils.get_packs_base_paths() registrar.register_packs(base_dirs=packs_base_paths) # Ref is provided pack_db = Pack.get_by_name('dummy_pack_6') self.assertEqual(pack_db.ref, 'dummy_pack_6_ref') self.assertEqual(len(pack_db.contributors), 0) # Ref is not provided, directory name should be used pack_db = Pack.get_by_name('dummy_pack_1') self.assertEqual(pack_db.ref, 'dummy_pack_1') # "ref" is not provided, but "name" is registrar._register_pack_db(pack_name=None, pack_dir=PACK_PATH_7) pack_db = Pack.get_by_name('dummy_pack_7_name') self.assertEqual(pack_db.ref, 'dummy_pack_7_name') # "ref" is not provided and "name" contains invalid characters expected_msg = 'contains invalid characters' self.assertRaisesRegexp(ValueError, expected_msg, registrar._register_pack_db, pack_name=None, pack_dir=PACK_PATH_8)
def test_register_packs(self): # Verify DB is empty pack_dbs = Pack.get_all() config_schema_dbs = ConfigSchema.get_all() self.assertEqual(len(pack_dbs), 0) self.assertEqual(len(config_schema_dbs), 0) registrar = ResourceRegistrar(use_pack_cache=False) registrar._pack_loader.get_packs = mock.Mock() registrar._pack_loader.get_packs.return_value = { 'dummy_pack_1': PACK_PATH_1 } packs_base_paths = content_utils.get_packs_base_paths() registrar.register_packs(base_dirs=packs_base_paths) # Verify pack and schema have been registered pack_dbs = Pack.get_all() config_schema_dbs = ConfigSchema.get_all() self.assertEqual(len(pack_dbs), 1) self.assertEqual(len(config_schema_dbs), 1) pack_db = pack_dbs[0] config_schema_db = config_schema_dbs[0] self.assertEqual(pack_db.name, 'dummy_pack_1') self.assertEqual(len(pack_db.contributors), 2) self.assertEqual(pack_db.contributors[0], 'John Doe1 <*****@*****.**>') self.assertEqual(pack_db.contributors[1], 'John Doe2 <*****@*****.**>') self.assertTrue('api_key' in config_schema_db.attributes) self.assertTrue('api_secret' in config_schema_db.attributes) # Verify pack_db.files is correct and doesn't contain excluded files (*.pyc, .git/*, etc.) # Note: We can't test that .git/* files are excluded since git doesn't allow you to add # .git directory to existing repo index :/ excluded_files = [ '__init__.pyc', 'actions/dummy1.pyc', 'actions/dummy2.pyc', ] for excluded_file in excluded_files: self.assertTrue(excluded_file not in pack_db.files)
def test_register_packs(self): # Verify DB is empty pack_dbs = Pack.get_all() config_schema_dbs = ConfigSchema.get_all() self.assertEqual(len(pack_dbs), 0) self.assertEqual(len(config_schema_dbs), 0) registrar = ResourceRegistrar(use_pack_cache=False) registrar._pack_loader.get_packs = mock.Mock() registrar._pack_loader.get_packs.return_value = {'dummy_pack_1': PACK_PATH_1} packs_base_paths = content_utils.get_packs_base_paths() registrar.register_packs(base_dirs=packs_base_paths) # Verify pack and schema have been registered pack_dbs = Pack.get_all() config_schema_dbs = ConfigSchema.get_all() self.assertEqual(len(pack_dbs), 1) self.assertEqual(len(config_schema_dbs), 1) pack_db = pack_dbs[0] config_schema_db = config_schema_dbs[0] self.assertEqual(pack_db.name, 'dummy_pack_1') self.assertEqual(len(pack_db.contributors), 2) self.assertEqual(pack_db.contributors[0], 'John Doe1 <*****@*****.**>') self.assertEqual(pack_db.contributors[1], 'John Doe2 <*****@*****.**>') self.assertTrue('api_key' in config_schema_db.attributes) self.assertTrue('api_secret' in config_schema_db.attributes) # Verify pack_db.files is correct and doesn't contain excluded files (*.pyc, .git/*, etc.) # Note: We can't test that .git/* files are excluded since git doesn't allow you to add # .git directory to existing repo index :/ excluded_files = [ '__init__.pyc', 'actions/dummy1.pyc', 'actions/dummy2.pyc', ] for excluded_file in excluded_files: self.assertTrue(excluded_file not in pack_db.files)
def test_register_all_configs_invalid_config_no_config_schema(self): # verify_ configs is on, but ConfigSchema for the pack doesn't exist so # validation should proceed normally # Verify DB is empty pack_dbs = Pack.get_all() config_dbs = Config.get_all() self.assertEqual(len(pack_dbs), 0) self.assertEqual(len(config_dbs), 0) registrar = ConfigsRegistrar(use_pack_cache=False, validate_configs=False) registrar._pack_loader.get_packs = mock.Mock() registrar._pack_loader.get_packs.return_value = {'dummy_pack_6': PACK_6_PATH} packs_base_paths = content_utils.get_packs_base_paths() registrar.register_from_packs(base_dirs=packs_base_paths) # Verify pack and schema have been registered pack_dbs = Pack.get_all() config_dbs = Config.get_all() self.assertEqual(len(pack_dbs), 1) self.assertEqual(len(config_dbs), 1)
def test_register_pack_old_style_non_semver_version_is_normalized_to_valid_version(self): # Verify DB is empty pack_dbs = Pack.get_all() self.assertEqual(len(pack_dbs), 0) registrar = ResourceRegistrar(use_pack_cache=False) registrar._pack_loader.get_packs = mock.Mock() registrar._pack_loader.get_packs.return_value = {'dummy_pack_11': PACK_PATH_11} packs_base_paths = content_utils.get_packs_base_paths() registrar.register_packs(base_dirs=packs_base_paths) # Non-semver valid version 0.2 should be normalize to 0.2.0 pack_db = Pack.get_by_name('dummy_pack_11') self.assertEqual(pack_db.version, '0.2.0')
def setUp(self): super(UnloadActionTestCase, self).setUp() # Register mock pack # Verify DB is empty pack_dbs = Pack.get_all() config_schema_dbs = ConfigSchema.get_all() config_dbs = Config.get_all() self.assertEqual(len(pack_dbs), 0) self.assertEqual(len(config_schema_dbs), 0) self.assertEqual(len(config_dbs), 0) # Register the pack with all the content # TODO: Don't use pack cache cfg.CONF.set_override(name='all', override=True, group='register') cfg.CONF.set_override(name='pack', override=PACK_PATH_1, group='register') cfg.CONF.set_override(name='no_fail_on_failure', override=True, group='register') register_content()
def test_register_all_configs_with_config_schema_validation_validation_failure_2( self, ): # Verify DB is empty pack_dbs = Pack.get_all() config_dbs = Config.get_all() self.assertEqual(len(pack_dbs), 0) self.assertEqual(len(config_dbs), 0) registrar = ConfigsRegistrar(use_pack_cache=False, fail_on_failure=True, validate_configs=True) registrar._pack_loader.get_packs = mock.Mock() registrar._pack_loader.get_packs.return_value = { "dummy_pack_19": PACK_19_PATH } # Register ConfigSchema for pack registrar._register_pack_db = mock.Mock() registrar._register_pack(pack_name="dummy_pack_19", pack_dir=PACK_19_PATH) packs_base_paths = content_utils.get_packs_base_paths() if six.PY3: expected_msg = ( 'Failed validating attribute "instances.0.alias" in config for pack ' "\"dummy_pack_19\" (.*?): {'not': 'string'} is not of type " "'string'") else: expected_msg = ( 'Failed validating attribute "instances.0.alias" in config for pack ' "\"dummy_pack_19\" (.*?): {'not': 'string'} is not of type " "u'string'") self.assertRaisesRegexp( ValueError, expected_msg, registrar.register_from_packs, base_dirs=packs_base_paths, )
def test_register_pack_pack_ref(self): # Verify DB is empty pack_dbs = Pack.get_all() self.assertEqual(len(pack_dbs), 0) registrar = ResourceRegistrar(use_pack_cache=False) registrar._pack_loader.get_packs = mock.Mock() registrar._pack_loader.get_packs.return_value = { "dummy_pack_1": PACK_PATH_1, "dummy_pack_6": PACK_PATH_6, } packs_base_paths = content_utils.get_packs_base_paths() registrar.register_packs(base_dirs=packs_base_paths) # Ref is provided pack_db = Pack.get_by_name("dummy_pack_6") self.assertEqual(pack_db.ref, "dummy_pack_6_ref") self.assertEqual(len(pack_db.contributors), 0) # Ref is not provided, directory name should be used pack_db = Pack.get_by_name("dummy_pack_1") self.assertEqual(pack_db.ref, "dummy_pack_1") # "ref" is not provided, but "name" is registrar._register_pack_db(pack_name=None, pack_dir=PACK_PATH_7) pack_db = Pack.get_by_name("dummy_pack_7_name") self.assertEqual(pack_db.ref, "dummy_pack_7_name") # "ref" is not provided and "name" contains invalid characters expected_msg = "contains invalid characters" self.assertRaisesRegexp( ValueError, expected_msg, registrar._register_pack_db, pack_name=None, pack_dir=PACK_PATH_8, )
def test_register_all_configs_with_config_schema_validation_validation_failure_4( self, ): # This test checks for default values containing "decrypt_kv" jinja filter for # keys which have "secret: True" set. # Verify DB is empty pack_dbs = Pack.get_all() config_dbs = Config.get_all() self.assertEqual(len(pack_dbs), 0) self.assertEqual(len(config_dbs), 0) registrar = ConfigsRegistrar(use_pack_cache=False, fail_on_failure=True, validate_configs=True) registrar._pack_loader.get_packs = mock.Mock() registrar._pack_loader.get_packs.return_value = { "dummy_pack_22": PACK_22_PATH } # Register ConfigSchema for pack registrar._register_pack_db = mock.Mock() registrar._register_pack(pack_name="dummy_pack_22", pack_dir=PACK_22_PATH) packs_base_paths = content_utils.get_packs_base_paths() expected_msg = ( 'Values specified as "secret: True" in config schema are automatically ' 'decrypted by default. Use of "decrypt_kv" jinja filter is not allowed ' "for such values. Please check the specified values in the config or " "the default values in the schema.") self.assertRaisesRegexp( ValueError, expected_msg, registrar.register_from_packs, base_dirs=packs_base_paths, )