Skip to content

Commit

Permalink
feat: reset deployment between tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stevana committed Oct 29, 2024
1 parent ff86100 commit 2c114d9
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 6 deletions.
1 change: 1 addition & 0 deletions spex.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ library
Spex.Verifier.Generator.Env
Spex.Verifier.HealthChecker
Spex.Verifier.HttpClient
Spex.Verifier.Reseter

autogen-modules: Paths_spex
other-modules: Paths_spex
Expand Down
2 changes: 2 additions & 0 deletions src/Spex/Monad.hs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ data AppError
| HttpClientDecodeError Op ByteString String
| HttpClientUnexpectedStatusCode Int ByteString
| HealthCheckFailed
| ResetFailed
| TestFailure String Int

throwA :: AppError -> App e
Expand Down Expand Up @@ -173,6 +174,7 @@ displayAppError spec = \case
HttpClientDecodeError op body e -> "Couldn't decode the response of:\n\n " <> displayOp displayValue op <> "\n\nfrom the body of the request: '" <> BS8.unpack body <> "'\n\nThe error being: " <> e
HttpClientUnexpectedStatusCode _ _ -> "HTTP client returned 1xx or 3xx"
HealthCheckFailed -> "Health check failed, make sure that the deployment is running."
ResetFailed -> "Reset of the deploymnet failed, make sure that reset returns 2xx or exits with 0."
TestFailure e seed -> "Test failure: " <> e <>
"\nUse --seed " <> show seed <> " to reproduce"

Expand Down
1 change: 0 additions & 1 deletion src/Spex/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

module Spex.Syntax where

import Data.ByteString (ByteString)
import Data.ByteString.Char8 as BS8
import Data.String (IsString)

Expand Down
8 changes: 5 additions & 3 deletions src/Spex/Syntax/Type.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ type Record a = Map Field a
newtype Field = Field ByteString
deriving (Eq, Ord, Show, IsString)

data Method = Get | Post
data Method = Get | Post | Put | Delete
deriving Show

displayMethod :: Method -> String
displayMethod Get = "GET"
displayMethod Post = "POST"
displayMethod Get = "GET"
displayMethod Post = "POST"
displayMethod Put = "PUT"
displayMethod Delete = "DELETE"

displayType :: Type -> String
displayType UnitT = "Unit"
Expand Down
2 changes: 2 additions & 0 deletions src/Spex/Verifier.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Spex.Verifier.Codec.Json
import Spex.Verifier.Generator
import Spex.Verifier.Generator.Env
import Spex.Verifier.HttpClient
import Spex.Verifier.Reseter

------------------------------------------------------------------------

Expand Down Expand Up @@ -48,6 +49,7 @@ verify spec deployment = do
mSeed <- asks mSeed
(prng, seed) <- liftIO (newPrng mSeed)
client <- newHttpClient deployment
reseter client deployment.reset
go numTests [] seed prng client 0 Map.empty
where
go :: Word -> [Op] -> Int -> Prng -> HttpClient -> Word -> Coverage -> App Result
Expand Down
3 changes: 3 additions & 0 deletions src/Spex/Verifier/HealthChecker.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Spex.Verifier.HealthChecker where

import Control.Concurrent (threadDelay)
import Control.Monad (when)
import Data.ByteString (ByteString)
import System.Exit
import System.Process

Expand All @@ -24,6 +25,7 @@ healthChecker deployment@(Deployment _hostPort health _reset) = do
http 50 path client
HealthCheckScript fp -> script 50 fp
where
script :: Word -> FilePath -> App ()
script 0 _fp = info_ "" >> throwA HealthCheckFailed
script n fp = do
(exitCode, _out, _err) <- liftIO (readProcessWithExitCode fp [] "")
Expand All @@ -33,6 +35,7 @@ healthChecker deployment@(Deployment _hostPort health _reset) = do
wait n
script (n - 1) fp

http :: Word -> ByteString -> HttpClient -> App ()
http 0 _path _client = info_ "" >> throwA HealthCheckFailed
http n path client = do
eResp <- tryA $
Expand Down
6 changes: 4 additions & 2 deletions src/Spex/Verifier/HttpClient.hs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ httpRequest client op = do
status.statusCode status.statusMessage)

toHttpMethod :: Method -> Http.Method
toHttpMethod Get = Http.methodGet
toHttpMethod Post = Http.methodPost
toHttpMethod Get = Http.methodGet
toHttpMethod Post = Http.methodPost
toHttpMethod Put = Http.methodPut
toHttpMethod Delete = Http.methodDelete

toHttpPath :: [PathSegment Value] -> ByteString
toHttpPath = BS8.intercalate "/" . map aux
Expand Down
34 changes: 34 additions & 0 deletions src/Spex/Verifier/Reseter.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{-# LANGUAGE OverloadedStrings #-}

module Spex.Verifier.Reseter where

import System.Exit
import System.Process

import Spex.Monad
import Spex.Syntax
import Spex.Syntax.Operation
import Spex.Syntax.Type
import Spex.Syntax.Value
import Spex.Verifier.HttpClient

------------------------------------------------------------------------

reseter :: HttpClient -> Reset -> App ()
reseter client reset = do
case reset of
ResetPath path -> do
let op = Op "_reset" Delete [Path path] Nothing UnitT
debug (displayOp displayValue op)
eResp <- tryA $ httpRequest client op
case eResp of
Left _err -> throwA ResetFailed
Right (Ok2xx _body) -> return ()
Right ClientError4xx {} -> throwA ResetFailed
Right ServerError5xx {} -> throwA ResetFailed

ResetScript fp -> do
(exitCode, _out, _err) <- liftIO (readProcessWithExitCode fp [] "")
case exitCode of
ExitSuccess -> return ()
ExitFailure _code -> throwA ResetFailed

0 comments on commit 2c114d9

Please sign in to comment.