github.com/darrenli6/fabric-sdk-example@v0.0.0-20220109053535-94b13b56df8c/test/feature/environment.py (about) 1 # Copyright IBM Corp. 2017 All Rights Reserved. 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 # 15 16 import subprocess 17 import shutil 18 19 20 def getDockerComposeFileArgsFromYamlFile(composeYaml): 21 parts = composeYaml.split() 22 args = [] 23 for part in parts: 24 args = args + ["-f"] + [part] 25 return args 26 27 28 def getLogFiles(containers, fileSuffix): 29 """ This will gather the logs for the different component containers as well as 30 the chaincode containers. If the containers is a list of strings, it is 31 assumed this is a chaincode container list. Otherwise, the list is a list 32 of Container objects. 33 """ 34 for container in containers: 35 if isinstance(container, str): 36 namePart, sep, _ = container.rpartition("-") 37 containerName = container 38 else: 39 namePart = container.containerName 40 containerName = container.containerName 41 with open(namePart + fileSuffix, "w+") as logfile: 42 rc = subprocess.call(["docker", "logs", containerName], stdout=logfile, stderr=logfile) 43 if rc !=0 : 44 print("Cannot get logs for {0}. Docker rc = {1}".format(namePart, rc)) 45 46 47 def after_scenario(context, scenario): 48 getLogs = context.config.userdata.get("logs", "N") 49 if getLogs.lower() == "force" or (scenario.status == "failed" and getLogs.lower() == "y" and "compose_containers" in context): 50 print("Scenario {0} failed. Getting container logs".format(scenario.name)) 51 fileSuffix = "_" + scenario.name.replace(" ", "_") + ".log" 52 # get logs from the peer containers 53 getLogFiles(containers, fileSuffix) 54 # get logs from the chaincode containers 55 chaincodeContainers = subprocess.call(["docker", "ps", "-f", "name=dev-", "--format", "{{.Names}}"]) 56 getLogFiles(chaincodeContainers.splitlines(), fileSuffix) 57 58 if 'doNotDecompose' in scenario.tags: 59 if 'compose_yaml' in context: 60 print("Not going to decompose after scenario {0}, with yaml '{1}'".format(scenario.name, context.compose_yaml)) 61 elif 'composition' in context: 62 # Remove config data and docker containers 63 shutil.rmtree("configs/%s" % context.composition.projectName) 64 context.composition.decompose() 65 66 # stop any running peer that could get in the way before starting the tests 67 def before_all(context): 68 pass 69 70 # stop any running peer that could get in the way before starting the tests 71 def after_all(context): 72 print("context.failed = {0}".format(context.failed))