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

6
import           NeatInterpolation
Valentin Reis's avatar
Valentin Reis committed
7 8 9
import           Argo.Stack
import           Argo.Utils
import           Argo.Args
10
import           Turtle hiding (text)
Valentin Reis's avatar
Valentin Reis committed
11 12 13 14 15
import           Prelude                 hiding ( FilePath )
import           Data.Default
import           System.Environment
import           Options.Applicative     hiding ( action )
import           Data.Text                     as T
16 17 18
                                                ( unpack
                                                , Text
                                                )
Valentin Reis's avatar
Valentin Reis committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

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)
36
  descTest ttype = description (configureTest ttype)
Valentin Reis's avatar
Valentin Reis committed
37
  commandTest ttype =
38
    command (show ttype) $ info (action ttype) (progDesc $ T.unpack $ descTest ttype)
Valentin Reis's avatar
Valentin Reis committed
39
  commandTests ttypes cmdStr descStr =
40
    command cmdStr $ info (pure $ mapM_ (doTest sa) ttypes) (progDesc $ T.unpack descStr)
Valentin Reis's avatar
Valentin Reis committed
41 42 43 44 45 46 47 48 49 50 51

data TestType =
    DaemonOnly
  | DaemonAndApp
  | CsvLogs
  | TestHello
  | TestListen
  | TestPerfwrapper
  | TestPower
  | TestSTREAM
  | RunAMG
Valentin Reis's avatar
Valentin Reis committed
52 53
  | RunSTREAM
  | RunLAMMPS deriving (Enum,Bounded,Show)
Valentin Reis's avatar
Valentin Reis committed
54 55 56 57

data TestSpec = TestSpec
  { stackArgsUpdate :: StackArgs -> StackArgs
  , isTest :: IsTest
58
  , description :: Text }
Valentin Reis's avatar
Valentin Reis committed
59 60 61 62 63 64 65 66 67 68 69

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
70
  printTest $ description spec
Valentin Reis's avatar
Valentin Reis committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
  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")
      }
98 99 100 101 102
    , 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
103 104 105 106 107 108 109 110 111 112 113 114 115
    }
  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")
      }
116 117 118 119
    , 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
120 121 122 123 124 125 126 127 128
    , isTest = IsTest
    }
  TestListen -> TestSpec
    { stackArgsUpdate = \sa -> sa
      { app       = AppName "sleep"
      , args      = [AppArg "1"]
      , daemon    = daemonBehavior
      , cmdrun    = runBehavior
      , cmdlisten = listentestBehavior
129 130 131
                      (TestText
                        (TextBehaviorStdout (WaitFor "container_exit"))
                        (TextBehaviorStderr ExpectClean)
Valentin Reis's avatar
Valentin Reis committed
132 133
                      )
      }
134 135 136 137
    , 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
138 139 140 141 142 143 144 145 146 147
    , isTest = IsTest
    }
  TestPerfwrapper -> TestSpec
    { stackArgsUpdate = \sa -> sa
      { manifestName         = "perfwrap.json"
      , app                  = AppName "sleep"
      , args                 = [AppArg "15"]
      , daemon               = daemonBehavior
      , cmdrun               = runBehavior
      , cmdlistenperformance = listenperformancetestBehavior
148 149 150
                                 (TestText
                                   (TextBehaviorStdout (WaitFor "performance"))
                                   (TextBehaviorStderr ExpectClean)
Valentin Reis's avatar
Valentin Reis committed
151 152
                                 )
      }
153 154 155 156 157
    , 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
158 159 160 161 162 163 164 165 166 167 168 169 170
    , 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)
                           )
      }
171 172 173 174
    , 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
175 176 177 178 179 180 181 182 183
    , isTest = IsTest
    }
  TestSTREAM -> TestSpec
    { stackArgsUpdate = \sa -> sa
      { app               = AppName "stream_c_20"
      , args              = []
      , daemon            = daemonBehavior
      , cmdrun            = runBehavior
      , cmdlistenprogress = listenprogresstestBehavior
184 185 186
                              (TestText
                                (TextBehaviorStdout (WaitFor "progress"))
                                (TextBehaviorStderr ExpectClean)
Valentin Reis's avatar
Valentin Reis committed
187 188
                              )
      }
189 190 191 192
    , 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
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
    , isTest = IsTest
    }
  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") []
Valentin Reis's avatar
Valentin Reis committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
  RunLAMMPS -> TestSpec
      { stackArgsUpdate = \sa -> sa
        { app                  = (AppName "mpirun")
        , args                 =
          let (ManifestDir d) = manifestDir sa
              Right p = toText (d </> "in.lj")
           in [ AppArg "-n"
              , AppArg "24"
              , AppArg "lmp_serial"
              , AppArg "-i"
              , AppArg $ p
              ]
        , 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
      }
Valentin Reis's avatar
Valentin Reis committed
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 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
 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)
289
    Died stacki errorcode _ _ tsl -> case isTest of
Valentin Reis's avatar
Valentin Reis committed
290 291
      IsTest -> do
        printError
292 293 294 295
          (  repr stacki
          <> " died before a message could be found with error code "
          <> repr errorcode
          )
Valentin Reis's avatar
Valentin Reis committed
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
        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
  manifests <- getEnv "MANIFESTS"
  let a = def { manifestDir = ManifestDir $ decodeString manifests }
  turtle <- execParser (info (opts a <**> helper) idm)
  sh turtle