Skip to content

Commit 81cd41b

Browse files
committed
FAB-1438: Add up, down, scale to compose util
This updates the compose util to allow one to start, up, stop, and scale a container based on a docker service. It also allows for manipulating a specific container. Change-Id: I424c64d0ea65d9a00344163cd839404117e0b807 Signed-off-by: Latitia M Haskins <[email protected]>
1 parent 008ee85 commit 81cd41b

File tree

1 file changed

+51
-12
lines changed

1 file changed

+51
-12
lines changed

bddtests/steps/compose.py

+51-12
Original file line numberDiff line numberDiff line change
@@ -64,26 +64,48 @@ def GetCompositionCallbacksFromContext(cls, context):
6464
def GetUUID(cls):
6565
return GetDockerSafeUUID()
6666

67-
def __init__(self, context, composeFilesYaml, projectName = None):
67+
def __init__(self, context, composeFilesYaml, projectName = None,
68+
force_recreate = True, components = []):
6869
self.contextHelper = ContextHelper.GetHelper(context=context)
6970
if not projectName:
7071
projectName = self.contextHelper.getGuuid()
7172
self.projectName = projectName
7273
self.context = context
7374
self.containerDataList = []
7475
self.composeFilesYaml = composeFilesYaml
75-
self.serviceNames = []
76-
self.serviceNames = self._collectServiceNames()
76+
7777
[callback.composing(self, context) for callback in Composition.GetCompositionCallbacksFromContext(context)]
78-
self.issueCommand(["up", "--force-recreate", "-d"])
78+
self.up(context, force_recreate, components)
7979

8080
def _collectServiceNames(self):
8181
'First collect the services names.'
8282
servicesList = [service for service in self.issueCommand(["config", "--services"]).splitlines() if "WARNING" not in service]
8383
return servicesList
8484

85+
def up(self, context, force_recreate=True, components=[]):
86+
self.serviceNames = self._collectServiceNames()
87+
command = ["up", "-d"]
88+
if force_recreate:
89+
command += ["--force-recreate"]
90+
self.issueCommand(command + components)
91+
92+
def scale(self, context, serviceName, count=1):
93+
self.serviceNames = self._collectServiceNames()
94+
command = ["scale", "%s=%d" %(serviceName, count)]
95+
self.issueCommand(command)
96+
97+
def stop(self, context, components=[]):
98+
self.serviceNames = self._collectServiceNames()
99+
command = ["stop"]
100+
self.issueCommand(command, components)
101+
102+
def start(self, context, components=[]):
103+
self.serviceNames = self._collectServiceNames()
104+
command = ["start"]
105+
self.issueCommand(command, components)
106+
85107
def getServiceNames(self):
86-
return list(self.serviceNames)
108+
return list(self.serviceNames)
87109

88110
def parseComposeFilesArg(self, composeFileArgs):
89111
args = [arg for sublist in [["-f", file] for file in [file if not os.path.isdir(file) else os.path.join(file, 'docker-compose.yml') for file in composeFileArgs.split()]] for arg in sublist]
@@ -120,14 +142,31 @@ def refreshContainerIDs(self):
120142
def _callCLI(self, argList, expect_success, env):
121143
return bdd_test_util.cli_call(argList, expect_success=expect_success, env=env)
122144

123-
def issueCommand(self, args):
124-
cmdArgs = self.getFileArgs()+ args
125-
# output, error, returncode = \
126-
# bdd_test_util.cli_call(["docker-compose"] + cmdArgs, expect_success=True, env=self.getEnv())
145+
def issueCommand(self, command, components=[]):
146+
componentList = []
147+
useCompose = True
148+
for component in components:
149+
if '_' in component:
150+
useCompose = False
151+
componentList.append("%s_%s" % (self.projectName, component))
152+
else:
153+
break
154+
155+
# If we need to perform an operation on a specific container, use
156+
# docker not docker-compose
157+
if useCompose:
158+
cmdArgs = self.getFileArgs()+ command + components
159+
cmd = ["docker-compose"] + cmdArgs
160+
else:
161+
cmdArgs = command + componentList
162+
cmd = ["docker"] + cmdArgs
163+
164+
#print("cmd:", cmd)
127165
output, error, returncode = \
128-
self._callCLI(["docker-compose"] + cmdArgs, expect_success=True, env=self.getEnv())
166+
self._callCLI(cmd, expect_success=True, env=self.getEnv())
167+
129168
# Don't rebuild if ps command
130-
if args[0] !="ps" and args[0] !="config":
169+
if command[0] !="ps" and command[0] !="config":
131170
self.rebuildContainerData()
132171
return output
133172

@@ -162,6 +201,7 @@ def rebuildContainerData(self):
162201

163202
def decompose(self):
164203
self.issueCommand(["unpause"])
204+
self.issueCommand(["down"])
165205
self.issueCommand(["kill"])
166206
self.issueCommand(["rm", "-f"])
167207

@@ -181,4 +221,3 @@ def decompose(self):
181221

182222
# Invoke callbacks
183223
[callback.decomposing(self, self.context) for callback in Composition.GetCompositionCallbacksFromContext(self.context)]
184-

0 commit comments

Comments
 (0)