Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packs/fixtures/actions/datastore_test_action.yaml
Original file line number Diff line number Diff line change
@@ -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"
59 changes: 59 additions & 0 deletions packs/fixtures/actions/pythonactions/datastore_test_action.py
Original file line number Diff line number Diff line change
@@ -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')
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}