Skip to content

Commit

Permalink
Add examples to mistral itests
Browse files Browse the repository at this point in the history
  • Loading branch information
m4dcoder committed Apr 28, 2015
1 parent 851fe5f commit 498f674
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 54 deletions.
31 changes: 16 additions & 15 deletions contrib/examples/actions/workflows/mistral-handle-retry.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,23 @@ workflows:
main:
type: direct
tasks:
init:
action: core.local cmd="rm -f /tmp/done"
on-success:
- create-file
- test-error-undo-retry
create-file:
action: core.local cmd="touch /tmp/done"
wait-before: 10
test-error-undo-retry:
workflow: work
policies:
retry:
count: 10
delay: 2
retry:
count: 30
delay: 1
on-success:
- delete-file
create-file:
action: core.local
input:
cmd: "touch /tmp/done"
policies:
wait-before: 10
delete-file:
action: core.local
input:
cmd: "rm -f /tmp/done"
action: core.local cmd="rm -f /tmp/done"

work:
type: direct
Expand All @@ -45,5 +44,7 @@ workflows:
action: core.local
input:
cmd: "echo 'Define rollback tasks here.'"
on-success:
- fail
on-complete:
- throw
throw:
action: std.fail
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ examples.mistral-repeat-with-items:
with-items: cmd in <% $.cmds %>
action: core.local cmd=<% $.cmd %>
publish:
resut: <% $.repeat.stdout %>
result: <% $.repeat.stdout %>
62 changes: 62 additions & 0 deletions st2tests/integration/mistral/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import eventlet
import unittest2

from st2client import client as st2
from st2client import models


class TestWorkflowExecution(unittest2.TestCase):

@classmethod
def setUpClass(cls):
cls.st2client = st2.Client(base_url='http://localhost')

def _execute_workflow(self, action, parameters=None):
if parameters is None:
parameters = {}

execution = models.LiveAction(action=action, parameters=parameters)
execution = self.st2client.liveactions.create(execution)
self.assertIsNotNone(execution.id)
self.assertEqual(execution.action['ref'], action)
self.assertIn(execution.status, ['scheduled', 'running'])

return execution

def _wait_for_completion(self, execution, wait=300):
for i in range(wait):
eventlet.sleep(3)
execution = self.st2client.liveactions.get_by_id(execution.id)
if execution.status in ['succeeded', 'failed']:
break

return execution

def _assert_success(self, execution):
self.assertEqual(execution.status, 'succeeded')
tasks = execution.result['tasks']
for task in tasks:
self.assertIn('state', task)
self.assertEqual(task['state'], 'SUCCESS')

def _assert_failure(self, execution):
self.assertEqual(execution.status, 'failed')
tasks = execution.result['tasks']
for task in tasks:
self.assertIn('state', task)
self.assertEqual(task['state'], 'ERROR')
53 changes: 53 additions & 0 deletions st2tests/integration/mistral/test_examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from integration.mistral import base


class ExamplesTest(base.TestWorkflowExecution):

def test_workbook_multiple_subflows(self):
execution = self._execute_workflow('examples.mistral-workbook-multiple-subflows')
execution = self._wait_for_completion(execution)
self.assertEqual(execution.status, 'succeeded')

def test_handle_error(self):
execution = self._execute_workflow('examples.mistral-handle-error')
execution = self._wait_for_completion(execution)
self.assertEqual(execution.status, 'failed')
tasks = {t['name']: t for t in execution.result['tasks']}
self.assertEqual(tasks['task1']['state'], 'ERROR')
self.assertEqual(tasks['notify_on_error']['state'], 'SUCCESS')

def test_handle_retry(self):
execution = self._execute_workflow('examples.mistral-handle-retry')
execution = self._wait_for_completion(execution)
self.assertEqual(execution.status, 'succeeded')

def test_repeat(self):
inputs = {'cmd': 'echo "Yo!"'}
execution = self._execute_workflow('examples.mistral-repeat', parameters=inputs)
execution = self._wait_for_completion(execution)
self.assertEqual(execution.status, 'succeeded')
self.assertEqual(len(execution.result['result']), 3)
self.assertListEqual(execution.result['result'], ['Yo!\n', 'Yo!\n', 'Yo!\n'])

def test_repeat_with_items(self):
inputs = {'cmds': ['echo "a"', 'echo "b"', 'echo "c"']}
execution = self._execute_workflow('examples.mistral-repeat-with-items', parameters=inputs)
execution = self._wait_for_completion(execution)
self.assertEqual(execution.status, 'succeeded')
self.assertEqual(len(execution.result['result']), 3)
self.assertListEqual(sorted(execution.result['result']), ['a\n', 'b\n', 'c\n'])
40 changes: 2 additions & 38 deletions st2tests/integration/mistral/test_wiring.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,47 +14,11 @@
# limitations under the License.

import eventlet
import unittest2

from st2client import client as st2
from st2client import models
from integration.mistral import base


class TestWorkflowExecution(unittest2.TestCase):

@classmethod
def setUpClass(cls):
cls.st2client = st2.Client(base_url='http://localhost')

def _execute_workflow(self, action, parameters):
execution = models.LiveAction(action=action, parameters=parameters)
execution = self.st2client.liveactions.create(execution)
self.assertIsNotNone(execution.id)
self.assertEqual(execution.action['ref'], action)
self.assertIn(execution.status, ['scheduled', 'running'])
return execution

def _wait_for_completion(self, execution, wait=300):
for i in range(wait):
eventlet.sleep(3)
execution = self.st2client.liveactions.get_by_id(execution.id)
if execution.status in ['succeeded', 'failed']:
break
return execution

def _assert_success(self, execution):
self.assertEqual(execution.status, 'succeeded')
tasks = execution.result['tasks']
for task in tasks:
self.assertIn('state', task)
self.assertEqual(task['state'], 'SUCCESS')

def _assert_failure(self, execution):
self.assertEqual(execution.status, 'failed')
tasks = execution.result['tasks']
for task in tasks:
self.assertIn('state', task)
self.assertEqual(task['state'], 'ERROR')
class WiringTest(base.TestWorkflowExecution):

def test_basic_workflow(self):
execution = self._execute_workflow('examples.mistral-basic', {'cmd': 'date'})
Expand Down

0 comments on commit 498f674

Please sign in to comment.