Skip to content

Commit

Permalink
Fix port selection in the db migration tests
Browse files Browse the repository at this point in the history
Each test running in turn was using the same port.

However due to the SO_LINGER TCP socket option, the port remains in
use for a short time after the wallet server process exits.
  • Loading branch information
rvl committed May 21, 2020
1 parent 64d48e9 commit 0b85218
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
19 changes: 15 additions & 4 deletions lib/jormungandr/test/migration/migration-test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ import Data.Aeson
import Data.Aeson.Lens
( key, values, _Integral, _String )
import Data.Maybe
( isNothing )
( fromMaybe, isNothing )
import Data.Text
( Text )
import Network.Wreq
Expand All @@ -74,6 +74,8 @@ import System.Environment
( getArgs )
import System.Exit
( ExitCode (..), exitFailure, exitWith )
import Text.Read
( readMaybe )

import qualified Data.Text as T

Expand All @@ -83,19 +85,19 @@ main = do

cfg <- defaultConfigStdout
withTrace cfg "migration-test" $ \tr ->
testMain @('Testnet 0) tr 9090 testAction launchArgs >>= exitWith
testMain @('Testnet 0) tr testAction launchArgs >>= exitWith

-- | Something to do while the server is running.
type TestAction (t :: NetworkDiscriminant) = Trace IO Text -> ApiBase -> IO ExitCode

testMain
:: forall t. (DecodeAddress t, EncodeAddress t)
=> Trace IO Text
-> Int
-> TestAction t
-> [String]
-> IO ExitCode
testMain tr serverPort testAction launchArgs = do
testMain tr testAction launchArgs = do
let serverPort = getServerPort launchArgs
let apiBase = mkApiBase serverPort
let cmd = Command "cardano-wallet-jormungandr" launchArgs (pure ()) Inherit Inherit
res <- withBackendProcess (trMessageText tr) cmd $ do
Expand All @@ -113,6 +115,15 @@ testMain tr serverPort testAction launchArgs = do
logError tr ("Process exited with status " <> T.pack (show st))
pure (ExitFailure 14)

-- | Scan through cardano-wallet server CLI arguments to find a port.
-- Returns the default 8090 if none found.
getServerPort :: [String] -> Int
getServerPort = fromMaybe 8090 . portArg
where
portArg ("--port":port:_) = readMaybe port
portArg (_:args) = portArg args
portArg [] = Nothing

-- | @run@ action
doRun
:: forall t. (DecodeAddress t, EncodeAddress t)
Expand Down
9 changes: 8 additions & 1 deletion nix/launch-migration-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ if [ -z "${configFile:-}" ]; then
exit 1
fi

# Find an unused TCP port to run the test on.
# Source: https://unix.stackexchange.com/a/132524
find_unused_port() {
python -c 'import socket; s=socket.socket(); s.bind(("", 0)); print(s.getsockname()[1]); s.close()'
}

# Sanity-check versions
cardano-wallet-jormungandr version
jormungandr --version

exec migration-test $1 launch --port 9090 --state-dir $stateDir --genesis-block $genesisDataDir/block0.bin -- --secret $genesisDataDir/secret.yaml --config $configFile
exec migration-test $1 launch --port "$(find_unused_port)" --state-dir $stateDir --genesis-block $genesisDataDir/block0.bin -- --secret $genesisDataDir/secret.yaml --config $configFile
1 change: 1 addition & 0 deletions nix/migration-tests.nix
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ let
migrationTest
pkgs.bash
pkgs.coreutils
pkgs.python3
]}
export genesisDataDir=${latestRelease.src}/lib/jormungandr/test/data/jormungandr
export configFile=${targetRelease.src}/lib/jormungandr/test/data/jormungandr/config.yaml
Expand Down

0 comments on commit 0b85218

Please sign in to comment.