Revert "[server/test] create new HGE per test"

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7255
GitOrigin-RevId: c971e9e227263b0956069c0687acacf4d3ff90a9
This commit is contained in:
Gil Mizrahi 2022-12-13 14:36:03 +02:00 committed by hasura-bot
parent 51fc104793
commit 570e16efe7
27 changed files with 383 additions and 200 deletions

View File

@ -5,7 +5,7 @@ test-bigquery: remove-tix-file
docker compose up -d --wait postgres
$(call stop_after, \
HASURA_TEST_BACKEND_TYPE=BigQuery \
cabal run api-tests:exe:api-tests -- --jobs=4)
cabal run api-tests:exe:api-tests)
.PHONY: test-sqlserver
## test-sqlserver: run tests for MS SQL Server backend
@ -13,7 +13,7 @@ test-sqlserver: remove-tix-file
docker compose up -d --wait postgres sqlserver-healthcheck
$(call stop_after, \
HASURA_TEST_BACKEND_TYPE=SQLServer \
cabal run api-tests:exe:api-tests -- --jobs=4)
cabal run api-tests:exe:api-tests)
.PHONY: test-citus
## test-citus: run tests for Citus backend
@ -21,7 +21,7 @@ test-citus: remove-tix-file
docker compose up -d --wait postgres citus
$(call stop_after, \
HASURA_TEST_BACKEND_TYPE=Citus \
cabal run api-tests:exe:api-tests -- --jobs=4)
cabal run api-tests:exe:api-tests)
.PHONY: test-data-connectors
## test-data-connectors: run tests for Data Connectors
@ -30,7 +30,7 @@ test-data-connectors: remove-tix-file
docker compose up -d --wait postgres dc-reference-agent dc-sqlite-agent
$(call stop_after, \
HASURA_TEST_BACKEND_TYPE=DataConnector \
cabal run api-tests:exe:api-tests -- --jobs=4)
cabal run api-tests:exe:api-tests)
.PHONY: test-cockroach
## test-cockroach: run tests for Cockroach backend
@ -38,7 +38,7 @@ test-cockroach: remove-tix-file
docker compose up -d --wait postgres cockroach
$(call stop_after, \
HASURA_TEST_BACKEND_TYPE=Cockroach \
cabal run api-tests:exe:api-tests -- --jobs=4)
cabal run api-tests:exe:api-tests)
.PHONY: test-postgres
## test-postgres: run tests for Postgres backend
@ -48,7 +48,7 @@ test-postgres: remove-tix-file
docker compose up -d --wait postgres cockroach citus dc-sqlite-agent
$(call stop_after, \
HASURA_TEST_BACKEND_TYPE=Postgres \
cabal run api-tests:exe:api-tests -- --jobs=4)
cabal run api-tests:exe:api-tests)
.PHONY: test-no-backends
## test-no-backends
@ -56,14 +56,14 @@ test-postgres: remove-tix-file
test-no-backends: start-backends remove-tix-file
$(call stop_after, \
HASURA_TEST_BACKEND_TYPE=None \
cabal run api-tests:exe:api-tests -- --jobs=4)
cabal run api-tests:exe:api-tests)
.PHONY: test-backends
## test-backends: run tests for all backends
# BigQuery tests will require some setup detailed here: https://github.com/hasura/graphql-engine-mono/tree/main/server/lib/api-tests#required-setup-for-bigquery-tests
test-backends: start-backends remove-tix-file
$(call stop_after, \
cabal run api-tests:exe:api-tests -- --jobs=4)
cabal run api-tests:exe:api-tests)
.PHONY: test-matrix
## test-matrix: postgres test matrix generator

View File

@ -14,14 +14,15 @@ import Data.IORef
import Data.List qualified as List
import Database.PostgreSQL.Simple.Options qualified as Options
import Harness.Exceptions (HasCallStack, bracket)
import Harness.GraphqlEngine (startServerThread)
import Harness.Logging
import Harness.Test.BackendType (BackendType (..))
import Harness.TestEnvironment (GlobalTestEnvironment (..), TestingMode (..))
import Harness.TestEnvironment (GlobalTestEnvironment (..), TestingMode (..), stopServer)
import Hasura.Prelude
import System.Environment (getEnvironment)
import System.IO.Unsafe (unsafePerformIO)
import System.Log.FastLogger qualified as FL
import Test.Hspec (Spec, SpecWith, aroundAllWith, parallel, runIO)
import Test.Hspec (Spec, SpecWith, aroundAllWith, runIO)
import Test.Hspec.Core.Spec (Item (..), filterForestWithLabels, mapSpecForest, modifyConfig)
--------------------------------------------------------------------------------
@ -68,16 +69,19 @@ parseBackendType backendType =
_ -> Nothing
setupTestEnvironment :: TestingMode -> Logger -> IO GlobalTestEnvironment
setupTestEnvironment testingMode logger =
setupTestEnvironment testingMode logger = do
server <- startServerThread
pure
GlobalTestEnvironment
{ logger = logger,
testingMode = testingMode
testingMode = testingMode,
server = server
}
-- | this used to teardown the server, but now that's the concern of each test
-- | tear down the shared server
teardownTestEnvironment :: GlobalTestEnvironment -> IO ()
teardownTestEnvironment _ = pure ()
teardownTestEnvironment (GlobalTestEnvironment {server}) = stopServer server
-- | allow setting log output type
setupLogType :: IO FL.LogType
@ -117,9 +121,8 @@ hook specs = do
TestNoBackends -> True -- this is for catching "everything else"
TestNewPostgresVariant {} -> "Postgres" `elem` labels
parallel $
aroundAllWith (const . bracket (setupTestEnvironment testingMode logger) teardownTestEnvironment) $
mapSpecForest (filterForestWithLabels shouldRunTest) (contextualizeLogger specs)
aroundAllWith (const . bracket (setupTestEnvironment testingMode logger) teardownTestEnvironment) $
mapSpecForest (filterForestWithLabels shouldRunTest) (contextualizeLogger specs)
{-# NOINLINE globalConfigRef #-}
globalConfigRef :: IORef (Maybe (TestingMode, FL.LogType))

View File

@ -16,7 +16,6 @@ import Harness.Quoter.Yaml (yaml)
import Harness.Test.Fixture qualified as Fixture
import Harness.Test.Schema (Table (..), table)
import Harness.Test.Schema qualified as Schema
import Harness.Test.SetupAction qualified as SetupAction
import Harness.TestEnvironment (GlobalTestEnvironment, TestEnvironment)
import Harness.Yaml (shouldReturnYaml)
import Hasura.Prelude
@ -315,4 +314,41 @@ setupMetadata testEnvironment = do
id: x-hasura-user-id
|]
SetupAction.noTeardown setup
teardown :: IO ()
teardown =
postMetadata_
testEnvironment
[yaml|
type: bulk
args:
- type: pg_drop_delete_permission
args:
table:
schema: hasura
name: author
source: postgres
role: frontend_only_role
- type: pg_drop_delete_permission
args:
table:
schema: hasura
name: author
source: postgres
role: backend_only_role
- type: pg_drop_update_permission
args:
table:
schema: hasura
name: author
source: postgres
role: frontend_only_role
- type: pg_drop_update_permission
args:
table:
schema: hasura
name: author
source: postgres
role: backend_only_role
|]
Fixture.SetupAction setup \_ -> teardown

View File

@ -16,7 +16,6 @@ import Harness.Quoter.Yaml (yaml)
import Harness.Test.Fixture qualified as Fixture
import Harness.Test.Schema (Table (..))
import Harness.Test.Schema qualified as Schema
import Harness.Test.SetupAction qualified as SetupAction
import Harness.TestEnvironment (GlobalTestEnvironment, TestEnvironment)
import Harness.Yaml (shouldReturnYaml)
import Hasura.Prelude
@ -152,4 +151,20 @@ setupMetadata testEnvironment = do
columns: '*'
|]
SetupAction.noTeardown setup
teardown :: IO ()
teardown =
postMetadata_
testEnvironment
[yaml|
type: bulk
args:
- type: pg_drop_select_permission
args:
source: postgres
table:
schema: hasura
name: author
role: user
|]
Fixture.SetupAction setup \_ -> teardown

View File

@ -29,7 +29,11 @@ spec =
-- so that the server can be referenced while testing
Fixture.mkLocalTestEnvironment = const Webhook.run,
Fixture.setupTeardown = \(testEnvironment, (_webhookServer, _)) ->
[ Sqlserver.setupTablesAction (schema "authors" "articles") testEnvironment
[ Sqlserver.setupTablesAction (schema "authors" "articles") testEnvironment,
Fixture.SetupAction
{ Fixture.setupAction = pure (),
Fixture.teardownAction = \_ -> mssqlTeardown testEnvironment
}
]
}
]
@ -178,3 +182,13 @@ mssqlSetupWithEventTriggers testEnvironment webhookServer triggerOnReplication =
update:
columns: "*"
|]
mssqlTeardown :: TestEnvironment -> IO ()
mssqlTeardown testEnvironment = do
GraphqlEngine.postMetadata_ testEnvironment $
[yaml|
type: mssql_delete_event_trigger
args:
name: author_trigger
source: mssql
|]

View File

@ -13,7 +13,6 @@ import Harness.Quoter.Yaml
import Harness.Test.Fixture qualified as Fixture
import Harness.Test.Schema (Table (..), table)
import Harness.Test.Schema qualified as Schema
import Harness.Test.SetupAction qualified as SetupAction
import Harness.TestEnvironment (GlobalTestEnvironment, TestEnvironment)
import Harness.Webhook qualified as Webhook
import Harness.Yaml (shouldBeYaml, shouldReturnYaml)
@ -35,7 +34,10 @@ spec =
Fixture.mkLocalTestEnvironment = const Webhook.run,
Fixture.setupTeardown = \(testEnvironment, (webhookServer, _)) ->
[ Sqlserver.setupTablesAction (schema "authors" "articles") testEnvironment,
SetupAction.noTeardown (mssqlSetupWithEventTriggers testEnvironment webhookServer)
Fixture.SetupAction
{ Fixture.setupAction = mssqlSetupWithEventTriggers testEnvironment webhookServer,
Fixture.teardownAction = \_ -> mssqlTeardown testEnvironment
}
]
}
]
@ -250,3 +252,15 @@ getReplaceMetadata testEnvironment webhookServer =
timeout_sec: 60
webhook: #{webhookServerEchoEndpoint}
|]
mssqlTeardown :: TestEnvironment -> IO ()
mssqlTeardown testEnvironment = do
GraphqlEngine.postMetadata_ testEnvironment $
[yaml|
type: bulk
args:
- type: mssql_delete_event_trigger
args:
name: authors_all
source: mssql
|]

View File

@ -16,7 +16,6 @@ import Harness.Test.Fixture qualified as Fixture
import Harness.Test.Schema (Table (..), table)
import Harness.Test.Schema qualified as Schema
import Harness.Test.SetupAction (permitTeardownFail)
import Harness.Test.SetupAction qualified as SetupAction
import Harness.TestEnvironment (GlobalTestEnvironment, TestEnvironment)
import Harness.Webhook qualified as Webhook
import Harness.Yaml (shouldBeYaml, shouldReturnYaml)
@ -38,7 +37,10 @@ spec =
Fixture.mkLocalTestEnvironment = const Webhook.run,
Fixture.setupTeardown = \(testEnvironment, (webhookServer, _)) ->
[ permitTeardownFail (Postgres.setupTablesAction schema testEnvironment),
SetupAction.noTeardown (postgresSetup testEnvironment webhookServer)
Fixture.SetupAction
{ Fixture.setupAction = postgresSetup testEnvironment webhookServer,
Fixture.teardownAction = \_ -> postgresTeardown testEnvironment
}
]
}
]
@ -217,3 +219,25 @@ postgresSetup testEnvironment webhookServer = do
insert:
columns: "*"
|]
postgresTeardown :: HasCallStack => TestEnvironment -> IO ()
postgresTeardown testEnvironment = do
GraphqlEngine.postMetadata_ testEnvironment $
[yaml|
type: bulk
args:
- type: pg_delete_event_trigger
args:
name: authors_all
source: hge_test
|]
GraphqlEngine.postMetadata_ testEnvironment $
[yaml|
type: bulk
args:
- type: pg_drop_source
args:
name: hge_test
cascade: true
|]

View File

@ -29,7 +29,11 @@ spec =
-- so that the server can be referenced while testing
Fixture.mkLocalTestEnvironment = const Webhook.run,
Fixture.setupTeardown = \(testEnvironment, (_webhookServer, _)) ->
[ Postgres.setupTablesAction (schema "authors" "articles") testEnvironment
[ Postgres.setupTablesAction (schema "authors" "articles") testEnvironment,
Fixture.SetupAction
{ Fixture.setupAction = pure (),
Fixture.teardownAction = \_ -> postgresTeardown testEnvironment
}
]
}
]
@ -180,3 +184,13 @@ postgresSetupWithEventTriggers testEnvironment webhookServer triggerOnReplicatio
update:
columns: "*"
|]
postgresTeardown :: TestEnvironment -> IO ()
postgresTeardown testEnvironment = do
GraphqlEngine.postMetadata_ testEnvironment $
[yaml|
type: pg_delete_event_trigger
args:
name: author_trigger
source: postgres
|]

View File

@ -14,7 +14,6 @@ import Harness.Quoter.Yaml
import Harness.Test.Fixture qualified as Fixture
import Harness.Test.Schema (Table (..), table)
import Harness.Test.Schema qualified as Schema
import Harness.Test.SetupAction qualified as SetupAction
import Harness.TestEnvironment (GlobalTestEnvironment, TestEnvironment)
import Harness.Webhook qualified as Webhook
import Harness.Yaml (shouldReturnYaml)
@ -32,7 +31,10 @@ spec =
{ Fixture.mkLocalTestEnvironment = const Webhook.run,
Fixture.setupTeardown = \(testEnvironment, _) ->
[ Postgres.setupTablesAction schema testEnvironment,
SetupAction.noTeardown (postgresSetup testEnvironment)
Fixture.SetupAction
{ Fixture.setupAction = postgresSetup testEnvironment,
Fixture.teardownAction = \_ -> postgresTeardown testEnvironment
}
]
}
]
@ -159,6 +161,23 @@ postgresSetup testEnvironment = do
CREATE EVENT TRIGGER pg_get_ddl_command on ddl_command_end EXECUTE PROCEDURE #{schemaName}.log_ddl_command();
|]
postgresTeardown :: TestEnvironment -> IO ()
postgresTeardown testEnvironment = do
let schemaName :: Schema.SchemaName
schemaName = Schema.getSchemaName testEnvironment
GraphqlEngine.postV2Query_ testEnvironment $
[interpolateYaml|
type: run_sql
args:
source: postgres
sql: |
DROP EVENT TRIGGER pg_get_ddl_command;
DROP FUNCTION #{schemaName}.log_ddl_command;
DROP TABLE #{schemaName}.ddl_history;
|]
--------------------------------------------------------------------------------
-- * Tests

View File

@ -18,7 +18,6 @@ import Harness.Test.Fixture qualified as Fixture
import Harness.Test.Schema (Table (..), table)
import Harness.Test.Schema qualified as Schema
import Harness.Test.SetupAction (permitTeardownFail)
import Harness.Test.SetupAction qualified as SetupAction
import Harness.TestEnvironment (GlobalTestEnvironment, Server (..), TestEnvironment, getServer)
import Harness.Webhook qualified as Webhook
import Harness.Yaml (shouldBeYaml, shouldReturnYaml)
@ -41,7 +40,10 @@ spec =
Fixture.mkLocalTestEnvironment = const Webhook.run,
Fixture.setupTeardown = \(testEnvironment, (webhookServer, _)) ->
[ permitTeardownFail (Postgres.setupTablesAction (schema "authors") testEnvironment),
SetupAction.noTeardown (postgresSetup testEnvironment webhookServer)
Fixture.SetupAction
{ Fixture.setupAction = postgresSetup testEnvironment webhookServer,
Fixture.teardownAction = \_ -> postgresTeardown testEnvironment
}
]
}
]
@ -319,3 +321,21 @@ postgresSetup testEnvironment webhookServer = do
- name
- created_at
|]
postgresTeardown :: TestEnvironment -> IO ()
postgresTeardown testEnvironment = do
GraphqlEngine.postMetadata_ testEnvironment $
[yaml|
type: bulk
args:
- type: pg_delete_event_trigger
args:
name: authors_all
source: postgres
|]
-- only authors table needs to be tear down because
-- the users table has already been dropped in the
-- `dropTableContainingTriggerTest` test.
-- The authors table was renamed in the `renameTableContainingTriggerTests` test
Postgres.dropTableIfExists testEnvironment (authorsTable "authors_new")

View File

@ -13,7 +13,6 @@ import Harness.Quoter.Yaml
import Harness.Test.Fixture qualified as Fixture
import Harness.Test.Schema (Table (..), table)
import Harness.Test.Schema qualified as Schema
import Harness.Test.SetupAction qualified as SetupAction
import Harness.TestEnvironment (GlobalTestEnvironment, TestEnvironment)
import Harness.Webhook qualified as Webhook
import Harness.Yaml (shouldBeYaml, shouldReturnYaml)
@ -35,7 +34,10 @@ spec =
Fixture.mkLocalTestEnvironment = const Webhook.run,
Fixture.setupTeardown = \(testEnvironment, (webhookServer, _)) ->
[ Postgres.setupTablesAction (schema "authors" "articles") testEnvironment,
SetupAction.noTeardown (postgresSetup testEnvironment webhookServer)
Fixture.SetupAction
{ Fixture.setupAction = postgresSetup testEnvironment webhookServer,
Fixture.teardownAction = \_ -> postgresTeardown testEnvironment
}
]
}
]
@ -250,3 +252,15 @@ getReplaceMetadata testEnvironment webhookServer =
timeout_sec: 60
webhook: #{webhookServerEchoEndpoint}
|]
postgresTeardown :: TestEnvironment -> IO ()
postgresTeardown testEnvironment = do
GraphqlEngine.postMetadata_ testEnvironment $
[yaml|
type: bulk
args:
- type: pg_delete_event_trigger
args:
name: authors_all
source: postgres
|]

View File

@ -15,7 +15,6 @@ import Harness.Quoter.Yaml
import Harness.Test.Fixture qualified as Fixture
import Harness.Test.Schema (Table (..), table)
import Harness.Test.Schema qualified as Schema
import Harness.Test.SetupAction qualified as SetupAction
import Harness.TestEnvironment (GlobalTestEnvironment, TestEnvironment)
import Harness.Yaml (shouldReturnOneOfYaml, shouldReturnYaml)
import Hasura.Prelude
@ -293,4 +292,18 @@ setupMetadata testEnvironment = do
author_name: name
|]
SetupAction.noTeardown setup
teardown :: IO ()
teardown =
postMetadata_
testEnvironment
[yaml|
type: pg_drop_relationship
args:
source: postgres
table:
schema: hasura
name: article
relationship: author
|]
Fixture.SetupAction setup \_ -> teardown

View File

@ -17,7 +17,6 @@ import Harness.Quoter.Yaml (interpolateYaml, yaml)
import Harness.Test.Fixture qualified as Fixture
import Harness.Test.Schema (Table (..), table)
import Harness.Test.Schema qualified as Schema
import Harness.Test.SetupAction qualified as SetupAction
import Harness.TestEnvironment (GlobalTestEnvironment, TestEnvironment)
import Harness.Yaml (shouldReturnYaml)
import Hasura.Prelude
@ -151,4 +150,18 @@ setupMetadata backendTypeMetadata testEnvironment = do
subscription_root_fields: ["select_stream"]
|]
SetupAction.noTeardown setup
teardown :: IO ()
teardown =
postMetadata_
testEnvironment
[interpolateYaml|
type: #{backendPrefix}_drop_select_permission
args:
source: #{source}
table:
#{schemaKeyword}: #{schemaName}
name: logs
role: user
|]
Fixture.SetupAction setup \_ -> teardown

View File

@ -20,7 +20,6 @@ import Harness.Quoter.Yaml (interpolateYaml)
import Harness.Test.Fixture qualified as Fixture
import Harness.Test.Schema (Table (..), table)
import Harness.Test.Schema qualified as Schema
import Harness.Test.SetupAction qualified as SetupAction
import Harness.TestEnvironment (GlobalTestEnvironment, TestEnvironment)
import Harness.Yaml (shouldReturnYaml)
import Hasura.Prelude
@ -276,4 +275,34 @@ setupMetadata backendTypeMetadata testEnvironment = do
- name
|]
SetupAction.noTeardown setup
teardown :: IO ()
teardown =
postMetadata_
testEnvironment
[interpolateYaml|
type: bulk
args:
- type: #{backendPrefix}_drop_insert_permission
args:
source: #{source}
table:
#{schemaKeyword}: #{schemaName}
name: article
role: user
- type: #{backendPrefix}_drop_select_permission
args:
source: #{source}
table:
#{schemaKeyword}: #{schemaName}
name: article
role: user
- type: #{backendPrefix}_drop_insert_permission
args:
source: #{source}
table:
#{schemaKeyword}: #{schemaName}
name: author
role: user
|]
Fixture.SetupAction setup \_ -> teardown

View File

@ -19,7 +19,6 @@ import Harness.Quoter.Yaml (interpolateYaml)
import Harness.Test.Fixture qualified as Fixture
import Harness.Test.Schema (Table (..), table)
import Harness.Test.Schema qualified as Schema
import Harness.Test.SetupAction qualified as SetupAction
import Harness.TestEnvironment (GlobalTestEnvironment, TestEnvironment)
import Harness.Yaml (shouldReturnYaml)
import Hasura.Prelude
@ -257,4 +256,27 @@ setupMetadata backendTypeMetadata testEnvironment = do
columns: "*"
|]
SetupAction.noTeardown setup
teardown :: IO ()
teardown =
postMetadata_
testEnvironment
[interpolateYaml|
type: bulk
args:
- type: #{backendPrefix}_drop_select_permission
args:
source: #{source}
table:
name: article
#{schemaKeyword}: #{schemaName}
role: author
- type: #{backendPrefix}_drop_select_permission
args:
source: #{source}
table:
name: article
#{schemaKeyword}: #{schemaName}
role: user
|]
Fixture.SetupAction setup \_ -> teardown

View File

@ -18,7 +18,6 @@ import Harness.Quoter.Yaml (interpolateYaml, yaml)
import Harness.Test.Fixture qualified as Fixture
import Harness.Test.Schema (Table (..), table)
import Harness.Test.Schema qualified as Schema
import Harness.Test.SetupAction qualified as SetupAction
import Harness.TestEnvironment (GlobalTestEnvironment, TestEnvironment)
import Harness.Yaml (shouldReturnYaml)
import Hasura.Prelude
@ -259,4 +258,27 @@ setupMetadata backendTypeMetadata testEnvironment = do
columns: '*'
|]
SetupAction.noTeardown setup
teardown :: IO ()
teardown =
postMetadata_
testEnvironment
[interpolateYaml|
type: bulk
args:
- type: #{backendPrefix}_drop_select_permission
args:
source: #{source}
table:
schema: #{schemaName}
name: author
role: user
- type: #{backendPrefix}_drop_insert_permission
args:
source: #{source}
table:
schema: #{schemaName}
name: author
role: user
|]
Fixture.SetupAction setup \_ -> teardown

View File

@ -21,6 +21,7 @@ where
--------------------------------------------------------------------------------
import Control.Concurrent.Async (concurrently_)
import Control.Concurrent.Extended
import Data.List qualified as List
import Data.String
@ -265,7 +266,9 @@ setup tables' (testEnvironment, _) = do
teardown :: [Schema.Table] -> (TestEnvironment, ()) -> IO ()
teardown _ (testEnvironment, _) = do
let schemaName = Schema.getSchemaName testEnvironment
removeDataset schemaName
concurrently_
(GraphqlEngine.setSources testEnvironment mempty Nothing)
(removeDataset schemaName)
setupTablesAction :: HasCallStack => [Schema.Table] -> TestEnvironment -> SetupAction
setupTablesAction ts env =

View File

@ -320,7 +320,8 @@ setupPermissionsAction permissions env =
-- | Teardown the schema and tracking in the most expected way.
-- NOTE: Certain test modules may warrant having their own version.
teardown :: HasCallStack => [Schema.Table] -> (TestEnvironment, ()) -> IO ()
teardown _ _ = pure ()
teardown _ (testEnvironment, _) =
GraphqlEngine.setSources testEnvironment mempty Nothing
-- | Setup the given permissions to the graphql engine in a TestEnvironment.
setupPermissions :: [Permissions.Permission] -> TestEnvironment -> IO ()

View File

@ -314,7 +314,8 @@ setup tables (testEnvironment, _) = do
-- Because the Fixture takes care of dropping the DB, all we do here is
-- clear the metadata with `replace_metadata`.
teardown :: HasCallStack => [Schema.Table] -> (TestEnvironment, ()) -> IO ()
teardown _ _ = pure ()
teardown _ (testEnvironment, _) =
GraphqlEngine.setSources testEnvironment mempty Nothing
setupTablesAction :: [Schema.Table] -> TestEnvironment -> SetupAction
setupTablesAction ts env =

View File

@ -12,9 +12,7 @@ module Harness.Backend.Postgres
runSQL,
defaultSourceMetadata,
defaultSourceConfiguration,
createMetadataDatabase,
createDatabase,
dropMetadataDatabase,
dropDatabase,
createTable,
insertTable,
@ -69,7 +67,7 @@ import Harness.Test.Schema
)
import Harness.Test.Schema qualified as Schema
import Harness.Test.SetupAction (SetupAction (..))
import Harness.TestEnvironment (GlobalTestEnvironment (..), TestEnvironment (..), TestingMode (..), UniqueTestId, testLogMessage)
import Harness.TestEnvironment (GlobalTestEnvironment (..), TestEnvironment (..), TestingMode (..), testLogMessage)
import Hasura.Prelude
import System.Process.Typed
import Text.Pretty.Simple (pShow)
@ -133,20 +131,18 @@ makeFreshDbConnectionString testEnvironment =
{ Postgres.connectDatabase = uniqueDbName (uniqueTestId testEnvironment)
}
metadataLivenessCheck :: HasCallStack => TestEnvironment -> IO ()
metadataLivenessCheck :: HasCallStack => IO ()
metadataLivenessCheck =
doLivenessCheck
. fromString
. postgresqlMetadataConnectionString
. uniqueTestId
doLivenessCheck $
fromString postgresqlMetadataConnectionString
livenessCheck :: HasCallStack => TestEnvironment -> IO ()
livenessCheck = doLivenessCheck . makeFreshDbConnectionString
-- PostgreSQL 15.1 on x86_64-pc-linux-musl, com ....
-- forgive me, padre
parsePostgresVersion :: String -> Maybe Int
parsePostgresVersion =
_parsePostgresVersion :: String -> Maybe Int
_parsePostgresVersion =
readMaybe
. takeWhile (not . (==) '.')
. drop (length @[] "PostgreSQL ")
@ -401,17 +397,6 @@ untrackTable :: TestEnvironment -> Schema.Table -> IO ()
untrackTable testEnvironment table =
Schema.untrackTable (BackendType.backendSourceName backendTypeMetadata) table testEnvironment
createMetadataDatabase :: GlobalTestEnvironment -> UniqueTestId -> IO ()
createMetadataDatabase globalTestEnvironment uniqueTestId =
runWithInitialDb_
globalTestEnvironment
("CREATE DATABASE " <> Constants.postgresMetadataDb uniqueTestId <> ";")
dropMetadataDatabase :: TestEnvironment -> IO ()
dropMetadataDatabase testEnvironment = do
let dbName = postgresMetadataDb (uniqueTestId testEnvironment)
dropDatabaseInternal dbName testEnvironment
-- | create a database to use and later drop for these tests
-- note we use the 'initial' connection string here, ie, the one we started
-- with.
@ -431,37 +416,21 @@ dropDatabase testEnvironment = do
-- up.
dropDatabaseInternal :: String -> TestEnvironment -> IO ()
dropDatabaseInternal dbName testEnvironment = do
([Postgres.Only version]) <-
queryWithInitialDb @(Postgres.Only String)
void $
queryWithInitialDb @(Postgres.Only Bool)
testEnvironment
"SELECT version();"
[i|
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = '#{dbName}'
AND pid <> pg_backend_pid();
|]
case parsePostgresVersion version of
Just pgVersion | pgVersion >= 13 -> do
-- if we are on Postgres 13 or more, we can use WITH (FORCE);
runWithInitialDb_
(globalEnvironment testEnvironment)
("DROP DATABASE " <> dbName <> " WITH (FORCE);")
`catch` \(ex :: SomeException) -> testLogMessage testEnvironment (LogDropDBFailedWarning (T.pack dbName) ex)
-- for older Postgres versions, we Do Our Best
_ -> do
-- throw all the other users off the database
void $
queryWithInitialDb @(Postgres.Only Bool)
testEnvironment
[i|
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = '#{dbName}'
AND pid <> pg_backend_pid();
|]
-- if this fails, don't make the test fail
runWithInitialDb_
(globalEnvironment testEnvironment)
("DROP DATABASE " <> dbName <> ";")
`catch` \(ex :: SomeException) -> testLogMessage testEnvironment (LogDropDBFailedWarning (T.pack dbName) ex)
-- if this fails, don't make the test fail
runWithInitialDb_
(globalEnvironment testEnvironment)
("DROP DATABASE " <> dbName <> ";")
`catch` \(ex :: SomeException) -> testLogMessage testEnvironment (LogDropDBFailedWarning (T.pack dbName) ex)
-- Because the test harness sets the schema name we use for testing, we need
-- to make sure it exists before we run the tests.
@ -502,7 +471,8 @@ setup tables (testEnvironment, _) = do
-- NOTE: Certain test modules may warrant having their own version.
-- we replace metadata with nothing.
teardown :: [Schema.Table] -> (TestEnvironment, ()) -> IO ()
teardown _ _ = pure ()
teardown _ (testEnvironment, _) =
GraphqlEngine.setSources testEnvironment mempty Nothing
setupTablesAction :: [Schema.Table] -> TestEnvironment -> SetupAction
setupTablesAction ts env =

View File

@ -323,7 +323,8 @@ setup tables (testEnvironment, _) = do
-- | Teardown the schema and tracking in the most expected way.
-- NOTE: Certain test modules may warrant having their own version.
teardown :: HasCallStack => [Schema.Table] -> (TestEnvironment, ()) -> IO ()
teardown _ _ = pure ()
teardown _ (testEnvironment, _) =
GraphqlEngine.setSources testEnvironment mempty Nothing
setupTablesAction :: [Schema.Table] -> TestEnvironment -> SetupAction
setupTablesAction ts env =

View File

@ -8,8 +8,8 @@ module Harness.Constants
postgresDb,
postgresHost,
postgresPort,
postgresMetadataDb,
postgresqlMetadataConnectionString,
postgresMetadataDb,
postgresLivenessCheckAttempts,
postgresLivenessCheckIntervalSeconds,
defaultPostgresPort,
@ -82,9 +82,8 @@ postgresMetadataPassword = "hasura"
postgresMetadataUser :: String
postgresMetadataUser = "hasura"
postgresMetadataDb :: UniqueTestId -> String
postgresMetadataDb uniqueTestId =
"hasura_metadata_" <> uniqueDbName uniqueTestId
postgresMetadataDb :: String
postgresMetadataDb = "hasura_metadata"
postgresMetadataHost :: String
postgresMetadataHost = "127.0.0.1"
@ -92,8 +91,8 @@ postgresMetadataHost = "127.0.0.1"
postgresMetadataPort :: Word16
postgresMetadataPort = 65002
postgresqlMetadataConnectionString :: UniqueTestId -> String
postgresqlMetadataConnectionString uniqueTestId =
postgresqlMetadataConnectionString :: String
postgresqlMetadataConnectionString =
"postgres://"
++ postgresMetadataUser
++ ":"
@ -103,7 +102,7 @@ postgresqlMetadataConnectionString uniqueTestId =
++ ":"
++ show postgresMetadataPort
++ "/"
++ postgresMetadataDb uniqueTestId
++ postgresMetadataDb
-- * Postgres

View File

@ -56,7 +56,7 @@ import Harness.Exceptions (bracket, withFrozenCallStack)
import Harness.Http qualified as Http
import Harness.Logging
import Harness.Quoter.Yaml (fromYaml, yaml)
import Harness.TestEnvironment (Server (..), TestEnvironment (..), UniqueTestId, getServer, serverUrl, testLogMessage)
import Harness.TestEnvironment (Server (..), TestEnvironment (..), getServer, serverUrl, testLogMessage)
import Hasura.App (Loggers (..), ServeCtx (..))
import Hasura.App qualified as App
import Hasura.Logging (Hasura)
@ -264,8 +264,8 @@ args:
-- available before returning.
--
-- The port availability is subject to races.
startServerThread :: UniqueTestId -> IO Server
startServerThread uniqueTestId = do
startServerThread :: IO Server
startServerThread = do
port <- bracket (Warp.openFreePort) (Socket.close . snd) (pure . fst)
let urlPrefix = "http://127.0.0.1"
backendConfigs =
@ -278,7 +278,6 @@ startServerThread uniqueTestId = do
thread <-
Async.async
( runApp
uniqueTestId
Constants.serveOptions
{ soPort = unsafePort port,
soMetadataDefaults = backendConfigs
@ -291,14 +290,14 @@ startServerThread uniqueTestId = do
-------------------------------------------------------------------------------
-- | Run the graphql-engine server.
runApp :: UniqueTestId -> ServeOptions Hasura.Logging.Hasura -> IO ()
runApp uniqueTestId serveOptions = do
runApp :: ServeOptions Hasura.Logging.Hasura -> IO ()
runApp serveOptions = do
let rci =
PostgresConnInfo
{ _pciDatabaseConn = Nothing,
_pciRetries = Nothing
}
metadataDbUrl = Just $ Constants.postgresqlMetadataConnectionString uniqueTestId
metadataDbUrl = Just $ Constants.postgresqlMetadataConnectionString
env <- Env.getEnvironment
initTime <- liftIO getCurrentTime
globalCtx <- App.initGlobalCtx env metadataDbUrl rci

View File

@ -15,8 +15,6 @@ module Harness.Logging.Messages
LogDropDBFailedWarning (..),
LogSubscriptionInit (..),
LogSubscriptionResponse (..),
LogServerSetupDuration (..),
LogServerTeardownDuration (..),
LogFixtureTestStart (..),
LogFixtureSetupFailed (..),
LogFixtureSetupSucceeded (..),
@ -37,7 +35,6 @@ import Data.ByteString.Lazy qualified as LBS
import Data.Text qualified as T
import Data.Text.Encoding
import Data.Text.Lazy qualified as LT
import Data.Time (NominalDiffTime)
import GHC.TypeLits (ErrorMessage (..), TypeError)
import Hasura.Prelude hiding (Seconds)
import System.Log.FastLogger qualified as FL
@ -320,26 +317,6 @@ instance LoggableMessage LogFixtureTeardownFailed where
("step", Number (fromIntegral lftfStep))
]
data LogServerSetupDuration = LogServerSetupDuration
{lssdDuration :: NominalDiffTime}
instance LoggableMessage LogServerSetupDuration where
fromLoggableMessage LogServerSetupDuration {..} =
object
[ ("type", String "LogServerSetupDuration"),
("duration", Number (realToFrac lssdDuration))
]
data LogServerTeardownDuration = LogServerTeardownDuration
{lstdDuration :: NominalDiffTime}
instance LoggableMessage LogServerTeardownDuration where
fromLoggableMessage LogServerTeardownDuration {..} =
object
[ ("type", String "LogServerTeardownDuration"),
("duration", Number (realToFrac lstdDuration))
]
-- | Temporary message type for messages logged from within the Harness modules.
-- Ideally these should have more bespoke message types to make the logs easier
-- to sort through.

View File

@ -35,7 +35,8 @@ import Data.Map.Strict qualified as Map
import Harness.Exceptions (throw, withFrozenCallStack)
import Harness.Logging.Messages
import Harness.TestEnvironment
( Server (..),
( GlobalTestEnvironment (..),
Server (..),
TestEnvironment (..),
testLogMessage,
)
@ -96,7 +97,7 @@ newtype SubscriptionHandle = SubscriptionHandle {unSubscriptionHandle :: MVar Va
-- > actual `shouldBe` expected
withSubscriptions :: SpecWith (Value -> [Pair] -> IO SubscriptionHandle, TestEnvironment) -> SpecWith TestEnvironment
withSubscriptions = aroundAllWith \actionWithSubAndTest testEnvironment -> do
WS.runClient "127.0.0.1" (fromIntegral $ port $ server testEnvironment) "/v1/graphql" \conn -> do
WS.runClient "127.0.0.1" (fromIntegral $ port $ server $ globalEnvironment testEnvironment) "/v1/graphql" \conn -> do
-- CAVE: loads of stuff still outstanding:
-- * trimming threads, NDAT-228
-- * multiplexing handles, NDAT-229

View File

@ -29,19 +29,16 @@ module Harness.Test.Fixture
)
where
import Control.Concurrent (forkIO)
import Control.Monad.Managed (Managed, runManaged, with)
import Data.Aeson (Value)
import Data.Set qualified as S
import Data.Text qualified as T
import Data.Time (diffUTCTime, getCurrentTime)
import Data.UUID.V4 (nextRandom)
import Harness.Backend.Citus qualified as Citus
import Harness.Backend.Cockroach qualified as Cockroach
import Harness.Backend.Postgres qualified as Postgres
import Harness.Backend.Sqlserver qualified as Sqlserver
import Harness.Exceptions
import Harness.GraphqlEngine (startServerThread)
import Harness.Logging
import Harness.Test.BackendType
import Harness.Test.CustomOptions
@ -54,7 +51,6 @@ import Harness.TestEnvironment
TestingMode (..),
UniqueTestId (..),
logger,
stopServer,
)
import Hasura.Prelude hiding (log)
import Test.Hspec
@ -66,7 +62,6 @@ import Test.Hspec
describe,
pendingWith,
)
import Test.Hspec.Core.Spec (sequential)
-- | Runs the given tests, for each provided 'Fixture'@ ()@.
--
@ -86,8 +81,9 @@ import Test.Hspec.Core.Spec (sequential)
-- however it makes CI punishingly slow, so we defer to the "worse" version for
-- now. When we come to run specs in parallel this will be helpful.
run :: NonEmpty (Fixture ()) -> (Options -> SpecWith TestEnvironment) -> SpecWith GlobalTestEnvironment
run fixtures tests =
runWithLocalTestEnvironment fixtures (\opts -> beforeWith (\(te, ()) -> return te) (tests opts))
run = runSingleSetup
-- runWithLocalTestEnvironment fixtures (\opts -> beforeWith (\(te, ()) -> return te) (tests opts))
{-# DEPRECATED runSingleSetup "runSingleSetup lets all specs in aFixture share a single database environment, which impedes parallelisation and out-of-order execution." #-}
runSingleSetup :: NonEmpty (Fixture ()) -> (Options -> SpecWith TestEnvironment) -> SpecWith GlobalTestEnvironment
@ -119,8 +115,7 @@ runWithLocalTestEnvironmentSingleSetup ::
NonEmpty (Fixture a) ->
(Options -> SpecWith (TestEnvironment, a)) ->
SpecWith GlobalTestEnvironment
runWithLocalTestEnvironmentSingleSetup fixtures tests =
runWithLocalTestEnvironmentInternal aroundAllWith fixtures (\opts -> sequential (tests opts))
runWithLocalTestEnvironmentSingleSetup = runWithLocalTestEnvironmentInternal aroundAllWith
runWithLocalTestEnvironmentInternal ::
forall a.
@ -149,21 +144,7 @@ runWithLocalTestEnvironmentInternal aroundSomeWith fixtures tests =
TestNoBackends -> S.null (backendTypesForFixture fixtureName)
TestEverything -> True
-- data connector tests currently depend on being run sequentially
shouldForceSequentialRun :: FixtureName -> Bool
shouldForceSequentialRun fixtureName =
let isDataConnector = \case
DataConnector _dc -> True
_ -> False
in any isDataConnector (backendTypesForFixture fixtureName)
-- how should we run these?
let concurrency =
if shouldForceSequentialRun n
then sequential
else id
concurrency $ describe (show n) do
describe (show n) do
flip aroundSomeWith (tests options) \test globalTestEnvironment ->
if not (n `shouldRunIn` testingMode globalTestEnvironment)
then pendingWith $ "Inapplicable test."
@ -202,8 +183,8 @@ fixtureBracket
-- run test-specific clean up
cleanup
-- fork cleanup, don't wait
void $ forkIO $ teardownTestEnvironment testEnvironment name
-- drop all DBs created for the tests
dropDatabases name testEnvironment
-- | given the `FixtureName` and `uniqueTestId`, spin up all necessary
-- databases for these tests
@ -224,8 +205,7 @@ createDatabases fixtureName testEnvironment =
(backendTypesForFixture fixtureName)
dropDatabases :: FixtureName -> TestEnvironment -> IO ()
dropDatabases fixtureName testEnvironment = do
Postgres.dropMetadataDatabase testEnvironment
dropDatabases fixtureName testEnvironment =
traverse_
( \case
Postgres ->
@ -245,13 +225,6 @@ dropDatabases fixtureName testEnvironment = do
setupTestEnvironment :: FixtureName -> GlobalTestEnvironment -> IO TestEnvironment
setupTestEnvironment name globalTestEnvironment = do
uniqueTestId <- UniqueTestId <$> nextRandom
-- start timing
startTime <- getCurrentTime
-- create a fresh metadata DB
Postgres.createMetadataDatabase globalTestEnvironment uniqueTestId
-- start a nice fresh HGE
server <- startServerThread uniqueTestId
let testEnvironment =
TestEnvironment
@ -259,36 +232,13 @@ setupTestEnvironment name globalTestEnvironment = do
Backend db -> Just db
_ -> Nothing,
uniqueTestId = uniqueTestId,
globalEnvironment = globalTestEnvironment,
server = server
globalEnvironment = globalTestEnvironment
}
-- create source databases
createDatabases name testEnvironment
-- get end time
endTime <- getCurrentTime
-- log duration
liftIO $
runLogger (logger globalTestEnvironment) $
LogServerSetupDuration (diffUTCTime endTime startTime)
pure testEnvironment
teardownTestEnvironment :: TestEnvironment -> FixtureName -> IO ()
teardownTestEnvironment testEnvironment name = do
-- don't wait for teardown
startTime <- getCurrentTime
-- kill server
stopServer (server testEnvironment)
-- drop db
dropDatabases name testEnvironment
-- end time
endTime <- getCurrentTime
-- log duration
liftIO $
runLogger (logger (globalEnvironment testEnvironment)) $
LogServerTeardownDuration (diffUTCTime endTime startTime)
-- | A function that makes it easy to perform setup and teardown when
-- debugging/developing tests within a repl.
fixtureRepl ::

View File

@ -9,6 +9,7 @@ module Harness.TestEnvironment
TestingMode (..),
UniqueTestId (..),
getServer,
getTestingMode,
serverUrl,
stopServer,
testLogTrace,
@ -50,15 +51,15 @@ data GlobalTestEnvironment = GlobalTestEnvironment
logger :: Logger,
-- | the mode in which we're running the tests. See 'TestingMode' for
-- details'.
testingMode :: TestingMode
testingMode :: TestingMode,
-- | connection details for the instance of HGE we're connecting to
server :: Server
}
-- | A testEnvironment that's passed to all tests.
data TestEnvironment = TestEnvironment
{ -- | shared setup not related to a particular test
globalEnvironment :: GlobalTestEnvironment,
-- | connection details for the instance of HGE we're connecting to
server :: Server,
-- | a uuid generated for each test suite used to generate a unique
-- `SchemaName`
uniqueTestId :: UniqueTestId,
@ -67,6 +68,10 @@ data TestEnvironment = TestEnvironment
backendTypeConfig :: Maybe BackendTypeConfig
}
instance Show TestEnvironment where
show TestEnvironment {globalEnvironment} =
"<TestEnvironment: " ++ urlPrefix (server globalEnvironment) ++ ":" ++ show (port (server globalEnvironment)) ++ " >"
-- | Credentials for our testing modes. See 'SpecHook.setupTestingMode' for the
-- practical consequences of this type.
data TestingMode
@ -93,7 +98,7 @@ data Server = Server
-- | Retrieve the 'Server' associated with some 'TestEnvironment'.
getServer :: TestEnvironment -> Server
getServer TestEnvironment {server} = server
getServer TestEnvironment {globalEnvironment} = server globalEnvironment
-- | Extracts the full URL prefix and port number from a given 'Server'.
--
@ -104,6 +109,10 @@ getServer TestEnvironment {server} = server
serverUrl :: Server -> String
serverUrl Server {urlPrefix, port} = urlPrefix ++ ":" ++ show port
-- | Retrieve the 'TestingMode' associated with some 'TestEnvironment'
getTestingMode :: TestEnvironment -> TestingMode
getTestingMode = testingMode . globalEnvironment
-- | Forcibly stop a given 'Server'.
stopServer :: Server -> IO ()
stopServer Server {thread} = Async.cancel thread