diff --git a/packs/fixtures/actions/datastore_test_action.yaml b/packs/fixtures/actions/datastore_test_action.yaml new file mode 100644 index 0000000..07c1a5f --- /dev/null +++ b/packs/fixtures/actions/datastore_test_action.yaml @@ -0,0 +1,7 @@ +--- +description: Simple python action to test datastore access. +enabled: true +entry_point: pythonactions/datastore_test_action.py +name: datastore_test_action +parameters: {} +runner_type: "python-script" diff --git a/packs/fixtures/actions/pythonactions/datastore_test_action.py b/packs/fixtures/actions/pythonactions/datastore_test_action.py new file mode 100644 index 0000000..306cf46 --- /dev/null +++ b/packs/fixtures/actions/pythonactions/datastore_test_action.py @@ -0,0 +1,59 @@ +import json + +from st2actions.runners.pythonrunner import Action +from st2client.client import Client +from st2client.models import KeyValuePair + + +__all__ = [ + 'DatastoreTestAction' +] + + +class DatastoreTestAction(Action): + + def run(self): + self._test_datastore_actions_via_client() + self._test_datastore_actions_via_action_service() + + def _test_datastore_actions_via_client(self): + print('Test datastore access via raw client.') + client = Client(base_url='http://localhost') + + test_name = 'st2tests.putin' + test_value = 'putout' + # Put + kv = KeyValuePair(name=test_name, value=test_value) + client.keys.update(kv) + # print('Wrote key: %s value: %s to datastore.' % (test_name, test_value)) + + # Get + val = client.keys.get_by_name(name_or_id=test_name) + if val.value != test_value: + raise Exception('KeyValue access failed on GET: %s' % test_name) + # print('Got value: %s from datastore.' % val.value) + + # Delete + client.keys.delete(val) + # print('Successfully delete key: %s from datastore.' % test_name) + + def _test_datastore_actions_via_action_service(self): + print('Test datastore access via action_service') + data = {'somedata': 'foobar'} + data_json_str = json.dumps(data) + + # Add a value to the datastore + self.action_service.set_value(name='cache', value=data_json_str) + + # Retrieve a value + value = self.action_service.get_value('cache') + retrieved_data = json.loads(value) + if 'somedata' not in retrieved_data: + raise Exception('Retrieved incorrect value from datastore: %s. Expected: %s' % + (retrieved_data, data)) + + if retrieved_data['somedata'] != 'foobar': + raise Exception('Datastore value corrupted!!!') + + # Delete a value + self.action_service.delete_value('cache') diff --git a/packs/tests/actions/chains/test_quickstart_python_actions.yaml b/packs/tests/actions/chains/test_quickstart_python_actions.yaml index e0ce216..8360fd4 100644 --- a/packs/tests/actions/chains/test_quickstart_python_actions.yaml +++ b/packs/tests/actions/chains/test_quickstart_python_actions.yaml @@ -57,3 +57,8 @@ chain: object: "{{test_exception_python_action}}" key: "exit_code" value: 0 + on-success: "test_datastore_access" + - + name: "test_datastore_access" + ref: "fixtures.datastore_test_action" + params: {}