2019-10-21 19:01:05 +03:00
|
|
|
module Main (main) where
|
|
|
|
|
|
|
|
import Hasura.Prelude
|
|
|
|
|
2019-11-27 01:49:42 +03:00
|
|
|
import Control.Concurrent.MVar
|
2020-01-14 00:56:51 +03:00
|
|
|
import Control.Natural ((:~>) (..))
|
|
|
|
import Data.Time.Clock (getCurrentTime)
|
2019-10-21 19:01:05 +03:00
|
|
|
import Options.Applicative
|
2020-01-14 00:56:51 +03:00
|
|
|
import System.Environment (getEnvironment)
|
|
|
|
import System.Exit (exitFailure)
|
2019-10-21 19:01:05 +03:00
|
|
|
import Test.Hspec
|
2019-11-18 21:45:54 +03:00
|
|
|
|
2020-01-14 00:56:51 +03:00
|
|
|
import qualified Data.Aeson as A
|
|
|
|
import qualified Data.ByteString.Lazy.Char8 as BL
|
|
|
|
import qualified Database.PG.Query as Q
|
|
|
|
import qualified Network.HTTP.Client as HTTP
|
|
|
|
import qualified Network.HTTP.Client.TLS as HTTP
|
|
|
|
import qualified Test.Hspec.Runner as Hspec
|
2019-11-27 01:49:42 +03:00
|
|
|
|
2020-06-16 20:44:59 +03:00
|
|
|
import Hasura.Db (mkPGExecCtx)
|
2020-04-24 12:10:53 +03:00
|
|
|
import Hasura.RQL.Types (SQLGenCtx (..))
|
2019-12-09 01:17:39 +03:00
|
|
|
import Hasura.RQL.Types.Run
|
2020-02-13 20:38:23 +03:00
|
|
|
import Hasura.Server.Init (RawConnInfo, mkConnInfo, mkRawConnInfo,
|
|
|
|
parseRawConnInfo, runWithEnv)
|
2019-11-20 21:21:30 +03:00
|
|
|
import Hasura.Server.Migrate
|
2020-01-23 00:55:55 +03:00
|
|
|
import Hasura.Server.Version
|
2020-04-24 12:10:53 +03:00
|
|
|
import Hasura.Session (adminUserInfo)
|
2019-11-18 21:45:54 +03:00
|
|
|
|
2020-01-14 00:56:51 +03:00
|
|
|
import qualified Data.Parser.CacheControlSpec as CacheControlParser
|
2020-04-16 09:45:21 +03:00
|
|
|
import qualified Data.Parser.JSONPathSpec as JsonPath
|
2020-02-13 20:38:23 +03:00
|
|
|
import qualified Data.Parser.URLTemplate as URLTemplate
|
|
|
|
import qualified Data.TimeSpec as TimeSpec
|
2020-01-14 00:56:51 +03:00
|
|
|
import qualified Hasura.IncrementalSpec as IncrementalSpec
|
2020-03-26 14:52:20 +03:00
|
|
|
-- import qualified Hasura.RQL.MetadataSpec as MetadataSpec
|
2020-01-14 00:56:51 +03:00
|
|
|
import qualified Hasura.Server.MigrateSpec as MigrateSpec
|
2020-02-13 20:38:23 +03:00
|
|
|
import qualified Hasura.Server.TelemetrySpec as TelemetrySpec
|
2020-05-28 19:18:26 +03:00
|
|
|
import qualified Hasura.Server.AuthSpec as AuthSpec
|
2019-11-18 21:45:54 +03:00
|
|
|
|
|
|
|
data TestSuites
|
|
|
|
= AllSuites !RawConnInfo
|
2020-02-13 20:38:23 +03:00
|
|
|
-- ^ Run all test suites. It probably doesn't make sense to be able to specify additional
|
2020-01-21 21:12:27 +03:00
|
|
|
-- hspec args here.
|
|
|
|
| SingleSuite ![String] !TestSuite
|
|
|
|
-- ^ Args to pass through to hspec (as if from 'getArgs'), and the specific suite to run.
|
2019-11-18 21:45:54 +03:00
|
|
|
|
|
|
|
data TestSuite
|
|
|
|
= UnitSuite
|
|
|
|
| PostgresSuite !RawConnInfo
|
2019-10-21 19:01:05 +03:00
|
|
|
|
2019-12-14 09:47:38 +03:00
|
|
|
main :: IO ()
|
2020-01-23 00:55:55 +03:00
|
|
|
main = withVersion $$(getVersionFromEnvironment) $ parseArgs >>= \case
|
2019-11-18 21:45:54 +03:00
|
|
|
AllSuites pgConnOptions -> do
|
|
|
|
postgresSpecs <- buildPostgresSpecs pgConnOptions
|
2020-01-21 21:12:27 +03:00
|
|
|
runHspec [] (unitSpecs *> postgresSpecs)
|
|
|
|
SingleSuite hspecArgs suite -> runHspec hspecArgs =<< case suite of
|
|
|
|
UnitSuite -> pure unitSpecs
|
|
|
|
PostgresSuite pgConnOptions -> buildPostgresSpecs pgConnOptions
|
2019-11-18 21:45:54 +03:00
|
|
|
|
|
|
|
unitSpecs :: Spec
|
|
|
|
unitSpecs = do
|
2020-01-14 00:56:51 +03:00
|
|
|
describe "Data.Parser.CacheControl" CacheControlParser.spec
|
2020-02-13 20:38:23 +03:00
|
|
|
describe "Data.Parser.URLTemplate" URLTemplate.spec
|
2020-04-16 09:45:21 +03:00
|
|
|
describe "Data.Parser.JsonPath" JsonPath.spec
|
2019-11-18 21:45:54 +03:00
|
|
|
describe "Hasura.Incremental" IncrementalSpec.spec
|
2020-03-26 14:52:20 +03:00
|
|
|
-- describe "Hasura.RQL.Metadata" MetadataSpec.spec -- Commenting until optimizing the test in CI
|
2020-01-16 04:56:57 +03:00
|
|
|
describe "Data.Time" TimeSpec.spec
|
|
|
|
describe "Hasura.Server.Telemetry" TelemetrySpec.spec
|
2020-05-28 19:18:26 +03:00
|
|
|
describe "Hasura.Server.Auth" AuthSpec.spec
|
2019-11-18 21:45:54 +03:00
|
|
|
|
2020-01-23 00:55:55 +03:00
|
|
|
buildPostgresSpecs :: (HasVersion) => RawConnInfo -> IO Spec
|
2019-11-18 21:45:54 +03:00
|
|
|
buildPostgresSpecs pgConnOptions = do
|
|
|
|
env <- getEnvironment
|
|
|
|
|
|
|
|
rawPGConnInfo <- flip onLeft printErrExit $ runWithEnv env (mkRawConnInfo pgConnOptions)
|
|
|
|
pgConnInfo <- flip onLeft printErrExit $ mkConnInfo rawPGConnInfo
|
|
|
|
|
2019-11-20 21:21:30 +03:00
|
|
|
let setupCacheRef = do
|
|
|
|
pgPool <- Q.initPGPool pgConnInfo Q.defaultConnParams { Q.cpConns = 1 } print
|
2019-11-18 21:45:54 +03:00
|
|
|
|
2019-11-20 21:21:30 +03:00
|
|
|
httpManager <- HTTP.newManager HTTP.tlsManagerSettings
|
|
|
|
let runContext = RunCtx adminUserInfo httpManager (SQLGenCtx False)
|
2020-06-16 20:44:59 +03:00
|
|
|
pgContext = mkPGExecCtx Q.Serializable pgPool
|
2019-11-20 21:21:30 +03:00
|
|
|
|
|
|
|
runAsAdmin :: Run a -> IO a
|
|
|
|
runAsAdmin =
|
|
|
|
peelRun runContext pgContext Q.ReadWrite
|
|
|
|
>>> runExceptT
|
|
|
|
>=> flip onLeft printErrJExit
|
|
|
|
|
|
|
|
schemaCache <- snd <$> runAsAdmin (migrateCatalog =<< liftIO getCurrentTime)
|
|
|
|
cacheRef <- newMVar schemaCache
|
|
|
|
pure $ NT (runAsAdmin . flip MigrateSpec.runCacheRefT cacheRef)
|
|
|
|
|
|
|
|
pure $ beforeAll setupCacheRef $
|
|
|
|
describe "Hasura.Server.Migrate" $ MigrateSpec.spec pgConnInfo
|
2019-11-18 21:45:54 +03:00
|
|
|
|
|
|
|
parseArgs :: IO TestSuites
|
|
|
|
parseArgs = execParser $ info (helper <*> (parseNoCommand <|> parseSubCommand)) $
|
|
|
|
fullDesc <> header "Hasura GraphQL Engine test suite"
|
2019-10-21 19:01:05 +03:00
|
|
|
where
|
2019-11-18 21:45:54 +03:00
|
|
|
parseNoCommand = AllSuites <$> parseRawConnInfo
|
2020-02-13 20:38:23 +03:00
|
|
|
parseSubCommand = SingleSuite <$> parseHspecPassThroughArgs <*> subCmd
|
|
|
|
where
|
2020-01-21 21:12:27 +03:00
|
|
|
subCmd = subparser $ mconcat
|
|
|
|
[ command "unit" $ info (pure UnitSuite) $
|
|
|
|
progDesc "Only run unit tests"
|
|
|
|
, command "postgres" $ info (helper <*> (PostgresSuite <$> parseRawConnInfo)) $
|
|
|
|
progDesc "Only run Postgres integration tests"
|
|
|
|
]
|
|
|
|
-- Add additional arguments and tweak as needed:
|
|
|
|
hspecArgs = ["match", "skip"]
|
|
|
|
-- parse to a list of arguments as they'd appear from 'getArgs':
|
|
|
|
parseHspecPassThroughArgs :: Parser [String]
|
|
|
|
parseHspecPassThroughArgs = fmap concat $ for hspecArgs $ \nm->
|
|
|
|
fmap (maybe [] (\a -> ["--"<>nm , a])) $ optional $
|
|
|
|
strOption ( long nm <>
|
|
|
|
metavar "<PATTERN>" <>
|
|
|
|
help "Flag passed through to hspec (see hspec docs)." )
|
|
|
|
|
|
|
|
|
|
|
|
runHspec :: [String] -> Spec -> IO ()
|
|
|
|
runHspec hspecArgs m = do
|
|
|
|
config <- Hspec.readConfig Hspec.defaultConfig hspecArgs
|
2019-11-18 21:45:54 +03:00
|
|
|
Hspec.evaluateSummary =<< Hspec.runSpec m config
|
|
|
|
|
|
|
|
printErrExit :: String -> IO a
|
|
|
|
printErrExit = (*> exitFailure) . putStrLn
|
2019-11-20 21:21:30 +03:00
|
|
|
|
|
|
|
printErrJExit :: (A.ToJSON a) => a -> IO b
|
|
|
|
printErrJExit = (*> exitFailure) . BL.putStrLn . A.encode
|