2021-05-26 19:19:26 +03:00
|
|
|
{-# LANGUAGE UndecidableInstances #-}
|
2019-10-21 19:01:05 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
module Main (main) where
|
2020-12-28 15:56:00 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
import Control.Concurrent.MVar
|
|
|
|
import Control.Natural ((:~>) (..))
|
|
|
|
import Data.Aeson qualified as A
|
|
|
|
import Data.ByteString.Lazy.Char8 qualified as BL
|
2021-11-09 17:21:48 +03:00
|
|
|
import Data.ByteString.Lazy.UTF8 qualified as LBS
|
2021-09-24 01:56:37 +03:00
|
|
|
import Data.Environment qualified as Env
|
2022-02-03 19:13:50 +03:00
|
|
|
import Data.HashMap.Strict.ExtendedSpec qualified as HashMapExtendedSpec
|
2021-09-24 01:56:37 +03:00
|
|
|
import Data.NonNegativeIntSpec qualified as NonNegetiveIntSpec
|
|
|
|
import Data.Parser.CacheControlSpec qualified as CacheControlParser
|
|
|
|
import Data.Parser.JSONPathSpec qualified as JsonPath
|
|
|
|
import Data.Time.Clock (getCurrentTime)
|
|
|
|
import Data.TimeSpec qualified as TimeSpec
|
|
|
|
import Data.URL.Template
|
|
|
|
import Database.MSSQL.TransactionSpec qualified as TransactionSpec
|
|
|
|
import Database.PG.Query qualified as Q
|
|
|
|
import Hasura.App
|
|
|
|
( PGMetadataStorageAppT (..),
|
2022-01-04 14:53:50 +03:00
|
|
|
mkMSSQLSourceResolver,
|
2021-09-24 01:56:37 +03:00
|
|
|
mkPgSourceResolver,
|
|
|
|
)
|
2021-12-01 15:49:30 +03:00
|
|
|
import Hasura.Backends.MySQL.DataLoader.ExecuteTests qualified as MySQLDataLoader
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.EventingSpec qualified as EventingSpec
|
2021-10-29 17:42:07 +03:00
|
|
|
import Hasura.GraphQL.NamespaceSpec qualified as NamespaceSpec
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.GraphQL.Parser.DirectivesTest qualified as GraphQLDirectivesSpec
|
|
|
|
import Hasura.GraphQL.Schema.RemoteTest qualified as GraphRemoteSchemaSpec
|
|
|
|
import Hasura.IncrementalSpec qualified as IncrementalSpec
|
2021-11-09 17:21:48 +03:00
|
|
|
import Hasura.Logging
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.Metadata.Class
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.DDL.Schema.Cache
|
|
|
|
import Hasura.RQL.DDL.Schema.Cache.Common
|
2021-12-14 09:45:13 +03:00
|
|
|
import Hasura.RQL.MetadataSpec qualified as MetadataSpec
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.RQL.PermissionSpec qualified as PermSpec
|
|
|
|
import Hasura.RQL.Types
|
|
|
|
import Hasura.RQL.Types.CommonSpec qualified as CommonTypesSpec
|
|
|
|
import Hasura.RQL.Types.EndpointSpec qualified as EndpointSpec
|
2022-01-19 07:46:42 +03:00
|
|
|
import Hasura.RQL.WebhookTransformsSpec qualified as WebhookTransformsSpec
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.SQL.WKTSpec qualified as WKTSpec
|
2022-01-28 03:17:53 +03:00
|
|
|
import Hasura.Server.Auth.JWTSpec qualified as JWTSpec
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.Server.AuthSpec qualified as AuthSpec
|
|
|
|
import Hasura.Server.Init
|
|
|
|
import Hasura.Server.Migrate
|
|
|
|
import Hasura.Server.MigrateSpec qualified as MigrateSpec
|
|
|
|
import Hasura.Server.TelemetrySpec qualified as TelemetrySpec
|
|
|
|
import Hasura.Server.Types
|
|
|
|
import Network.HTTP.Client qualified as HTTP
|
|
|
|
import Network.HTTP.Client.TLS qualified as HTTP
|
|
|
|
import Network.HTTP.Client.TransformableSpec qualified as TransformableSpec
|
|
|
|
import Options.Applicative
|
|
|
|
import System.Environment (getEnvironment)
|
|
|
|
import System.Exit (exitFailure)
|
|
|
|
import Test.Hspec
|
|
|
|
import Test.Hspec.Runner qualified as Hspec
|
2019-11-18 21:45:54 +03:00
|
|
|
|
|
|
|
data TestSuites
|
2021-09-09 10:59:04 +03:00
|
|
|
= -- | Run all test suites. It probably doesn't make sense to be able to specify additional
|
|
|
|
-- hspec args here.
|
2021-12-06 18:46:52 +03:00
|
|
|
AllSuites
|
2021-09-09 10:59:04 +03:00
|
|
|
| -- | Args to pass through to hspec (as if from 'getArgs'), and the specific suite to run.
|
|
|
|
SingleSuite ![String] !TestSuite
|
2019-11-18 21:45:54 +03:00
|
|
|
|
|
|
|
data TestSuite
|
|
|
|
= UnitSuite
|
2021-12-06 18:46:52 +03:00
|
|
|
| PostgresSuite
|
|
|
|
| MSSQLSuite
|
2019-10-21 19:01:05 +03:00
|
|
|
|
2019-12-14 09:47:38 +03:00
|
|
|
main :: IO ()
|
2021-09-09 10:59:04 +03:00
|
|
|
main =
|
2021-10-13 19:38:56 +03:00
|
|
|
parseArgs >>= \case
|
2021-12-06 18:46:52 +03:00
|
|
|
AllSuites -> do
|
|
|
|
postgresSpecs <- buildPostgresSpecs
|
|
|
|
mssqlSpecs <- buildMSSQLSpecs
|
2021-10-13 19:38:56 +03:00
|
|
|
runHspec [] (unitSpecs *> postgresSpecs *> mssqlSpecs)
|
|
|
|
SingleSuite hspecArgs suite ->
|
|
|
|
runHspec hspecArgs =<< case suite of
|
|
|
|
UnitSuite -> pure unitSpecs
|
2021-12-06 18:46:52 +03:00
|
|
|
PostgresSuite -> buildPostgresSpecs
|
|
|
|
MSSQLSuite -> buildMSSQLSpecs
|
2019-11-18 21:45:54 +03:00
|
|
|
|
|
|
|
unitSpecs :: Spec
|
|
|
|
unitSpecs = do
|
2022-02-03 19:13:50 +03:00
|
|
|
describe "Data.HashMap.Strict.ExtendedSpec" HashMapExtendedSpec.spec
|
2021-05-20 13:03:02 +03:00
|
|
|
describe "Data.NonNegativeInt" NonNegetiveIntSpec.spec
|
2020-01-14 00:56:51 +03:00
|
|
|
describe "Data.Parser.CacheControl" CacheControlParser.spec
|
2020-08-31 19:40:01 +03:00
|
|
|
describe "Data.Parser.JSONPath" JsonPath.spec
|
2020-01-16 04:56:57 +03:00
|
|
|
describe "Data.Time" TimeSpec.spec
|
2021-12-01 15:49:30 +03:00
|
|
|
describe "Hasura.Backends.MySQL.DataLoader.ExecuteTests" MySQLDataLoader.spec
|
2021-05-20 13:03:02 +03:00
|
|
|
describe "Hasura.Eventing" EventingSpec.spec
|
|
|
|
describe "Hasura.GraphQL.Parser.Directives" GraphQLDirectivesSpec.spec
|
2021-05-24 23:12:53 +03:00
|
|
|
describe "Hasura.GraphQL.Schema.Remote" GraphRemoteSchemaSpec.spec
|
2021-05-20 13:03:02 +03:00
|
|
|
describe "Hasura.Incremental" IncrementalSpec.spec
|
2021-09-06 19:59:18 +03:00
|
|
|
describe "Hasura.RQL.Types.Common" CommonTypesSpec.spec
|
2021-01-29 04:02:34 +03:00
|
|
|
describe "Hasura.RQL.Types.Endpoint" EndpointSpec.spec
|
2021-10-29 17:42:07 +03:00
|
|
|
describe "Hasura.GraphQL.Namespace" NamespaceSpec.spec
|
2021-03-26 19:59:16 +03:00
|
|
|
describe "Hasura.SQL.WKT" WKTSpec.spec
|
2022-01-28 03:17:53 +03:00
|
|
|
describe "Hasura.Server.Auth.JWT" JWTSpec.spec
|
2021-05-20 13:03:02 +03:00
|
|
|
describe "Hasura.Server.Auth" AuthSpec.spec
|
|
|
|
describe "Hasura.Server.Telemetry" TelemetrySpec.spec
|
2021-08-09 13:20:04 +03:00
|
|
|
describe "Hasura.RQL.PermissionSpec" PermSpec.spec
|
2021-12-14 09:45:13 +03:00
|
|
|
describe "Hasura.RQL.MetadataSpec" MetadataSpec.spec
|
2022-01-19 07:46:42 +03:00
|
|
|
describe "Hasura.RQL.WebhookTransformsSpec" WebhookTransformsSpec.spec
|
2021-09-16 14:03:01 +03:00
|
|
|
describe "Network.HTTP.Client.TransformableSpec" TransformableSpec.spec
|
2019-11-18 21:45:54 +03:00
|
|
|
|
2021-12-06 18:46:52 +03:00
|
|
|
buildMSSQLSpecs :: IO Spec
|
|
|
|
buildMSSQLSpecs = do
|
2021-09-09 10:59:04 +03:00
|
|
|
env <- liftIO getEnvironment
|
2021-12-06 18:46:52 +03:00
|
|
|
connStr <- flip onLeft printErrExit $
|
2021-09-09 10:59:04 +03:00
|
|
|
runWithEnv env $ do
|
|
|
|
let envVar = fst mssqlConnectionString
|
2021-12-06 18:46:52 +03:00
|
|
|
maybeV <- considerEnv envVar
|
2021-09-09 10:59:04 +03:00
|
|
|
onNothing maybeV $
|
|
|
|
throwError $
|
|
|
|
"Expected: " <> envVar
|
|
|
|
pure $ describe "Database.MSSQL.TransactionSpec" $ TransactionSpec.spec connStr
|
|
|
|
|
|
|
|
mssqlConnectionString :: (String, String)
|
|
|
|
mssqlConnectionString =
|
|
|
|
( "HASURA_MSSQL_CONN_STR",
|
2021-12-06 18:46:52 +03:00
|
|
|
"SQL Server database connection string. Example DRIVER={ODBC Driver 17 for SQL Server};SERVER=localhost,1433;Uid=user;Pwd=pass;"
|
2021-09-09 10:59:04 +03:00
|
|
|
)
|
|
|
|
|
2021-12-06 18:46:52 +03:00
|
|
|
buildPostgresSpecs :: IO Spec
|
|
|
|
buildPostgresSpecs = do
|
2019-11-18 21:45:54 +03:00
|
|
|
env <- getEnvironment
|
2020-12-28 15:56:00 +03:00
|
|
|
let envMap = Env.mkEnvironment env
|
|
|
|
|
2021-12-06 18:46:52 +03:00
|
|
|
pgUrlText <- flip onLeft printErrExit $
|
2021-09-24 01:56:37 +03:00
|
|
|
runWithEnv env $ do
|
|
|
|
let envVar = fst databaseUrlEnv
|
2021-12-06 18:46:52 +03:00
|
|
|
maybeV <- considerEnv envVar
|
2021-09-24 01:56:37 +03:00
|
|
|
onNothing maybeV $
|
2021-12-06 18:46:52 +03:00
|
|
|
throwError $ "Expected: " <> envVar
|
2019-11-18 21:45:54 +03:00
|
|
|
|
2020-12-28 15:56:00 +03:00
|
|
|
let pgConnInfo = Q.ConnInfo 1 $ Q.CDDatabaseURI $ txtToBs pgUrlText
|
2021-12-06 18:46:52 +03:00
|
|
|
urlConf = UrlValue $ InputWebhook $ mkPlainURLTemplate pgUrlText
|
2021-04-28 19:49:23 +03:00
|
|
|
sourceConnInfo =
|
2021-05-21 04:49:50 +03:00
|
|
|
PostgresSourceConnInfo urlConf (Just setPostgresPoolSettings) True Q.ReadCommitted Nothing
|
2021-02-14 09:07:52 +03:00
|
|
|
sourceConfig = PostgresConnConfiguration sourceConnInfo Nothing
|
2019-11-18 21:45:54 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
pgPool <- Q.initPGPool pgConnInfo Q.defaultConnParams {Q.cpConns = 1} print
|
2020-12-28 15:56:00 +03:00
|
|
|
let pgContext = mkPGExecCtx Q.Serializable pgPool
|
|
|
|
|
2021-11-09 17:21:48 +03:00
|
|
|
logger :: Logger Hasura = Logger $ \l -> do
|
|
|
|
let (logLevel, logType :: EngineLogType Hasura, logDetail) = toEngineLog l
|
|
|
|
t <- liftIO $ getFormattedTime Nothing
|
|
|
|
liftIO $ putStrLn $ LBS.toString $ A.encode $ EngineLog t logLevel logType logDetail
|
|
|
|
|
2020-12-28 15:56:00 +03:00
|
|
|
setupCacheRef = do
|
2019-11-20 21:21:30 +03:00
|
|
|
httpManager <- HTTP.newManager HTTP.tlsManagerSettings
|
2022-02-03 19:13:50 +03:00
|
|
|
let sqlGenCtx = SQLGenCtx False False False
|
2021-02-18 19:46:14 +03:00
|
|
|
maintenanceMode = MaintenanceModeDisabled
|
2021-12-08 09:26:46 +03:00
|
|
|
readOnlyMode = ReadOnlyModeDisabled
|
2021-02-18 19:46:14 +03:00
|
|
|
serverConfigCtx =
|
2021-12-08 09:26:46 +03:00
|
|
|
ServerConfigCtx FunctionPermissionsInferred RemoteSchemaPermsDisabled sqlGenCtx maintenanceMode mempty EventingEnabled readOnlyMode
|
2022-01-04 14:53:50 +03:00
|
|
|
cacheBuildParams = CacheBuildParams httpManager (mkPgSourceResolver print) mkMSSQLSourceResolver serverConfigCtx
|
2021-05-26 19:19:26 +03:00
|
|
|
pgLogger = print
|
2019-11-20 21:21:30 +03:00
|
|
|
|
2021-05-26 19:19:26 +03:00
|
|
|
run :: MetadataStorageT (PGMetadataStorageAppT CacheBuild) a -> IO a
|
2020-12-28 15:56:00 +03:00
|
|
|
run =
|
2021-05-26 19:19:26 +03:00
|
|
|
runMetadataStorageT
|
2021-09-24 01:56:37 +03:00
|
|
|
>>> flip runPGMetadataStorageAppT (pgPool, pgLogger)
|
|
|
|
>>> runCacheBuild cacheBuildParams
|
|
|
|
>>> runExceptT
|
|
|
|
>=> flip onLeft printErrJExit
|
|
|
|
>=> flip onLeft printErrJExit
|
2019-11-20 21:21:30 +03:00
|
|
|
|
2020-12-28 15:56:00 +03:00
|
|
|
(metadata, schemaCache) <- run do
|
2021-09-24 01:56:37 +03:00
|
|
|
metadata <-
|
|
|
|
snd
|
|
|
|
<$> (liftEitherM . runExceptT . runTx pgContext Q.ReadWrite)
|
|
|
|
(migrateCatalog (Just sourceConfig) maintenanceMode =<< liftIO getCurrentTime)
|
2021-11-09 17:21:48 +03:00
|
|
|
schemaCache <- lift $ lift $ buildRebuildableSchemaCache logger envMap metadata
|
2020-12-28 15:56:00 +03:00
|
|
|
pure (metadata, schemaCache)
|
|
|
|
|
2019-11-20 21:21:30 +03:00
|
|
|
cacheRef <- newMVar schemaCache
|
2020-12-28 15:56:00 +03:00
|
|
|
pure $ NT (run . flip MigrateSpec.runCacheRefT cacheRef . fmap fst . runMetadataT metadata)
|
2019-11-20 21:21:30 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
pure $
|
|
|
|
beforeAll setupCacheRef $
|
|
|
|
describe "Hasura.Server.Migrate" $ MigrateSpec.spec sourceConfig pgContext pgConnInfo
|
2019-11-18 21:45:54 +03:00
|
|
|
|
|
|
|
parseArgs :: IO TestSuites
|
2021-09-24 01:56:37 +03:00
|
|
|
parseArgs =
|
|
|
|
execParser $
|
|
|
|
info (helper <*> (parseNoCommand <|> parseSubCommand)) $
|
|
|
|
fullDesc <> header "Hasura GraphQL Engine test suite"
|
2019-10-21 19:01:05 +03:00
|
|
|
where
|
2021-12-06 18:46:52 +03:00
|
|
|
parseNoCommand = pure AllSuites
|
2020-02-13 20:38:23 +03:00
|
|
|
parseSubCommand = SingleSuite <$> parseHspecPassThroughArgs <*> subCmd
|
|
|
|
where
|
2021-09-09 10:59:04 +03:00
|
|
|
subCmd =
|
|
|
|
subparser $
|
|
|
|
mconcat
|
|
|
|
[ command "unit" $
|
|
|
|
info (pure UnitSuite) $
|
|
|
|
progDesc "Only run unit tests",
|
|
|
|
command "postgres" $
|
2021-12-06 18:46:52 +03:00
|
|
|
info (pure PostgresSuite) $
|
2021-09-09 10:59:04 +03:00
|
|
|
progDesc "Only run Postgres integration tests",
|
|
|
|
command "mssql" $
|
2021-12-06 18:46:52 +03:00
|
|
|
info (pure MSSQLSuite) $
|
2021-09-09 10:59:04 +03:00
|
|
|
progDesc "Only run SQL Server unit tests"
|
|
|
|
]
|
2020-01-21 21:12:27 +03:00
|
|
|
-- 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]
|
2021-09-24 01:56:37 +03:00
|
|
|
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)."
|
|
|
|
)
|
2020-01-21 21:12:27 +03:00
|
|
|
|
|
|
|
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
|