argotk.hs 9.1 KB
Newer Older
1
#! /usr/bin/env runhaskell
2
{-# LANGUAGE OverloadedStrings, RecordWildCards, LambdaCase #-}
3 4 5

import           Argo.Stack
import           Argo.Utils
6
import           Argo.Args
7 8 9 10
import           Turtle
import           Prelude                 hiding ( FilePath )
import           Data.Default
import           System.Environment
Valentin Reis's avatar
Valentin Reis committed
11
import           Options.Applicative     hiding ( action )
12 13
import           Data.Text                     as T
                                                ( pack )
14

15
opts :: StackArgs -> Parser (Shell ())
16 17
opts sa = hsubparser
  (  command "clean"
18
             (info (pure $ clean sa) (progDesc "Clean sockets, logfiles."))
19
  <> mconcat (fmap commandTest [(minBound :: TestType) ..])
20 21
  <> commandTests [TestHello, TestListen, TestPerfwrapper]
                  "tests"
22
                  "Run hardware-independent CI tests"
23 24 25 26
  <> help
       "Type of test to run. There are extensive options under each action,\
       \ but be careful, these do not all have the same defaults. The default\
       \ values are printed when you call --help on these actions."
27
  )
28 29 30 31
 where
  action ttype = doOverridenTest ttype
    <$> parseExtendStackArgs ((stackArgsUpdate $ configureTest ttype) sa)
  descTest ttype = "test" ++ description (configureTest ttype)
Valentin Reis's avatar
Valentin Reis committed
32 33
  commandTest ttype =
    command (show ttype) $ info (action ttype) (progDesc $ descTest ttype)
Valentin Reis's avatar
Valentin Reis committed
34 35
  commandTests ttypes cmdStr descStr =
    command cmdStr $ info (pure $ mapM_ (doTest sa) ttypes) (progDesc descStr)
36

37 38 39 40 41 42 43
data TestType =
    DaemonOnly
  | DaemonAndApp
  | CsvLogs
  | TestHello
  | TestListen
  | TestPerfwrapper
44
  | TestPower
45 46 47 48
  | TestAMG
  | TestSTREAM
  | RunAMG
  | RunSTREAM deriving (Enum,Bounded,Show)
49

Valentin Reis's avatar
Valentin Reis committed
50 51
data TestSpec = TestSpec
  { stackArgsUpdate :: StackArgs -> StackArgs
52
  , isTest :: IsTest
Valentin Reis's avatar
Valentin Reis committed
53 54 55 56 57
  , description :: String }

doTest :: StackArgs -> TestType -> Shell ()
doTest stackArgs ttype = doSpec spec
  $ (stackArgsUpdate $ configureTest ttype) stackArgs
58 59 60
  where spec = configureTest ttype

doOverridenTest :: TestType -> StackArgs -> Shell ()
Valentin Reis's avatar
Valentin Reis committed
61
doOverridenTest ttype = doSpec spec where spec = configureTest ttype
62

Valentin Reis's avatar
Valentin Reis committed
63
doSpec :: TestSpec -> StackArgs -> Shell ()
64 65
doSpec spec stackArgs = do
  printTest $ T.pack $ description spec
66
  fullStack (isTest spec) stackArgs
67 68 69 70
  printSuccess "Test Successful."

configureTest :: TestType -> TestSpec
configureTest = \case
Valentin Reis's avatar
Valentin Reis committed
71 72 73
  DaemonOnly -> TestSpec
    { stackArgsUpdate = \sa -> sa { daemon = daemonBehavior }
    , description     = "Set up and launch the daemon in synchronous mode."
74
    , isTest          = IsTest False
Valentin Reis's avatar
Valentin Reis committed
75
    }
Valentin Reis's avatar
Valentin Reis committed
76
  DaemonAndApp -> TestSpec
77 78
    { stackArgsUpdate = \sa ->
      sa { daemon = daemonBehavior, cmdrun = runBehavior }
79 80 81 82
    , description     = "Set up and start daemon, run a command in a container."
    , isTest          = IsTest False
    }
  CsvLogs -> TestSpec
83
    { stackArgsUpdate = \sa -> sa
84 85 86
      { manifestName         = "perfwrap.json"
      , daemon               = daemonBehavior
      , cmdrun               = runBehavior
87 88 89
      , cmdlistenperformance = JustRun (StdOutLog "performance.csv")
                                       (StdErrLog "performance.log")
      , cmdlistenpower = JustRun (StdOutLog "power.csv") (StdErrLog "power.log")
90 91
      , cmdlistenprogress    = JustRun (StdOutLog "progress.csv")
                                       (StdErrLog "progress.log")
92
      }
Valentin Reis's avatar
Valentin Reis committed
93
    , description     = "Set up and start daemon, run a command in a container."
94
    , isTest          = IsTest False
Valentin Reis's avatar
Valentin Reis committed
95
    }
96
  TestHello -> TestSpec
Valentin Reis's avatar
Valentin Reis committed
97
    { stackArgsUpdate = \sa -> sa
Valentin Reis's avatar
Valentin Reis committed
98
      { app    = AppName "echo"
99
      , args   = [AppArg msg]
Valentin Reis's avatar
Valentin Reis committed
100 101 102 103
      , daemon = daemonBehavior
      , cmdrun = SucceedTestOnMessage (TestText msg)
                                      (StdOutLog "monitored-cmdrun-out.log")
                                      (StdErrLog "monitored-cmdrun-err.log")
Valentin Reis's avatar
Valentin Reis committed
104
      }
105 106
    , description = "1: Setup stack and check that a hello world app sends \
                    \message back to cmd."
107
    , isTest = IsTest True
108 109
    }
  TestListen -> TestSpec
Valentin Reis's avatar
Valentin Reis committed
110 111
    { stackArgsUpdate = \sa -> sa
      { app       = AppName "sleep"
112
      , args      = [AppArg "15"]
Valentin Reis's avatar
Valentin Reis committed
113 114
      , daemon    = daemonBehavior
      , cmdrun    = runBehavior
115
      , cmdlisten = listentestBehavior (TestText ",")
Valentin Reis's avatar
Valentin Reis committed
116
      }
117 118
    , description = "2: Setup stack and check that argo-perf-wrapper sends\
                    \ at least one message to the daemon."
119
    , isTest = IsTest True
120 121
    }
  TestPerfwrapper -> TestSpec
Valentin Reis's avatar
Valentin Reis committed
122 123 124
    { stackArgsUpdate = \sa -> sa
      { manifestName = "perfwrap.json"
      , app          = AppName "sleep"
125
      , args         = [AppArg "15"]
Valentin Reis's avatar
Valentin Reis committed
126 127 128
      , daemon       = daemonBehavior
      , cmdrun       = runBehavior
      , cmdlisten    = listentestBehavior (TestText "performance")
Valentin Reis's avatar
Valentin Reis committed
129
      }
130 131 132
    , description = "3: Setup stack and check that argo-perf-wrapper sends\
                    \ at least one *performance* message to cmd listen through the\
                    \ daemon."
133
    , isTest = IsTest True
134
    }
Valentin Reis's avatar
Valentin Reis committed
135
  TestPower -> TestSpec
Valentin Reis's avatar
Valentin Reis committed
136 137
    { stackArgsUpdate = \sa -> sa
      { app       = AppName "sleep"
138
      , args      = [AppArg "15"]
Valentin Reis's avatar
Valentin Reis committed
139 140
      , daemon    = daemonBehavior
      , cmdrun    = runBehavior
Valentin Reis's avatar
Valentin Reis committed
141 142
      , cmdlisten = listentestBehavior (TestText "power")
      }
Valentin Reis's avatar
Valentin Reis committed
143 144 145
    , description = "4: Setup stack and check that argo-perf-wrapper sends\
                    \ at least one *power* message to cmd listen through the\
                    \ daemon."
146
    , isTest = IsTest True
Valentin Reis's avatar
Valentin Reis committed
147
    }
148 149
  TestAMG -> TestSpec
    { stackArgsUpdate = \sa -> sa
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
      { manifestName      = "parallel.json"
      , app               = AppName "mpiexec"
      , args              = [ AppArg "-n"
                            , AppArg "24"
                            , AppArg "amg"
                            , AppArg "-problem"
                            , AppArg "2"
                            , AppArg "-n"
                            , AppArg "1"
                            , AppArg "1"
                            , AppArg "1"
                            , AppArg "-P"
                            , AppArg "8"
                            , AppArg "3"
                            , AppArg "1"
                            ]
      , daemon            = daemonBehavior
      , cmdrun            = runBehavior
      , cmdlistenprogress = listenprogresstestBehavior (TestText ",")
      }
    , description = "5: Setup stack, run STREAM and check that it sends\
                    \ at least one progress message to the daemon."
    , isTest = IsTest True
    }
  TestSTREAM -> TestSpec
    { stackArgsUpdate = \sa -> sa
      { app               = AppName "stream_c_20"
      , args              = []
      , daemon            = daemonBehavior
      , cmdrun            = runBehavior
      , cmdlistenprogress = listenprogresstestBehavior (TestText ",")
      }
    , description = "6: Setup stack, run AMG and check that it sends\
                    \ at least one progress message to the daemon."
    , isTest = IsTest True
    }
  RunAMG -> runAppSpec
    (AppName "mpiexec")
    [ AppArg "-n"
    , AppArg "24"
    , AppArg "amg"
    , AppArg "-problem"
    , AppArg "2"
    , AppArg "-n"
    , AppArg "10"
    , AppArg "10"
    , AppArg "10"
    , AppArg "-P"
    , AppArg "8"
    , AppArg "3"
    , AppArg "1"
    ]
  RunSTREAM -> runAppSpec (AppName "stream_c_20000") []
 where
  runAppSpec appName appArgs = TestSpec
    { stackArgsUpdate = \sa -> sa
      { app                  = appName
      , args                 = appArgs
      , manifestName         = "parallel.json"
209 210 211 212 213 214 215 216
      , daemon               = daemonBehavior
      , cmdrun               = runBehavior
      , cmdlistenperformance = JustRun (StdOutLog "performance.csv")
                                       (StdErrLog "performance.log")
      , cmdlistenpower = JustRun (StdOutLog "power.csv") (StdErrLog "power.log")
      , cmdlistenprogress    = JustRun (StdOutLog "progress.csv")
                                       (StdErrLog "progress.log")
      }
217
    , description     = "Set up and start daemon, run app in a container."
218 219
    , isTest          = IsTest False
    }
Valentin Reis's avatar
Valentin Reis committed
220
  msg = "someComplicatedMessage"
221 222 223 224
  daemonBehavior =
    JustRun (StdOutLog "daemon_out.log") (StdErrLog "daemon_err.log")
  runBehavior =
    JustRun (StdOutLog "cmd_run_out.log") (StdErrLog "cmd_run_err.log")
Valentin Reis's avatar
Valentin Reis committed
225 226 227 228
  listentestBehavior t = SucceedTestOnMessage
    t
    (StdOutLog "cmd_listen_out.log")
    (StdErrLog "cmd_listen_err.log")
229 230 231 232
  listenprogresstestBehavior t = SucceedTestOnMessage
    t
    (StdOutLog "cmd_listen_progress_out.log")
    (StdErrLog "cmd_listen_progress_err.log")
233

234 235 236 237 238 239 240 241 242 243 244 245 246 247
newtype IsTest = IsTest Bool

fullStack :: IsTest -> StackArgs -> Shell ()
fullStack (IsTest b) a@StackArgs {..} = runStack a >>= \case
  FoundMessage          -> printSuccess "Found message!\n"
  Died stacki errorcode -> if b
    then
      printError
          (  repr stacki
          <> " died before a message could be found:"
          <> repr errorcode
          <> "\n"
          )
        >> exit (ExitFailure 1)
248
    else exit ExitSuccess
249

250
clean :: StackArgs -> Shell ()
Valentin Reis's avatar
Valentin Reis committed
251
clean StackArgs {..} = cleanLeftovers workingDirectory
252 253 254 255

main :: IO ()
main = do
  manifests <- getEnv "MANIFESTS"
Valentin Reis's avatar
Valentin Reis committed
256
  let a = def { manifestDir = ManifestDir $ decodeString manifests }
257 258
  turtle <- execParser (info (opts a <**> helper) idm)
  sh turtle