@@ -180,11 +180,11 @@ import Control.Monad.Trans.Resource
180180 runResourceT ,
181181 )
182182import Data.Aeson (decode' )
183- import qualified Data.Aeson.Optics as Optics
184183import qualified Data.ByteString.Lazy.Char8 as LazyByteString
185184import Data.Function ((&) )
185+ import Data.Functor ((<&>) )
186186import Data.List (find , stripPrefix )
187- import Data.Maybe (fromMaybe )
187+ import Data.Maybe (fromMaybe , listToMaybe )
188188import Data.String (IsString (.. ))
189189import Data.Text (Text , pack , splitOn , strip , unpack )
190190import Data.Text.Encoding (encodeUtf8 )
@@ -205,9 +205,6 @@ import Network.HTTP.Client
205205 )
206206import Network.HTTP.Types (statusCode )
207207import qualified Network.Socket as Socket
208- import Optics.Fold (pre )
209- import Optics.Operators ((^?) )
210- import Optics.Optic ((%) , (<&>) )
211208import System.Directory (doesFileExist )
212209import System.Environment (lookupEnv )
213210import System.IO (Handle , hClose )
@@ -233,6 +230,7 @@ import TestContainers.Docker.Internal
233230 dockerWithStdin ,
234231 prefixedLogConsumer ,
235232 )
233+ import TestContainers.Docker.JSON (asText , eachMember , eachValue , lookupKey )
236234import TestContainers.Docker.Network
237235 ( Network ,
238236 NetworkId ,
@@ -923,7 +921,7 @@ waitForState isReady = WaitReady $ \Container {id, image} -> do
923921 internalInspect configTracer id
924922
925923 let state = containerState inspectOutput
926- containerName = inspectOutput ^? Optics. key " Name" % Optics. _String
924+ containerName = lookupKey " Name" inspectOutput >>= asText
927925 exception =
928926 InvalidStateException
929927 { id = id ,
@@ -1123,7 +1121,7 @@ waitUntilReady container@Container {id} input = do
11231121 case result of
11241122 Nothing -> do
11251123 let Container {image, inspectOutput} = container
1126- containerName = inspectOutput ^? Optics. key " Name" % Optics. _String
1124+ containerName = lookupKey " Name" inspectOutput >>= asText
11271125 throwM $
11281126 TimeoutException
11291127 { id ,
@@ -1200,12 +1198,9 @@ containerIp =
12001198-- | Get the IP address of a running Docker container using @docker inspect@.
12011199internalContainerIp :: Container -> Text
12021200internalContainerIp Container {id , inspectOutput, image} =
1203- case inspectOutput
1204- ^? Optics. key " NetworkSettings"
1205- % Optics. key " IPAddress"
1206- % Optics. _String of
1201+ case lookupKey " NetworkSettings" inspectOutput >>= lookupKey " IPAddress" >>= asText of
12071202 Nothing -> do
1208- let containerName = inspectOutput ^? Optics. key " Name" % Optics. _String
1203+ let containerName = lookupKey " Name" inspectOutput >>= asText
12091204 throw $
12101205 InspectOutputUnexpected
12111206 { id ,
@@ -1221,50 +1216,42 @@ internalContainerIp Container {id, inspectOutput, image} =
12211216-- @since 0.5.0.0
12221217containerAlias :: Container -> Text
12231218containerAlias Container {id , inspectOutput, image} =
1224- case inspectOutput
1225- ^? pre
1226- ( Optics. key " NetworkSettings"
1227- % Optics. key " Networks"
1228- % Optics. members
1229- % Optics. key " Aliases"
1230- % Optics. values
1231- % Optics. _String
1232- ) of
1233- Nothing -> do
1234- let containerName = inspectOutput ^? Optics. key " Name" % Optics. _String
1235- throw $
1236- InspectOutputMissingNetwork
1237- { id ,
1238- imageName = Just (imageTag image),
1239- containerName = containerName
1240- }
1241- Just alias ->
1242- alias
1219+ let aliases = do
1220+ network <- maybe [] eachMember (lookupKey " NetworkSettings" inspectOutput >>= lookupKey " Networks" )
1221+ aliasValue <- maybe [] eachValue (lookupKey " Aliases" network)
1222+ maybe [] pure (asText aliasValue)
1223+ in case listToMaybe aliases of
1224+ Nothing -> do
1225+ let containerName = lookupKey " Name" inspectOutput >>= asText
1226+ throw $
1227+ InspectOutputMissingNetwork
1228+ { id ,
1229+ imageName = Just (imageTag image),
1230+ containerName = containerName
1231+ }
1232+ Just alias ->
1233+ alias
12431234
12441235-- | Get the IP address for the container's gateway, i.e. the host.
12451236-- Takes the first gateway address found.
12461237--
12471238-- @since 0.5.0.0
12481239containerGateway :: Container -> Text
12491240containerGateway Container {id , inspectOutput, image} =
1250- case inspectOutput
1251- ^? pre
1252- ( Optics. key " NetworkSettings"
1253- % Optics. key " Networks"
1254- % Optics. members
1255- % Optics. key " Gateway"
1256- % Optics. _String
1257- ) of
1258- Nothing -> do
1259- let containerName = inspectOutput ^? Optics. key " Name" % Optics. _String
1260- throw $
1261- InspectOutputMissingNetwork
1262- { id ,
1263- imageName = Just (imageTag image),
1264- containerName = containerName
1265- }
1266- Just gatewayIp ->
1267- gatewayIp
1241+ let gateways = do
1242+ network <- maybe [] eachMember (lookupKey " NetworkSettings" inspectOutput >>= lookupKey " Networks" )
1243+ maybe [] pure (lookupKey " Gateway" network >>= asText)
1244+ in case listToMaybe gateways of
1245+ Nothing -> do
1246+ let containerName = lookupKey " Name" inspectOutput >>= asText
1247+ throw $
1248+ InspectOutputMissingNetwork
1249+ { id ,
1250+ imageName = Just (imageTag image),
1251+ containerName = containerName
1252+ }
1253+ Just gatewayIp ->
1254+ gatewayIp
12681255
12691256-- | Looks up an exposed port on the host.
12701257--
@@ -1278,26 +1265,28 @@ containerPort Container {id, inspectOutput, image} Port {port, protocol} =
12781265 in -- TODO be more mindful, make sure to grab the
12791266 -- port from the right host address
12801267
1281- case inspectOutput
1282- ^? pre
1283- ( Optics. key " NetworkSettings"
1284- % Optics. key " Ports"
1285- % Optics. key textPort
1286- % Optics. values
1287- % Optics. key " HostPort"
1288- % Optics. _String
1289- ) of
1290- Nothing ->
1291- let containerName = inspectOutput ^? Optics. key " Name" % Optics. _String
1292- in throw $
1293- UnknownPortMapping
1294- { id ,
1295- port = textPort,
1296- imageName = Just (imageTag image),
1297- containerName = containerName
1298- }
1299- Just hostPort ->
1300- read (unpack hostPort)
1268+ let hostPorts = do
1269+ entry <-
1270+ maybe
1271+ []
1272+ eachValue
1273+ ( lookupKey " NetworkSettings" inspectOutput
1274+ >>= lookupKey " Ports"
1275+ >>= lookupKey (show port <> " /" <> unpack protocol)
1276+ )
1277+ maybe [] pure (lookupKey " HostPort" entry >>= asText)
1278+ in case listToMaybe hostPorts of
1279+ Nothing ->
1280+ let containerName = lookupKey " Name" inspectOutput >>= asText
1281+ in throw $
1282+ UnknownPortMapping
1283+ { id ,
1284+ port = textPort,
1285+ imageName = Just (imageTag image),
1286+ containerName = containerName
1287+ }
1288+ Just hostPort ->
1289+ read (unpack hostPort)
13011290
13021291-- | Returns the domain and port exposing the given container's port. Differs
13031292-- from 'containerPort' in that 'containerAddress' will return the container's
0 commit comments