Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit 1a8f9ec

Browse files
ksaricHiroto Shioi
authored and
Hiroto Shioi
committed
[GH-313] Test together launcher/updater. (#331)
* [GH-313] Test together launcher/updater. * --amend
1 parent cd154b0 commit 1a8f9ec

File tree

5 files changed

+77
-49
lines changed

5 files changed

+77
-49
lines changed

cardano-launcher/app/Main.hs

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,10 @@ import Cardano.Shell.Configuration (ConfigurationOptions (..),
2626
getWPath, getWargs,
2727
setWorkingDirectory)
2828
import Cardano.Shell.Launcher (LoggingDependencies (..), TLSError,
29-
TLSPath (..), WalletMode (..),
30-
generateTlsCertificates,
31-
runWalletProcess, walletRunnerProcess)
32-
import Cardano.Shell.Update.Lib (RemoveArchiveAfterInstall (..),
33-
UpdaterData (..),
34-
runDefaultUpdateProcess, runUpdater)
29+
TLSPath (..), generateTlsCertificates,
30+
runLauncher, walletRunnerProcess)
31+
import Cardano.Shell.Update.Lib (UpdaterData (..),
32+
runDefaultUpdateProcess)
3533
import Control.Exception.Safe (throwM)
3634
import Data.Text.Lazy.Builder (fromString, fromText)
3735

@@ -77,8 +75,7 @@ main = silence $ do
7775
Trace.logError baseTrace $ "Working directory does not exist: " <> toS workingDir
7876
throwM . WorkingDirectoryDoesNotExist $ workingDir
7977

80-
-- Really no clue what to put there and how will the wallet work.
81-
-- These will be refactored in the future
78+
-- Configuration from the launcher options.
8279
let configurationOptions :: ConfigurationOptions
8380
configurationOptions = loConfiguration launcherOptions
8481

@@ -91,11 +88,12 @@ main = silence $ do
9188
let updaterData :: UpdaterData
9289
updaterData = getUpdaterData launcherOptions
9390

91+
9492
-- where to generate the certificates
9593
let mTlsPath :: Maybe TLSPath
9694
mTlsPath = TLSPath <$> loTlsPath launcherOptions
9795

98-
96+
-- If the path doesn't exist, then TLS has been disabled!
9997
case mTlsPath of
10098
Just tlsPath -> do
10199
-- | If we need to, we first check if there are certificates so we don't have
@@ -114,26 +112,17 @@ main = silence $ do
114112
Right _ -> return ()
115113
Nothing -> pure () -- TLS generation has been disabled
116114

117-
-- In the case the user wants to avoid installing the update now, we
118-
-- run the update (if there is one) when we have it downloaded.
119-
void $ runUpdater
120-
RemoveArchiveAfterInstall
121-
runDefaultUpdateProcess
122-
loggingDependencies
123-
updaterData
124-
125-
-- You still want to run the wallet even if the update fails
126-
exitCode <- runWalletProcess
115+
-- Finally, run the launcher once everything is set up!
116+
exitCode <- runLauncher
127117
loggingDependencies
128-
WalletModeNormal
118+
-- WalletPath -> WalletArguments -> IO ExitCode
119+
walletRunnerProcess
129120
walletPath
130121
walletArgs
131-
walletRunnerProcess
122+
-- FilePath -> [String] -> IO ExitCode
123+
runDefaultUpdateProcess
132124
updaterData
133125

134-
Trace.logNotice baseTrace $
135-
"Shutting down cardano-launcher with exitcode: " <> show exitCode
136-
137126
-- Exit the program with exit code.
138127
exitWith exitCode
139128

cardano-launcher/src/Cardano/Shell/Configuration.hs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
33

44
module Cardano.Shell.Configuration
5-
( WalletArguments(..)
6-
, WalletPath(..)
7-
, LauncherOptions(..)
8-
, ConfigurationOptions(..)
5+
( WalletArguments (..)
6+
, WalletPath (..)
7+
, LauncherOptions (..)
8+
, ConfigurationOptions (..)
99
-- * Getters
1010
, getUpdaterData
1111
, getWargs
@@ -42,14 +42,14 @@ newtype WalletPath = WalletPath
4242
-- Todo: Add haddock comment for each field
4343
-- | Launcher options
4444
data LauncherOptions = LauncherOptions
45-
{ loConfiguration :: !ConfigurationOptions
46-
, loTlsPath :: !(Maybe FilePath)
47-
, loUpdaterPath :: !FilePath
48-
, loUpdaterArgs :: ![Text]
49-
, loUpdateArchive :: !FilePath
50-
, loWalletPath :: !FilePath
51-
, loWalletArgs :: ![Text]
52-
, loWorkingDirectory :: !FilePath
45+
{ loConfiguration :: !ConfigurationOptions
46+
, loTlsPath :: !(Maybe FilePath)
47+
, loUpdaterPath :: !FilePath
48+
, loUpdaterArgs :: ![Text]
49+
, loUpdateArchive :: !FilePath
50+
, loWalletPath :: !FilePath
51+
, loWalletArgs :: ![Text]
52+
, loWorkingDirectory :: !FilePath
5353
-- On WIN it should set this directory as current.
5454
} deriving (Show, Generic)
5555

cardano-launcher/src/Cardano/Shell/Launcher.hs

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module Cardano.Shell.Launcher
99
, walletRunnerProcess
1010
, LoggingDependencies (..)
1111
-- * Functions
12+
, runLauncher
1213
, runWalletProcess
1314
-- * Critical exports (testing)
1415
, DaedalusExitCode (..)
@@ -34,8 +35,8 @@ import Cardano.Shell.Configuration (ConfigurationOptions (..),
3435
WalletPath (..))
3536
import Cardano.Shell.Types (LoggingDependencies (..))
3637
import Cardano.Shell.Update.Lib (RemoveArchiveAfterInstall (..),
37-
UpdaterData (..),
38-
runDefaultUpdateProcess, runUpdater)
38+
RunUpdateFunc, UpdaterData (..),
39+
runUpdater)
3940
import Cardano.X509.Configuration (ConfigurationKey (..),
4041
DirConfiguration (..), certChecks,
4142
certFilename, certOutDir,
@@ -167,9 +168,17 @@ runWalletProcess
167168
-> WalletPath
168169
-> WalletArguments
169170
-> WalletRunner
171+
-> RunUpdateFunc
170172
-> UpdaterData
171173
-> IO ExitCode
172-
runWalletProcess logDep walletMode walletPath walletArguments walletRunner updaterData = do
174+
runWalletProcess
175+
logDep
176+
walletMode
177+
walletPath
178+
walletArguments
179+
walletRunner
180+
runUpdateFunc
181+
updaterData = do
173182

174183
-- Parametrized by @WalletMode@ so we can change it on restart depending
175184
-- on the Daedalus exit code.
@@ -180,6 +189,7 @@ runWalletProcess logDep walletMode walletPath walletArguments walletRunner updat
180189
walletPath
181190
walletArguments
182191
walletRunner
192+
runUpdateFunc
183193
updaterData
184194

185195
-- Additional arguments we need to pass if it's a SAFE mode.
@@ -225,7 +235,7 @@ runWalletProcess logDep walletMode walletPath walletArguments walletRunner updat
225235
-- We separate the description of the computation, from the computation itself.
226236
-- There are other ways of doing this, of course.
227237
isoFrom <$> handleDaedalusExitCode
228-
(UpdateRunner $ runUpdater RemoveArchiveAfterInstall runDefaultUpdateProcess logDep updaterData)
238+
(UpdateRunner $ runUpdater logDep RemoveArchiveAfterInstall runUpdateFunc updaterData)
229239
(RestartRunner restart)
230240
exitCode
231241

@@ -259,6 +269,35 @@ newtype TLSPath = TLSPath { getTLSFilePath :: FilePath }
259269
-- Functions
260270
--------------------------------------------------------------------------------
261271

272+
-- | The function that runs the launcher once everything is set up!
273+
runLauncher
274+
:: LoggingDependencies
275+
-> WalletRunner
276+
-> WalletPath
277+
-> WalletArguments
278+
-> RunUpdateFunc
279+
-> UpdaterData
280+
-> IO ExitCode
281+
runLauncher loggingDependencies walletRunner walletPath walletArgs runUpdateFunc updaterData = do
282+
283+
-- In the case the user wants to avoid installing the update now, we
284+
-- run the update (if there is one) when we have it downloaded.
285+
void $ runUpdater
286+
loggingDependencies
287+
RemoveArchiveAfterInstall
288+
runUpdateFunc
289+
updaterData
290+
291+
-- You still want to run the wallet even if the update fails
292+
runWalletProcess
293+
loggingDependencies
294+
WalletModeNormal
295+
walletPath
296+
walletArgs
297+
walletRunner
298+
runUpdateFunc
299+
updaterData
300+
262301
-- | Generation of the TLS certificates.
263302
-- This just covers the generation of the TLS certificates and nothing else.
264303
generateTlsCertificates

cardano-launcher/src/Cardano/Shell/Update/Lib.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
module Cardano.Shell.Update.Lib
99
( UpdaterData (..)
10-
, RunCmdFunc
10+
, RunUpdateFunc
1111
, runUpdater
1212
, runDefaultUpdateProcess
1313
-- * Intepretable language
@@ -84,7 +84,7 @@ runDefaultUpdateProcess path args =
8484
$ \_in _out _err ph -> waitForProcess ph
8585

8686
-- The function for executing the update.
87-
type RunCmdFunc = FilePath -> [String] -> IO ExitCode
87+
type RunUpdateFunc = FilePath -> [String] -> IO ExitCode
8888

8989
-- | A flag for the existence of the updater file.
9090
data UpdaterExists
@@ -128,7 +128,7 @@ isUpdaterRunOnUnix (UnixRunUpdate _ _) = True
128128
isUpdaterRunOnUnix _ = False
129129

130130
-- | Interpret the small language into the "real" semantics.
131-
evaluateUpdaterCmdExitCode :: RunCmdFunc -> UpdaterCommand -> IO ExitCode
131+
evaluateUpdaterCmdExitCode :: RunUpdateFunc -> UpdaterCommand -> IO ExitCode
132132
evaluateUpdaterCmdExitCode runCommand = \case
133133
-- The update needs to be run on Windows.
134134
WindowsRunUpdate updaterPath args -> do
@@ -162,12 +162,12 @@ evaluateUpdaterCmdLogging loggingDep = \case
162162
-- first have to generate a @.bat@ file which will act as a script.
163163
-- After it being generated, you run that script.
164164
runUpdater
165-
:: RemoveArchiveAfterInstall
166-
-> RunCmdFunc
167-
-> LoggingDependencies
165+
:: LoggingDependencies
166+
-> RemoveArchiveAfterInstall
167+
-> RunUpdateFunc
168168
-> UpdaterData
169169
-> IO ExitCode
170-
runUpdater removeArchive runCommand loggingDep updaterData = do
170+
runUpdater loggingDep removeArchive runCommand updaterData = do
171171

172172
-- The update installation.
173173
let archivePath = udArchivePath updaterData

cardano-launcher/test/UpdaterSpec.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@ updaterSpec :: Spec
2424
updaterSpec = describe "Update system" $ do
2525
it "should be successful" $ monadicIO $ do
2626
exitCode <- run $ runUpdater
27+
nullLogging
2728
DoNotRemoveArchiveAfterInstall
2829
runDefaultUpdateProcess
29-
nullLogging
3030
testUpdaterData
3131

3232
run . putTextLn . show $ exitCode
3333
assert $ exitCode == ExitSuccess
3434

3535
prop "should return expected error" $ \(exitNum :: ExitNum) -> monadicIO $ do
3636
exitCode <- run $ runUpdater
37+
nullLogging
3738
DoNotRemoveArchiveAfterInstall
3839
(testRunCmd exitNum)
39-
nullLogging
4040
testUpdaterData
4141

4242
assert $ exitCode == (ExitFailure . getExitNum $ exitNum)

0 commit comments

Comments
 (0)