Skip to content

Commit 09ba01b

Browse files
committed
[FAB-3758]: Stop and Start containers
This allows for the implementation to stop/start docker containers for the system behave tests. Change-Id: I48ad73b8b7c2cc5eaaf51d494f4cb29a4ca316b5 Signed-off-by: Latitia M Haskins <[email protected]>
1 parent 52f1942 commit 09ba01b

File tree

2 files changed

+47
-19
lines changed

2 files changed

+47
-19
lines changed

test/feature/steps/basic_impl.py

+25-5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515

1616
from behave import *
1717
import time
18+
import os
19+
import uuid
20+
import compose_util
21+
import config_util
22+
import endorser_util
1823

1924

2025
ORDERER_TYPES = ["solo",
@@ -33,21 +38,36 @@ def step_impl(context, seconds):
3338
time.sleep(float(seconds))
3439

3540
@given(u'we compose "{composeYamlFile}"')
36-
def compose_impl(context, composeYamlFile):
37-
pass
41+
def compose_impl(context, composeYamlFile, projectName=None, startContainers=True):
42+
composition = compose_util.Composition(context, composeYamlFile,
43+
projectName=projectName,
44+
startContainers=startContainers)
45+
context.compose_containers = composition.containerDataList
46+
context.composition = composition
3847

3948
@given(u'I have a bootstrapped fabric network')
4049
def step_impl(context):
4150
bootstrapped_impl(context, "solo")
4251

4352
@given(u'I have a bootstrapped fabric network of type {networkType}')
4453
def bootstrapped_impl(context, networkType):
45-
pass
54+
assert networkType in ORDERER_TYPES, "Unknown network type '%s'" % networkType
55+
curpath = os.path.realpath('.')
56+
context.composeFile = "%s/docker-compose/docker-compose-%s.yml" % (curpath, networkType)
57+
assert os.path.exists(context.composeFile), "The docker compose file does not exist: {0}".format(context.composeFile)
58+
profile = PROFILE_TYPES.get(networkType, "SampleInsecureSolo")
59+
channelID = endorser_util.TEST_CHANNEL_ID
60+
projectName = str(uuid.uuid1()).replace('-','')
61+
config_util.generateCrypto(projectName)
62+
config_util.generateConfig(channelID, profile, projectName)
63+
compose_impl(context, context.composeFile, projectName=projectName)
4664

4765
@given(u'{component} is taken down')
4866
def step_impl(context, component):
49-
pass
67+
assert component in context.composition.collectServiceNames(), "Unknown component '{0}'".format(component)
68+
context.composition.stop([component])
5069

5170
@given(u'{component} comes back up')
5271
def step_impl(context, component):
53-
pass
72+
assert component in context.composition.collectServiceNames(), "Unknown component '{0}'".format(component)
73+
context.composition.start([component])

test/feature/steps/compose_util.py

+22-14
Original file line numberDiff line numberDiff line change
@@ -41,44 +41,51 @@ def getEnv(self, key):
4141
class Composition:
4242

4343
def __init__(self, context, composeFilesYaml, projectName = None,
44-
force_recreate = True, components = []):
44+
force_recreate = True, components = [], startContainers=True):
4545
if not projectName:
4646
projectName = str(uuid.uuid1()).replace('-','')
4747
self.projectName = projectName
4848
self.context = context
4949
self.containerDataList = []
5050
self.composeFilesYaml = composeFilesYaml
51-
self.serviceNames = []
52-
self.serviceNames = self._collectServiceNames()
53-
self.up(context, force_recreate, components)
51+
if startContainers:
52+
self.up(force_recreate, components)
5453

55-
def _collectServiceNames(self):
54+
def collectServiceNames(self):
5655
'First collect the services names.'
5756
servicesList = [service for service in self.issueCommand(["config", "--services"]).splitlines() if "WARNING" not in service]
5857
return servicesList
5958

60-
def up(self, context, force_recreate=True, components=[]):
61-
self.serviceNames = self._collectServiceNames()
59+
def up(self, force_recreate=True, components=[]):
60+
self.serviceNames = self.collectServiceNames()
6261
command = ["up", "-d"]
6362
if force_recreate:
6463
command += ["--force-recreate"]
6564
self.issueCommand(command + components)
6665

67-
def scale(self, context, serviceName, count=1):
68-
self.serviceNames = self._collectServiceNames()
66+
def scale(self, serviceName, count=1):
67+
self.serviceNames = self.collectServiceNames()
6968
command = ["scale", "%s=%d" %(serviceName, count)]
7069
self.issueCommand(command)
7170

72-
def stop(self, context, components=[]):
73-
self.serviceNames = self._collectServiceNames()
71+
def stop(self, components=[]):
72+
self.serviceNames = self.collectServiceNames()
7473
command = ["stop"]
7574
self.issueCommand(command, components)
7675

77-
def start(self, context, components=[]):
78-
self.serviceNames = self._collectServiceNames()
76+
def start(self, components=[]):
77+
self.serviceNames = self.collectServiceNames()
7978
command = ["start"]
8079
self.issueCommand(command, components)
8180

81+
def docker_exec(self, command, components=[]):
82+
results = {}
83+
updatedCommand = " ".join(command)
84+
for component in components:
85+
execCommand = ["exec", component, updatedCommand]
86+
results[component] = self.issueCommand(execCommand, [])
87+
return results
88+
8289
def parseComposeFilesArg(self, composeFileArgs):
8390
composeFileList = []
8491
for composeFile in composeFileArgs.split():
@@ -138,7 +145,8 @@ def issueCommand(self, command, components=[]):
138145
cmdArgs = command + componentList
139146
cmd = ["docker"] + cmdArgs
140147

141-
output = subprocess.check_output(cmd, env=self.getEnv())
148+
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=self.getEnv())
149+
output, _error = process.communicate()
142150

143151
# Don't rebuild if ps command
144152
if command[0] !="ps" and command[0] !="config":

0 commit comments

Comments
 (0)