2022-04-10 07:47:15 +03:00
|
|
|
module Main (main) where
|
|
|
|
|
|
|
|
import Command (AgentCapabilities (..), Command (..), TestOptions (..), parseCommandLine)
|
|
|
|
import Control.Exception (throwIO)
|
|
|
|
import Control.Monad ((>=>))
|
|
|
|
import Data.Aeson.Text (encodeToLazyText)
|
|
|
|
import Data.Proxy (Proxy (..))
|
|
|
|
import Data.Text.Lazy.IO qualified as Text
|
2022-06-29 10:42:51 +03:00
|
|
|
import Hasura.Backends.DataConnector.API (Routes (..), apiClient, openApiSchema)
|
2022-05-02 08:03:12 +03:00
|
|
|
import Hasura.Backends.DataConnector.API qualified as API
|
2022-04-10 07:47:15 +03:00
|
|
|
import Network.HTTP.Client (defaultManagerSettings, newManager)
|
|
|
|
import Servant.API (NamedRoutes)
|
|
|
|
import Servant.Client (Client, ClientError, hoistClient, mkClientEnv, runClientM, (//))
|
2022-06-03 11:06:31 +03:00
|
|
|
import Test.CapabilitiesSpec qualified
|
2022-06-30 11:36:54 +03:00
|
|
|
import Test.HealthSpec qualified
|
2022-04-10 07:47:15 +03:00
|
|
|
import Test.Hspec (Spec)
|
|
|
|
import Test.Hspec.Core.Runner (runSpec)
|
|
|
|
import Test.Hspec.Core.Util (filterPredicate)
|
|
|
|
import Test.Hspec.Runner (Config (..), defaultConfig, evaluateSummary)
|
|
|
|
import Test.QuerySpec qualified
|
|
|
|
import Test.SchemaSpec qualified
|
|
|
|
import Prelude
|
|
|
|
|
2022-06-02 08:22:44 +03:00
|
|
|
testSourceName :: API.SourceName
|
|
|
|
testSourceName = "dc-api-tests"
|
|
|
|
|
|
|
|
tests :: Client IO (NamedRoutes Routes) -> API.SourceName -> API.Config -> API.Capabilities -> Spec
|
|
|
|
tests api sourceName agentConfig capabilities = do
|
2022-06-30 11:36:54 +03:00
|
|
|
Test.HealthSpec.spec api sourceName agentConfig
|
2022-06-03 11:06:31 +03:00
|
|
|
Test.CapabilitiesSpec.spec api agentConfig capabilities
|
|
|
|
Test.SchemaSpec.spec api sourceName agentConfig
|
2022-06-02 08:22:44 +03:00
|
|
|
Test.QuerySpec.spec api sourceName agentConfig capabilities
|
2022-04-10 07:47:15 +03:00
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = do
|
|
|
|
command <- parseCommandLine
|
|
|
|
case command of
|
2022-05-02 02:01:11 +03:00
|
|
|
Test testOptions@TestOptions {..} -> do
|
2022-04-10 07:47:15 +03:00
|
|
|
api <- mkIOApiClient testOptions
|
2022-06-03 11:06:31 +03:00
|
|
|
agentCapabilities <- getAgentCapabilities api _toAgentCapabilities
|
2022-06-02 08:22:44 +03:00
|
|
|
runSpec (tests api testSourceName _toAgentConfig agentCapabilities) (applyTestConfig defaultConfig testOptions) >>= evaluateSummary
|
2022-04-10 07:47:15 +03:00
|
|
|
ExportOpenAPISpec ->
|
2022-06-29 10:42:51 +03:00
|
|
|
Text.putStrLn $ encodeToLazyText openApiSchema
|
2022-04-10 07:47:15 +03:00
|
|
|
|
|
|
|
pure ()
|
|
|
|
|
|
|
|
mkIOApiClient :: TestOptions -> IO (Client IO (NamedRoutes Routes))
|
|
|
|
mkIOApiClient TestOptions {..} = do
|
|
|
|
manager <- newManager defaultManagerSettings
|
|
|
|
let clientEnv = mkClientEnv manager _toAgentBaseUrl
|
|
|
|
pure $ hoistClient (Proxy @(NamedRoutes Routes)) (flip runClientM clientEnv >=> throwClientError) apiClient
|
|
|
|
|
|
|
|
throwClientError :: Either ClientError a -> IO a
|
|
|
|
throwClientError = either throwIO pure
|
|
|
|
|
2022-06-03 11:06:31 +03:00
|
|
|
getAgentCapabilities :: Client IO (NamedRoutes Routes) -> AgentCapabilities -> IO API.Capabilities
|
|
|
|
getAgentCapabilities api = \case
|
|
|
|
AutoDetect -> API.crCapabilities <$> (api // _capabilities)
|
2022-04-10 07:47:15 +03:00
|
|
|
Explicit capabilities -> pure capabilities
|
|
|
|
|
|
|
|
applyTestConfig :: Config -> TestOptions -> Config
|
|
|
|
applyTestConfig config TestOptions {..} =
|
|
|
|
config
|
|
|
|
{ configConcurrentJobs = _toParallelDegree,
|
|
|
|
configFilterPredicate = filterPredicate <$> _toMatch,
|
|
|
|
configSkipPredicate = filterPredicate <$> _toSkip
|
|
|
|
}
|