def test_adhoc_actions(self):
        provider = adhoc.AdHocActionProvider()

        action_descs = provider.find_all()

        self.assertEqual(0, len(action_descs))

        action_txt = """
        version: '2.0'

        my_adhoc_action:
          base: std.echo
          base-input:
            output: "<% $.s1 %>+<% $.s2 %>"
          input:
            - s1: "a"
            - s2
          output: "<% $ %> and <% $ %>"
        """

        adhoc_action_service.create_actions(action_txt)

        action_descs = provider.find_all()

        self.assertEqual(1, len(action_descs))

        action_desc = action_descs[0]

        self.assertEqual('my_adhoc_action', action_desc.name)
        self.assertEqual(action_txt, action_desc.definition)
Ejemplo n.º 2
0
    def test_get_all_pagination(self):
        # Create an adhoc action for the purpose of the test.
        adhoc_actions.create_actions(ADHOC_ACTION_YAML)

        resp = self.app.get('/v2/actions?limit=1&sort_keys=id,name')

        self.assertEqual(200, resp.status_int)
        self.assertIn('next', resp.json)
        self.assertEqual(1, len(resp.json['actions']))

        self.check_adhoc_action_json(resp.json['actions'][0])

        param_dict = utils.get_dict_from_string(
            resp.json['next'].split('?')[1],
            delimiter='&'
        )

        action_def = db_api.get_action_definition('my_action')

        # TODO(rakhmerov): In this case we can't use IDs for marker because
        # in general we don't identify action descriptors with IDs.
        expected_dict = {
            'marker': action_def.id,
            'limit': 1,
            'sort_keys': 'id,name',
            'sort_dirs': 'asc,asc'
        }

        self.assertTrue(
            set(expected_dict.items()).issubset(set(param_dict.items()))
        )
Ejemplo n.º 3
0
    def test_get_all(self):
        # Create an adhoc action for the purpose of the test.
        adhoc_actions.create_actions(ADHOC_ACTION_YAML)

        resp = self.app.get('/v2/actions')

        self.assertEqual(200, resp.status_int)

        actions_json = resp.json['actions']

        # There will be 'std.' actions and the one we've just created.
        self.assertGreater(len(actions_json), 1)

        # Let's check some of the well-known 'std.' actions.
        self._assert_single_item(actions_json, name='std.echo')
        self._assert_single_item(actions_json, name='std.ssh')
        self._assert_single_item(actions_json, name='std.fail')
        self._assert_single_item(actions_json, name='std.noop')
        self._assert_single_item(actions_json, name='std.async_noop')

        # Now let's check the ad-hoc action data.
        adhoc_action_json = self._assert_single_item(
            actions_json,
            name='my_action'
        )

        self.check_adhoc_action_json(adhoc_action_json)
Ejemplo n.º 4
0
    def test_get_all_operational_error(self, mocked_get_all):
        # Create an adhoc action for the purpose of the test.
        adhoc_actions.create_actions(ADHOC_ACTION_YAML)

        action_def = db_api.get_action_definition('my_action')

        mocked_get_all.side_effect = [
            # Emulating DB OperationalError
            sa.exc.OperationalError('Mock', 'mock', 'mock'),
            [action_def]  # Successful run
        ]

        resp = self.app.get('/v2/actions')

        actions_json = resp.json['actions']

        # There will be 'std.' actions and the one we've just created.
        self.assertGreater(len(actions_json), 1)

        # Let's check some of the well-known 'std.' actions.
        self._assert_single_item(actions_json, name='std.echo')
        self._assert_single_item(actions_json, name='std.ssh')
        self._assert_single_item(actions_json, name='std.fail')
        self._assert_single_item(actions_json, name='std.noop')
        self._assert_single_item(actions_json, name='std.async_noop')

        # Now let's check the ad-hoc action data.
        adhoc_action_json = self._assert_single_item(
            actions_json,
            name='my_action'
        )

        self.check_adhoc_action_json(adhoc_action_json)
Ejemplo n.º 5
0
    def test_get(self):
        # Create an adhoc action for the purpose of the test.
        adhoc_actions.create_actions(ADHOC_ACTION_YAML)

        resp = self.app.get('/v2/actions/my_action')

        self.assertEqual(200, resp.status_int)

        self.check_adhoc_action_json(resp.json)
Ejemplo n.º 6
0
    def test_action_get_allowed(self):
        # Create an adhoc action for the purpose of the test.
        adhoc_actions.create_actions(ADHOC_ACTION_YAML)

        self.policy.change_policy_definition(
            {"actions:get": "role:FAKE or rule:admin_or_owner"})

        resp = self.app.get('/v2/actions/my_action')

        self.assertEqual(200, resp.status_int)
Ejemplo n.º 7
0
    def test_delete(self):
        # Create an adhoc action for the purpose of the test.
        adhoc_actions.create_actions(ADHOC_ACTION_YAML)

        self.assertIsNotNone(db_api.load_action_definition('my_action'))

        resp = self.app.delete('/v2/actions/my_action')

        self.assertEqual(204, resp.status_int)

        self.assertIsNone(db_api.load_action_definition('my_action'))
Ejemplo n.º 8
0
    def test_put(self):
        # Create an adhoc action for the purpose of the test.
        adhoc_actions.create_actions(ADHOC_ACTION_YAML)

        resp = self.app.put(
            '/v2/actions',
            UPDATED_ADHOC_ACTION_YAML,
            headers={'Content-Type': 'text/plain'}
        )

        self.assertEqual(200, resp.status_int)

        self.check_adhoc_action_json(resp.json['actions'][0])
Ejemplo n.º 9
0
    def test_get_within_project_id(self):
        # Create an adhoc action for the purpose of the test.
        adhoc_actions.create_actions(ADHOC_ACTION_YAML)

        # We should not be able to change 'project_id' even with a
        # direct DB call.
        db_api.update_action_definition(
            'my_action',
            {'project_id': 'foobar'}
        )

        resp = self.app.get('/v2/actions/my_action')

        self.assertEqual(200, resp.status_int)
        self.assertEqual('<default-project>', resp.json['project_id'])
    def test_delete_action(self):

        # Create action.
        adhoc_action_service.create_actions(ACTION_LIST, namespace=NAMESPACE)

        action = db_api.get_action_definition('action1', namespace=NAMESPACE)
        self.assertEqual(NAMESPACE, action.get('namespace'))
        self.assertEqual('action1', action.get('name'))

        # Delete action.
        db_api.delete_action_definition('action1', namespace=NAMESPACE)

        self.assertRaises(DBEntityNotFoundError,
                          db_api.get_action_definition,
                          name='action1',
                          namespace=NAMESPACE)
    def test_update_actions(self):
        db_actions = adhoc_action_service.create_actions(ACTION_LIST,
                                                         namespace=NAMESPACE)

        self.assertEqual(2, len(db_actions))

        action1_db = self._assert_single_item(db_actions, name='action1')
        action1_spec = spec_parser.get_action_spec(action1_db.spec)

        self.assertEqual('action1', action1_spec.get_name())
        self.assertEqual('std.echo', action1_spec.get_base())
        self.assertDictEqual({'output': 'Hi'}, action1_spec.get_base_input())
        self.assertDictEqual({}, action1_spec.get_input())

        db_actions = adhoc_action_service.update_actions(UPDATED_ACTION_LIST,
                                                         namespace=NAMESPACE)

        # Action 1.
        action1_db = self._assert_single_item(db_actions, name='action1')
        action1_spec = spec_parser.get_action_spec(action1_db.spec)

        self.assertEqual('action1', action1_spec.get_name())
        self.assertListEqual([], action1_spec.get_tags())
        self.assertEqual('std.echo', action1_spec.get_base())
        self.assertDictEqual({'output': 'Hi'}, action1_spec.get_base_input())
        self.assertIn('param1', action1_spec.get_input())
        self.assertIs(action1_spec.get_input().get('param1'), utils.NotDefined)

        self.assertRaises(DBEntityNotFoundError,
                          adhoc_action_service.update_actions,
                          UPDATED_ACTION_LIST,
                          namespace='')
Ejemplo n.º 12
0
    def test_get_operational_error(self, mocked_get):
        # Create an adhoc action for the purpose of the test.
        adhoc_actions.create_actions(ADHOC_ACTION_YAML)

        action_def = db_api.get_action_definition('my_action')

        mocked_get.side_effect = [
            # Emulating DB OperationalError
            sa.exc.OperationalError('Mock', 'mock', 'mock'),
            action_def  # Successful run
        ]

        resp = self.app.get('/v2/actions/my_action')

        self.assertEqual(200, resp.status_int)

        self.check_adhoc_action_json(resp.json)
Ejemplo n.º 13
0
    def test_put_system(self):
        # Create an adhoc action for the purpose of the test.
        adhoc_actions.create_actions(ADHOC_ACTION_YAML)

        db_api.update_action_definition('my_action', {'is_system': True})

        resp = self.app.put(
            '/v2/actions',
            ADHOC_ACTION_YAML,
            headers={'Content-Type': 'text/plain'},
            expect_errors=True
        )

        self.assertEqual(400, resp.status_int)
        self.assertIn(
            'Attempt to modify a system action: my_action',
            resp.body.decode()
        )
    def test_create_actions_in_namespace(self):
        db_actions = adhoc_action_service.create_actions(ACTION_LIST,
                                                         namespace=NAMESPACE)

        self.assertEqual(2, len(db_actions))

        self._assert_single_item(db_actions,
                                 name='action1',
                                 namespace=NAMESPACE)

        self._assert_single_item(db_actions,
                                 name='action2',
                                 namespace=NAMESPACE)

        self.assertRaises(DBEntityNotFoundError,
                          db_api.get_action_definition,
                          name='action1',
                          namespace='')
    def test_create_actions(self):
        db_actions = adhoc_action_service.create_actions(ACTION_LIST)

        self.assertEqual(2, len(db_actions))

        # Action 1.
        action1_db = self._assert_single_item(db_actions, name='action1')
        action1_spec = spec_parser.get_action_spec(action1_db.spec)

        self.assertEqual('action1', action1_spec.get_name())
        self.assertListEqual(['test', 'v2'], action1_spec.get_tags())
        self.assertEqual('std.echo', action1_spec.get_base())
        self.assertDictEqual({'output': 'Hi'}, action1_spec.get_base_input())

        # Action 2.
        action2_db = self._assert_single_item(db_actions, name='action2')
        self.assertEqual('', action2_db.namespace)
        action2_spec = spec_parser.get_action_spec(action2_db.spec)

        self.assertEqual('action2', action2_spec.get_name())
        self.assertEqual('std.echo', action1_spec.get_base())
        self.assertDictEqual({'output': 'Hey'}, action2_spec.get_base_input())
Ejemplo n.º 16
0
 def _create_action_definitions():
     with db_api.transaction():
         return adhoc_actions.create_actions(definition,
                                             scope=scope,
                                             namespace=namespace)