Files
python-tripleoclient/tripleoclient/tests/workflows/test_base.py
James Slagle 026cb4a76e Handle failed actions
The result of syncrhonously called Mistral actions wasn't being checked
to see if the action passed or failed. The result is now checked and if
the action has failed, an exception will be raised.

Change-Id: I95ae8c98fec94cf91f3f209b593f6c1815729fd4
Closes-Bug: #1686811
2017-05-04 14:47:21 -04:00

83 lines
2.6 KiB
Python

# -*- coding: utf-8 -*-
# Licensed 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 mock
from osc_lib.tests import utils
from tripleoclient import exceptions
from tripleoclient.workflows import base
class TestBaseWorkflows(utils.TestCommand):
def test_wait_for_messages_success(self):
payload_a = {
'status': 'ERROR',
'execution': {'id': 2}
}
payload_b = {
'status': 'ERROR',
'execution': {'id': 1}
}
mistral = mock.Mock()
websocket = mock.Mock()
websocket.wait_for_messages.return_value = iter([payload_a, payload_b])
execution = mock.Mock()
execution.id = 1
messages = list(base.wait_for_messages(mistral, websocket, execution))
self.assertEqual([payload_a, payload_b], messages)
self.assertFalse(mistral.executions.get.called)
websocket.wait_for_messages.assert_called_with(timeout=None)
def test_wait_for_messages_timeout(self):
mistral = mock.Mock()
websocket = mock.Mock()
websocket.wait_for_messages.side_effect = exceptions.WebSocketTimeout
execution = mock.Mock()
execution.id = 1
messages = base.wait_for_messages(mistral, websocket, execution)
self.assertRaises(exceptions.WebSocketTimeout, list, messages)
self.assertTrue(mistral.executions.get.called)
websocket.wait_for_messages.assert_called_with(timeout=None)
def test_call_action_success(self):
mistral = mock.Mock()
action = 'test-action'
result = mock.Mock()
result.output = '{"result":"test-result"}'
mistral.action_executions.create = mock.Mock(return_value=result)
self.assertEqual(base.call_action(mistral, action), "test-result")
def test_call_action_fail(self):
mistral = mock.Mock()
action = 'test-action'
result = mock.Mock()
result.output = '{"result":"test-result"}'
result.state = 'ERROR'
mistral.action_executions.create = mock.Mock(return_value=result)
self.assertRaises(exceptions.WorkflowActionError,
base.call_action, mistral, action)