Skip to content

Commit

Permalink
fixup! [testcontainers#50] Consider Docker host IP during port mappin…
Browse files Browse the repository at this point in the history
…g resolution
  • Loading branch information
rvem committed Apr 22, 2024
1 parent 3407d06 commit b36fb18
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
15 changes: 8 additions & 7 deletions src/TestContainers/Docker.hs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ import TestContainers.Monad
TestContainer,
)
import TestContainers.Trace (Trace (..), Tracer, newTracer, withTrace)
import Text.Read (readMaybe)
import Prelude hiding (error, id)
import qualified Prelude

Expand Down Expand Up @@ -942,10 +943,10 @@ waitForHttp port path acceptableStatusCodes = WaitReady $ \container -> do
retry manager = do
let (endpointHost, endpointPort) =
containerAddress container port
endpointHostText = pack $ case endpointHost of
IPv4 addr -> show addr
endpointHostText = case readMaybe $ unpack endpointHost of
-- Ugly, but http-client expects IPv6 address to be wrapped by '[]'
IPv6 addr -> "[" <> show addr <> "]"
Just (IPv6 _) -> "[" <> endpointHost <> "]"
_ -> endpointHost
let request =
defaultRequest
{ host = encodeUtf8 endpointHostText,
Expand Down Expand Up @@ -1001,7 +1002,7 @@ waitUntilMappedPortReachable port = WaitReady $ \container -> do
wait = do
let (endpointHost, endpointPort) =
containerAddress container port
result <- try (resolve (show endpointHost) endpointPort >>= open)
result <- try (resolve (unpack endpointHost) endpointPort >>= open)
case result of
Right socket -> do
withTrace configTracer (TraceOpenSocket endpointHost endpointPort Nothing)
Expand Down Expand Up @@ -1283,12 +1284,12 @@ containerPort Container {id, inspectOutput, hostIp} Port {port, protocol} =
-- 'containerAddress' will use the exposed port on the Docker host.
--
-- @since 0.5.0.0
containerAddress :: Container -> Port -> (IP, Int)
containerAddress :: Container -> Port -> (Text, Int)
containerAddress container Port {port, protocol} =
let inDocker = unsafePerformIO isRunningInDocker
in if inDocker
then (containerIPAddress container, port)
else (hostIp container, containerPort container (Port {port, protocol}))
then (pack $ show $ containerIPAddress container, port)
else (pack $ show $ hostIp container, containerPort container (Port {port, protocol}))

-- | Runs the `docker inspect` command. Memoizes the result.
--
Expand Down
7 changes: 3 additions & 4 deletions src/TestContainers/Docker/Reaper.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ where

import Control.Monad (replicateM)
import Control.Monad.Trans.Resource (MonadResource, allocate)
import Data.IP (IP)
import Data.Text (Text, pack)
import Data.Text (Text, pack, unpack)
import Data.Text.Encoding (encodeUtf8)
import qualified Network.Socket as Socket
import qualified Network.Socket.ByteString as Socket
Expand Down Expand Up @@ -74,7 +73,7 @@ ryukPort =
newRyukReaper ::
(MonadResource m) =>
-- | Host
IP ->
Text ->
-- | Port
Int ->
m Reaper
Expand All @@ -88,7 +87,7 @@ newRyukReaper host port = do
let hints =
Socket.defaultHints {Socket.addrSocketType = Socket.Stream}
address <-
head <$> Socket.getAddrInfo (Just hints) (Just (show host)) (Just (show port))
head <$> Socket.getAddrInfo (Just hints) (Just (unpack host)) (Just (show port))
socket <-
Socket.socket
(Socket.addrFamily address)
Expand Down
5 changes: 2 additions & 3 deletions src/TestContainers/Trace.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ where

import Control.Exception (IOException)
import Control.Monad.IO.Class (MonadIO, liftIO)
import Data.IP (IP)
import Data.Text (Text)
import System.Exit (ExitCode)

Expand All @@ -36,9 +35,9 @@ data Trace
-- timeout to wait (in seconds).
TraceWaitUntilReady (Maybe Int)
| -- | Opening socket
TraceOpenSocket IP Int (Maybe IOException)
TraceOpenSocket Text Int (Maybe IOException)
| -- | Call HTTP endpoint
TraceHttpCall IP Int (Either String Int)
TraceHttpCall Text Int (Either String Int)
deriving stock (Eq, Show)

-- | Traces execution within testcontainers library.
Expand Down

0 comments on commit b36fb18

Please sign in to comment.