Exemplo n.º 1
0
    def setUpClass(cls):
        super(TestTimersHolder, cls).setUpClass()

        loader = FixturesLoader()
        TestTimersHolder.MODELS = loader.load_fixtures(
            fixtures_pack=PACK, fixtures_dict=FIXTURES)['triggers']
        loader.save_fixtures_to_db(fixtures_pack=PACK, fixtures_dict=FIXTURES)
Exemplo n.º 2
0
    def test_create_or_update_trigger_db_simple_triggers(self):
        test_fixtures = {
            'triggertypes': ['triggertype1.yaml']
        }
        loader = FixturesLoader()
        fixtures = loader.save_fixtures_to_db(fixtures_pack='generic', fixtures_dict=test_fixtures)
        triggertypes = fixtures['triggertypes']
        trigger_type_ref = ResourceReference.to_string_reference(
            name=triggertypes['triggertype1.yaml']['name'],
            pack=triggertypes['triggertype1.yaml']['pack'])

        trigger = {
            'name': triggertypes['triggertype1.yaml']['name'],
            'pack': triggertypes['triggertype1.yaml']['pack'],
            'type': trigger_type_ref
        }
        trigger_service.create_or_update_trigger_db(trigger)
        triggers = Trigger.get_all()
        self.assertTrue(len(triggers) == 1, 'Only one trigger should be created.')
        self.assertTrue(triggers[0]['name'] == triggertypes['triggertype1.yaml']['name'])

        # Try adding duplicate
        trigger_service.create_or_update_trigger_db(trigger)
        triggers = Trigger.get_all()
        self.assertTrue(len(triggers) == 1, 'Only one trigger should be present.')
        self.assertTrue(triggers[0]['name'] == triggertypes['triggertype1.yaml']['name'])
Exemplo n.º 3
0
    def setUpClass(cls):
        EventletTestCase.setUpClass()
        DbTestCase.setUpClass()

        loader = FixturesLoader()
        loader.save_fixtures_to_db(fixtures_pack=PACK,
                                   fixtures_dict=TEST_FIXTURES)
Exemplo n.º 4
0
    def test_exception_thrown_when_rule_creation_no_trigger_yes_triggertype(self):
        test_fixtures = {
            'triggertypes': ['triggertype1.yaml']
        }
        loader = FixturesLoader()
        fixtures = loader.save_fixtures_to_db(fixtures_pack='generic', fixtures_dict=test_fixtures)
        triggertypes = fixtures['triggertypes']
        trigger_type_ref = ResourceReference.to_string_reference(
            name=triggertypes['triggertype1.yaml']['name'],
            pack=triggertypes['triggertype1.yaml']['pack'])

        rule = {
            'name': 'fancyrule',
            'trigger': {
                'type': trigger_type_ref
            },
            'criteria': {

            },
            'action': {
                'ref': 'core.local',
                'parameters': {
                    'cmd': 'date'
                }
            }
        }
        rule_api = RuleAPI(**rule)
        self.assertRaises(TriggerDoesNotExistException,
                          trigger_service.create_trigger_db_from_rule, rule_api)
Exemplo n.º 5
0
 def setUpClass(cls):
     super(BackstopRuleMatcherTestCase, cls).setUpClass()
     fixturesloader = FixturesLoader()
     # Create TriggerTypes before creation of Rule to avoid failure. Rule requires the
     # Trigger and therefore TriggerType to be created prior to rule creation.
     cls.models = fixturesloader.save_fixtures_to_db(
         fixtures_pack=PACK, fixtures_dict=FIXTURES_TRIGGERS)
     cls.models.update(fixturesloader.save_fixtures_to_db(
         fixtures_pack=PACK, fixtures_dict=FIXTURES_RULES))
Exemplo n.º 6
0
    def setUpClass(cls):
        EventletTestCase.setUpClass()
        DbTestCase.setUpClass()

        # Register common policy types
        register_policy_types(st2common)

        loader = FixturesLoader()
        loader.save_fixtures_to_db(fixtures_pack=PACK, fixtures_dict=TEST_FIXTURES)
Exemplo n.º 7
0
    def setUp(self):
        super(SchedulerPoliciesTest, self).setUp()

        # Register common policy types
        register_policy_types(st2common)

        loader = FixturesLoader()
        models = loader.save_fixtures_to_db(fixtures_pack=PACK,
                                            fixtures_dict=TEST_FIXTURES_2)

        # Policy with "post_run" application
        self.policy_db = models['policies']['policy_1.yaml']
Exemplo n.º 8
0
    def setUpClass(cls):
        ExecutionDbTestCase.setUpClass()

        # Register runners
        runners_registrar.register_runners()

        # Register common policy types
        register_policy_types(st2common)

        loader = FixturesLoader()
        loader.save_fixtures_to_db(fixtures_pack=PACK,
                                   fixtures_dict=TEST_FIXTURES)
Exemplo n.º 9
0
    def setUp(self):
        super(NotifierPoliciesTestCase, self).setUp()

        # Register runners
        runners_registrar.register_runners()

        # Register common policy types
        register_policy_types(st2common)

        loader = FixturesLoader()
        models = loader.save_fixtures_to_db(fixtures_pack=PACK,
                                            fixtures_dict=TEST_FIXTURES_1)

        # Policy with "post_run" application
        self.policy_db = models['policies']['policy_4.yaml']
Exemplo n.º 10
0
    def setUpClass(cls):
        EventletTestCase.setUpClass()
        DbTestCase.setUpClass()

        # Override the coordinator to use the noop driver otherwise the tests will be blocked.
        tests_config.parse_args(coordinator_noop=True)
        coordination.COORDINATOR = None

        # Register runners
        runners_registrar.register_runners()

        # Register common policy types
        register_policy_types(st2common)

        loader = FixturesLoader()
        loader.save_fixtures_to_db(fixtures_pack=PACK,
                                   fixtures_dict=TEST_FIXTURES)
Exemplo n.º 11
0
    def test_create_trigger_db_from_rule_duplicate(self):
        test_fixtures = {
            'rules': ['cron_timer_rule_1.yaml', 'cron_timer_rule_2.yaml']
        }
        loader = FixturesLoader()
        fixtures = loader.load_fixtures(fixtures_pack='generic', fixtures_dict=test_fixtures)
        rules = fixtures['rules']

        trigger_db_ret_1 = trigger_service.create_trigger_db_from_rule(
            RuleAPI(**rules['cron_timer_rule_1.yaml']))
        self.assertTrue(trigger_db_ret_1 is not None)
        trigger_db_ret_2 = trigger_service.create_trigger_db_from_rule(
            RuleAPI(**rules['cron_timer_rule_2.yaml']))
        self.assertTrue(trigger_db_ret_2 is not None)
        self.assertEqual(trigger_db_ret_1, trigger_db_ret_2, 'Should reuse same trigger.')
        trigger_db = Trigger.get_by_id(trigger_db_ret_1.id)
        self.assertDictEqual(trigger_db.parameters,
                             rules['cron_timer_rule_1.yaml']['trigger']['parameters'])
Exemplo n.º 12
0
    def setUp(self):
        super(RetryPolicyTestCase, self).setUp()

        # Register common policy types
        register_policy_types(st2actions)

        loader = FixturesLoader()
        models = loader.save_fixtures_to_db(fixtures_pack=PACK,
                                            fixtures_dict=TEST_FIXTURES)

        # Instantiate policy applicator we will use in the tests
        policy_db = models['policies']['policy_4.yaml']
        retry_on = policy_db.parameters['retry_on']
        max_retry_count = policy_db.parameters['max_retry_count']
        self.policy = ExecutionRetryPolicyApplicator(policy_ref='test_policy',
                                                     policy_type='action.retry',
                                                     retry_on=retry_on,
                                                     max_retry_count=max_retry_count,
                                                     delay=0)
Exemplo n.º 13
0
    def setUpClass(cls):
        super(TraceInjectionTests, cls).setUpClass()
        cls.models = FixturesLoader().save_fixtures_to_db(
            fixtures_pack=FIXTURES_PACK, fixtures_dict=TEST_MODELS)

        cls.traceable_liveaction = cls.models['liveactions'][
            'traceable_liveaction.yaml']
        cls.traceable_execution = cls.models['executions'][
            'traceable_execution.yaml']
        cls.action = cls.models['actions']['chain1.yaml']
Exemplo n.º 14
0
    def setUpClass(cls):
        super(TestApiKeyController, cls).setUpClass()

        cfg.CONF.set_override(name='mask_secrets', override=True, group='api')
        cfg.CONF.set_override(name='mask_secrets', override=True, group='log')

        models = FixturesLoader().save_fixtures_to_db(
            fixtures_pack=FIXTURES_PACK, fixtures_dict=TEST_MODELS)
        cls.apikey1 = models['apikeys']['apikey1.yaml']
        cls.apikey2 = models['apikeys']['apikey2.yaml']
        cls.apikey3 = models['apikeys']['apikey3.yaml']
Exemplo n.º 15
0
    def setUp(self):
        super(RetryPolicyTestCase, self).setUp()

        # Register common policy types
        register_policy_types(st2actions)

        loader = FixturesLoader()
        models = loader.save_fixtures_to_db(fixtures_pack=PACK,
                                            fixtures_dict=TEST_FIXTURES)

        # Instantiate policy applicator we will use in the tests
        policy_db = models['policies']['policy_4.yaml']
        retry_on = policy_db.parameters['retry_on']
        max_retry_count = policy_db.parameters['max_retry_count']
        self.policy = ExecutionRetryPolicyApplicator(
            policy_ref='test_policy',
            policy_type='action.retry',
            retry_on=retry_on,
            max_retry_count=max_retry_count,
            delay=0)
Exemplo n.º 16
0
 def test_matching_trigger_from_file(self):
     FixturesLoader().save_fixtures_to_db(fixtures_pack=FIXTURES_PACK,
                                          fixtures_dict=TEST_MODELS_ACTIONS)
     rule_file_path = os.path.join(BASE_PATH, '../fixtures/rule.yaml')
     trigger_instance_file_path = os.path.join(
         BASE_PATH, '../fixtures/trigger_instance_1.yaml')
     tester = RuleTester(
         rule_file_path=rule_file_path,
         trigger_instance_file_path=trigger_instance_file_path)
     matching = tester.evaluate()
     self.assertTrue(matching)
Exemplo n.º 17
0
class RunnerTypesControllerRBACTestCase(APIControllerWithRBACTestCase):
    fixtures_loader = FixturesLoader()

    def setUp(self):
        super(RunnerTypesControllerRBACTestCase, self).setUp()
        self.fixtures_loader.save_fixtures_to_db(fixtures_pack=FIXTURES_PACK,
                                                 fixtures_dict=TEST_FIXTURES)

    def test_get_all_and_get_one_no_permissions(self):
        # get all
        user_db = self.users['no_permissions']
        self.use_user(user_db)

        resp = self.app.get('/v1/runnertypes', expect_errors=True)
        expected_msg = (
            'User "no_permissions" doesn\'t have required permission '
            '"runner_type_list"')
        self.assertEqual(resp.status_code, http_client.FORBIDDEN)
        self.assertEqual(resp.json['faultstring'], expected_msg)

        # get one
        resp = self.app.get('/v1/runnertypes/test-runner-1',
                            expect_errors=True)
        expected_msg = (
            'User "no_permissions" doesn\'t have required permission '
            '"runner_type_view" on resource "runner_type:test-runner-1"')
        self.assertEqual(resp.status_code, http_client.FORBIDDEN)
        self.assertEqual(resp.json['faultstring'], expected_msg)

    def test_put_disable_runner_no_permissions(self):
        user_db = self.users['admin']
        self.use_user(user_db)

        runnertype_id = 'test-runner-1'
        resp = self.app.get('/v1/runnertypes/%s' % runnertype_id)

        # Disable the runner
        user_db = self.users['no_permissions']
        self.use_user(user_db)

        update_input = resp.json
        update_input['enabled'] = False

        resp = self.__do_put(runnertype_id, update_input)
        expected_msg = (
            'User "no_permissions" doesn\'t have required permission '
            '"runner_type_modify" on resource "runner_type:test-runner-1"')
        self.assertEqual(resp.status_code, http_client.FORBIDDEN)
        self.assertEqual(resp.json['faultstring'], expected_msg)

    def __do_put(self, runner_type_id, runner_type):
        return self.app.put_json('/v1/runnertypes/%s' % runner_type_id,
                                 runner_type,
                                 expect_errors=True)
Exemplo n.º 18
0
    def setUpClass(cls):
        super(AliasExecutionTestCase, cls).setUpClass()
        cls.models = FixturesLoader().save_fixtures_to_db(
            fixtures_pack=FIXTURES_PACK, fixtures_dict=TEST_MODELS)

        cls.runner1 = cls.models['runners']['runner1.yaml']
        cls.action1 = cls.models['actions']['action1.yaml']
        cls.alias1 = cls.models['aliases']['alias1.yaml']
        cls.alias2 = cls.models['aliases']['alias2.yaml']
        cls.alias4 = cls.models['aliases']['alias4.yaml']
        cls.alias_with_undefined_jinja_in_ack_format = \
            cls.models['aliases']['alias_with_undefined_jinja_in_ack_format.yaml']
Exemplo n.º 19
0
    def test_create_trigger_db_from_rule(self):
        test_fixtures = {
            'rules': ['cron_timer_rule_1.yaml', 'cron_timer_rule_3.yaml']
        }
        loader = FixturesLoader()
        fixtures = loader.load_fixtures(fixtures_pack='generic',
                                        fixtures_dict=test_fixtures)
        rules = fixtures['rules']

        trigger_db_ret_1 = trigger_service.create_trigger_db_from_rule(
            RuleAPI(**rules['cron_timer_rule_1.yaml']))
        self.assertIsNotNone(trigger_db_ret_1)
        trigger_db = Trigger.get_by_id(trigger_db_ret_1.id)
        self.assertDictEqual(
            trigger_db.parameters,
            rules['cron_timer_rule_1.yaml']['trigger']['parameters'])

        trigger_db_ret_2 = trigger_service.create_trigger_db_from_rule(
            RuleAPI(**rules['cron_timer_rule_3.yaml']))
        self.assertIsNotNone(trigger_db_ret_2)
        self.assertTrue(trigger_db_ret_2.id != trigger_db_ret_1.id)
Exemplo n.º 20
0
    def test_create_trigger_db_from_rule_duplicate(self):
        test_fixtures = {
            'rules': ['cron_timer_rule_1.json', 'cron_timer_rule_2.json']
        }
        loader = FixturesLoader()
        fixtures = loader.load_fixtures(fixtures_pack='generic',
                                        fixtures_dict=test_fixtures)
        rules = fixtures['rules']

        trigger_db_ret_1 = trigger_service.create_trigger_db_from_rule(
            RuleAPI(**rules['cron_timer_rule_1.json']))
        self.assertTrue(trigger_db_ret_1 is not None)
        trigger_db_ret_2 = trigger_service.create_trigger_db_from_rule(
            RuleAPI(**rules['cron_timer_rule_2.json']))
        self.assertTrue(trigger_db_ret_2 is not None)
        self.assertEqual(trigger_db_ret_1, trigger_db_ret_2,
                         'Should reuse same trigger.')
        trigger_db = Trigger.get_by_id(trigger_db_ret_1.id)
        self.assertDictEqual(
            trigger_db.parameters,
            rules['cron_timer_rule_1.json']['trigger']['parameters'])
Exemplo n.º 21
0
    def setUpClass(cls):
        super(TestApiKeyController, cls).setUpClass()

        cfg.CONF.set_override(name="mask_secrets", override=True, group="api")
        cfg.CONF.set_override(name="mask_secrets", override=True, group="log")

        models = FixturesLoader().save_fixtures_to_db(
            fixtures_pack=FIXTURES_PACK, fixtures_dict=TEST_MODELS)
        cls.apikey1 = models["apikeys"]["apikey1.yaml"]
        cls.apikey2 = models["apikeys"]["apikey2.yaml"]
        cls.apikey3 = models["apikeys"]["apikey3.yaml"]
        cls.apikey4 = models["apikeys"]["apikey_disabled.yaml"]
        cls.apikey5 = models["apikeys"]["apikey_malformed.yaml"]
Exemplo n.º 22
0
    def setUpClass(cls):
        super(ActionAliasControllerTestCase, cls).setUpClass()
        cls.models = FixturesLoader().save_fixtures_to_db(
            fixtures_pack=FIXTURES_PACK, fixtures_dict=TEST_MODELS
        )
        cls.alias1 = cls.models["aliases"]["alias1.yaml"]
        cls.alias2 = cls.models["aliases"]["alias2.yaml"]

        loaded_models = FixturesLoader().load_models(
            fixtures_pack=FIXTURES_PACK, fixtures_dict=TEST_LOAD_MODELS
        )
        cls.alias3 = loaded_models["aliases"]["alias3.yaml"]

        FixturesLoader().save_fixtures_to_db(
            fixtures_pack=GENERIC_FIXTURES_PACK,
            fixtures_dict={"aliases": ["alias7.yaml"]},
        )

        loaded_models = FixturesLoader().load_models(
            fixtures_pack=GENERIC_FIXTURES_PACK, fixtures_dict=TEST_LOAD_MODELS_GENERIC
        )
        cls.alias3_generic = loaded_models["aliases"]["alias3.yaml"]
Exemplo n.º 23
0
    def test_create_trigger_db_from_rule_duplicate(self):
        test_fixtures = {
            "rules": ["cron_timer_rule_1.yaml", "cron_timer_rule_2.yaml"]
        }
        loader = FixturesLoader()
        fixtures = loader.load_fixtures(fixtures_pack="generic",
                                        fixtures_dict=test_fixtures)
        rules = fixtures["rules"]

        trigger_db_ret_1 = trigger_service.create_trigger_db_from_rule(
            RuleAPI(**rules["cron_timer_rule_1.yaml"]))
        self.assertIsNotNone(trigger_db_ret_1)
        trigger_db_ret_2 = trigger_service.create_trigger_db_from_rule(
            RuleAPI(**rules["cron_timer_rule_2.yaml"]))
        self.assertIsNotNone(trigger_db_ret_2)
        self.assertEqual(trigger_db_ret_1, trigger_db_ret_2,
                         "Should reuse same trigger.")
        trigger_db = Trigger.get_by_id(trigger_db_ret_1.id)
        self.assertDictEqual(
            trigger_db.parameters,
            rules["cron_timer_rule_1.yaml"]["trigger"]["parameters"],
        )
Exemplo n.º 24
0
    def setUpClass(cls):
        super(TestActionExecutionControllerAuthEnabled, cls).setUpClass()
        cls.action = copy.deepcopy(ACTION_1)
        headers = {
            'content-type': 'application/json',
            'X-Auth-Token': str(SYS_TOKEN.token)
        }
        post_resp = cls.app.post_json('/v1/actions',
                                      cls.action,
                                      headers=headers)
        cls.action['id'] = post_resp.json['id']

        FixturesLoader().save_fixtures_to_db(fixtures_pack=FIXTURES_PACK,
                                             fixtures_dict=FIXTURES)
Exemplo n.º 25
0
    def setUpClass(cls):
        super(TestTraceService, cls).setUpClass()
        cls.models = FixturesLoader().save_fixtures_to_db(
            fixtures_pack=FIXTURES_PACK, fixtures_dict=TEST_MODELS)
        cls.trace1 = cls.models['traces']['trace_multiple_components.yaml']
        cls.trace2 = cls.models['traces']['trace_one_each.yaml']
        cls.trace3 = cls.models['traces']['trace_one_each_dup.yaml']
        cls.trace_empty = cls.models['traces']['trace_empty.yaml']
        cls.trace_execution = cls.models['traces']['trace_execution.yaml']

        cls.traceable_liveaction = cls.models['liveactions'][
            'traceable_liveaction.yaml']
        cls.traceable_execution = cls.models['executions'][
            'traceable_execution.yaml']
Exemplo n.º 26
0
class TestDumper(DbTestCase):

    fixtures_loader = FixturesLoader()
    loaded_fixtures = fixtures_loader.load_fixtures(fixtures_pack=DESCENDANTS_PACK,
                                                    fixtures_dict=DESCENDANTS_FIXTURES)
    loaded_executions = loaded_fixtures['executions']
    execution_apis = []
    for execution in loaded_executions.values():
        execution_apis.append(ActionExecutionAPI(**execution))

    def get_queue(self):
        executions_queue = queue.Queue()

        for execution in self.execution_apis:
            executions_queue.put(execution)
        return executions_queue

    @mock.patch.object(os.path, 'exists', mock.MagicMock(return_value=True))
    def test_write_marker_to_db(self):
        executions_queue = self.get_queue()
        dumper = Dumper(queue=executions_queue,
                        export_dir='/tmp', batch_size=5,
                        max_files_per_sleep=1,
                        file_prefix='st2-stuff-', file_format='json')
        timestamps = [isotime.parse(execution.end_timestamp) for execution in self.execution_apis]
        max_timestamp = max(timestamps)
        marker_db = dumper._write_marker_to_db(max_timestamp)
        persisted_marker = marker_db.marker
        self.assertTrue(isinstance(persisted_marker, six.string_types))
        self.assertEqual(isotime.parse(persisted_marker), max_timestamp)

    @mock.patch.object(os.path, 'exists', mock.MagicMock(return_value=True))
    def test_write_marker_to_db_marker_exists(self):
        executions_queue = self.get_queue()
        dumper = Dumper(queue=executions_queue,
                        export_dir='/tmp', batch_size=5,
                        max_files_per_sleep=1,
                        file_prefix='st2-stuff-', file_format='json')
        timestamps = [isotime.parse(execution.end_timestamp) for execution in self.execution_apis]
        max_timestamp = max(timestamps)
        first_marker_db = dumper._write_marker_to_db(max_timestamp)
        second_marker_db = dumper._write_marker_to_db(max_timestamp + datetime.timedelta(hours=1))
        markers = DumperMarker.get_all()
        self.assertEqual(len(markers), 1)
        final_marker_id = markers[0].id
        self.assertEqual(first_marker_db.id, final_marker_id)
        self.assertEqual(second_marker_db.id, final_marker_id)
        self.assertEqual(markers[0].marker, second_marker_db.marker)
        self.assertTrue(second_marker_db.updated_at > first_marker_db.updated_at)
Exemplo n.º 27
0
    def setUpClass(cls):
        super(ActionExecutionControllerTestCaseAuthEnabled, cls).setUpClass()
        cls.action = copy.deepcopy(ACTION_1)
        headers = {
            'content-type': 'application/json',
            'X-Auth-Token': str(SYS_TOKEN.token)
        }
        post_resp = cls.app.post_json('/v1/actions',
                                      cls.action,
                                      headers=headers)
        cls.action['id'] = post_resp.json['id']

        cls.action_encrypt = copy.deepcopy(ACTION_DEFAULT_ENCRYPT)
        post_resp = cls.app.post_json('/v1/actions',
                                      cls.action_encrypt,
                                      headers=headers)
        cls.action_encrypt['id'] = post_resp.json['id']

        FixturesLoader().save_fixtures_to_db(fixtures_pack=FIXTURES_PACK,
                                             fixtures_dict=FIXTURES)

        # register datastore values which are used in this tests
        KeyValuePairAPI._setup_crypto()
        register_items = [
            {
                'name':
                'secret',
                'secret':
                True,
                'value':
                crypto_utils.symmetric_encrypt(KeyValuePairAPI.crypto_key,
                                               'foo')
            },
            {
                'name':
                'user1:secret',
                'secret':
                True,
                'scope':
                FULL_USER_SCOPE,
                'value':
                crypto_utils.symmetric_encrypt(KeyValuePairAPI.crypto_key,
                                               'bar')
            },
        ]
        cls.kvps = [
            KeyValuePair.add_or_update(KeyValuePairDB(**x))
            for x in register_items
        ]
Exemplo n.º 28
0
    def setUpClass(cls):
        super(ActionExecutionControllerTestCaseAuthEnabled, cls).setUpClass()
        cls.action = copy.deepcopy(ACTION_1)
        headers = {
            "content-type": "application/json",
            "X-Auth-Token": str(SYS_TOKEN.token),
        }
        post_resp = cls.app.post_json("/v1/actions",
                                      cls.action,
                                      headers=headers)
        cls.action["id"] = post_resp.json["id"]

        cls.action_encrypt = copy.deepcopy(ACTION_DEFAULT_ENCRYPT)
        post_resp = cls.app.post_json("/v1/actions",
                                      cls.action_encrypt,
                                      headers=headers)
        cls.action_encrypt["id"] = post_resp.json["id"]

        FixturesLoader().save_fixtures_to_db(fixtures_pack=FIXTURES_PACK,
                                             fixtures_dict=FIXTURES)

        # register datastore values which are used in this tests
        KeyValuePairAPI._setup_crypto()
        register_items = [
            {
                "name":
                "secret",
                "secret":
                True,
                "value":
                crypto_utils.symmetric_encrypt(KeyValuePairAPI.crypto_key,
                                               "foo"),
            },
            {
                "name":
                "user1:secret",
                "secret":
                True,
                "scope":
                FULL_USER_SCOPE,
                "value":
                crypto_utils.symmetric_encrypt(KeyValuePairAPI.crypto_key,
                                               "bar"),
            },
        ]
        cls.kvps = [
            KeyValuePair.add_or_update(KeyValuePairDB(**x))
            for x in register_items
        ]
Exemplo n.º 29
0
    def setUp(self):
        super(RetryPolicyTestCase, self).setUp()

        # Register runners
        runners_registrar.register_runners()

        # Register common policy types
        register_policy_types(st2actions)

        loader = FixturesLoader()
        models = loader.save_fixtures_to_db(fixtures_pack=PACK,
                                            fixtures_dict=TEST_FIXTURES)

        # Instantiate policy applicator we will use in the tests
        policy_db = models["policies"]["policy_4.yaml"]
        retry_on = policy_db.parameters["retry_on"]
        max_retry_count = policy_db.parameters["max_retry_count"]
        self.policy = ExecutionRetryPolicyApplicator(
            policy_ref="test_policy",
            policy_type="action.retry",
            retry_on=retry_on,
            max_retry_count=max_retry_count,
            delay=0,
        )
Exemplo n.º 30
0
class WorkerTestCase(DbTestCase):
    fixtures_loader = FixturesLoader()

    @classmethod
    def setUpClass(cls):
        super(WorkerTestCase, cls).setUpClass()
        models = WorkerTestCase.fixtures_loader.save_fixtures_to_db(
            fixtures_pack=FIXTURES_PACK, fixtures_dict=TEST_FIXTURES)
        WorkerTestCase.local_runnertype_db = models['runners'][
            'run-local.yaml']
        WorkerTestCase.local_action_db = models['actions']['local.yaml']

    @mock.patch.object(
        LocalShellRunner, 'run',
        mock.MagicMock(
            return_value=(action_constants.LIVEACTION_STATUS_SUCCEEDED,
                          NON_UTF8_RESULT, None)))
    def test_non_utf8_action_result_string(self):
        action_worker = actions_worker.get_worker()
        params = {'cmd': "python -c 'print \"\\x82\"'"}
        liveaction_db = self._get_liveaction_model(
            WorkerTestCase.local_action_db, params)
        liveaction_db = LiveAction.add_or_update(liveaction_db)
        execution_db = executions.create_execution_object(liveaction_db)

        try:
            action_worker._run_action(liveaction_db)
        except InvalidStringData:
            liveaction_db = LiveAction.get_by_id(liveaction_db.id)
            self.assertEqual(liveaction_db.status, "failed")
            self.assertTrue('error' in liveaction_db.result)
            self.assertTrue('traceback' in liveaction_db.result)
            execution_db = ActionExecution.get_by_id(execution_db.id)
            self.assertEqual(liveaction_db.status, "failed")

    def _get_liveaction_model(self, action_db, params):
        status = action_constants.LIVEACTION_STATUS_REQUESTED
        start_timestamp = date_utils.get_datetime_utc_now()
        action_ref = ResourceReference(name=action_db.name,
                                       pack=action_db.pack).ref
        parameters = params
        context = {'user': cfg.CONF.system_user.user}
        liveaction_db = LiveActionDB(status=status,
                                     start_timestamp=start_timestamp,
                                     action=action_ref,
                                     parameters=parameters,
                                     context=context)
        return liveaction_db
Exemplo n.º 31
0
    def test_file_partitioner(self):
        partition_file = FixturesLoader().get_fixture_file_path_abs(
            fixtures_pack=PACK, fixtures_type='sensors', fixture_name='partition_file.yaml')
        cfg.CONF.set_override(name='partition_provider',
                              override={'name': FILE_PARTITION_LOADER,
                                        'partition_file': partition_file},
                              group='sensorcontainer')

        provider = get_sensors_partitioner()
        sensors = provider.get_sensors()

        self.assertEqual(len(sensors), 2)

        sensor1 = self.models['sensors']['sensor1.yaml']
        self.assertTrue(provider.is_sensor_owner(sensor1))

        sensor3 = self.models['sensors']['sensor3.yaml']
        self.assertFalse(provider.is_sensor_owner(sensor3))
Exemplo n.º 32
0
class TestJsonConverter(unittest2.TestCase):

    fixtures_loader = FixturesLoader()
    loaded_fixtures = fixtures_loader.load_fixtures(
        fixtures_pack=DESCENDANTS_PACK, fixtures_dict=DESCENDANTS_FIXTURES)

    def test_convert(self):
        executions_list = list(self.loaded_fixtures['executions'].values())
        converter = JsonConverter()
        converted_doc = converter.convert(executions_list)
        self.assertTrue(type(converted_doc), 'string')
        reversed_doc = json.loads(converted_doc)
        self.assertListEqual(executions_list, reversed_doc)

    def test_convert_non_list(self):
        executions_dict = self.loaded_fixtures['executions']
        converter = JsonConverter()
        try:
            converter.convert(executions_dict)
            self.fail('Should have thrown exception.')
        except ValueError:
            pass
from st2common.transport.publishers import PoolPublisher
from st2common.util import date as date_utils
from st2common.util import param as param_utils
from st2common.util.config_loader import get_config
from st2tests import DbTestCase
from st2tests.fixturesloader import FixturesLoader


FIXTURES_PACK = 'generic'

TEST_MODELS = {
    'actions': ['action_4_action_context_param.yaml', 'action_system_default.yaml'],
    'runners': ['testrunner1.yaml']
}

FIXTURES = FixturesLoader().load_models(fixtures_pack=FIXTURES_PACK,
                                        fixtures_dict=TEST_MODELS)


@mock.patch.object(PoolPublisher, 'publish', mock.MagicMock())
class ParamsUtilsTest(DbTestCase):
    action_db = FIXTURES['actions']['action_4_action_context_param.yaml']
    action_system_default_db = FIXTURES['actions']['action_system_default.yaml']
    runnertype_db = FIXTURES['runners']['testrunner1.yaml']

    def test_get_finalized_params(self):
        params = {
            'actionstr': 'foo',
            'some_key_that_aint_exist_in_action_or_runner': 'bar',
            'runnerint': 555,
            'runnerimmutable': 'failed_override',
            'actionimmutable': 'failed_override'
Exemplo n.º 34
0
        WF_INVALID_SYNTAX_FILE,
        WF_INVALID_YAQL_FILE
    ],
    'actions': [
        'a1.yaml',
        'a2.yaml',
        'action1.yaml'
    ],
    'runners': [
        'testrunner1.yaml',
        'testrunner2.yaml'
    ]
}

PACK = 'generic'
LOADER = FixturesLoader()
FIXTURES = LOADER.load_fixtures(fixtures_pack=PACK, fixtures_dict=TEST_FIXTURES)
WB_PRE_XFORM_PATH = LOADER.get_fixture_file_path_abs(PACK, 'workflows', WB_PRE_XFORM_FILE)
WB_INVALID_SYNTAX_PATH = LOADER.get_fixture_file_path_abs(PACK, 'workflows', WB_INVALID_SYNTAX_FILE)
WB_INVALID_YAQL_PATH = LOADER.get_fixture_file_path_abs(PACK, 'workflows', WB_INVALID_YAQL_FILE)
WF_PRE_XFORM_PATH = LOADER.get_fixture_file_path_abs(PACK, 'workflows', WF_PRE_XFORM_FILE)
WF_NO_REQ_PARAM_PATH = LOADER.get_fixture_file_path_abs(PACK, 'workflows', WF_NO_REQ_PARAM_FILE)
WF_UNEXP_PARAM_PATH = LOADER.get_fixture_file_path_abs(PACK, 'workflows', WF_UNEXP_PARAM_FILE)
WF_INVALID_SYNTAX_PATH = LOADER.get_fixture_file_path_abs(PACK, 'workflows', WF_INVALID_SYNTAX_FILE)
WF_INVALID_YAQL_PATH = LOADER.get_fixture_file_path_abs(PACK, 'workflows', WF_INVALID_YAQL_FILE)


class MistralValidationControllerTest(FunctionalTest):

    @classmethod
    def setUpClass(cls):
class LocalShellCommandRunnerTestCase(RunnerTestCase, CleanDbTestCase):
    fixtures_loader = FixturesLoader()

    def setUp(self):
        super(LocalShellCommandRunnerTestCase, self).setUp()

        # False is a default behavior so end result should be the same
        cfg.CONF.set_override(name='stream_output',
                              group='actionrunner',
                              override=False)

    def test_shell_command_action_basic(self):
        models = self.fixtures_loader.load_models(
            fixtures_pack='generic', fixtures_dict={'actions': ['local.yaml']})
        action_db = models['actions']['local.yaml']

        runner = self._get_runner(action_db, cmd='echo 10')
        runner.pre_run()
        status, result, _ = runner.run({})
        runner.post_run(status, result)

        self.assertEquals(status, action_constants.LIVEACTION_STATUS_SUCCEEDED)
        self.assertEquals(result['stdout'], 10)

        # End result should be the same when streaming is enabled
        cfg.CONF.set_override(name='stream_output',
                              group='actionrunner',
                              override=True)

        # Verify initial state
        output_dbs = ActionExecutionOutput.get_all()
        self.assertEqual(len(output_dbs), 0)

        runner = self._get_runner(action_db, cmd='echo 10')
        runner.pre_run()
        status, result, _ = runner.run({})
        runner.post_run(status, result)

        self.assertEquals(status, action_constants.LIVEACTION_STATUS_SUCCEEDED)
        self.assertEquals(result['stdout'], 10)

        output_dbs = ActionExecutionOutput.get_all()
        self.assertEqual(len(output_dbs), 1)
        self.assertEqual(output_dbs[0].output_type, 'stdout')
        self.assertEqual(output_dbs[0].data, '10\n')

    def test_shell_script_action(self):
        models = self.fixtures_loader.load_models(
            fixtures_pack='localrunner_pack',
            fixtures_dict={'actions': ['text_gen.yml']})
        action_db = models['actions']['text_gen.yml']
        entry_point = self.fixtures_loader.get_fixture_file_path_abs(
            'localrunner_pack', 'actions', 'text_gen.py')
        runner = self._get_runner(action_db, entry_point=entry_point)
        runner.pre_run()
        status, result, _ = runner.run({'chars': 1000})
        runner.post_run(status, result)
        self.assertEquals(status, action_constants.LIVEACTION_STATUS_SUCCEEDED)
        self.assertEquals(len(result['stdout']), 1000)

    def test_timeout(self):
        models = self.fixtures_loader.load_models(
            fixtures_pack='generic', fixtures_dict={'actions': ['local.yaml']})
        action_db = models['actions']['local.yaml']
        # smaller timeout == faster tests.
        runner = self._get_runner(action_db, cmd='sleep 10', timeout=0.01)
        runner.pre_run()
        status, result, _ = runner.run({})
        runner.post_run(status, result)
        self.assertEquals(status, action_constants.LIVEACTION_STATUS_TIMED_OUT)

    @mock.patch.object(shell, 'run_command',
                       mock.MagicMock(return_value=(-15, '', '', False)))
    def test_shutdown(self):
        models = self.fixtures_loader.load_models(
            fixtures_pack='generic', fixtures_dict={'actions': ['local.yaml']})
        action_db = models['actions']['local.yaml']
        runner = self._get_runner(action_db, cmd='sleep 0.1')
        runner.pre_run()
        status, result, _ = runner.run({})
        self.assertEquals(status, action_constants.LIVEACTION_STATUS_ABANDONED)

    def test_large_stdout(self):
        models = self.fixtures_loader.load_models(
            fixtures_pack='localrunner_pack',
            fixtures_dict={'actions': ['text_gen.yml']})
        action_db = models['actions']['text_gen.yml']
        entry_point = self.fixtures_loader.get_fixture_file_path_abs(
            'localrunner_pack', 'actions', 'text_gen.py')
        runner = self._get_runner(action_db, entry_point=entry_point)
        runner.pre_run()
        char_count = 10**6  # Note 10^7 succeeds but ends up being slow.
        status, result, _ = runner.run({'chars': char_count})
        runner.post_run(status, result)
        self.assertEquals(status, action_constants.LIVEACTION_STATUS_SUCCEEDED)
        self.assertEquals(len(result['stdout']), char_count)

    def test_common_st2_env_vars_are_available_to_the_action(self):
        models = self.fixtures_loader.load_models(
            fixtures_pack='generic', fixtures_dict={'actions': ['local.yaml']})
        action_db = models['actions']['local.yaml']

        runner = self._get_runner(action_db, cmd='echo $ST2_ACTION_API_URL')
        runner.pre_run()
        status, result, _ = runner.run({})
        runner.post_run(status, result)

        self.assertEquals(status, action_constants.LIVEACTION_STATUS_SUCCEEDED)
        self.assertEqual(result['stdout'].strip(), get_full_public_api_url())

        runner = self._get_runner(action_db, cmd='echo $ST2_ACTION_AUTH_TOKEN')
        runner.pre_run()
        status, result, _ = runner.run({})
        runner.post_run(status, result)

        self.assertEquals(status, action_constants.LIVEACTION_STATUS_SUCCEEDED)
        self.assertEqual(result['stdout'].strip(), 'mock-token')

    def test_sudo_and_env_variable_preservation(self):
        # Verify that the environment environment are correctly preserved when running as a
        # root / non-system user
        # Note: This test will fail if SETENV option is not present in the sudoers file
        models = self.fixtures_loader.load_models(
            fixtures_pack='generic', fixtures_dict={'actions': ['local.yaml']})
        action_db = models['actions']['local.yaml']

        cmd = 'echo `whoami` ; echo ${VAR1}'
        env = {'VAR1': 'poniesponies'}
        runner = self._get_runner(action_db, cmd=cmd, sudo=True, env=env)
        runner.pre_run()
        status, result, _ = runner.run({})
        runner.post_run(status, result)

        self.assertEquals(status, action_constants.LIVEACTION_STATUS_SUCCEEDED)
        self.assertEqual(result['stdout'].strip(), 'root\nponiesponies')

    @mock.patch('st2common.util.green.shell.subprocess.Popen')
    @mock.patch('st2common.util.green.shell.eventlet.spawn')
    def test_action_stdout_and_stderr_is_stored_in_the_db(
            self, mock_spawn, mock_popen):
        # Feature is enabled
        cfg.CONF.set_override(name='stream_output',
                              group='actionrunner',
                              override=True)

        # Note: We need to mock spawn function so we can test everything in single event loop
        # iteration
        mock_spawn.side_effect = blocking_eventlet_spawn

        # No output to stdout and no result (implicit None)
        mock_stdout = [
            'stdout line 1\n',
            'stdout line 2\n',
        ]
        mock_stderr = ['stderr line 1\n', 'stderr line 2\n', 'stderr line 3\n']

        mock_process = mock.Mock()
        mock_process.returncode = 0
        mock_popen.return_value = mock_process
        mock_process.stdout.closed = False
        mock_process.stderr.closed = False
        mock_process.stdout.readline = make_mock_stream_readline(
            mock_process.stdout, mock_stdout, stop_counter=2)
        mock_process.stderr.readline = make_mock_stream_readline(
            mock_process.stderr, mock_stderr, stop_counter=3)

        models = self.fixtures_loader.load_models(
            fixtures_pack='generic', fixtures_dict={'actions': ['local.yaml']})
        action_db = models['actions']['local.yaml']

        runner = self._get_runner(action_db, cmd='echo $ST2_ACTION_API_URL')
        runner.pre_run()
        status, result, _ = runner.run({})
        runner.post_run(status, result)

        self.assertEquals(status, action_constants.LIVEACTION_STATUS_SUCCEEDED)

        self.assertEqual(result['stdout'], 'stdout line 1\nstdout line 2')
        self.assertEqual(result['stderr'],
                         'stderr line 1\nstderr line 2\nstderr line 3')
        self.assertEqual(result['return_code'], 0)

        # Verify stdout and stderr lines have been correctly stored in the db
        output_dbs = ActionExecutionOutput.query(output_type='stdout')
        self.assertEqual(len(output_dbs), 2)
        self.assertEqual(output_dbs[0].data, mock_stdout[0])
        self.assertEqual(output_dbs[1].data, mock_stdout[1])

        output_dbs = ActionExecutionOutput.query(output_type='stderr')
        self.assertEqual(len(output_dbs), 3)
        self.assertEqual(output_dbs[0].data, mock_stderr[0])
        self.assertEqual(output_dbs[1].data, mock_stderr[1])
        self.assertEqual(output_dbs[2].data, mock_stderr[2])

    @mock.patch('st2common.util.green.shell.subprocess.Popen')
    @mock.patch('st2common.util.green.shell.eventlet.spawn')
    def test_action_stdout_and_stderr_is_stored_in_the_db_short_running_action(
            self, mock_spawn, mock_popen):
        # Verify that we correctly retrieve all the output and wait for stdout and stderr reading
        # threads for short running actions.
        models = self.fixtures_loader.load_models(
            fixtures_pack='generic', fixtures_dict={'actions': ['local.yaml']})
        action_db = models['actions']['local.yaml']

        # Feature is enabled
        cfg.CONF.set_override(name='stream_output',
                              group='actionrunner',
                              override=True)

        # Note: We need to mock spawn function so we can test everything in single event loop
        # iteration
        mock_spawn.side_effect = blocking_eventlet_spawn

        # No output to stdout and no result (implicit None)
        mock_stdout = ['stdout line 1\n', 'stdout line 2\n']
        mock_stderr = ['stderr line 1\n', 'stderr line 2\n']

        # We add a sleep to simulate action process exiting before we finish reading data from
        mock_process = mock.Mock()
        mock_process.returncode = 0
        mock_popen.return_value = mock_process
        mock_process.stdout.closed = False
        mock_process.stderr.closed = False
        mock_process.stdout.readline = make_mock_stream_readline(
            mock_process.stdout, mock_stdout, stop_counter=2, sleep_delay=1)
        mock_process.stderr.readline = make_mock_stream_readline(
            mock_process.stderr, mock_stderr, stop_counter=2)

        for index in range(1, 4):
            mock_process.stdout.closed = False
            mock_process.stderr.closed = False

            mock_process.stdout.counter = 0
            mock_process.stderr.counter = 0

            runner = self._get_runner(action_db, cmd='echo "foobar"')
            runner.pre_run()
            status, result, _ = runner.run({})

            self.assertEquals(status,
                              action_constants.LIVEACTION_STATUS_SUCCEEDED)

            self.assertEqual(result['stdout'], 'stdout line 1\nstdout line 2')
            self.assertEqual(result['stderr'], 'stderr line 1\nstderr line 2')
            self.assertEqual(result['return_code'], 0)

            # Verify stdout and stderr lines have been correctly stored in the db
            output_dbs = ActionExecutionOutput.query(output_type='stdout')

            if index == 1:
                db_index_1 = 0
                db_index_2 = 1
            elif index == 2:
                db_index_1 = 2
                db_index_2 = 3
            elif index == 3:
                db_index_1 = 4
                db_index_2 = 5
            elif index == 4:
                db_index_1 = 6
                db_index_2 = 7

            self.assertEqual(len(output_dbs), (index * 2))
            self.assertEqual(output_dbs[db_index_1].data, mock_stdout[0])
            self.assertEqual(output_dbs[db_index_2].data, mock_stdout[1])

            output_dbs = ActionExecutionOutput.query(output_type='stderr')
            self.assertEqual(len(output_dbs), (index * 2))
            self.assertEqual(output_dbs[db_index_1].data, mock_stderr[0])
            self.assertEqual(output_dbs[db_index_2].data, mock_stderr[1])

    @staticmethod
    def _get_runner(action_db,
                    entry_point=None,
                    cmd=None,
                    on_behalf_user=None,
                    user=None,
                    kwarg_op=local_runner.DEFAULT_KWARG_OP,
                    timeout=LOCAL_RUNNER_DEFAULT_ACTION_TIMEOUT,
                    sudo=False,
                    env=None):
        runner = local_runner.LocalShellRunner(uuid.uuid4().hex)
        runner.container_service = RunnerContainerService()
        runner.execution = MOCK_EXECUTION
        runner.action = action_db
        runner.action_name = action_db.name
        runner.liveaction_id = uuid.uuid4().hex
        runner.entry_point = entry_point
        runner.runner_parameters = {
            local_runner.RUNNER_COMMAND: cmd,
            local_runner.RUNNER_SUDO: sudo,
            local_runner.RUNNER_ENV: env,
            local_runner.RUNNER_ON_BEHALF_USER: user,
            local_runner.RUNNER_KWARG_OP: kwarg_op,
            local_runner.RUNNER_TIMEOUT: timeout
        }
        runner.context = dict()
        runner.callback = dict()
        runner.libs_dir_path = None
        runner.auth_token = mock.Mock()
        runner.auth_token.token = 'mock-token'
        return runner
class LocalShellScriptRunnerTestCase(RunnerTestCase, CleanDbTestCase):
    fixtures_loader = FixturesLoader()

    def setUp(self):
        super(LocalShellScriptRunnerTestCase, self).setUp()

        # False is a default behavior so end result should be the same
        cfg.CONF.set_override(name='stream_output',
                              group='actionrunner',
                              override=False)

    def test_script_with_paramters_parameter_serialization(self):
        models = self.fixtures_loader.load_models(
            fixtures_pack='generic',
            fixtures_dict={'actions': ['local_script_with_params.yaml']})
        action_db = models['actions']['local_script_with_params.yaml']
        entry_point = os.path.join(
            get_fixtures_base_path(),
            'generic/actions/local_script_with_params.sh')

        action_parameters = {
            'param_string': 'test string',
            'param_integer': 1,
            'param_float': 2.55,
            'param_boolean': True,
            'param_list': ['a', 'b', 'c'],
            'param_object': {
                'foo': 'bar'
            }
        }

        runner = self._get_runner(action_db=action_db, entry_point=entry_point)
        runner.pre_run()
        status, result, _ = runner.run(action_parameters=action_parameters)
        runner.post_run(status, result)

        self.assertEqual(status, action_constants.LIVEACTION_STATUS_SUCCEEDED)
        self.assertTrue('PARAM_STRING=test string' in result['stdout'])
        self.assertTrue('PARAM_INTEGER=1' in result['stdout'])
        self.assertTrue('PARAM_FLOAT=2.55' in result['stdout'])
        self.assertTrue('PARAM_BOOLEAN=1' in result['stdout'])
        self.assertTrue('PARAM_LIST=a,b,c' in result['stdout'])
        self.assertTrue('PARAM_OBJECT={"foo": "bar"}' in result['stdout'])

        action_parameters = {
            'param_string': 'test string',
            'param_integer': 1,
            'param_float': 2.55,
            'param_boolean': False,
            'param_list': ['a', 'b', 'c'],
            'param_object': {
                'foo': 'bar'
            }
        }

        runner = self._get_runner(action_db=action_db, entry_point=entry_point)
        runner.pre_run()
        status, result, _ = runner.run(action_parameters=action_parameters)
        runner.post_run(status, result)

        self.assertEqual(status, action_constants.LIVEACTION_STATUS_SUCCEEDED)
        self.assertTrue('PARAM_BOOLEAN=0' in result['stdout'])

        action_parameters = {
            'param_string': '',
            'param_integer': None,
            'param_float': None,
        }

        runner = self._get_runner(action_db=action_db, entry_point=entry_point)
        runner.pre_run()
        status, result, _ = runner.run(action_parameters=action_parameters)
        runner.post_run(status, result)

        self.assertEqual(status, action_constants.LIVEACTION_STATUS_SUCCEEDED)
        self.assertTrue('PARAM_STRING=\n' in result['stdout'])
        self.assertTrue('PARAM_INTEGER=\n' in result['stdout'])
        self.assertTrue('PARAM_FLOAT=\n' in result['stdout'])

        # End result should be the same when streaming is enabled
        cfg.CONF.set_override(name='stream_output',
                              group='actionrunner',
                              override=True)

        # Verify initial state
        output_dbs = ActionExecutionOutput.get_all()
        self.assertEqual(len(output_dbs), 0)

        action_parameters = {
            'param_string': 'test string',
            'param_integer': 1,
            'param_float': 2.55,
            'param_boolean': True,
            'param_list': ['a', 'b', 'c'],
            'param_object': {
                'foo': 'bar'
            }
        }

        runner = self._get_runner(action_db=action_db, entry_point=entry_point)
        runner.pre_run()
        status, result, _ = runner.run(action_parameters=action_parameters)
        runner.post_run(status, result)

        self.assertEqual(status, action_constants.LIVEACTION_STATUS_SUCCEEDED)
        self.assertTrue('PARAM_STRING=test string' in result['stdout'])
        self.assertTrue('PARAM_INTEGER=1' in result['stdout'])
        self.assertTrue('PARAM_FLOAT=2.55' in result['stdout'])
        self.assertTrue('PARAM_BOOLEAN=1' in result['stdout'])
        self.assertTrue('PARAM_LIST=a,b,c' in result['stdout'])
        self.assertTrue('PARAM_OBJECT={"foo": "bar"}' in result['stdout'])

        output_dbs = ActionExecutionOutput.query(output_type='stdout')
        self.assertEqual(len(output_dbs), 6)
        self.assertEqual(output_dbs[0].data, 'PARAM_STRING=test string\n')
        self.assertEqual(output_dbs[5].data, 'PARAM_OBJECT={"foo": "bar"}\n')

        output_dbs = ActionExecutionOutput.query(output_type='stderr')
        self.assertEqual(len(output_dbs), 0)

    @mock.patch('st2common.util.green.shell.subprocess.Popen')
    @mock.patch('st2common.util.green.shell.eventlet.spawn')
    def test_action_stdout_and_stderr_is_stored_in_the_db(
            self, mock_spawn, mock_popen):
        # Feature is enabled
        cfg.CONF.set_override(name='stream_output',
                              group='actionrunner',
                              override=True)

        # Note: We need to mock spawn function so we can test everything in single event loop
        # iteration
        mock_spawn.side_effect = blocking_eventlet_spawn

        # No output to stdout and no result (implicit None)
        mock_stdout = [
            'stdout line 1\n', 'stdout line 2\n', 'stdout line 3\n',
            'stdout line 4\n'
        ]
        mock_stderr = ['stderr line 1\n', 'stderr line 2\n', 'stderr line 3\n']

        mock_process = mock.Mock()
        mock_process.returncode = 0
        mock_popen.return_value = mock_process
        mock_process.stdout.closed = False
        mock_process.stderr.closed = False
        mock_process.stdout.readline = make_mock_stream_readline(
            mock_process.stdout, mock_stdout, stop_counter=4)
        mock_process.stderr.readline = make_mock_stream_readline(
            mock_process.stderr, mock_stderr, stop_counter=3)

        models = self.fixtures_loader.load_models(
            fixtures_pack='generic',
            fixtures_dict={'actions': ['local_script_with_params.yaml']})
        action_db = models['actions']['local_script_with_params.yaml']
        entry_point = os.path.join(
            get_fixtures_base_path(),
            'generic/actions/local_script_with_params.sh')

        action_parameters = {
            'param_string': 'test string',
            'param_integer': 1,
            'param_float': 2.55,
            'param_boolean': True,
            'param_list': ['a', 'b', 'c'],
            'param_object': {
                'foo': 'bar'
            }
        }

        runner = self._get_runner(action_db=action_db, entry_point=entry_point)
        runner.pre_run()
        status, result, _ = runner.run(action_parameters=action_parameters)
        runner.post_run(status, result)

        self.assertEqual(
            result['stdout'],
            'stdout line 1\nstdout line 2\nstdout line 3\nstdout line 4')
        self.assertEqual(result['stderr'],
                         'stderr line 1\nstderr line 2\nstderr line 3')
        self.assertEqual(result['return_code'], 0)

        # Verify stdout and stderr lines have been correctly stored in the db
        output_dbs = ActionExecutionOutput.query(output_type='stdout')
        self.assertEqual(len(output_dbs), 4)
        self.assertEqual(output_dbs[0].data, mock_stdout[0])
        self.assertEqual(output_dbs[1].data, mock_stdout[1])
        self.assertEqual(output_dbs[2].data, mock_stdout[2])
        self.assertEqual(output_dbs[3].data, mock_stdout[3])

        output_dbs = ActionExecutionOutput.query(output_type='stderr')
        self.assertEqual(len(output_dbs), 3)
        self.assertEqual(output_dbs[0].data, mock_stderr[0])
        self.assertEqual(output_dbs[1].data, mock_stderr[1])
        self.assertEqual(output_dbs[2].data, mock_stderr[2])

    def _get_runner(self, action_db, entry_point):
        runner = local_runner.LocalShellRunner(uuid.uuid4().hex)
        runner.container_service = RunnerContainerService()
        runner.execution = MOCK_EXECUTION
        runner.action = action_db
        runner.action_name = action_db.name
        runner.liveaction_id = uuid.uuid4().hex
        runner.entry_point = entry_point
        runner.runner_parameters = {}
        runner.context = dict()
        runner.callback = dict()
        runner.libs_dir_path = None
        runner.auth_token = mock.Mock()
        runner.auth_token.token = 'mock-token'
        return runner
Exemplo n.º 37
0
    def setUpClass(cls):
        super(TestTimersController, cls).setUpClass()

        loader = FixturesLoader()
        TestTimersController.MODELS = loader.save_fixtures_to_db(fixtures_pack=PACK, fixtures_dict=FIXTURES)["triggers"]
Exemplo n.º 38
0
TEST_FIXTURES = {
    'actions': [
        'action1.yaml'
    ],
    'policytypes': [
        'fake_policy_type_1.yaml',
        'fake_policy_type_2.yaml'
    ],
    'policies': [
        'policy_1.yaml',
        'policy_2.yaml'
    ]
}

PACK = 'generic'
LOADER = FixturesLoader()
FIXTURES = LOADER.load_fixtures(fixtures_pack=PACK, fixtures_dict=TEST_FIXTURES)


@mock.patch.object(
    CUDPublisher, 'publish_update',
    mock.MagicMock(side_effect=MockExecutionPublisher.publish_update))
@mock.patch.object(
    CUDPublisher, 'publish_create',
    mock.MagicMock(return_value=None))
@mock.patch.object(
    LiveActionPublisher, 'publish_state',
    mock.MagicMock(side_effect=MockLiveActionPublisher.publish_state))
@mock.patch('st2common.runners.base.get_runner', mock.Mock(return_value=runner.get_runner()))
@mock.patch('st2actions.container.base.get_runner', mock.Mock(return_value=runner.get_runner()))
class SchedulingPolicyTest(ExecutionDbTestCase):
Exemplo n.º 39
0
class WorkerTestCase(DbTestCase):
    fixtures_loader = FixturesLoader()

    @classmethod
    def setUpClass(cls):
        super(WorkerTestCase, cls).setUpClass()
        models = WorkerTestCase.fixtures_loader.save_fixtures_to_db(
            fixtures_pack=FIXTURES_PACK, fixtures_dict=TEST_FIXTURES)
        WorkerTestCase.local_runnertype_db = models['runners'][
            'run-local.yaml']
        WorkerTestCase.local_action_db = models['actions']['local.yaml']

    def _get_liveaction_model(self, action_db, params):
        status = action_constants.LIVEACTION_STATUS_REQUESTED
        start_timestamp = date_utils.get_datetime_utc_now()
        action_ref = ResourceReference(name=action_db.name,
                                       pack=action_db.pack).ref
        parameters = params
        context = {'user': cfg.CONF.system_user.user}
        liveaction_db = LiveActionDB(status=status,
                                     start_timestamp=start_timestamp,
                                     action=action_ref,
                                     parameters=parameters,
                                     context=context)
        return liveaction_db

    @mock.patch.object(
        LocalShellCommandRunner, 'run',
        mock.MagicMock(
            return_value=(action_constants.LIVEACTION_STATUS_SUCCEEDED,
                          NON_UTF8_RESULT, None)))
    def test_non_utf8_action_result_string(self):
        action_worker = actions_worker.get_worker()
        params = {'cmd': "python -c 'print \"\\x82\"'"}
        liveaction_db = self._get_liveaction_model(
            WorkerTestCase.local_action_db, params)
        liveaction_db = LiveAction.add_or_update(liveaction_db)
        execution_db = executions.create_execution_object(liveaction_db)

        try:
            action_worker._run_action(liveaction_db)
        except InvalidStringData:
            liveaction_db = LiveAction.get_by_id(liveaction_db.id)
            self.assertEqual(liveaction_db.status,
                             action_constants.LIVEACTION_STATUS_FAILED)
            self.assertTrue('error' in liveaction_db.result)
            self.assertTrue('traceback' in liveaction_db.result)
            execution_db = ActionExecution.get_by_id(execution_db.id)
            self.assertEqual(liveaction_db.status,
                             action_constants.LIVEACTION_STATUS_FAILED)

    def test_worker_shutdown(self):
        action_worker = actions_worker.get_worker()
        temp_file = None

        # Create a temporary file that is deleted when the file is closed and then set up an
        # action to wait for this file to be deleted. This allows this test to run the action
        # over a separate thread, run the shutdown sequence on the main thread, and then let
        # the local runner to exit gracefully and allow _run_action to finish execution.
        with tempfile.NamedTemporaryFile() as fp:
            temp_file = fp.name
            self.assertIsNotNone(temp_file)
            self.assertTrue(os.path.isfile(temp_file))

            # Launch the action execution in a separate thread.
            params = {
                'cmd': 'while [ -e \'%s\' ]; do sleep 0.1; done' % temp_file
            }
            liveaction_db = self._get_liveaction_model(
                WorkerTestCase.local_action_db, params)
            liveaction_db = LiveAction.add_or_update(liveaction_db)
            executions.create_execution_object(liveaction_db)
            runner_thread = eventlet.spawn(action_worker._run_action,
                                           liveaction_db)

            # Wait for the worker up to 10s to add the liveaction to _running_liveactions.
            for i in range(0, int(10 / 0.1)):
                eventlet.sleep(0.1)
                if len(action_worker._running_liveactions) > 0:
                    break

            self.assertEqual(len(action_worker._running_liveactions), 1)

            # Shutdown the worker to trigger the abandon process.
            action_worker.shutdown()
            liveaction_db = LiveAction.get_by_id(liveaction_db.id)

            # Verify that _running_liveactions is empty and the liveaction is abandoned.
            self.assertEqual(len(action_worker._running_liveactions), 0)
            self.assertEqual(liveaction_db.status,
                             action_constants.LIVEACTION_STATUS_ABANDONED)

        # Make sure the temporary file has been deleted.
        self.assertFalse(os.path.isfile(temp_file))

        # Wait for the local runner to complete. This will activate the finally block in
        # _run_action but will not result in KeyError because the discard method is used to
        # to remove the liveaction from _running_liveactions.
        runner_thread.wait()
Exemplo n.º 40
0
class ActionExecutionRBACControllerTestCase(
        BaseActionExecutionControllerTestCase, APIControllerWithRBACTestCase):

    fixtures_loader = FixturesLoader()

    @mock.patch.object(action_validator, 'validate_action',
                       mock.MagicMock(return_value=True))
    def setUp(self):
        super(ActionExecutionRBACControllerTestCase, self).setUp()

        self.fixtures_loader.save_fixtures_to_db(fixtures_pack=FIXTURES_PACK,
                                                 fixtures_dict=TEST_FIXTURES)

        # Insert mock users, roles and assignments

        # Users
        user_1_db = UserDB(name='multiple_roles')
        user_1_db = User.add_or_update(user_1_db)
        self.users['multiple_roles'] = user_1_db

        # Roles
        roles = ['role_1', 'role_2', 'role_3']
        for role in roles:
            role_db = RoleDB(name=role)
            Role.add_or_update(role_db)

        # Role assignments
        user_db = self.users['multiple_roles']
        role_assignment_db = UserRoleAssignmentDB(
            user=user_db.name,
            role='admin',
            source='assignments/%s.yaml' % user_db.name)
        UserRoleAssignment.add_or_update(role_assignment_db)

        for role in roles:
            role_assignment_db = UserRoleAssignmentDB(
                user=user_db.name,
                role=role,
                source='assignments/%s.yaml' % user_db.name)
            UserRoleAssignment.add_or_update(role_assignment_db)

    def test_post_rbac_info_in_context_success(self):
        # When RBAC is enabled, additional RBAC related info should be included in action_context
        data = {
            'action': 'wolfpack.action-1',
            'parameters': {
                'actionstr': 'foo'
            }
        }

        # User with one role assignment
        user_db = self.users['admin']
        self.use_user(user_db)

        resp = self._do_post(data)
        self.assertEqual(resp.status_int, 201)

        expected_context = {
            'pack': 'wolfpack',
            'user': '******',
            'rbac': {
                'user': '******',
                'roles': ['admin']
            }
        }

        self.assertEqual(resp.json['context'], expected_context)

        # User with multiple role assignments
        user_db = self.users['multiple_roles']
        self.use_user(user_db)

        resp = self._do_post(data)
        self.assertEqual(resp.status_int, 201)

        expected_context = {
            'pack': 'wolfpack',
            'user': '******',
            'rbac': {
                'user': '******',
                'roles': ['admin', 'role_1', 'role_2', 'role_3']
            }
        }

        self.assertEqual(resp.json['context'], expected_context)

    def test_get_all_limit_minus_one(self):
        user_db = self.users['observer']
        self.use_user(user_db)

        resp = self.app.get('/v1/actionexecutions?limit=-1',
                            expect_errors=True)
        self.assertEqual(resp.status_code, http_client.FORBIDDEN)

        user_db = self.users['admin']
        self.use_user(user_db)

        resp = self.app.get('/v1/actionexecutions?limit=-1')
        self.assertEqual(resp.status_code, http_client.OK)
    ],
    'actions': [
        'local.yaml',
        'a1.yaml',
        'a2.yaml',
        'action1.yaml'
    ],
    'runners': [
        'run-local.yaml',
        'testrunner1.yaml',
        'testrunner2.yaml'
    ]
}

PACK = 'generic'
LOADER = FixturesLoader()
FIXTURES = LOADER.load_fixtures(fixtures_pack=PACK, fixtures_dict=TEST_FIXTURES)
WB_PRE_XFORM_PATH = LOADER.get_fixture_file_path_abs(PACK, 'workflows', WB_PRE_XFORM_FILE)
WB_PRE_XFORM_DEF = FIXTURES['workflows'][WB_PRE_XFORM_FILE]
WB_POST_XFORM_PATH = LOADER.get_fixture_file_path_abs(PACK, 'workflows', WB_POST_XFORM_FILE)
WB_POST_XFORM_DEF = FIXTURES['workflows'][WB_POST_XFORM_FILE]
WF_PRE_XFORM_PATH = LOADER.get_fixture_file_path_abs(PACK, 'workflows', WF_PRE_XFORM_FILE)
WF_PRE_XFORM_DEF = FIXTURES['workflows'][WF_PRE_XFORM_FILE]
WF_POST_XFORM_PATH = LOADER.get_fixture_file_path_abs(PACK, 'workflows', WF_POST_XFORM_FILE)
WF_POST_XFORM_DEF = FIXTURES['workflows'][WF_POST_XFORM_FILE]
WF_JINJA_ST2KV_PRE_XFORM_PATH = LOADER.get_fixture_file_path_abs(
    PACK, 'workflows', WF_JINJA_ST2KV_PRE_XFORM_FILE)
WF_JINJA_ST2KV_PRE_XFORM_DEF = FIXTURES['workflows'][WF_JINJA_ST2KV_PRE_XFORM_FILE]
WF_JINJA_ST2KV_POST_XFORM_PATH = LOADER.get_fixture_file_path_abs(
    PACK, 'workflows', WF_JINJA_ST2KV_POST_XFORM_FILE)
WF_JINJA_MIXED_CTX1_DEF = FIXTURES['workflows'][WF_JINJA_MIXED_CTX1_FILE]
Exemplo n.º 42
0
 def setUp(self):
     super(TestPurgeExecutions, self).setUp()
     fixtures_loader = FixturesLoader()
     self.models = fixtures_loader.load_models(fixtures_pack='generic',
                                               fixtures_dict=TEST_FIXTURES)
Exemplo n.º 43
0
 def setUpClass(cls):
     super(TestApiKeyBasedAuth, cls).setUpClass()
     models = FixturesLoader().save_fixtures_to_db(
         fixtures_pack=FIXTURES_PACK, fixtures_dict=TEST_MODELS)
     cls.apikey1 = models['apikeys']['apikey1.yaml']
     cls.apikey_disabled = models['apikeys']['apikey_disabled.yaml']
Exemplo n.º 44
0
from oslo_config import cfg

import st2actions.resultstracker.resultstracker as results_tracker
from st2common.constants import action as action_constants
from st2common.exceptions.db import StackStormDBObjectNotFoundError
from st2common.persistence.action import Action
from st2common.persistence.executionstate import ActionExecutionState
from st2common.persistence.liveaction import LiveAction
from st2common.runners import utils as runners_utils
from st2common.services import executions
from st2common.util import action_db as action_db_utils
from st2tests.base import (DbTestCase, EventletTestCase)
from st2tests.fixturesloader import FixturesLoader

FIXTURES_LOADER = FixturesLoader()
FIXTURES_PACK = 'generic'
FIXTURES = {
    'actionstates': ['state1.yaml', 'state2.yaml'],
    'liveactions': ['liveaction1.yaml', 'liveaction2.yaml']
}


@mock.patch.object(executions, 'update_execution',
                   mock.MagicMock(return_value=None))
@mock.patch.object(LiveAction, 'publish_update',
                   mock.MagicMock(return_value=None))
class ResultsTrackerTests(EventletTestCase, DbTestCase):
    states = None
    models = None
    liveactions = None
Exemplo n.º 45
0
 def setUpClass(cls):
     super(TestExportWorker, cls).setUpClass()
     fixtures_loader = FixturesLoader()
     loaded_fixtures = fixtures_loader.save_fixtures_to_db(fixtures_pack=DESCENDANTS_PACK,
                                                           fixtures_dict=DESCENDANTS_FIXTURES)
     TestExportWorker.saved_executions = loaded_fixtures['executions']
Exemplo n.º 46
0
class TraceControllerRBACTestCase(APIControllerWithRBACTestCase):
    fixtures_loader = FixturesLoader()

    def setUp(self):
        super(TraceControllerRBACTestCase, self).setUp()
        self.models = self.fixtures_loader.save_fixtures_to_db(fixtures_pack=FIXTURES_PACK,
                                                               fixtures_dict=TEST_FIXTURES)

        file_name = 'trace_for_test_enforce.yaml'
        TraceControllerRBACTestCase.TRACE_1 = self.fixtures_loader.load_fixtures(
            fixtures_pack=FIXTURES_PACK,
            fixtures_dict={'traces': [file_name]})['traces'][file_name]

        file_name = 'trace_for_test_enforce_2.yaml'
        TraceControllerRBACTestCase.TRACE_1 = self.fixtures_loader.load_fixtures(
            fixtures_pack=FIXTURES_PACK,
            fixtures_dict={'traces': [file_name]})['traces'][file_name]

        file_name = 'trace_for_test_enforce_3.yaml'
        TraceControllerRBACTestCase.TRACE_1 = self.fixtures_loader.load_fixtures(
            fixtures_pack=FIXTURES_PACK,
            fixtures_dict={'traces': [file_name]})['traces'][file_name]

        # Insert mock users, roles and assignments

        # Users
        user_1_db = UserDB(name='trace_list')
        user_1_db = User.add_or_update(user_1_db)
        self.users['trace_list'] = user_1_db

        user_2_db = UserDB(name='trace_view')
        user_2_db = User.add_or_update(user_2_db)
        self.users['trace_view'] = user_2_db

        # Roles
        # trace_list
        grant_db = PermissionGrantDB(resource_uid=None,
                                     resource_type=ResourceType.TRACE,
                                     permission_types=[PermissionType.TRACE_LIST])
        grant_db = PermissionGrant.add_or_update(grant_db)
        permission_grants = [str(grant_db.id)]
        role_1_db = RoleDB(name='trace_list', permission_grants=permission_grants)
        role_1_db = Role.add_or_update(role_1_db)
        self.roles['trace_list'] = role_1_db

        # trace_view on trace 1
        trace_uid = self.models['traces']['trace_for_test_enforce.yaml'].get_uid()
        grant_db = PermissionGrantDB(resource_uid=trace_uid,
                                     resource_type=ResourceType.TRACE,
                                     permission_types=[PermissionType.TRACE_VIEW])
        grant_db = PermissionGrant.add_or_update(grant_db)
        permission_grants = [str(grant_db.id)]
        role_1_db = RoleDB(name='trace_view', permission_grants=permission_grants)
        role_1_db = Role.add_or_update(role_1_db)
        self.roles['trace_view'] = role_1_db

        # Role assignments
        role_assignment_db = UserRoleAssignmentDB(
            user=self.users['trace_list'].name,
            role=self.roles['trace_list'].name)
        UserRoleAssignment.add_or_update(role_assignment_db)

        role_assignment_db = UserRoleAssignmentDB(
            user=self.users['trace_view'].name,
            role=self.roles['trace_view'].name)
        UserRoleAssignment.add_or_update(role_assignment_db)

    def test_get_all_no_permissions(self):
        user_db = self.users['no_permissions']
        self.use_user(user_db)

        resp = self.app.get('/v1/traces', expect_errors=True)
        expected_msg = ('User "no_permissions" doesn\'t have required permission "trace_list"')
        self.assertEqual(resp.status_code, httplib.FORBIDDEN)
        self.assertEqual(resp.json['faultstring'], expected_msg)

    def test_get_one_no_permissions(self):
        user_db = self.users['no_permissions']
        self.use_user(user_db)

        trace_id = self.models['traces']['trace_for_test_enforce.yaml'].id
        trace_uid = self.models['traces']['trace_for_test_enforce.yaml'].get_uid()
        resp = self.app.get('/v1/traces/%s' % (trace_id), expect_errors=True)
        expected_msg = ('User "no_permissions" doesn\'t have required permission "trace_view"'
                        ' on resource "%s"' % (trace_uid))
        self.assertEqual(resp.status_code, httplib.FORBIDDEN)
        self.assertEqual(resp.json['faultstring'], expected_msg)

    def test_get_all_permission_success_get_one_no_permission_failure(self):
        user_db = self.users['trace_list']
        self.use_user(user_db)

        # trace_list permission, but no trace_view permission
        resp = self.app.get('/v1/traces')
        self.assertEqual(resp.status_code, httplib.OK)
        self.assertEqual(len(resp.json), 3)

        trace_id = self.models['traces']['trace_for_test_enforce.yaml'].id
        trace_uid = self.models['traces']['trace_for_test_enforce.yaml'].get_uid()
        resp = self.app.get('/v1/traces/%s' % (trace_id), expect_errors=True)
        expected_msg = ('User "trace_list" doesn\'t have required permission "trace_view"'
                        ' on resource "%s"' % (trace_uid))
        self.assertEqual(resp.status_code, httplib.FORBIDDEN)
        self.assertEqual(resp.json['faultstring'], expected_msg)

    def test_get_one_permission_success_get_all_no_permission_failure(self):
        user_db = self.users['trace_view']
        self.use_user(user_db)

        # trace_view permission, but no trace_list permission
        trace_id = self.models['traces']['trace_for_test_enforce.yaml'].id
        trace_uid = self.models['traces']['trace_for_test_enforce.yaml'].get_uid()

        resp = self.app.get('/v1/traces/%s' % (trace_id))
        self.assertEqual(resp.status_code, httplib.OK)
        self.assertEqual(resp.json['uid'], trace_uid)

        resp = self.app.get('/v1/traces', expect_errors=True)
        expected_msg = ('User "trace_view" doesn\'t have required permission "trace_list"')
        self.assertEqual(resp.status_code, httplib.FORBIDDEN)
        self.assertEqual(resp.json['faultstring'], expected_msg)
Exemplo n.º 47
0
    def setUpClass(cls):
        super(PolicyTest, cls).setUpClass()

        loader = FixturesLoader()
        loader.save_fixtures_to_db(fixtures_pack=PACK, fixtures_dict=TEST_FIXTURES)
Exemplo n.º 48
0
class ActionViewsControllerRBACTestCase(
        APIControllerWithRBACTestCase,
        APIControllerWithIncludeAndExcludeFilterTestCase):

    # Attributes used by APIControllerWithIncludeAndExcludeFilterTestCase
    get_all_path = '/v1/actions/views/overview'
    controller_cls = OverviewController
    include_attribute_field_name = 'entry_point'
    exclude_attribute_field_name = 'parameters'
    rbac_enabled = True

    fixtures_loader = FixturesLoader()

    def setUp(self):
        super(ActionViewsControllerRBACTestCase, self).setUp()
        self.models = self.fixtures_loader.save_fixtures_to_db(
            fixtures_pack=FIXTURES_PACK, fixtures_dict=TEST_FIXTURES)

        file_name = 'a1.yaml'
        ActionViewsControllerRBACTestCase.ACTION_1 = self.fixtures_loader.load_fixtures(
            fixtures_pack=FIXTURES_PACK,
            fixtures_dict={'actions': [file_name]})['actions'][file_name]

        file_name = 'a2.yaml'
        ActionViewsControllerRBACTestCase.ACTION_1 = self.fixtures_loader.load_fixtures(
            fixtures_pack=FIXTURES_PACK,
            fixtures_dict={'actions': [file_name]})['actions'][file_name]

        # Insert mock users, roles and assignments

        # Users
        user_2_db = UserDB(name='action_view_a1')
        user_2_db = User.add_or_update(user_2_db)
        self.users['action_view_a1'] = user_2_db

        # Roles

        # action_view on a1
        action_uid = self.models['actions']['a1.yaml'].get_uid()
        grant_db = PermissionGrantDB(
            resource_uid=action_uid,
            resource_type=ResourceType.ACTION,
            permission_types=[PermissionType.ACTION_VIEW])
        grant_db = PermissionGrant.add_or_update(grant_db)
        permission_grants = [str(grant_db.id)]
        role_1_db = RoleDB(name='action_view_a1',
                           permission_grants=permission_grants)
        role_1_db = Role.add_or_update(role_1_db)
        self.roles['action_view_a1'] = role_1_db

        # Role assignments
        role_assignment_db = UserRoleAssignmentDB(
            user=self.users['action_view_a1'].name,
            role=self.roles['action_view_a1'].name,
            source='assignments/%s.yaml' % self.users['action_view_a1'].name)
        UserRoleAssignment.add_or_update(role_assignment_db)

    def test_get_entry_point_view_no_permission(self):
        user_db = self.users['no_permissions']
        self.use_user(user_db)

        action_id = self.models['actions']['a1.yaml'].id
        action_uid = self.models['actions']['a1.yaml'].get_uid()
        resp = self.app.get('/v1/actions/views/entry_point/%s' % (action_id),
                            expect_errors=True)
        expected_msg = (
            'User "no_permissions" doesn\'t have required permission "action_view"'
            ' on resource "%s"' % (action_uid))
        self.assertEqual(resp.status_code, http_client.FORBIDDEN)
        self.assertEqual(resp.json['faultstring'], expected_msg)

    @mock.patch.object(content_utils, 'get_entry_point_abs_path',
                       mock.MagicMock(return_value='/path/to/file'))
    @mock.patch(mock_open_name,
                mock.mock_open(read_data='file content'),
                create=True)
    def test_get_entry_point_view_success(self):
        user_db = self.users['action_view_a1']
        self.use_user(user_db)

        # action_view on a1, but no permissions on a2
        action_id = self.models['actions']['a1.yaml'].id
        resp = self.app.get('/v1/actions/views/entry_point/%s' % (action_id))
        self.assertEqual(resp.status_code, http_client.OK)

        action_id = self.models['actions']['a2.yaml'].id
        action_uid = self.models['actions']['a2.yaml'].get_uid()
        resp = self.app.get('/v1/actions/views/entry_point/%s' % (action_id),
                            expect_errors=True)
        expected_msg = (
            'User "action_view_a1" doesn\'t have required permission "action_view"'
            ' on resource "%s"' % (action_uid))
        self.assertEqual(resp.status_code, http_client.FORBIDDEN)
        self.assertEqual(resp.json['faultstring'], expected_msg)

    def test_get_parameters_view_no_permission(self):
        user_db = self.users['no_permissions']
        self.use_user(user_db)

        action_id = self.models['actions']['a1.yaml'].id
        action_uid = self.models['actions']['a1.yaml'].get_uid()
        resp = self.app.get('/v1/actions/views/parameters/%s' % (action_id),
                            expect_errors=True)
        expected_msg = (
            'User "no_permissions" doesn\'t have required permission "action_view"'
            ' on resource "%s"' % (action_uid))
        self.assertEqual(resp.status_code, http_client.FORBIDDEN)
        self.assertEqual(resp.json['faultstring'], expected_msg)

    def test_get_parameters_view_success(self):
        user_db = self.users['action_view_a1']
        self.use_user(user_db)

        # action_view on a1, but no permissions on a2
        action_id = self.models['actions']['a1.yaml'].id
        resp = self.app.get('/v1/actions/views/parameters/%s' % (action_id))
        self.assertEqual(resp.status_code, http_client.OK)

        action_id = self.models['actions']['a2.yaml'].id
        action_uid = self.models['actions']['a2.yaml'].get_uid()
        resp = self.app.get('/v1/actions/views/parameters/%s' % (action_id),
                            expect_errors=True)
        expected_msg = (
            'User "action_view_a1" doesn\'t have required permission "action_view"'
            ' on resource "%s"' % (action_uid))
        self.assertEqual(resp.status_code, http_client.FORBIDDEN)
        self.assertEqual(resp.json['faultstring'], expected_msg)

    def _insert_mock_models(self):
        action_ids = [
            action['id'] for action in self.models['actions'].values()
        ]
        return action_ids
Exemplo n.º 49
0
        'workflow_v2_many_workflows.yaml'
    ],
    'actions': [
        'workbook_v2.yaml',
        'workbook_v2_many_workflows.yaml',
        'workbook_v2_many_workflows_no_default.yaml',
        'workflow_v2.yaml',
        'workflow_v2_many_workflows.yaml',
        'workbook_v2_name_mismatch.yaml',
        'workflow_v2_name_mismatch.yaml',
        'local.yaml'
    ]
}

PACK = 'generic'
LOADER = FixturesLoader()
FIXTURES = LOADER.load_fixtures(fixtures_pack=PACK, fixtures_dict=TEST_FIXTURES)

MISTRAL_EXECUTION = {'id': str(uuid.uuid4()), 'state': 'RUNNING', 'workflow_name': None}

# Workbook with a single workflow
WB1_YAML_FILE_NAME = TEST_FIXTURES['workflows'][0]
WB1_YAML_FILE_PATH = LOADER.get_fixture_file_path_abs(PACK, 'workflows', WB1_YAML_FILE_NAME)
WB1_SPEC = FIXTURES['workflows'][WB1_YAML_FILE_NAME]
WB1_YAML = yaml.safe_dump(WB1_SPEC, default_flow_style=False)
WB1_NAME = '%s.%s' % (PACK, WB1_YAML_FILE_NAME.replace('.yaml', ''))
WB1 = workbooks.Workbook(None, {'name': WB1_NAME, 'definition': WB1_YAML})
WB1_OLD = workbooks.Workbook(None, {'name': WB1_NAME, 'definition': ''})
WB1_EXEC = copy.deepcopy(MISTRAL_EXECUTION)
WB1_EXEC['workflow_name'] = WB1_NAME
    def setUpClass(cls):
        super(PolicyTestCase, cls).setUpClass()

        loader = FixturesLoader()
        loader.save_fixtures_to_db(fixtures_pack=PACK,
                                   fixtures_dict=TEST_FIXTURES)
WB_PRE_XFORM_FILE = 'wb_pre_xform.yaml'
WB_POST_XFORM_FILE = 'wb_post_xform.yaml'
WF_PRE_XFORM_FILE = 'wf_pre_xform.yaml'
WF_POST_XFORM_FILE = 'wf_post_xform.yaml'
TEST_FIXTURES = {
    'workflows': [
        WB_PRE_XFORM_FILE,
        WB_POST_XFORM_FILE,
        WF_PRE_XFORM_FILE,
        WF_POST_XFORM_FILE
    ],
    'actions': ['local.yaml']
}

PACK = 'generic'
LOADER = FixturesLoader()
FIXTURES = LOADER.load_fixtures(fixtures_pack=PACK, fixtures_dict=TEST_FIXTURES)
WB_PRE_XFORM_PATH = LOADER.get_fixture_file_path_abs(PACK, 'workflows', WB_PRE_XFORM_FILE)
WB_PRE_XFORM_DEF = FIXTURES['workflows'][WB_PRE_XFORM_FILE]
WB_POST_XFORM_PATH = LOADER.get_fixture_file_path_abs(PACK, 'workflows', WB_POST_XFORM_FILE)
WB_POST_XFORM_DEF = FIXTURES['workflows'][WB_POST_XFORM_FILE]
WF_PRE_XFORM_PATH = LOADER.get_fixture_file_path_abs(PACK, 'workflows', WF_PRE_XFORM_FILE)
WF_PRE_XFORM_DEF = FIXTURES['workflows'][WF_PRE_XFORM_FILE]
WF_POST_XFORM_PATH = LOADER.get_fixture_file_path_abs(PACK, 'workflows', WF_POST_XFORM_FILE)
WF_POST_XFORM_DEF = FIXTURES['workflows'][WF_POST_XFORM_FILE]


def _read_file_content(path):
    with open(path, 'r') as f:
        return f.read()