
Rather than always returning JSON or YAML, add functionality to return table responses when applicable for ease of reading. Adds some marker exceptions to the Shipyard API Client to handle cases where the user of the client would generally want to take a specific and repeatable course of action instead of handling the response in a case-by-case basis. Moved cli action testing to use the responses library instead of as many internal mocks. Added test response generators for standard api responses. Change-Id: I3a593fb29b6e76d971adc7f3bb3a4b7f378ed091
120 lines
3.9 KiB
Python
120 lines
3.9 KiB
Python
# Copyright 2017 AT&T Intellectual Property. All other rights reserved.
|
|
#
|
|
# 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
|
|
|
|
import responses
|
|
|
|
from shipyard_client.api_client.base_client import BaseClient
|
|
from shipyard_client.cli.control.actions import Control
|
|
from shipyard_client.tests.unit.cli import stubs
|
|
|
|
|
|
@responses.activate
|
|
@mock.patch.object(BaseClient, 'get_endpoint', lambda x: 'http://shiptest')
|
|
@mock.patch.object(BaseClient, 'get_token', lambda x: 'abc')
|
|
def test_Control(*args):
|
|
responses.add(
|
|
responses.POST,
|
|
'http://shiptest/actions/01BTG32JW87G0YKA1K29TKNAFX/control/pause',
|
|
body=None,
|
|
status=202
|
|
)
|
|
control_verb = 'pause'
|
|
id = '01BTG32JW87G0YKA1K29TKNAFX'
|
|
response = Control(stubs.StubCliContext(),
|
|
control_verb,
|
|
id).invoke_and_return_resp()
|
|
# test correct function was called
|
|
assert response == ('pause successfully submitted for action'
|
|
' 01BTG32JW87G0YKA1K29TKNAFX')
|
|
|
|
|
|
@responses.activate
|
|
@mock.patch.object(BaseClient, 'get_endpoint', lambda x: 'http://shiptest')
|
|
@mock.patch.object(BaseClient, 'get_token', lambda x: 'abc')
|
|
def test_control_unpause(*args):
|
|
responses.add(
|
|
responses.POST,
|
|
'http://shiptest/actions/01BTG32JW87G0YKA1K29TKNAFX/control/unpause',
|
|
body=None,
|
|
status=202
|
|
)
|
|
control_verb = 'unpause'
|
|
id = '01BTG32JW87G0YKA1K29TKNAFX'
|
|
response = Control(stubs.StubCliContext(),
|
|
control_verb,
|
|
id).invoke_and_return_resp()
|
|
# test correct function was called
|
|
assert response == ('unpause successfully submitted for action'
|
|
' 01BTG32JW87G0YKA1K29TKNAFX')
|
|
|
|
|
|
@responses.activate
|
|
@mock.patch.object(BaseClient, 'get_endpoint', lambda x: 'http://shiptest')
|
|
@mock.patch.object(BaseClient, 'get_token', lambda x: 'abc')
|
|
def test_control_stop(*args):
|
|
responses.add(
|
|
responses.POST,
|
|
'http://shiptest/actions/01BTG32JW87G0YKA1K29TKNAFX/control/stop',
|
|
body=None,
|
|
status=202
|
|
)
|
|
control_verb = 'stop'
|
|
id = '01BTG32JW87G0YKA1K29TKNAFX'
|
|
response = Control(stubs.StubCliContext(),
|
|
control_verb,
|
|
id).invoke_and_return_resp()
|
|
# test correct function was called
|
|
assert response == ('stop successfully submitted for action'
|
|
' 01BTG32JW87G0YKA1K29TKNAFX')
|
|
|
|
|
|
resp_body = """
|
|
{
|
|
"message": "Unable to pause action",
|
|
"details": {
|
|
"messageList": [
|
|
{
|
|
"message": "Conflicting things",
|
|
"error": true
|
|
},
|
|
{
|
|
"message": "Try soup",
|
|
"error": false
|
|
}
|
|
]
|
|
},
|
|
"reason": "Conflicts"
|
|
}
|
|
"""
|
|
|
|
|
|
@responses.activate
|
|
@mock.patch.object(BaseClient, 'get_endpoint', lambda x: 'http://shiptest')
|
|
@mock.patch.object(BaseClient, 'get_token', lambda x: 'abc')
|
|
def test_control_409(*args):
|
|
responses.add(
|
|
responses.POST,
|
|
'http://shiptest/actions/01BTG32JW87G0YKA1K29TKNAFX/control/pause',
|
|
body=resp_body,
|
|
status=409
|
|
)
|
|
control_verb = 'pause'
|
|
id = '01BTG32JW87G0YKA1K29TKNAFX'
|
|
response = Control(stubs.StubCliContext(),
|
|
control_verb,
|
|
id).invoke_and_return_resp()
|
|
# test correct function was called
|
|
assert 'Unable to pause action' in response
|