20
20
from bdd_json_util import getAttributeFromJSON
21
21
from bdd_test_util import cli_call , bdd_log
22
22
23
- class ContainerData :
24
- def __init__ (self , containerName , ipAddress , envFromInspect , composeService ):
25
- self .containerName = containerName
23
+ class Container :
24
+ def __init__ (self , name , ipAddress , envFromInspect , composeService ):
25
+ self .name = name
26
26
self .ipAddress = ipAddress
27
27
self .envFromInspect = envFromInspect
28
28
self .composeService = composeService
@@ -34,11 +34,11 @@ def getEnv(self, key):
34
34
envValue = val [len (key ):]
35
35
break
36
36
if envValue == None :
37
- raise Exception ("ENV key not found ({0}) for container ({1})" .format (key , self .containerName ))
37
+ raise Exception ("ENV key not found ({0}) for container ({1})" .format (key , self .name ))
38
38
return envValue
39
39
40
40
def __str__ (self ):
41
- return "{} - {}" .format (self .containerName , self .ipAddress )
41
+ return "{} - {}" .format (self .name , self .ipAddress )
42
42
43
43
def __repr__ (self ):
44
44
return self .__str__ ()
@@ -87,7 +87,7 @@ def parseComposeOutput(context):
87
87
dockerComposeService = [composeService [27 :] for composeService in labels if composeService .startswith ("com.docker.compose.service:" )][0 ]
88
88
bdd_log ("dockerComposeService = {0}" .format (dockerComposeService ))
89
89
bdd_log ("container {0} has env = {1}" .format (containerName , env ))
90
- containerDataList .append (ContainerData (containerName , ipAddress , env , dockerComposeService ))
90
+ containerDataList .append (Container (containerName , ipAddress , env , dockerComposeService ))
91
91
# Now merge the new containerData info with existing
92
92
newContainerDataList = []
93
93
if "compose_containers" in context :
@@ -119,13 +119,13 @@ def allContainersAreReadyWithinTimeout(context, timeout):
119
119
def containerIsInitializedByTimestamp (container , timeoutTimestamp ):
120
120
while containerIsNotInitialized (container ):
121
121
if timestampExceeded (timeoutTimestamp ):
122
- bdd_log ("Timed out waiting for {} to initialize" .format (container .containerName ))
122
+ bdd_log ("Timed out waiting for {} to initialize" .format (container .name ))
123
123
return False
124
124
125
- bdd_log ("{} not initialized, waiting..." .format (container .containerName ))
125
+ bdd_log ("{} not initialized, waiting..." .format (container .name ))
126
126
time .sleep (1 )
127
127
128
- bdd_log ("{} now available" .format (container .containerName ))
128
+ bdd_log ("{} now available" .format (container .name ))
129
129
return True
130
130
131
131
def timestampExceeded (timeoutTimestamp ):
@@ -141,13 +141,13 @@ def containerIsInitialized(container):
141
141
return isReady
142
142
143
143
def tcpPortsAreReady (container ):
144
- netstatOutput = getContainerNetstatOutput (container .containerName )
144
+ netstatOutput = getContainerNetstatOutput (container .name )
145
145
146
146
for line in netstatOutput .splitlines ():
147
147
if re .search ("ESTABLISHED|LISTEN" , line ):
148
148
return True
149
149
150
- bdd_log ("No TCP connections are ready in container {}" .format (container .containerName ))
150
+ bdd_log ("No TCP connections are ready in container {}" .format (container .name ))
151
151
return False
152
152
153
153
def getContainerNetstatOutput (containerName ):
@@ -157,7 +157,7 @@ def getContainerNetstatOutput(containerName):
157
157
return stdout
158
158
159
159
def restPortRespondsIfContainerIsPeer (container ):
160
- containerName = container .containerName
160
+ containerName = container .name
161
161
command = ["docker" , "exec" , containerName , "curl" , "localhost:{}" .format (CORE_REST_PORT )]
162
162
163
163
if containerIsPeer (container ):
@@ -195,18 +195,18 @@ def containerIsPeer(container):
195
195
# is to determine if the container is listening on the REST port. However, this method
196
196
# may run before the listening port is ready. Hence, as along as the current
197
197
# convention of vp[0-9] is adhered to this function will be good enough.
198
- return re .search ("vp[0-9]+" , container .containerName , re .IGNORECASE )
198
+ return re .search ("vp[0-9]+" , container .name , re .IGNORECASE )
199
199
200
200
def peerIsReadyByTimestamp (context , peerContainer , allPeerContainers , timeoutTimestamp ):
201
201
while peerIsNotReady (context , peerContainer , allPeerContainers ):
202
202
if timestampExceeded (timeoutTimestamp ):
203
- bdd_log ("Timed out waiting for peer {}" .format (peerContainer .containerName ))
203
+ bdd_log ("Timed out waiting for peer {}" .format (peerContainer .name ))
204
204
return False
205
205
206
- bdd_log ("Peer {} not ready, waiting..." .format (peerContainer .containerName ))
206
+ bdd_log ("Peer {} not ready, waiting..." .format (peerContainer .name ))
207
207
time .sleep (1 )
208
208
209
- bdd_log ("Peer {} now available" .format (peerContainer .containerName ))
209
+ bdd_log ("Peer {} now available" .format (peerContainer .name ))
210
210
return True
211
211
212
212
def peerIsNotReady (context , thisPeer , allPeers ):
@@ -235,4 +235,27 @@ def getConnectedPeersFromPeer(context, thisPeer):
235
235
if response .status_code != 200 :
236
236
return None
237
237
238
- return getAttributeFromJSON ("peers" , response .json (), "There should be a peer json attribute" )
238
+ return getAttributeFromJSON ("peers" , response .json (), "There should be a peer json attribute" )
239
+
240
+ def mapAliasesToContainers (context ):
241
+ aliasToContainerMap = {}
242
+
243
+ for container in context .compose_containers :
244
+ alias = extractAliasFromContainerName (container .name )
245
+ aliasToContainerMap [alias ] = container
246
+
247
+ return aliasToContainerMap
248
+
249
+ def extractAliasFromContainerName (containerName ):
250
+ """ Take a compose created container name and extract the alias to which it
251
+ will be refered. For example bddtests_vp1_0 will return vp0 """
252
+ return containerName .split ("_" )[1 ]
253
+
254
+ def mapContainerNamesToContainers (context ):
255
+ nameToContainerMap = {}
256
+
257
+ for container in context .compose_containers :
258
+ name = container .name
259
+ nameToContainerMap [name ] = container
260
+
261
+ return nameToContainerMap
0 commit comments