def test_register_from_pack_action_metadata_fails_validation(self): # No fail on failure flag, should succeed pack_dir = os.path.join(get_fixtures_packs_base_path(), 'dummy_pack_4') runner_dirs = os.path.join(get_fixtures_packs_base_path(), 'runners') opts = [ '--register-pack=%s' % (pack_dir), '--register-no-fail-on-failure', '--register-runner-dir=%s' % (runner_dirs), ] cmd = BASE_REGISTER_ACTIONS_CMD_ARGS + opts exit_code, _, stderr = run_command(cmd=cmd) self.assertTrue('Registered 0 actions.' in stderr) self.assertEqual(exit_code, 0) # Fail on failure, should fail pack_dir = os.path.join(get_fixtures_packs_base_path(), 'dummy_pack_4') opts = [ '--register-pack=%s' % (pack_dir), '--register-fail-on-failure', '--register-runner-dir=%s' % (runner_dirs), ] cmd = BASE_REGISTER_ACTIONS_CMD_ARGS + opts exit_code, _, stderr = run_command(cmd=cmd) self.assertTrue('object has no attribute \'get\'' in stderr) self.assertEqual(exit_code, 1)
def test_register_from_pack_action_metadata_fails_validation(self): # No fail on failure flag, should succeed pack_dir = os.path.join(get_fixtures_packs_base_path(), "dummy_pack_4") runner_dirs = os.path.join(get_fixtures_packs_base_path(), "runners") opts = [ "--register-pack=%s" % (pack_dir), "--register-no-fail-on-failure", "--register-runner-dir=%s" % (runner_dirs), ] cmd = BASE_REGISTER_ACTIONS_CMD_ARGS + opts exit_code, _, stderr = run_command(cmd=cmd) self.assertIn("Registered 0 actions.", stderr) self.assertEqual(exit_code, 0) # Fail on failure, should fail pack_dir = os.path.join(get_fixtures_packs_base_path(), "dummy_pack_4") opts = [ "--register-pack=%s" % (pack_dir), "--register-fail-on-failure", "--register-runner-dir=%s" % (runner_dirs), ] cmd = BASE_REGISTER_ACTIONS_CMD_ARGS + opts exit_code, _, stderr = run_command(cmd=cmd) self.assertIn("object has no attribute 'get'", stderr) self.assertEqual(exit_code, 1)
def test_get_pack_resource_file_abs_path(self): # Mock the packs path to point to the fixtures directory cfg.CONF.content.packs_base_paths = get_fixtures_packs_base_path() # Invalid resource type expected_msg = 'Invalid resource type: fooo' self.assertRaisesRegexp(ValueError, expected_msg, get_pack_resource_file_abs_path, pack_ref='dummy_pack_1', resource_type='fooo', file_path='test.py') # Invalid paths (directory traversal and absolute paths) file_paths = ['/tmp/foo.py', '../foo.py', '/etc/passwd', '../../foo.py'] for file_path in file_paths: expected_msg = 'Invalid file path: .*%s' % (file_path) self.assertRaisesRegexp(ValueError, expected_msg, get_pack_resource_file_abs_path, pack_ref='dummy_pack_1', resource_type='action', file_path=file_path) # Valid paths file_paths = ['foo.py', 'a/foo.py', 'a/b/foo.py'] for file_path in file_paths: expected = os.path.join(get_fixtures_packs_base_path(), 'dummy_pack_1/actions', file_path) result = get_pack_resource_file_abs_path(pack_ref='dummy_pack_1', resource_type='action', file_path=file_path) self.assertEqual(result, expected)
def test_register_recreate_virtualenvs(self): # 1. Register the pack and ensure it exists and doesn't rely on state from previous # test methods pack_dir = os.path.join(get_fixtures_packs_base_path(), "dummy_pack_1") cmd = BASE_CMD_ARGS + [ "--register-pack=%s" % (pack_dir), "--register-setup-virtualenvs", "--register-no-fail-on-failure", ] exit_code, stdout, stderr = run_command(cmd=cmd) self.assertIn('Setting up virtualenv for pack "dummy_pack_1"', stderr) self.assertIn("Setup virtualenv for 1 pack(s)", stderr) self.assertEqual(exit_code, 0) # 2. Run it again with --register-recreate-virtualenvs flag pack_dir = os.path.join(get_fixtures_packs_base_path(), "dummy_pack_1") cmd = BASE_CMD_ARGS + [ "--register-pack=%s" % (pack_dir), "--register-recreate-virtualenvs", "--register-no-fail-on-failure", ] exit_code, stdout, stderr = run_command(cmd=cmd) self.assertIn('Setting up virtualenv for pack "dummy_pack_1"', stderr) self.assertIn("Virtualenv successfully removed.", stderr) self.assertIn("Setup virtualenv for 1 pack(s)", stderr) self.assertEqual(exit_code, 0)
def test_get_pack_resource_file_abs_path(self): # Mock the packs path to point to the fixtures directory cfg.CONF.content.packs_base_paths = get_fixtures_packs_base_path() # Invalid resource type expected_msg = 'Invalid resource type: fooo' self.assertRaisesRegexp(ValueError, expected_msg, get_pack_resource_file_abs_path, pack_ref='dummy_pack_1', resource_type='fooo', file_path='test.py') # Invalid paths (directory traversal and absolute paths) file_paths = [ '/tmp/foo.py', '../foo.py', '/etc/passwd', '../../foo.py' ] for file_path in file_paths: expected_msg = 'Invalid file path: .*%s' % (file_path) self.assertRaisesRegexp(ValueError, expected_msg, get_pack_resource_file_abs_path, pack_ref='dummy_pack_1', resource_type='action', file_path=file_path) # Valid paths file_paths = ['foo.py', 'a/foo.py', 'a/b/foo.py'] for file_path in file_paths: expected = os.path.join(get_fixtures_packs_base_path(), 'dummy_pack_1/actions', file_path) result = get_pack_resource_file_abs_path(pack_ref='dummy_pack_1', resource_type='action', file_path=file_path) self.assertEqual(result, expected)
def test_register_from_pack_success(self): pack_dir = os.path.join(get_fixtures_packs_base_path(), "dummy_pack_1") runner_dirs = os.path.join(get_fixtures_packs_base_path(), "runners") opts = [ "--register-pack=%s" % (pack_dir), "--register-runner-dir=%s" % (runner_dirs), ] cmd = BASE_REGISTER_ACTIONS_CMD_ARGS + opts exit_code, _, stderr = run_command(cmd=cmd) self.assertIn("Registered 1 actions.", stderr) self.assertEqual(exit_code, 0)
def test_register_from_pack_success(self): pack_dir = os.path.join(get_fixtures_packs_base_path(), 'dummy_pack_1') runner_dirs = os.path.join(get_fixtures_packs_base_path(), 'runners') opts = [ '--register-pack=%s' % (pack_dir), '--register-runner-dir=%s' % (runner_dirs), ] cmd = BASE_REGISTER_ACTIONS_CMD_ARGS + opts exit_code, _, stderr = run_command(cmd=cmd) self.assertTrue('Registered 1 actions.' in stderr) self.assertEqual(exit_code, 0)
def test_get_pack_resource_file_abs_path(self): # Mock the packs path to point to the fixtures directory cfg.CONF.content.packs_base_paths = get_fixtures_packs_base_path() # Invalid resource type expected_msg = 'Invalid resource type: fooo' self.assertRaisesRegexp(ValueError, expected_msg, get_pack_resource_file_abs_path, pack_ref='dummy_pack_1', resource_type='fooo', file_path='test.py') # Invalid paths (directory traversal and absolute paths) file_paths = ['/tmp/foo.py', '../foo.py', '/etc/passwd', '../../foo.py', '/opt/stackstorm/packs/invalid_pack/actions/my_action.py', '../../foo.py'] for file_path in file_paths: # action resource_type expected_msg = (r'Invalid file path: ".*%s"\. File path needs to be relative to the ' r'pack actions directory (.*). For example "my_action.py"\.' % (file_path)) self.assertRaisesRegexp(ValueError, expected_msg, get_pack_resource_file_abs_path, pack_ref='dummy_pack_1', resource_type='action', file_path=file_path) # sensor resource_type expected_msg = (r'Invalid file path: ".*%s"\. File path needs to be relative to the ' r'pack sensors directory (.*). For example "my_sensor.py"\.' % (file_path)) self.assertRaisesRegexp(ValueError, expected_msg, get_pack_resource_file_abs_path, pack_ref='dummy_pack_1', resource_type='sensor', file_path=file_path) # no resource type expected_msg = (r'Invalid file path: ".*%s"\. File path needs to be relative to the ' r'pack directory (.*). For example "my_action.py"\.' % (file_path)) self.assertRaisesRegexp(ValueError, expected_msg, get_pack_file_abs_path, pack_ref='dummy_pack_1', file_path=file_path) # Valid paths file_paths = ['foo.py', 'a/foo.py', 'a/b/foo.py'] for file_path in file_paths: expected = os.path.join(get_fixtures_packs_base_path(), 'dummy_pack_1/actions', file_path) result = get_pack_resource_file_abs_path(pack_ref='dummy_pack_1', resource_type='action', file_path=file_path) self.assertEqual(result, expected)
def _override_common_opts(): packs_base_path = get_fixtures_packs_base_path() CONF.set_override(name='base_path', override=packs_base_path, group='system') CONF.set_override(name='validate_output_schema', override=True, group='system') CONF.set_override(name='system_packs_base_path', override=packs_base_path, group='content') CONF.set_override(name='packs_base_paths', override=packs_base_path, group='content') CONF.set_override(name='api_url', override='http://127.0.0.1', group='auth') CONF.set_override(name='mask_secrets', override=True, group='log') CONF.set_override(name='jitter_interval', override=0, group='mistral') CONF.set_override(name='query_interval', override=0.1, group='resultstracker') CONF.set_override(name='stream_output', override=False, group='actionrunner')
def test_register_triggers_from_pack(self): base_path = get_fixtures_packs_base_path() pack_dir = os.path.join(base_path, 'dummy_pack_1') trigger_type_dbs = TriggerType.get_all() self.assertEqual(len(trigger_type_dbs), 0) count = triggers_registrar.register_triggers(pack_dir=pack_dir) self.assertEqual(count, 2) # Verify TriggerTypeDB and corresponding TriggerDB objects have been created trigger_type_dbs = TriggerType.get_all() trigger_dbs = Trigger.get_all() self.assertEqual(len(trigger_type_dbs), 2) self.assertEqual(len(trigger_dbs), 2) self.assertEqual(trigger_type_dbs[0].name, 'event_handler') self.assertEqual(trigger_type_dbs[0].pack, 'dummy_pack_1') self.assertEqual(trigger_dbs[0].name, 'event_handler') self.assertEqual(trigger_dbs[0].pack, 'dummy_pack_1') self.assertEqual(trigger_dbs[0].type, 'dummy_pack_1.event_handler') self.assertEqual(trigger_type_dbs[1].name, 'head_sha_monitor') self.assertEqual(trigger_type_dbs[1].pack, 'dummy_pack_1') self.assertEqual(trigger_type_dbs[1].payload_schema['type'], 'object')
def _override_common_opts(): packs_base_path = get_fixtures_packs_base_path() runners_base_path = get_fixtures_runners_base_path() CONF.set_override(name='base_path', override=packs_base_path, group='system') CONF.set_override(name='system_packs_base_path', override=packs_base_path, group='content') CONF.set_override(name='packs_base_paths', override=packs_base_path, group='content') CONF.set_override(name='system_runners_base_path', override=runners_base_path, group='content') CONF.set_override(name='runners_base_paths', override=runners_base_path, group='content') CONF.set_override(name='api_url', override='http://127.0.0.1', group='auth') CONF.set_override(name='mask_secrets', override=True, group='log') CONF.set_override(name='url', override='zake://', group='coordination') CONF.set_override(name='lock_timeout', override=1, group='coordination') CONF.set_override(name='jitter_interval', override=0, group='mistral') CONF.set_override(name='query_interval', override=0.1, group='resultstracker')
def test_register_all_policies(self): policies_dbs = Policy.get_all() self.assertEqual(len(policies_dbs), 0) packs_base_path = get_fixtures_packs_base_path() count = policies_registrar.register_policies( packs_base_paths=[packs_base_path]) # Verify PolicyDB objects have been created policies_dbs = Policy.get_all() policies = { policies_db.name: { 'pack': policies_db.pack, 'type': policies_db.policy_type, 'parameters': policies_db.parameters } for policies_db in policies_dbs } expected_policies = { 'test_policy_1': { 'pack': 'dummy_pack_1', 'type': 'action.concurrency', 'parameters': { 'action': 'delay', 'threshold': 3 } }, 'test_policy_3': { 'pack': 'dummy_pack_1', 'type': 'action.retry', 'parameters': { 'retry_on': 'timeout', 'max_retry_count': 5 } }, 'cancel_on_concurrency': { 'pack': 'mistral_tests', 'type': 'action.concurrency', 'parameters': { 'action': 'cancel', 'threshold': 3 } }, 'cancel_on_concurrency_by_attr': { 'pack': 'mistral_tests', 'type': 'action.concurrency.attr', 'parameters': { 'action': 'cancel', 'threshold': 1, 'attributes': ['friend'] } } } self.assertEqual(len(expected_policies), count) self.assertEqual(len(expected_policies), len(policies_dbs)) self.assertDictEqual(expected_policies, policies)
def test_register_all_policies(self): policies_dbs = Policy.get_all() self.assertEqual(len(policies_dbs), 0) packs_base_path = get_fixtures_packs_base_path() count = policies_registrar.register_policies(packs_base_paths=[packs_base_path]) # Verify PolicyDB objects have been created policies_dbs = Policy.get_all() policies = { policies_db.name: { 'pack': policies_db.pack, 'type': policies_db.policy_type, 'parameters': policies_db.parameters } for policies_db in policies_dbs } expected_policies = { 'test_policy_1': { 'pack': 'dummy_pack_1', 'type': 'action.concurrency', 'parameters': { 'action': 'delay', 'threshold': 3 } }, 'test_policy_3': { 'pack': 'dummy_pack_1', 'type': 'action.retry', 'parameters': { 'retry_on': 'timeout', 'max_retry_count': 5 } }, 'cancel_on_concurrency': { 'pack': 'mistral_tests', 'type': 'action.concurrency', 'parameters': { 'action': 'cancel', 'threshold': 3 } }, 'cancel_on_concurrency_by_attr': { 'pack': 'mistral_tests', 'type': 'action.concurrency.attr', 'parameters': { 'action': 'cancel', 'threshold': 1, 'attributes': ['friend'] } } } self.assertEqual(len(expected_policies), count) self.assertEqual(len(expected_policies), len(policies_dbs)) self.assertDictEqual(expected_policies, policies)
def _override_common_opts(): packs_base_path = get_fixtures_packs_base_path() CONF.set_override(name='base_path', override=packs_base_path, group='system') CONF.set_override(name='system_packs_base_path', override=packs_base_path, group='content') CONF.set_override(name='packs_base_paths', override=packs_base_path, group='content') CONF.set_override(name='api_url', override='http://127.0.0.1', group='auth') CONF.set_override(name='mask_secrets', override=True, group='log') CONF.set_override(name='url', override='zake://', group='coordination') CONF.set_override(name='lock_timeout', override=1, group='coordination')
def test_register_policy_invalid_policy_type_references(self): # Policy references an invalid (inexistent) policy type registrar = PolicyRegistrar() policy_path = os.path.join(get_fixtures_packs_base_path(), 'dummy_pack_1/policies/policy_2.yaml') expected_msg = 'Referenced policy_type "action.mock_policy_error" doesnt exist' self.assertRaisesRegexp(ValueError, expected_msg, registrar._register_policy, pack='dummy_pack_1', policy=policy_path)
def test_make_sure_policy_parameters_are_validated_during_register(self): # Policy where specified parameters fail schema validation registrar = PolicyRegistrar() policy_path = os.path.join(get_fixtures_packs_base_path(), 'dummy_pack_2/policies/policy_3.yaml') expected_msg = '100 is greater than the maximum of 5' self.assertRaisesRegexp(jsonschema.ValidationError, expected_msg, registrar._register_policy, pack='dummy_pack_2', policy=policy_path)
def test_register_setup_virtualenvs(self): # Single pack pack_dir = os.path.join(get_fixtures_packs_base_path(), 'dummy_pack_1') cmd = BASE_CMD_ARGS + ['--register-pack=%s' % (pack_dir), '--register-setup-virtualenvs', '--register-no-fail-on-failure'] exit_code, stdout, stderr = run_command(cmd=cmd) self.assertTrue('Setting up virtualenv for pack "dummy_pack_1"' in stderr) self.assertTrue('Setup virtualenv for 1 pack(s)' in stderr) self.assertEqual(exit_code, 0)
def test_register_setup_virtualenvs(self): # Single pack pack_dir = os.path.join(get_fixtures_packs_base_path(), 'dummy_pack_1') cmd = BASE_CMD_ARGS + ['--register-pack=%s' % (pack_dir), '--register-setup-virtualenvs', '--register-no-fail-on-failure'] exit_code, stdout, stderr = run_command(cmd=cmd) self.assertIn('Setting up virtualenv for pack "dummy_pack_1"', stderr) self.assertIn('Setup virtualenv for 1 pack(s)', stderr) self.assertEqual(exit_code, 0)
def test_register_policies(self): # Note: Only one policy should be registered since second one fails validation pack_dir = os.path.join(fixturesloader.get_fixtures_packs_base_path(), 'dummy_pack_1') self.assertEqual(register_policies(pack_dir=pack_dir), 1) p1 = Policy.get_by_ref('dummy_pack_1.test_policy_1') self.assertEqual(p1.name, 'test_policy_1') self.assertEqual(p1.pack, 'dummy_pack_1') self.assertEqual(p1.resource_ref, 'dummy_pack_1.local') self.assertEqual(p1.policy_type, 'action.concurrency') p2 = Policy.get_by_ref('dummy_pack_1.test_policy_2') self.assertEqual(p2, None)
def test_register_all_triggers(self): trigger_type_dbs = TriggerType.get_all() self.assertEqual(len(trigger_type_dbs), 0) packs_base_path = get_fixtures_packs_base_path() count = triggers_registrar.register_triggers(packs_base_paths=[packs_base_path]) self.assertEqual(count, 2) # Verify TriggerTypeDB and corresponding TriggerDB objects have been created trigger_type_dbs = TriggerType.get_all() trigger_dbs = Trigger.get_all() self.assertEqual(len(trigger_type_dbs), 2) self.assertEqual(len(trigger_dbs), 2)
def _override_common_opts(): packs_base_path = get_fixtures_packs_base_path() CONF.set_override(name="base_path", override=packs_base_path, group="system") CONF.set_override(name="validate_output_schema", override=True, group="system") CONF.set_override( name="system_packs_base_path", override=packs_base_path, group="content" ) CONF.set_override( name="packs_base_paths", override=packs_base_path, group="content" ) CONF.set_override(name="api_url", override="http://127.0.0.1", group="auth") CONF.set_override(name="mask_secrets", override=True, group="log") CONF.set_override(name="stream_output", override=False, group="actionrunner")
def test_register_policies_from_pack(self): pack_dir = os.path.join(get_fixtures_packs_base_path(), 'dummy_pack_1') self.assertEqual(register_policies(pack_dir=pack_dir), 2) p1 = Policy.get_by_ref('dummy_pack_1.test_policy_1') self.assertEqual(p1.name, 'test_policy_1') self.assertEqual(p1.pack, 'dummy_pack_1') self.assertEqual(p1.resource_ref, 'dummy_pack_1.local') self.assertEqual(p1.policy_type, 'action.concurrency') # Verify that a default value for parameter "action" which isn't provided in the file is set self.assertEqual(p1.parameters['action'], 'delay') p2 = Policy.get_by_ref('dummy_pack_1.test_policy_2') self.assertEqual(p2, None)
def test_register_all_policies(self): policies_dbs = Policy.get_all() self.assertEqual(len(policies_dbs), 0) packs_base_path = get_fixtures_packs_base_path() count = policies_registrar.register_policies( packs_base_paths=[packs_base_path]) # Verify PolicyDB objects have been created policies_dbs = Policy.get_all() policies = { policies_db.name: { "pack": policies_db.pack, "type": policies_db.policy_type, "parameters": policies_db.parameters, } for policies_db in policies_dbs } expected_policies = { "test_policy_1": { "pack": "dummy_pack_1", "type": "action.concurrency", "parameters": { "action": "delay", "threshold": 3 }, }, "test_policy_3": { "pack": "dummy_pack_1", "type": "action.retry", "parameters": { "retry_on": "timeout", "max_retry_count": 5 }, }, "sequential.retry_on_failure": { "pack": "orquesta_tests", "type": "action.retry", "parameters": { "retry_on": "failure", "max_retry_count": 1 }, }, } self.assertEqual(len(expected_policies), count) self.assertEqual(len(expected_policies), len(policies_dbs)) self.assertDictEqual(expected_policies, policies)
def test_register_all_and_register_setup_virtualenvs(self): # Verify that --register-all works in combinations with --register-setup-virtualenvs # Single pack pack_dir = os.path.join(get_fixtures_packs_base_path(), 'dummy_pack_1') cmd = BASE_CMD_ARGS + [ '--register-pack=%s' % (pack_dir), '--register-all', '--register-setup-virtualenvs', '--register-no-fail-on-failure' ] exit_code, stdout, stderr = run_command(cmd=cmd) self.assertIn('Registering actions', stderr, 'Actual stderr: %s' % (stderr)) self.assertIn('Registering rules', stderr) self.assertIn('Setup virtualenv for %s pack(s)' % ('1'), stderr) self.assertEqual(exit_code, 0)
def test_register_recreate_virtualenvs(self): # Single pack pack_dir = os.path.join(get_fixtures_packs_base_path(), "dummy_pack_1") cmd = BASE_CMD_ARGS + [ "--register-pack=%s" % (pack_dir), "--register-recreate-virtualenvs", "--register-no-fail-on-failure", ] exit_code, stdout, stderr = run_command(cmd=cmd) self.assertIn('Setting up virtualenv for pack "dummy_pack_1"', stderr) self.assertIn("Setup virtualenv for 1 pack(s)", stderr) self.assertIn("Virtualenv successfully removed.", stderr) self.assertEqual(exit_code, 0)
def test_get_pack_resource_file_abs_path(self): # Mock the packs path to point to the fixtures directory cfg.CONF.content.packs_base_paths = get_fixtures_packs_base_path() # Invalid resource type expected_msg = "Invalid resource type: fooo" self.assertRaisesRegexp( ValueError, expected_msg, get_pack_resource_file_abs_path, pack_name="dummy_pack_1", resource_type="fooo", file_path="test.py", ) # Invalid paths (directory traversal and absolute paths) file_paths = ["/tmp/foo.py", "../foo.py", "/etc/passwd", "../../foo.py"] for file_path in file_paths: expected_msg = "Invalid file path: .*%s" % (file_path) self.assertRaisesRegexp( ValueError, expected_msg, get_pack_resource_file_abs_path, pack_name="dummy_pack_1", resource_type="action", file_path=file_path, ) # Valid paths file_paths = ["foo.py", "a/foo.py", "a/b/foo.py"] for file_path in file_paths: expected = os.path.join(get_fixtures_packs_base_path(), "dummy_pack_1/actions", file_path) result = get_pack_resource_file_abs_path( pack_name="dummy_pack_1", resource_type="action", file_path=file_path ) self.assertEqual(result, expected)
def test_register_all_and_register_setup_virtualenvs(self): # Verify that --register-all works in combinations with --register-setup-virtualenvs # Single pack pack_dir = os.path.join(get_fixtures_packs_base_path(), 'dummy_pack_1') cmd = BASE_CMD_ARGS + [ '--register-pack=%s' % (pack_dir), '--register-all', '--register-setup-virtualenvs', '--register-no-fail-on-failure' ] exit_code, stdout, stderr = run_command(cmd=cmd) self.assertTrue('Registering actions' in stderr, 'Actual stderr: %s' % (stderr)) self.assertTrue('Registering rules' in stderr) self.assertTrue('Setup virtualenv for %s pack(s)' % ('1') in stderr) self.assertEqual(exit_code, 0)
def test_register_policies_from_pack(self): pack_dir = os.path.join(get_fixtures_packs_base_path(), "dummy_pack_1") self.assertEqual(register_policies(pack_dir=pack_dir), 2) p1 = Policy.get_by_ref("dummy_pack_1.test_policy_1") self.assertEqual(p1.name, "test_policy_1") self.assertEqual(p1.pack, "dummy_pack_1") self.assertEqual(p1.resource_ref, "dummy_pack_1.local") self.assertEqual(p1.policy_type, "action.concurrency") # Verify that a default value for parameter "action" which isn't provided in the file is set self.assertEqual(p1.parameters["action"], "delay") self.assertEqual(p1.metadata_file, "policies/policy_1.yaml") p2 = Policy.get_by_ref("dummy_pack_1.test_policy_2") self.assertEqual(p2, None)
def test_register_all_and_register_setup_virtualenvs(self): # Verify that --register-all works in combinations with --register-setup-virtualenvs # Single pack pack_dir = os.path.join(get_fixtures_packs_base_path(), "dummy_pack_1") cmd = BASE_CMD_ARGS + [ "--register-pack=%s" % (pack_dir), "--register-all", "--register-setup-virtualenvs", "--register-no-fail-on-failure", ] exit_code, stdout, stderr = run_command(cmd=cmd) self.assertIn("Registering actions", stderr, "Actual stderr: %s" % (stderr)) self.assertIn("Registering rules", stderr) self.assertIn("Setup virtualenv for %s pack(s)" % ("1"), stderr) self.assertEqual(exit_code, 0)
def test_register_all_policies(self): policies_dbs = Policy.get_all() self.assertEqual(len(policies_dbs), 0) packs_base_path = get_fixtures_packs_base_path() count = policies_registrar.register_policies(packs_base_paths=[packs_base_path]) self.assertEqual(count, 2) # Verify PolicyDB objects have been created policies_dbs = Policy.get_all() self.assertEqual(len(policies_dbs), 2) self.assertEqual(policies_dbs[0].name, 'test_policy_1') self.assertEqual(policies_dbs[0].policy_type, 'action.concurrency') # Verify that a default value for parameter "action" which isn't provided in the file is set self.assertEqual(policies_dbs[0].parameters['action'], 'delay') self.assertEqual(policies_dbs[0].parameters['threshold'], 3)
def test_register_all_policies(self): policies_dbs = Policy.get_all() self.assertEqual(len(policies_dbs), 0) packs_base_path = get_fixtures_packs_base_path() count = policies_registrar.register_policies( packs_base_paths=[packs_base_path]) self.assertEqual(count, 2) # Verify PolicyDB objects have been created policies_dbs = Policy.get_all() self.assertEqual(len(policies_dbs), 2) self.assertEqual(policies_dbs[0].name, 'test_policy_1') self.assertEqual(policies_dbs[0].policy_type, 'action.concurrency') # Verify that a default value for parameter "action" which isn't provided in the file is set self.assertEqual(policies_dbs[0].parameters['action'], 'delay') self.assertEqual(policies_dbs[0].parameters['threshold'], 3)
def test_get_relative_path_to_pack_file(self): packs_base_paths = get_fixtures_packs_base_path() pack_ref = 'dummy_pack_1' # 1. Valid paths file_path = os.path.join(packs_base_paths, 'dummy_pack_1/pack.yaml') result = get_relative_path_to_pack_file(pack_ref=pack_ref, file_path=file_path) self.assertEqual(result, 'pack.yaml') file_path = os.path.join(packs_base_paths, 'dummy_pack_1/actions/action.meta.yaml') result = get_relative_path_to_pack_file(pack_ref=pack_ref, file_path=file_path) self.assertEqual(result, 'actions/action.meta.yaml') file_path = os.path.join(packs_base_paths, 'dummy_pack_1/actions/lib/foo.py') result = get_relative_path_to_pack_file(pack_ref=pack_ref, file_path=file_path) self.assertEqual(result, 'actions/lib/foo.py') # Already relative file_path = 'actions/lib/foo2.py' result = get_relative_path_to_pack_file(pack_ref=pack_ref, file_path=file_path) self.assertEqual(result, 'actions/lib/foo2.py') # 2. Invalid path - outside pack directory expected_msg = r'file_path (.*?) is not located inside the pack directory (.*?)' file_path = os.path.join(packs_base_paths, 'dummy_pack_2/actions/lib/foo.py') self.assertRaisesRegexp(ValueError, expected_msg, get_relative_path_to_pack_file, pack_ref=pack_ref, file_path=file_path) file_path = '/tmp/foo/bar.py' self.assertRaisesRegexp(ValueError, expected_msg, get_relative_path_to_pack_file, pack_ref=pack_ref, file_path=file_path) file_path = os.path.join(packs_base_paths, '../dummy_pack_1/pack.yaml') self.assertRaisesRegexp(ValueError, expected_msg, get_relative_path_to_pack_file, pack_ref=pack_ref, file_path=file_path) file_path = os.path.join(packs_base_paths, '../../dummy_pack_1/pack.yaml') self.assertRaisesRegexp(ValueError, expected_msg, get_relative_path_to_pack_file, pack_ref=pack_ref, file_path=file_path)
def test_register_from_pack_fail_on_failure_pack_dir_doesnt_exist(self): # No fail on failure flag, should succeed pack_dir = "doesntexistblah" runner_dirs = os.path.join(get_fixtures_packs_base_path(), "runners") opts = [ "--register-pack=%s" % (pack_dir), "--register-runner-dir=%s" % (runner_dirs), "--register-no-fail-on-failure", ] cmd = BASE_REGISTER_ACTIONS_CMD_ARGS + opts exit_code, _, _ = run_command(cmd=cmd) self.assertEqual(exit_code, 0) # Fail on failure, should fail opts = [ "--register-pack=%s" % (pack_dir), "--register-runner-dir=%s" % (runner_dirs), "--register-fail-on-failure", ] cmd = BASE_REGISTER_ACTIONS_CMD_ARGS + opts exit_code, _, stderr = run_command(cmd=cmd) self.assertIn('Directory "doesntexistblah" doesn\'t exist', stderr) self.assertEqual(exit_code, 1)
def test_register_from_pack_fail_on_failure_pack_dir_doesnt_exist(self): # No fail on failure flag, should succeed pack_dir = 'doesntexistblah' runner_dirs = os.path.join(get_fixtures_packs_base_path(), 'runners') opts = [ '--register-pack=%s' % (pack_dir), '--register-runner-dir=%s' % (runner_dirs), '--register-no-fail-on-failure' ] cmd = BASE_REGISTER_ACTIONS_CMD_ARGS + opts exit_code, _, _ = run_command(cmd=cmd) self.assertEqual(exit_code, 0) # Fail on failure, should fail opts = [ '--register-pack=%s' % (pack_dir), '--register-runner-dir=%s' % (runner_dirs), '--register-fail-on-failure' ] cmd = BASE_REGISTER_ACTIONS_CMD_ARGS + opts exit_code, _, stderr = run_command(cmd=cmd) self.assertTrue('Directory "doesntexistblah" doesn\'t exist' in stderr) self.assertEqual(exit_code, 1)
from st2common.persistence.rule import Rule from st2common.persistence.policy import Policy from st2common.persistence.sensor import SensorType as Sensor from st2common.persistence.trigger import TriggerType from st2tests.base import BaseActionTestCase from st2tests.base import CleanDbTestCase from st2tests import fixturesloader from pack_mgmt.unload import UnregisterPackAction __all__ = [ 'UnloadActionTestCase' ] PACK_PATH_1 = os.path.join(fixturesloader.get_fixtures_packs_base_path(), 'dummy_pack_1') class UnloadActionTestCase(BaseActionTestCase, CleanDbTestCase): action_cls = UnregisterPackAction 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)
import os import mock from st2common.content import utils as content_utils from st2common.bootstrap.configsregistrar import ConfigsRegistrar from st2common.persistence.pack import Pack from st2common.persistence.pack import Config from st2tests import DbTestCase from st2tests import fixturesloader __all__ = ["ConfigsRegistrarTestCase"] PACK_PATH = os.path.join(fixturesloader.get_fixtures_packs_base_path(), "dummy_pack_1") class ConfigsRegistrarTestCase(DbTestCase): 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()
# limitations under the License. from __future__ import absolute_import import os from st2common.bootstrap import aliasesregistrar from st2common.persistence.action import ActionAlias from st2tests import DbTestCase from st2tests import fixturesloader __all__ = [ 'TestAliasRegistrar' ] ALIASES_FIXTURE_PACK_PATH = os.path.join(fixturesloader.get_fixtures_packs_base_path(), 'dummy_pack_1') ALIASES_FIXTURE_PATH = os.path.join(ALIASES_FIXTURE_PACK_PATH, 'aliases') class TestAliasRegistrar(DbTestCase): def test_alias_registration(self): count = aliasesregistrar.register_aliases(pack_dir=ALIASES_FIXTURE_PACK_PATH) # expect all files to contain be aliases self.assertEqual(count, len(os.listdir(ALIASES_FIXTURE_PATH))) action_alias_dbs = ActionAlias.get_all() self.assertEqual(action_alias_dbs[0].metadata_file, 'aliases/alias1.yaml')
from st2tests.base import IntegrationTestCase from st2common.util.shell import run_command from st2tests import config as test_config from st2tests.fixturesloader import get_fixtures_packs_base_path BASE_DIR = os.path.dirname(os.path.abspath(__file__)) SCRIPT_PATH = os.path.join(BASE_DIR, "../../bin/st2-register-content") SCRIPT_PATH = os.path.abspath(SCRIPT_PATH) BASE_CMD_ARGS = [ sys.executable, SCRIPT_PATH, "--config-file=conf/st2.tests.conf", "-v" ] BASE_REGISTER_ACTIONS_CMD_ARGS = BASE_CMD_ARGS + ["--register-actions"] PACKS_PATH = get_fixtures_packs_base_path() PACKS_COUNT = len(glob.glob("%s/*/pack.yaml" % (PACKS_PATH))) assert PACKS_COUNT >= 2 class ContentRegisterScriptTestCase(IntegrationTestCase): def setUp(self): super(ContentRegisterScriptTestCase, self).setUp() test_config.parse_args() def test_register_from_pack_success(self): pack_dir = os.path.join(get_fixtures_packs_base_path(), "dummy_pack_1") runner_dirs = os.path.join(get_fixtures_packs_base_path(), "runners") opts = [ "--register-pack=%s" % (pack_dir),
from jsonschema import ValidationError from st2common.content import utils as content_utils from st2common.bootstrap.base import ResourceRegistrar from st2common.persistence.pack import Pack from st2common.persistence.pack import ConfigSchema from st2tests.base import CleanDbTestCase from st2tests import fixturesloader __all__ = [ 'ResourceRegistrarTestCase' ] PACK_PATH_1 = os.path.join(fixturesloader.get_fixtures_packs_base_path(), 'dummy_pack_1') PACK_PATH_6 = os.path.join(fixturesloader.get_fixtures_packs_base_path(), 'dummy_pack_6') PACK_PATH_7 = os.path.join(fixturesloader.get_fixtures_packs_base_path(), 'dummy_pack_7') PACK_PATH_8 = os.path.join(fixturesloader.get_fixtures_packs_base_path(), 'dummy_pack_8') PACK_PATH_9 = os.path.join(fixturesloader.get_fixtures_packs_base_path(), 'dummy_pack_9') PACK_PATH_10 = os.path.join(fixturesloader.get_fixtures_packs_base_path(), 'dummy_pack_10') PACK_PATH_11 = os.path.join(fixturesloader.get_fixtures_packs_base_path(), 'dummy_pack_11') PACK_PATH_12 = os.path.join(fixturesloader.get_fixtures_packs_base_path(), 'dummy_pack_12') PACK_PATH_13 = os.path.join(fixturesloader.get_fixtures_packs_base_path(), 'dummy_pack_13') PACK_PATH_14 = os.path.join(fixturesloader.get_fixtures_packs_base_path(), 'dummy_pack_14') class ResourceRegistrarTestCase(CleanDbTestCase): def test_register_packs(self): # Verify DB is empty pack_dbs = Pack.get_all()
from st2common.constants import action as action_constants from st2common.models.db.execution import ActionExecutionDB from st2common.models.db.liveaction import LiveActionDB from st2common.runners import base as runners from st2common.services import action as action_service from st2common.transport.liveaction import LiveActionPublisher from st2common.transport.publishers import CUDPublisher from st2common.util import loader from st2tests import ExecutionDbTestCase from st2tests import fixturesloader from st2tests.mocks.liveaction import MockLiveActionPublisher from st2tests.mocks.liveaction import MockLiveActionPublisherNonBlocking TEST_PACK = 'mistral_tests' TEST_PACK_PATH = fixturesloader.get_fixtures_packs_base_path() + '/' + TEST_PACK PACKS = [ TEST_PACK_PATH, fixturesloader.get_fixtures_packs_base_path() + '/core' ] # Action executions requirements ACTION_PARAMS = {'friend': 'Rocky'} NON_EMPTY_RESULT = 'non-empty' # Non-workbook with a single workflow WF1_META_FILE_NAME = 'workflow_v2.yaml' WF1_META_FILE_PATH = TEST_PACK_PATH + '/actions/' + WF1_META_FILE_NAME WF1_META_CONTENT = loader.load_meta_file(WF1_META_FILE_PATH) WF1_NAME = WF1_META_CONTENT['pack'] + '.' + WF1_META_CONTENT['name']
class ActionsControllerTestCase( FunctionalTest, APIControllerWithIncludeAndExcludeFilterTestCase, CleanFilesTestCase ): get_all_path = "/v1/actions" controller_cls = ActionsController include_attribute_field_name = "entry_point" exclude_attribute_field_name = "parameters" register_packs = True to_delete_files = [ os.path.join(get_fixtures_packs_base_path(), "dummy_pack_1/actions/filea.txt") ] @mock.patch.object( action_validator, "validate_action", mock.MagicMock(return_value=True) ) def test_get_one_using_id(self): post_resp = self.__do_post(ACTION_1) action_id = self.__get_action_id(post_resp) get_resp = self.__do_get_one(action_id) self.assertEqual(get_resp.status_int, 200) self.assertEqual(self.__get_action_id(get_resp), action_id) self.__do_delete(action_id) @mock.patch.object( action_validator, "validate_action", mock.MagicMock(return_value=True) ) def test_get_one_using_ref(self): ref = ".".join([ACTION_1["pack"], ACTION_1["name"]]) action_id = self.__get_action_id(self.__do_post(ACTION_1)) get_resp = self.__do_get_one(ref) self.assertEqual(get_resp.status_int, 200) self.assertEqual(self.__get_action_id(get_resp), action_id) self.assertEqual(get_resp.json["ref"], ref) self.__do_delete(action_id) @mock.patch.object( action_validator, "validate_action", mock.MagicMock(return_value=True) ) def test_get_one_validate_params(self): post_resp = self.__do_post(ACTION_1) action_id = self.__get_action_id(post_resp) get_resp = self.__do_get_one(action_id) self.assertEqual(get_resp.status_int, 200) self.assertEqual(self.__get_action_id(get_resp), action_id) expected_args = ACTION_1["parameters"] self.assertEqual(get_resp.json["parameters"], expected_args) self.__do_delete(action_id) @mock.patch.object( action_validator, "validate_action", mock.MagicMock(return_value=True) ) def test_get_all_and_with_minus_one(self): action_1_ref = ".".join([ACTION_1["pack"], ACTION_1["name"]]) action_1_id = self.__get_action_id(self.__do_post(ACTION_1)) action_2_id = self.__get_action_id(self.__do_post(ACTION_2)) resp = self.app.get("/v1/actions") self.assertEqual(resp.status_int, 200) self.assertEqual(len(resp.json), 2, "/v1/actions did not return all actions.") item = [i for i in resp.json if i["id"] == action_1_id][0] self.assertEqual(item["ref"], action_1_ref) resp = self.app.get("/v1/actions?limit=-1") self.assertEqual(resp.status_int, 200) self.assertEqual(len(resp.json), 2, "/v1/actions did not return all actions.") item = [i for i in resp.json if i["id"] == action_1_id][0] self.assertEqual(item["ref"], action_1_ref) self.__do_delete(action_1_id) self.__do_delete(action_2_id) @mock.patch( "st2common.rbac.backends.noop.NoOpRBACUtils.user_is_admin", mock.Mock(return_value=False), ) def test_get_all_invalid_limit_too_large_none_admin(self): # limit > max_page_size, but user is not admin resp = self.app.get("/v1/actions?limit=1000", expect_errors=True) self.assertEqual(resp.status_int, http_client.FORBIDDEN) self.assertEqual( resp.json["faultstring"], 'Limit "1000" specified, maximum value is' ' "100"', ) def test_get_all_limit_negative_number(self): resp = self.app.get("/v1/actions?limit=-22", expect_errors=True) self.assertEqual(resp.status_int, 400) self.assertEqual( resp.json["faultstring"], 'Limit, "-22" specified, must be a positive number.', ) @mock.patch.object( action_validator, "validate_action", mock.MagicMock(return_value=True) ) def test_get_all_include_attributes_filter(self): return super( ActionsControllerTestCase, self ).test_get_all_include_attributes_filter() @mock.patch.object( action_validator, "validate_action", mock.MagicMock(return_value=True) ) def test_get_all_exclude_attributes_filter(self): return super( ActionsControllerTestCase, self ).test_get_all_include_attributes_filter() @mock.patch.object( action_validator, "validate_action", mock.MagicMock(return_value=True) ) def test_query(self): action_1_id = self.__get_action_id(self.__do_post(ACTION_1)) action_2_id = self.__get_action_id(self.__do_post(ACTION_2)) resp = self.app.get("/v1/actions?name=%s" % ACTION_1["name"]) self.assertEqual(resp.status_int, 200) self.assertEqual(len(resp.json), 1, "/v1/actions did not return all actions.") self.__do_delete(action_1_id) self.__do_delete(action_2_id) @mock.patch.object( action_validator, "validate_action", mock.MagicMock(return_value=True) ) def test_get_one_fail(self): resp = self.app.get("/v1/actions/1", expect_errors=True) self.assertEqual(resp.status_int, 404) @mock.patch.object( action_validator, "validate_action", mock.MagicMock(return_value=True) ) def test_post_delete(self): post_resp = self.__do_post(ACTION_1) self.assertEqual(post_resp.status_int, 201) self.__do_delete(self.__get_action_id(post_resp)) @mock.patch.object( action_validator, "validate_action", mock.MagicMock(return_value=True) ) def test_post_action_with_bad_params(self): post_resp = self.__do_post(ACTION_9, expect_errors=True) self.assertEqual(post_resp.status_int, 400) @mock.patch.object( action_validator, "validate_action", mock.MagicMock(return_value=True) ) def test_post_no_description_field(self): post_resp = self.__do_post(ACTION_6) self.assertEqual(post_resp.status_int, 201) self.__do_delete(self.__get_action_id(post_resp)) @mock.patch.object( action_validator, "validate_action", mock.MagicMock(return_value=True) ) def test_post_no_enable_field(self): post_resp = self.__do_post(ACTION_3) self.assertEqual(post_resp.status_int, 201) self.assertIn(b"enabled", post_resp.body) # If enabled field is not provided it should default to True data = json.loads(post_resp.body) self.assertDictContainsSubset({"enabled": True}, data) self.__do_delete(self.__get_action_id(post_resp)) @mock.patch.object( action_validator, "validate_action", mock.MagicMock(return_value=True) ) def test_post_false_enable_field(self): post_resp = self.__do_post(ACTION_4) self.assertEqual(post_resp.status_int, 201) data = json.loads(post_resp.body) self.assertDictContainsSubset({"enabled": False}, data) self.__do_delete(self.__get_action_id(post_resp)) @mock.patch.object( action_validator, "validate_action", mock.MagicMock(return_value=True) ) def test_post_name_unicode_action_already_exists(self): # Verify that exception messages containing unicode characters don't result in internal # server errors action = copy.deepcopy(ACTION_1) # NOTE: We explicitly don't prefix this string value with u"" action["name"] = "žactionćšžži💩" # 1. Initial creation post_resp = self.__do_post(action, expect_errors=True) self.assertEqual(post_resp.status_int, 201) # 2. Action already exists post_resp = self.__do_post(action, expect_errors=True) self.assertEqual(post_resp.status_int, 409) self.assertIn( "Tried to save duplicate unique keys", post_resp.json["faultstring"] ) # 3. Action already exists (this time with unicode type) action["name"] = "žactionćšžži💩" post_resp = self.__do_post(action, expect_errors=True) self.assertEqual(post_resp.status_int, 409) self.assertIn( "Tried to save duplicate unique keys", post_resp.json["faultstring"] ) @mock.patch.object( action_validator, "validate_action", mock.MagicMock(return_value=True) ) def test_post_parameter_type_is_array_and_invalid(self): post_resp = self.__do_post(ACTION_13, expect_errors=True) self.assertEqual(post_resp.status_int, 400) if six.PY3: expected_error = ( b"['string', 'object'] is not valid under any of the given schemas" ) else: expected_error = ( b"[u'string', u'object'] is not valid under any of the given schemas" ) self.assertIn(expected_error, post_resp.body) @mock.patch.object( action_validator, "validate_action", mock.MagicMock(return_value=True) ) def test_post_discard_id_field(self): post_resp = self.__do_post(ACTION_7) self.assertEqual(post_resp.status_int, 201) self.assertIn(b"id", post_resp.body) data = json.loads(post_resp.body) # Verify that user-provided id is discarded. self.assertNotEquals(data["id"], ACTION_7["id"]) self.__do_delete(self.__get_action_id(post_resp)) @mock.patch.object( action_validator, "validate_action", mock.MagicMock(return_value=True) ) def test_post_duplicate(self): action_ids = [] post_resp = self.__do_post(ACTION_1) self.assertEqual(post_resp.status_int, 201) action_in_db = Action.get_by_name(ACTION_1.get("name")) self.assertIsNotNone(action_in_db, "Action must be in db.") action_ids.append(self.__get_action_id(post_resp)) post_resp = self.__do_post(ACTION_1, expect_errors=True) # Verify name conflict self.assertEqual(post_resp.status_int, 409) self.assertEqual(post_resp.json["conflict-id"], action_ids[0]) post_resp = self.__do_post(ACTION_10) action_ids.append(self.__get_action_id(post_resp)) # Verify action with same name but different pack is written. self.assertEqual(post_resp.status_int, 201) for i in action_ids: self.__do_delete(i) @mock.patch.object( action_validator, "validate_action", mock.MagicMock(return_value=True) ) def test_post_include_files(self): # Verify initial state pack_db = Pack.get_by_ref(ACTION_12["pack"]) self.assertNotIn("actions/filea.txt", pack_db.files) action = copy.deepcopy(ACTION_12) action["data_files"] = [{"file_path": "filea.txt", "content": "test content"}] post_resp = self.__do_post(action) # Verify file has been written on disk for file_path in self.to_delete_files: self.assertTrue(os.path.exists(file_path)) # Verify PackDB.files has been updated pack_db = Pack.get_by_ref(ACTION_12["pack"]) self.assertIn("actions/filea.txt", pack_db.files) self.__do_delete(self.__get_action_id(post_resp)) @mock.patch.object( action_validator, "validate_action", mock.MagicMock(return_value=True) ) def test_post_put_delete(self): action = copy.copy(ACTION_1) post_resp = self.__do_post(action) self.assertEqual(post_resp.status_int, 201) self.assertIn(b"id", post_resp.body) body = json.loads(post_resp.body) action["id"] = body["id"] action["description"] = "some other test description" pack = action["pack"] del action["pack"] self.assertNotIn("pack", action) put_resp = self.__do_put(action["id"], action) self.assertEqual(put_resp.status_int, 200) self.assertIn(b"description", put_resp.body) body = json.loads(put_resp.body) self.assertEqual(body["description"], action["description"]) self.assertEqual(body["pack"], pack) delete_resp = self.__do_delete(self.__get_action_id(post_resp)) self.assertEqual(delete_resp.status_int, 204) def test_post_invalid_runner_type(self): post_resp = self.__do_post(ACTION_5, expect_errors=True) self.assertEqual(post_resp.status_int, 400) def test_post_override_runner_param_not_allowed(self): post_resp = self.__do_post(ACTION_14, expect_errors=True) self.assertEqual(post_resp.status_int, 400) expected = ( 'The attribute "type" for the runner parameter "sudo" ' 'in action "dummy_pack_1.st2.dummy.action14" cannot be overridden.' ) self.assertEqual(post_resp.json.get("faultstring"), expected) def test_post_override_runner_param_allowed(self): post_resp = self.__do_post(ACTION_15) self.assertEqual(post_resp.status_int, 201) @mock.patch.object( action_validator, "validate_action", mock.MagicMock(return_value=True) ) def test_delete(self): post_resp = self.__do_post(ACTION_1) del_resp = self.__do_delete(self.__get_action_id(post_resp)) self.assertEqual(del_resp.status_int, 204) @mock.patch.object( action_validator, "validate_action", mock.MagicMock(return_value=True) ) def test_action_with_tags(self): post_resp = self.__do_post(ACTION_1) action_id = self.__get_action_id(post_resp) get_resp = self.__do_get_one(action_id) self.assertEqual(get_resp.status_int, 200) self.assertEqual(self.__get_action_id(get_resp), action_id) self.assertEqual(get_resp.json["tags"], ACTION_1["tags"]) self.__do_delete(action_id) @mock.patch.object( action_validator, "validate_action", mock.MagicMock(return_value=True) ) def test_action_with_notify_update(self): post_resp = self.__do_post(ACTION_WITH_NOTIFY) action_id = self.__get_action_id(post_resp) get_resp = self.__do_get_one(action_id) self.assertEqual(get_resp.status_int, 200) self.assertEqual(self.__get_action_id(get_resp), action_id) self.assertIsNotNone(get_resp.json["notify"]["on-complete"]) # Now post the same action with no notify ACTION_WITHOUT_NOTIFY = copy.copy(ACTION_WITH_NOTIFY) del ACTION_WITHOUT_NOTIFY["notify"] self.__do_put(action_id, ACTION_WITHOUT_NOTIFY) # Validate that notify section has vanished get_resp = self.__do_get_one(action_id) self.assertEqual(get_resp.json["notify"], {}) self.__do_delete(action_id) @mock.patch.object( action_validator, "validate_action", mock.MagicMock(return_value=True) ) def test_get_one_using_name_parameter(self): action_id, action_name = self.__get_action_id_and_additional_attribute( self.__do_post(ACTION_1), "name" ) get_resp = self.__do_get_actions_by_url_parameter("name", action_name) self.assertEqual(get_resp.status_int, 200) self.assertEqual(get_resp.json[0]["id"], action_id) self.assertEqual(get_resp.json[0]["name"], action_name) self.__do_delete(action_id) @mock.patch.object( action_validator, "validate_action", mock.MagicMock(return_value=True) ) def test_get_one_using_pack_parameter(self): action_id, action_pack = self.__get_action_id_and_additional_attribute( self.__do_post(ACTION_10), "pack" ) get_resp = self.__do_get_actions_by_url_parameter("pack", action_pack) self.assertEqual(get_resp.status_int, 200) self.assertEqual(get_resp.json[0]["id"], action_id) self.assertEqual(get_resp.json[0]["pack"], action_pack) self.__do_delete(action_id) @mock.patch.object( action_validator, "validate_action", mock.MagicMock(return_value=True) ) def test_get_one_using_tag_parameter(self): action_id, action_tags = self.__get_action_id_and_additional_attribute( self.__do_post(ACTION_1), "tags" ) get_resp = self.__do_get_actions_by_url_parameter( "tags", action_tags[0]["name"] ) self.assertEqual(get_resp.status_int, 200) self.assertEqual(get_resp.json[0]["id"], action_id) self.assertEqual(get_resp.json[0]["tags"], action_tags) self.__do_delete(action_id) # TODO: Re-enable those tests after we ensure DB is flushed in setUp # and each test starts in a clean state @unittest2.skip("Skip because of test polution") def test_update_action_belonging_to_system_pack(self): post_resp = self.__do_post(ACTION_11) action_id = self.__get_action_id(post_resp) put_resp = self.__do_put(action_id, ACTION_11, expect_errors=True) self.assertEqual(put_resp.status_int, 400) @unittest2.skip("Skip because of test polution") def test_delete_action_belonging_to_system_pack(self): post_resp = self.__do_post(ACTION_11) action_id = self.__get_action_id(post_resp) del_resp = self.__do_delete(action_id, expect_errors=True) self.assertEqual(del_resp.status_int, 400) def _insert_mock_models(self): action_1_id = self.__get_action_id(self.__do_post(ACTION_1)) action_2_id = self.__get_action_id(self.__do_post(ACTION_2)) return [action_1_id, action_2_id] def _do_delete(self, action_id, expect_errors=False): return self.__do_delete(action_id=action_id, expect_errors=expect_errors) @staticmethod def __get_action_id(resp): return resp.json["id"] @staticmethod def __get_action_name(resp): return resp.json["name"] @staticmethod def __get_action_tags(resp): return resp.json["tags"] @staticmethod def __get_action_id_and_additional_attribute(resp, attribute): return resp.json["id"], resp.json[attribute] def __do_get_one(self, action_id, expect_errors=False): return self.app.get("/v1/actions/%s" % action_id, expect_errors=expect_errors) def __do_get_actions_by_url_parameter(self, filter, value, expect_errors=False): return self.app.get( "/v1/actions?%s=%s" % (filter, value), expect_errors=expect_errors ) def __do_post(self, action, expect_errors=False): return self.app.post_json("/v1/actions", action, expect_errors=expect_errors) def __do_put(self, action_id, action, expect_errors=False): return self.app.put_json( "/v1/actions/%s" % action_id, action, expect_errors=expect_errors ) def __do_delete(self, action_id, expect_errors=False): return self.app.delete( "/v1/actions/%s" % action_id, expect_errors=expect_errors )
from st2common.services import action as action_service from st2common.transport.liveaction import LiveActionPublisher from st2common.transport.publishers import CUDPublisher from st2tests import ExecutionDbTestCase from st2tests import fixturesloader from st2tests.mocks.liveaction import MockLiveActionPublisherNonBlocking from six.moves import range TEST_FIXTURES = { 'chains': ['test_cancel.yaml', 'test_cancel_with_subworkflow.yaml'], 'actions': ['test_cancel.yaml', 'test_cancel_with_subworkflow.yaml'] } TEST_PACK = 'action_chain_tests' TEST_PACK_PATH = fixturesloader.get_fixtures_packs_base_path( ) + '/' + TEST_PACK PACKS = [ TEST_PACK_PATH, fixturesloader.get_fixtures_packs_base_path() + '/core' ] USERNAME = '******' @mock.patch.object(CUDPublisher, 'publish_update', mock.MagicMock(return_value=None)) @mock.patch.object(CUDPublisher, 'publish_create', mock.MagicMock(return_value=None)) @mock.patch.object( LiveActionPublisher, 'publish_state',
import os import mock from st2common.content import utils as content_utils from st2common.bootstrap.base import ResourceRegistrar from st2common.persistence.pack import Pack from st2common.persistence.pack import ConfigSchema from st2tests import DbTestCase from st2tests import fixturesloader __all__ = ['ResourceRegistrarTestCase'] PACK_PATH = os.path.join(fixturesloader.get_fixtures_packs_base_path(), 'dummy_pack_1') class ResourceRegistrarTestCase(DbTestCase): 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 = {
import six import mock from st2common.content import utils as content_utils from st2common.bootstrap.configsregistrar import ConfigsRegistrar from st2common.persistence.pack import Pack from st2common.persistence.pack import Config from st2tests.api import SUPER_SECRET_PARAMETER from st2tests.base import CleanDbTestCase from st2tests import fixturesloader __all__ = ["ConfigsRegistrarTestCase"] PACK_1_PATH = os.path.join(fixturesloader.get_fixtures_packs_base_path(), "dummy_pack_1") PACK_6_PATH = os.path.join(fixturesloader.get_fixtures_packs_base_path(), "dummy_pack_6") PACK_19_PATH = os.path.join(fixturesloader.get_fixtures_packs_base_path(), "dummy_pack_19") PACK_11_PATH = os.path.join(fixturesloader.get_fixtures_packs_base_path(), "dummy_pack_11") PACK_22_PATH = os.path.join(fixturesloader.get_fixtures_packs_base_path(), "dummy_pack_22") class ConfigsRegistrarTestCase(CleanDbTestCase): def test_register_configs_for_all_packs(self): # Verify DB is empty pack_dbs = Pack.get_all()
from st2common.content import utils as content_utils from st2common.bootstrap.configsregistrar import ConfigsRegistrar from st2common.persistence.pack import Pack from st2common.persistence.pack import Config from st2tests.api import SUPER_SECRET_PARAMETER from st2tests.base import CleanDbTestCase from st2tests import fixturesloader __all__ = [ 'ConfigsRegistrarTestCase' ] PACK_1_PATH = os.path.join(fixturesloader.get_fixtures_packs_base_path(), 'dummy_pack_1') PACK_6_PATH = os.path.join(fixturesloader.get_fixtures_packs_base_path(), 'dummy_pack_6') PACK_19_PATH = os.path.join(fixturesloader.get_fixtures_packs_base_path(), 'dummy_pack_19') class ConfigsRegistrarTestCase(CleanDbTestCase): 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()
import glob from st2tests.base import IntegrationTestCase from st2common.util.shell import run_command from st2tests import config as test_config from st2tests.fixturesloader import get_fixtures_packs_base_path BASE_DIR = os.path.dirname(os.path.abspath(__file__)) SCRIPT_PATH = os.path.join(BASE_DIR, '../../bin/st2-register-content') SCRIPT_PATH = os.path.abspath(SCRIPT_PATH) BASE_CMD_ARGS = [sys.executable, SCRIPT_PATH, '--config-file=conf/st2.tests.conf', '-v'] BASE_REGISTER_ACTIONS_CMD_ARGS = BASE_CMD_ARGS + ['--register-actions'] PACKS_PATH = get_fixtures_packs_base_path() PACKS_COUNT = len(glob.glob('%s/*/pack.yaml' % (PACKS_PATH))) assert(PACKS_COUNT >= 2) class ContentRegisterScriptTestCase(IntegrationTestCase): def setUp(self): super(ContentRegisterScriptTestCase, self).setUp() test_config.parse_args() def test_register_from_pack_success(self): pack_dir = os.path.join(get_fixtures_packs_base_path(), 'dummy_pack_1') runner_dirs = os.path.join(get_fixtures_packs_base_path(), 'runners') opts = [ '--register-pack=%s' % (pack_dir),