argotk.hs 14.7 KB
Newer Older
1 2 3 4
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE QuasiQuotes #-}
Valentin Reis's avatar
Valentin Reis committed
5

Valentin Reis's avatar
Valentin Reis committed
6 7 8 9 10 11 12
{-|
Module      : argotk.hs
Description : argo provisioner/executor
Copyright   : (c) Valentin Reis, 2018
License     : MIT
Maintainer  : fre@freux.fr -}

13
import           NeatInterpolation
14
import Data.Coerce (coerce)
Valentin Reis's avatar
Valentin Reis committed
15 16 17
import           Argo.Stack
import           Argo.Utils
import           Argo.Args
18
import           Turtle hiding (text)
Valentin Reis's avatar
Valentin Reis committed
19 20 21 22 23
import           Prelude                 hiding ( FilePath )
import           Data.Default
import           System.Environment
import           Options.Applicative     hiding ( action )
import           Data.Text                     as T
24 25 26
                                                ( unpack
                                                , Text
                                                )
Valentin Reis's avatar
Valentin Reis committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

opts :: StackArgs -> Parser (Shell ())
opts sa = hsubparser
  (  command "clean"
             (info (pure $ clean sa) (progDesc "Clean sockets, logfiles."))
  <> mconcat (fmap commandTest [(minBound :: TestType) ..])
  <> commandTests [TestHello, TestListen, TestPerfwrapper, TestSTREAM]
                  "tests"
                  "Run hardware-independent CI tests"
  <> 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."
  )
 where
  action ttype = doOverridenTest ttype
    <$> parseExtendStackArgs ((stackArgsUpdate $ configureTest ttype) sa)
Valentin Reis's avatar
Valentin Reis committed
44
  descTest ttype = description (configureTest ttype)
Valentin Reis's avatar
Valentin Reis committed
45
  commandTest ttype =
46
    command (show ttype) $ info (action ttype) (progDesc $ T.unpack $ descTest ttype)
Valentin Reis's avatar
Valentin Reis committed
47
  commandTests ttypes cmdStr descStr =
48
    command cmdStr $ info (pure $ mapM_ (doTest sa) ttypes) (progDesc $ T.unpack descStr)
Valentin Reis's avatar
Valentin Reis committed
49 50 51 52 53 54 55 56 57 58 59

data TestType =
    DaemonOnly
  | DaemonAndApp
  | CsvLogs
  | TestHello
  | TestListen
  | TestPerfwrapper
  | TestPower
  | TestSTREAM
  | RunAMG
Valentin Reis's avatar
Valentin Reis committed
60
  | RunQMCPack
Valentin Reis's avatar
Valentin Reis committed
61
  | RunOpenMC
Valentin Reis's avatar
Valentin Reis committed
62 63
  | RunSTREAM
  | RunLAMMPS deriving (Enum,Bounded,Show)
Valentin Reis's avatar
Valentin Reis committed
64 65 66 67

data TestSpec = TestSpec
  { stackArgsUpdate :: StackArgs -> StackArgs
  , isTest :: IsTest
68
  , description :: Text }
Valentin Reis's avatar
Valentin Reis committed
69

Valentin Reis's avatar
Valentin Reis committed
70 71 72 73 74 75
instance Default TestSpec where
  def = TestSpec { stackArgsUpdate = id
                 , isTest = NotTest
                 , description = ""
                 }

Valentin Reis's avatar
Valentin Reis committed
76 77 78 79 80 81 82 83 84 85
doTest :: StackArgs -> TestType -> Shell ()
doTest stackArgs ttype = doSpec spec
  $ (stackArgsUpdate $ configureTest ttype) stackArgs
  where spec = configureTest ttype

doOverridenTest :: TestType -> StackArgs -> Shell ()
doOverridenTest ttype = doSpec spec where spec = configureTest ttype

doSpec :: TestSpec -> StackArgs -> Shell ()
doSpec spec stackArgs = do
86
  printTest $ description spec
Valentin Reis's avatar
Valentin Reis committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  fullStack (isTest spec) stackArgs
  printSuccess "Test Successful.\n"

configureTest :: TestType -> TestSpec
configureTest = \case
  DaemonOnly -> TestSpec
    { stackArgsUpdate = \sa -> sa { daemon = daemonBehavior }
    , description     = "Set up and launch the daemon in synchronous mode."
    , isTest          = NotTest
    }
  DaemonAndApp -> TestSpec
    { stackArgsUpdate = \sa ->
      sa { daemon = daemonBehavior, cmdrun = runBehavior }
    , description     = "Set up and start daemon, run a command in a container."
    , isTest          = NotTest
    }
  CsvLogs -> TestSpec
    { stackArgsUpdate = \sa -> sa
      { manifestName         = "perfwrap.json"
      , 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")
      }
114 115 116 117 118
    , description = [text|
                      Set up and start daemon, run a command in a container and\
                      log performance+power+progress.
                    |]
    , isTest = NotTest
Valentin Reis's avatar
Valentin Reis committed
119 120 121 122 123 124 125 126 127 128 129 130 131
    }
  TestHello -> TestSpec
    { stackArgsUpdate = \sa -> sa
      { app    = AppName "echo"
      , args   = [AppArg msg]
      , daemon = daemonBehavior
      , cmdrun = Test
                   (TestText (TextBehaviorStdout (WaitFor msg))
                             (TextBehaviorStderr ExpectClean)
                   )
                   (StdOutLog "monitored-cmdrun-out.log")
                   (StdErrLog "monitored-cmdrun-err.log")
      }
132 133 134 135
    , description = [text|
                      Setup stack and check that a hello world app sends
                      message back to cmd's stdout.
                    |]
Valentin Reis's avatar
Valentin Reis committed
136 137 138 139 140 141 142 143 144
    , isTest = IsTest
    }
  TestListen -> TestSpec
    { stackArgsUpdate = \sa -> sa
      { app       = AppName "sleep"
      , args      = [AppArg "1"]
      , daemon    = daemonBehavior
      , cmdrun    = runBehavior
      , cmdlisten = listentestBehavior
145 146 147
                      (TestText
                        (TextBehaviorStdout (WaitFor "container_exit"))
                        (TextBehaviorStderr ExpectClean)
Valentin Reis's avatar
Valentin Reis committed
148 149
                      )
      }
150 151 152 153
    , description = [text|
                      Setup stack, run command and check that cmd listen receives
                      at least the container_exit message from the daemon.
                    |]
Valentin Reis's avatar
Valentin Reis committed
154 155 156 157 158 159 160 161 162 163
    , isTest = IsTest
    }
  TestPerfwrapper -> TestSpec
    { stackArgsUpdate = \sa -> sa
      { manifestName         = "perfwrap.json"
      , app                  = AppName "sleep"
      , args                 = [AppArg "15"]
      , daemon               = daemonBehavior
      , cmdrun               = runBehavior
      , cmdlistenperformance = listenperformancetestBehavior
164 165 166
                                 (TestText
                                   (TextBehaviorStdout (WaitFor "performance"))
                                   (TextBehaviorStderr ExpectClean)
Valentin Reis's avatar
Valentin Reis committed
167 168
                                 )
      }
169 170 171 172 173
    , description = [text|
                      Setup stack and check that argo-perf-wrapper sends
                       at least one *performance* message to cmd listen through the
                       daemon.
                    |]
Valentin Reis's avatar
Valentin Reis committed
174 175 176 177 178 179 180 181 182 183 184 185 186
    , isTest = IsTest
    }
  TestPower -> TestSpec
    { stackArgsUpdate = \sa -> sa
      { app            = AppName "sleep"
      , args           = [AppArg "15"]
      , daemon         = daemonBehavior
      , cmdrun         = runBehavior
      , cmdlistenpower = listenpowertestBehavior
                           (TestText (TextBehaviorStdout (WaitFor "power"))
                                     (TextBehaviorStderr ExpectClean)
                           )
      }
187 188 189 190
    , description = [text|
                      Setup stack and check that the daemon sends
                      at least one *power* message to cmd listen.
                    |]
Valentin Reis's avatar
Valentin Reis committed
191 192 193 194 195 196 197 198 199
    , isTest = IsTest
    }
  TestSTREAM -> TestSpec
    { stackArgsUpdate = \sa -> sa
      { app               = AppName "stream_c_20"
      , args              = []
      , daemon            = daemonBehavior
      , cmdrun            = runBehavior
      , cmdlistenprogress = listenprogresstestBehavior
200 201 202
                              (TestText
                                (TextBehaviorStdout (WaitFor "progress"))
                                (TextBehaviorStderr ExpectClean)
Valentin Reis's avatar
Valentin Reis committed
203 204
                              )
      }
205 206 207 208
    , description = [text|
                      Setup stack, run STREAM and check that it sends
                      at least one progress message to the daemon.
                    |]
Valentin Reis's avatar
Valentin Reis committed
209 210
    , isTest = IsTest
    }
Valentin Reis's avatar
Valentin Reis committed
211 212 213
  RunOpenMC ->
    TestSpec
      { stackArgsUpdate = \sa -> sa
Valentin Reis's avatar
Valentin Reis committed
214 215
        { preludeCommand = PreludeCommand "cp -r $OPENMC_PWD/* ."
        , app = AppName "mpiexec"
Valentin Reis's avatar
Valentin Reis committed
216 217 218
        , args =
          let tc = coerce (hwThreadCount sa):: Int
          in
Valentin Reis's avatar
Valentin Reis committed
219
          [ AppArg "-n"
Valentin Reis's avatar
Valentin Reis committed
220 221 222 223 224 225 226 227 228 229 230 231
           , AppArg $ repr tc
           , AppArg "openmc"
           ]
        , manifestName         = "parallel.json"
        , 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")
        }
Valentin Reis's avatar
Valentin Reis committed
232
      , description     = "Run OpenMC in the argo stack."
Valentin Reis's avatar
Valentin Reis committed
233 234
      , isTest          = NotTest
      }
Valentin Reis's avatar
Valentin Reis committed
235 236 237 238 239 240 241 242 243 244 245 246
  RunQMCPack ->
    TestSpec
      { stackArgsUpdate = \sa -> sa
        { app = AppName "mpirun"
        , args =
          let tc = coerce (hwThreadCount sa):: Int
              (ShareDir dirn) = shareDir sa
              Right inpath = toText (dirn </> "simple-H2O.xml")
          in
          [ AppArg "-n"
           , AppArg $ repr tc
           , AppArg "qmcpack"
Valentin Reis's avatar
Valentin Reis committed
247
           , AppArg inpath
Valentin Reis's avatar
Valentin Reis committed
248 249 250 251 252 253 254 255 256 257 258 259 260
           ]
        , manifestName         = "parallel.json"
        , 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")
        }
      , description     = "Run QMCPACK in the argo stack."
      , isTest          = NotTest
      }
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
  RunAMG ->
    TestSpec
      { stackArgsUpdate = \sa -> sa
        { app = AppName "mpiexec"
        , args = let tc = coerce (hwThreadCount sa):: Int in
          [ AppArg "-n"
           , AppArg $ repr tc
           , AppArg "amg"
           , AppArg "-problem"
           , AppArg "2"
           , AppArg "-n"
           , AppArg "3"
           , AppArg "3"
           , AppArg "3"
           , AppArg "-P"
           , AppArg "8"
           , AppArg $ repr $ quot tc 8
           , AppArg "1"
           ]
        , manifestName         = "parallel.json"
        , 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")
        }
Valentin Reis's avatar
Valentin Reis committed
289
      , description     = "Run AMG in the argo stack."
290 291
      , isTest          = NotTest
      }
Valentin Reis's avatar
Valentin Reis committed
292
  RunSTREAM -> runAppSpec (AppName "stream_c_2000") []
Valentin Reis's avatar
Valentin Reis committed
293 294
  RunLAMMPS -> TestSpec
      { stackArgsUpdate = \sa -> sa
Valentin Reis's avatar
Valentin Reis committed
295
        { app                  = AppName "mpirun"
Valentin Reis's avatar
Valentin Reis committed
296
        , args                 =
Valentin Reis's avatar
adding  
Valentin Reis committed
297 298
          let (ShareDir dirn)  = shareDir sa
              Right inpath = toText (dirn </> "modified.lj")
Valentin Reis's avatar
Valentin Reis committed
299
           in [ AppArg "-n"
300
              , AppArg $ repr (coerce (hwThreadCount sa):: Int)
Valentin Reis's avatar
things  
Valentin Reis committed
301
              , AppArg "lmp_mpi"
Valentin Reis's avatar
Valentin Reis committed
302
              , AppArg "-i"
Valentin Reis's avatar
Valentin Reis committed
303
              , AppArg inpath
Valentin Reis's avatar
Valentin Reis committed
304 305 306 307 308 309 310 311 312 313
              ]
        , manifestName         = "parallel.json"
        , 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")
        }
Valentin Reis's avatar
Valentin Reis committed
314
      , description     = "Run LAMMPS in the argo stack."
Valentin Reis's avatar
Valentin Reis committed
315 316
      , isTest          = NotTest
      }
Valentin Reis's avatar
Valentin Reis committed
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
 where
  runAppSpec appName appArgs = TestSpec
    { stackArgsUpdate = \sa -> sa
      { app                  = appName
      , args                 = appArgs
      , manifestName         = "parallel.json"
      , 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")
      }
    , description     = "Set up and start daemon, run app in a container."
    , isTest          = NotTest
    }
  msg = "someComplicatedMessage"
  daemonBehavior =
    JustRun (StdOutLog "daemon_out.log") (StdErrLog "daemon_err.log")
  runBehavior =
    JustRun (StdOutLog "cmd_run_out.log") (StdErrLog "cmd_run_err.log")
  listentestBehavior t = Test t
                              (StdOutLog "cmd_listen_stdout.log")
                              (StdErrLog "cmd_listen_stderr.log")
  listenprogresstestBehavior t =
    Test t (StdOutLog "progress_stdout.csv") (StdErrLog "progress_stderr.log")
  listenperformancetestBehavior t = Test t
                                         (StdOutLog "performance_stdout.csv")
                                         (StdErrLog "performance_stderr.log")
  listenpowertestBehavior t =
    Test t (StdOutLog "power_stdout.csv") (StdErrLog "power_stderr.log")

data IsTest = IsTest | NotTest

fullStack :: IsTest -> StackArgs -> Shell ()
fullStack isTest a@StackArgs {..} = do
  stackOutput <- runStack a
  case stackOutput of
    FoundMessage    msg -> printSuccess $ "Found string in message:" <> repr msg
    FoundTracebacks tsl -> do
      mapM_
        (\(stacki, fout, ferr) ->
          printError
            $  "Found Python Traceback when executing "
            <> repr stacki
            <> ". Files for this command: "
            <> repr fout
            <> " "
            <> repr ferr
        )
        tsl
      exit (ExitFailure 1)
370
    Died stacki errorcode _ _ tsl -> case isTest of
Valentin Reis's avatar
Valentin Reis committed
371 372
      IsTest -> do
        printError
373 374 375 376
          (  repr stacki
          <> " died before a message could be found with error code "
          <> repr errorcode
          )
Valentin Reis's avatar
Valentin Reis committed
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
        mapM_
          (\(stacki', fout, ferr) ->
            printError
              $  "Found Python Traceback when executing "
              <> repr stacki'
              <> ". Files for this command: "
              <> repr fout
              <> " "
              <> repr ferr
          )
          tsl
        exit (ExitFailure 1)
      NotTest -> exit ExitSuccess

clean :: StackArgs -> Shell ()
clean StackArgs {..} = cleanLeftovers workingDirectory

main :: IO ()
main = do
Valentin Reis's avatar
Valentin Reis committed
396
  argonixShare <- getEnv "ARGOTK_SHARE"
397 398 399
  hwlocTC <- single $ inshell "hwloc-calc machine:0 -N PU" empty
  let a = def { shareDir = ShareDir $ decodeString argonixShare
              , hwThreadCount = HwThreadCount (read $ unpack $ lineToText hwlocTC) }
Valentin Reis's avatar
Valentin Reis committed
400 401
  turtle <- execParser (info (opts a <**> helper) idm)
  sh turtle