github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/bddtests/steps/peer_cli_impl.py (about) 1 # 2 # Copyright IBM Corp. 2016 All Rights Reserved. 3 # 4 # Licensed under the Apache License, Version 2.0 (the "License"); 5 # you may not use this file except in compliance with the License. 6 # You may obtain a copy of the License at 7 # 8 # http://www.apache.org/licenses/LICENSE-2.0 9 # 10 # Unless required by applicable law or agreed to in writing, software 11 # distributed under the License is distributed on an "AS IS" BASIS, 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 # See the License for the specific language governing permissions and 14 # limitations under the License. 15 # 16 17 import json 18 from behave import * 19 from bdd_test_util import cli_call, fullNameFromContainerNamePart 20 from peer_basic_impl import getAttributeFromJSON 21 22 @when(u'I execute "{command}" in container {containerName}') 23 def step_impl(context, command, containerName): 24 print("Run command: \"{0}\" in container {1}".format(command, containerName)) 25 executeCommandInContainer(context, command, containerName) 26 print("stdout: {0}".format(context.command["stdout"])) 27 print("stderr: {0}".format(context.command["stderr"])) 28 print("returnCode: {0}".format(context.command["returnCode"])) 29 30 def executeCommandInContainer(context, command, container): 31 fullContainerName = fullNameFromContainerNamePart(container, context.compose_containers) 32 command = "docker exec {} {}".format(fullContainerName, command) 33 executeCommand(context, command) 34 35 def executeCommand(context, command): 36 # cli_call expects an array of arguments, hence splitting here. 37 commandArgs = command.split() 38 stdout, stderr, retcode = cli_call(commandArgs, expect_success=False) 39 40 context.command = { 41 "stdout": stdout, 42 "stderr": stderr, 43 "returnCode": retcode 44 } 45 46 @then(u'the command should not complete successfully') 47 def step_impl(context): 48 assert not commandCompletedSuccessfully(context) 49 50 @then(u'the command should complete successfully') 51 def step_impl(context): 52 assert commandCompletedSuccessfully(context) 53 54 def commandCompletedSuccessfully(context): 55 return isSuccessfulReturnCode(context.command["returnCode"]) 56 57 def isSuccessfulReturnCode(returnCode): 58 return returnCode == 0 59 60 @then(u'{stream} should contain JSON') 61 def step_impl(context, stream): 62 assertIsJson(context.command[stream]) 63 64 @then(u'{stream} should contain JSON with "{attribute}" array of length {length}') 65 def step_impl(context, stream, attribute, length): 66 data = context.command[stream] 67 assertIsJson(data) 68 69 json = decodeJson(data) 70 array = getAttribute(attribute, json) 71 assertLength(array, int(length)) 72 73 @then(u'I should get result with "{expectResult}"') 74 def step_impl(context, expectResult): 75 assert context.command["stdout"].strip('\n') == expectResult 76 77 def assertIsJson(data): 78 assert isJson(data), "Data is not in JSON format" 79 80 def isJson(data): 81 try: 82 decodeJson(data) 83 except ValueError: 84 return False 85 86 return True 87 88 def decodeJson(data): 89 return json.loads(data) 90 91 def getAttribute(attribute, json): 92 return getAttributeFromJSON(attribute, json, 93 "Attribute '{}' missing from JSON".format(attribute)) 94 95 def assertLength(array, length): 96 arrayLength = len(array) 97 assert arrayLength == length, "Unexpected array length. Expected {}, got {}".format(length, arrayLength)