argotk.hs 11 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) ..])
Valentin Reis's avatar
Valentin Reis committed
20
  <> commandTests [TestHello, TestListen, TestPerfwrapper, TestSTREAM]
21
                  "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)
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
  printSuccess "Test Successful.\n"
68 69 70

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          = NotTest
Valentin Reis's avatar
Valentin Reis committed
75
    }
76
  DaemonAndApp -> TestSpec
77 78
    { stackArgsUpdate = \sa ->
      sa { daemon = daemonBehavior, cmdrun = runBehavior }
79
    , description     = "Set up and start daemon, run a command in a container."
80
    , isTest          = NotTest
81 82
    }
  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          = NotTest
Valentin Reis's avatar
Valentin Reis committed
95
    }
96
  TestHello -> TestSpec
Valentin Reis's avatar
Valentin Reis committed
97
    { stackArgsUpdate = \sa -> sa
98
      { app    = AppName "echo"
99
      , args   = [AppArg msg]
100
      , daemon = daemonBehavior
101 102 103 104 105 106
      , cmdrun = Test
                   (TestText (TextBehaviorStdout (WaitFor msg))
                             (TextBehaviorStderr ExpectClean)
                   )
                   (StdOutLog "monitored-cmdrun-out.log")
                   (StdErrLog "monitored-cmdrun-err.log")
Valentin Reis's avatar
Valentin Reis committed
107
      }
108 109
    , description = "1: Setup stack and check that a hello world app sends \
                    \message back to cmd."
110
    , isTest = IsTest
111 112
    }
  TestListen -> TestSpec
Valentin Reis's avatar
Valentin Reis committed
113 114
    { stackArgsUpdate = \sa -> sa
      { app       = AppName "sleep"
115
      , args      = [AppArg "15"]
116 117
      , daemon    = daemonBehavior
      , cmdrun    = runBehavior
118 119 120 121
      , cmdlisten = listentestBehavior
                      (TestText (TextBehaviorStdout (WaitFor "start"))
                                (TextBehaviorStderr ExpectClean)
                      )
Valentin Reis's avatar
Valentin Reis committed
122
      }
123 124
    , description = "2: Setup stack and check that argo-perf-wrapper sends\
                    \ at least one message to the daemon."
125
    , isTest = IsTest
126 127
    }
  TestPerfwrapper -> TestSpec
Valentin Reis's avatar
Valentin Reis committed
128
    { stackArgsUpdate = \sa -> sa
129 130 131 132 133 134 135 136 137
      { manifestName         = "perfwrap.json"
      , app                  = AppName "sleep"
      , args                 = [AppArg "15"]
      , daemon               = daemonBehavior
      , cmdrun               = runBehavior
      , cmdlistenperformance = listenperformancetestBehavior
                                 (TestText (TextBehaviorStdout (WaitFor ","))
                                           (TextBehaviorStderr ExpectClean)
                                 )
Valentin Reis's avatar
Valentin Reis committed
138
      }
139 140 141
    , description = "3: Setup stack and check that argo-perf-wrapper sends\
                    \ at least one *performance* message to cmd listen through the\
                    \ daemon."
142
    , isTest = IsTest
143
    }
Valentin Reis's avatar
Valentin Reis committed
144
  TestPower -> TestSpec
Valentin Reis's avatar
Valentin Reis committed
145
    { stackArgsUpdate = \sa -> sa
146 147 148 149 150 151 152 153
      { app            = AppName "sleep"
      , args           = [AppArg "15"]
      , daemon         = daemonBehavior
      , cmdrun         = runBehavior
      , cmdlistenpower = listenpowertestBehavior
                           (TestText (TextBehaviorStdout (WaitFor ","))
                                     (TextBehaviorStderr ExpectClean)
                           )
Valentin Reis's avatar
Valentin Reis committed
154
      }
155 156 157
    , description = "4: Setup stack and check that the daemon sends\
                    \ at least one *power* message."
    , isTest = IsTest
Valentin Reis's avatar
Valentin Reis committed
158
    }
159 160
  TestAMG -> TestSpec
    { stackArgsUpdate = \sa -> sa
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
      { 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
179 180 181 182
      , cmdlistenprogress = listenprogresstestBehavior
                              (TestText (TextBehaviorStdout (WaitFor ","))
                                        (TextBehaviorStderr ExpectClean)
                              )
183
      }
Valentin Reis's avatar
Valentin Reis committed
184
    , description = "5: Setup stack, run AMG and check that it sends\
185
                    \ at least one progress message to the daemon."
186
    , isTest = IsTest
187 188 189 190 191 192 193
    }
  TestSTREAM -> TestSpec
    { stackArgsUpdate = \sa -> sa
      { app               = AppName "stream_c_20"
      , args              = []
      , daemon            = daemonBehavior
      , cmdrun            = runBehavior
194 195 196 197
      , cmdlistenprogress = listenprogresstestBehavior
                              (TestText (TextBehaviorStdout (WaitFor ","))
                                        (TextBehaviorStderr ExpectClean)
                              )
198
      }
Valentin Reis's avatar
Valentin Reis committed
199
    , description = "6: Setup stack, run STREAM and check that it sends\
200
                    \ at least one progress message to the daemon."
201
    , isTest = IsTest
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
    }
  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"
226 227 228 229 230 231 232 233
      , 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")
      }
234
    , description     = "Set up and start daemon, run app in a container."
235
    , isTest          = NotTest
236
    }
Valentin Reis's avatar
Valentin Reis committed
237
  msg = "someComplicatedMessage"
238 239 240 241
  daemonBehavior =
    JustRun (StdOutLog "daemon_out.log") (StdErrLog "daemon_err.log")
  runBehavior =
    JustRun (StdOutLog "cmd_run_out.log") (StdErrLog "cmd_run_err.log")
242 243 244 245 246 247 248 249 250 251
  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")
252

253
data IsTest = IsTest | NotTest
254 255

fullStack :: IsTest -> StackArgs -> Shell ()
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
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)
    Died stacki errorcode _ _ tsl-> case isTest of
      IsTest -> do
        printError
            (  repr stacki
            <> " died before a message could be found with error code "
            <> repr errorcode
            )
        mapM_
          (\(stacki', fout, ferr) ->
            printError
              $  "Found Python Traceback when executing "
              <> repr stacki'
              <> ". Files for this command: "
              <> repr fout
              <> " "
              <> repr ferr
289
          )
290 291 292
          tsl
        exit (ExitFailure 1)
      NotTest -> exit ExitSuccess
293

294
clean :: StackArgs -> Shell ()
Valentin Reis's avatar
Valentin Reis committed
295
clean StackArgs {..} = cleanLeftovers workingDirectory
296 297 298 299

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