From 0b85218588c607abb9c4fb96c0ae88a144b62f35 Mon Sep 17 00:00:00 2001 From: Rodney Lorrimar Date: Fri, 22 May 2020 01:21:15 +1000 Subject: [PATCH] Fix port selection in the db migration tests 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. --- .../test/migration/migration-test.hs | 19 +++++++++++++++---- nix/launch-migration-test.sh | 9 ++++++++- nix/migration-tests.nix | 1 + 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/lib/jormungandr/test/migration/migration-test.hs b/lib/jormungandr/test/migration/migration-test.hs index d579f460ab9..c43ac5291cf 100644 --- a/lib/jormungandr/test/migration/migration-test.hs +++ b/lib/jormungandr/test/migration/migration-test.hs @@ -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 @@ -74,6 +74,8 @@ import System.Environment ( getArgs ) import System.Exit ( ExitCode (..), exitFailure, exitWith ) +import Text.Read + ( readMaybe ) import qualified Data.Text as T @@ -83,7 +85,7 @@ 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 @@ -91,11 +93,11 @@ type TestAction (t :: NetworkDiscriminant) = Trace IO Text -> ApiBase -> IO Exit 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 @@ -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) diff --git a/nix/launch-migration-test.sh b/nix/launch-migration-test.sh index 04117e83048..bbfb31272da 100755 --- a/nix/launch-migration-test.sh +++ b/nix/launch-migration-test.sh @@ -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 diff --git a/nix/migration-tests.nix b/nix/migration-tests.nix index da49b463e08..5f2cbd2ea1a 100644 --- a/nix/migration-tests.nix +++ b/nix/migration-tests.nix @@ -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