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)
|
2022-08-17 04:46:18 +03:00
|
|
|
import Data.Foldable (for_)
|
2022-04-10 07:47:15 +03:00
|
|
|
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-08-17 04:46:18 +03:00
|
|
|
import Hasura.Backends.DataConnector.API.V0.Capabilities 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-08-29 06:38:24 +03:00
|
|
|
import Test.ExplainSpec 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)
|
2022-09-15 12:46:17 +03:00
|
|
|
import Test.Hspec.Runner (Config (..), Path, defaultConfig, evaluateSummary)
|
2022-08-17 04:46:18 +03:00
|
|
|
import Test.MetricsSpec qualified
|
2022-04-10 07:47:15 +03:00
|
|
|
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-08-17 04:46:18 +03:00
|
|
|
for_ (API.cMetrics capabilities) \m -> Test.MetricsSpec.spec api m
|
2022-08-29 06:38:24 +03:00
|
|
|
for_ (API.cExplain capabilities) \_ -> Test.ExplainSpec.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,
|
2022-09-15 12:46:17 +03:00
|
|
|
configSkipPredicate = filterPredicates _toSkip,
|
2022-09-14 09:26:13 +03:00
|
|
|
configDryRun = _toDryRun
|
2022-04-10 07:47:15 +03:00
|
|
|
}
|
2022-09-15 12:46:17 +03:00
|
|
|
|
|
|
|
filterPredicates :: [String] -> Maybe (Path -> Bool)
|
|
|
|
filterPredicates [] = Nothing
|
|
|
|
filterPredicates xs = Just (\p -> any ($ p) (filterPredicate <$> xs))
|