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 docker compose up -d --wait postgres
$(call stop_after, \ $(call stop_after, \
HASURA_TEST_BACKEND_TYPE=BigQuery \ HASURA_TEST_BACKEND_TYPE=BigQuery \
cabal run api-tests:exe:api-tests -- --jobs=4) cabal run api-tests:exe:api-tests)
.PHONY: test-sqlserver .PHONY: test-sqlserver
## test-sqlserver: run tests for MS SQL Server backend ## 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 docker compose up -d --wait postgres sqlserver-healthcheck
$(call stop_after, \ $(call stop_after, \
HASURA_TEST_BACKEND_TYPE=SQLServer \ HASURA_TEST_BACKEND_TYPE=SQLServer \
cabal run api-tests:exe:api-tests -- --jobs=4) cabal run api-tests:exe:api-tests)
.PHONY: test-citus .PHONY: test-citus
## test-citus: run tests for Citus backend ## test-citus: run tests for Citus backend
@ -21,7 +21,7 @@ test-citus: remove-tix-file
docker compose up -d --wait postgres citus docker compose up -d --wait postgres citus
$(call stop_after, \ $(call stop_after, \
HASURA_TEST_BACKEND_TYPE=Citus \ 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 .PHONY: test-data-connectors
## test-data-connectors: run tests for 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 docker compose up -d --wait postgres dc-reference-agent dc-sqlite-agent
$(call stop_after, \ $(call stop_after, \
HASURA_TEST_BACKEND_TYPE=DataConnector \ HASURA_TEST_BACKEND_TYPE=DataConnector \
cabal run api-tests:exe:api-tests -- --jobs=4) cabal run api-tests:exe:api-tests)
.PHONY: test-cockroach .PHONY: test-cockroach
## test-cockroach: run tests for Cockroach backend ## test-cockroach: run tests for Cockroach backend
@ -38,7 +38,7 @@ test-cockroach: remove-tix-file
docker compose up -d --wait postgres cockroach docker compose up -d --wait postgres cockroach
$(call stop_after, \ $(call stop_after, \
HASURA_TEST_BACKEND_TYPE=Cockroach \ HASURA_TEST_BACKEND_TYPE=Cockroach \
cabal run api-tests:exe:api-tests -- --jobs=4) cabal run api-tests:exe:api-tests)
.PHONY: test-postgres .PHONY: test-postgres
## test-postgres: run tests for Postgres backend ## 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 docker compose up -d --wait postgres cockroach citus dc-sqlite-agent
$(call stop_after, \ $(call stop_after, \
HASURA_TEST_BACKEND_TYPE=Postgres \ 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 .PHONY: test-no-backends
## test-no-backends ## test-no-backends
@ -56,14 +56,14 @@ test-postgres: remove-tix-file
test-no-backends: start-backends remove-tix-file test-no-backends: start-backends remove-tix-file
$(call stop_after, \ $(call stop_after, \
HASURA_TEST_BACKEND_TYPE=None \ HASURA_TEST_BACKEND_TYPE=None \
cabal run api-tests:exe:api-tests -- --jobs=4) cabal run api-tests:exe:api-tests)
.PHONY: test-backends .PHONY: test-backends
## test-backends: run tests for all 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 # 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 test-backends: start-backends remove-tix-file
$(call stop_after, \ $(call stop_after, \
cabal run api-tests:exe:api-tests -- --jobs=4) cabal run api-tests:exe:api-tests)
.PHONY: test-matrix .PHONY: test-matrix
## test-matrix: postgres test matrix generator ## test-matrix: postgres test matrix generator

View File

@ -14,14 +14,15 @@ import Data.IORef
import Data.List qualified as List import Data.List qualified as List
import Database.PostgreSQL.Simple.Options qualified as Options import Database.PostgreSQL.Simple.Options qualified as Options
import Harness.Exceptions (HasCallStack, bracket) import Harness.Exceptions (HasCallStack, bracket)
import Harness.GraphqlEngine (startServerThread)
import Harness.Logging import Harness.Logging
import Harness.Test.BackendType (BackendType (..)) import Harness.Test.BackendType (BackendType (..))
import Harness.TestEnvironment (GlobalTestEnvironment (..), TestingMode (..)) import Harness.TestEnvironment (GlobalTestEnvironment (..), TestingMode (..), stopServer)
import Hasura.Prelude import Hasura.Prelude
import System.Environment (getEnvironment) import System.Environment (getEnvironment)
import System.IO.Unsafe (unsafePerformIO) import System.IO.Unsafe (unsafePerformIO)
import System.Log.FastLogger qualified as FL 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) import Test.Hspec.Core.Spec (Item (..), filterForestWithLabels, mapSpecForest, modifyConfig)
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -68,16 +69,19 @@ parseBackendType backendType =
_ -> Nothing _ -> Nothing
setupTestEnvironment :: TestingMode -> Logger -> IO GlobalTestEnvironment setupTestEnvironment :: TestingMode -> Logger -> IO GlobalTestEnvironment
setupTestEnvironment testingMode logger = setupTestEnvironment testingMode logger = do
server <- startServerThread
pure pure
GlobalTestEnvironment GlobalTestEnvironment
{ logger = logger, { 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 :: GlobalTestEnvironment -> IO ()
teardownTestEnvironment _ = pure () teardownTestEnvironment (GlobalTestEnvironment {server}) = stopServer server
-- | allow setting log output type -- | allow setting log output type
setupLogType :: IO FL.LogType setupLogType :: IO FL.LogType
@ -117,7 +121,6 @@ hook specs = do
TestNoBackends -> True -- this is for catching "everything else" TestNoBackends -> True -- this is for catching "everything else"
TestNewPostgresVariant {} -> "Postgres" `elem` labels TestNewPostgresVariant {} -> "Postgres" `elem` labels
parallel $
aroundAllWith (const . bracket (setupTestEnvironment testingMode logger) teardownTestEnvironment) $ aroundAllWith (const . bracket (setupTestEnvironment testingMode logger) teardownTestEnvironment) $
mapSpecForest (filterForestWithLabels shouldRunTest) (contextualizeLogger specs) mapSpecForest (filterForestWithLabels shouldRunTest) (contextualizeLogger specs)

View File

@ -16,7 +16,6 @@ import Harness.Quoter.Yaml (yaml)
import Harness.Test.Fixture qualified as Fixture import Harness.Test.Fixture qualified as Fixture
import Harness.Test.Schema (Table (..), table) import Harness.Test.Schema (Table (..), table)
import Harness.Test.Schema qualified as Schema import Harness.Test.Schema qualified as Schema
import Harness.Test.SetupAction qualified as SetupAction
import Harness.TestEnvironment (GlobalTestEnvironment, TestEnvironment) import Harness.TestEnvironment (GlobalTestEnvironment, TestEnvironment)
import Harness.Yaml (shouldReturnYaml) import Harness.Yaml (shouldReturnYaml)
import Hasura.Prelude import Hasura.Prelude
@ -315,4 +314,41 @@ setupMetadata testEnvironment = do
id: x-hasura-user-id 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.Fixture qualified as Fixture
import Harness.Test.Schema (Table (..)) import Harness.Test.Schema (Table (..))
import Harness.Test.Schema qualified as Schema import Harness.Test.Schema qualified as Schema
import Harness.Test.SetupAction qualified as SetupAction
import Harness.TestEnvironment (GlobalTestEnvironment, TestEnvironment) import Harness.TestEnvironment (GlobalTestEnvironment, TestEnvironment)
import Harness.Yaml (shouldReturnYaml) import Harness.Yaml (shouldReturnYaml)
import Hasura.Prelude import Hasura.Prelude
@ -152,4 +151,20 @@ setupMetadata testEnvironment = do
columns: '*' 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 -- so that the server can be referenced while testing
Fixture.mkLocalTestEnvironment = const Webhook.run, Fixture.mkLocalTestEnvironment = const Webhook.run,
Fixture.setupTeardown = \(testEnvironment, (_webhookServer, _)) -> 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: update:
columns: "*" 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.Fixture qualified as Fixture
import Harness.Test.Schema (Table (..), table) import Harness.Test.Schema (Table (..), table)
import Harness.Test.Schema qualified as Schema import Harness.Test.Schema qualified as Schema
import Harness.Test.SetupAction qualified as SetupAction
import Harness.TestEnvironment (GlobalTestEnvironment, TestEnvironment) import Harness.TestEnvironment (GlobalTestEnvironment, TestEnvironment)
import Harness.Webhook qualified as Webhook import Harness.Webhook qualified as Webhook
import Harness.Yaml (shouldBeYaml, shouldReturnYaml) import Harness.Yaml (shouldBeYaml, shouldReturnYaml)
@ -35,7 +34,10 @@ spec =
Fixture.mkLocalTestEnvironment = const Webhook.run, Fixture.mkLocalTestEnvironment = const Webhook.run,
Fixture.setupTeardown = \(testEnvironment, (webhookServer, _)) -> Fixture.setupTeardown = \(testEnvironment, (webhookServer, _)) ->
[ Sqlserver.setupTablesAction (schema "authors" "articles") testEnvironment, [ 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 timeout_sec: 60
webhook: #{webhookServerEchoEndpoint} 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 (Table (..), table)
import Harness.Test.Schema qualified as Schema import Harness.Test.Schema qualified as Schema
import Harness.Test.SetupAction (permitTeardownFail) import Harness.Test.SetupAction (permitTeardownFail)
import Harness.Test.SetupAction qualified as SetupAction
import Harness.TestEnvironment (GlobalTestEnvironment, TestEnvironment) import Harness.TestEnvironment (GlobalTestEnvironment, TestEnvironment)
import Harness.Webhook qualified as Webhook import Harness.Webhook qualified as Webhook
import Harness.Yaml (shouldBeYaml, shouldReturnYaml) import Harness.Yaml (shouldBeYaml, shouldReturnYaml)
@ -38,7 +37,10 @@ spec =
Fixture.mkLocalTestEnvironment = const Webhook.run, Fixture.mkLocalTestEnvironment = const Webhook.run,
Fixture.setupTeardown = \(testEnvironment, (webhookServer, _)) -> Fixture.setupTeardown = \(testEnvironment, (webhookServer, _)) ->
[ permitTeardownFail (Postgres.setupTablesAction schema testEnvironment), [ 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: insert:
columns: "*" 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 -- so that the server can be referenced while testing
Fixture.mkLocalTestEnvironment = const Webhook.run, Fixture.mkLocalTestEnvironment = const Webhook.run,
Fixture.setupTeardown = \(testEnvironment, (_webhookServer, _)) -> 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: update:
columns: "*" 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.Fixture qualified as Fixture
import Harness.Test.Schema (Table (..), table) import Harness.Test.Schema (Table (..), table)
import Harness.Test.Schema qualified as Schema import Harness.Test.Schema qualified as Schema
import Harness.Test.SetupAction qualified as SetupAction
import Harness.TestEnvironment (GlobalTestEnvironment, TestEnvironment) import Harness.TestEnvironment (GlobalTestEnvironment, TestEnvironment)
import Harness.Webhook qualified as Webhook import Harness.Webhook qualified as Webhook
import Harness.Yaml (shouldReturnYaml) import Harness.Yaml (shouldReturnYaml)
@ -32,7 +31,10 @@ spec =
{ Fixture.mkLocalTestEnvironment = const Webhook.run, { Fixture.mkLocalTestEnvironment = const Webhook.run,
Fixture.setupTeardown = \(testEnvironment, _) -> Fixture.setupTeardown = \(testEnvironment, _) ->
[ Postgres.setupTablesAction schema 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(); 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 -- * Tests

View File

@ -18,7 +18,6 @@ import Harness.Test.Fixture qualified as Fixture
import Harness.Test.Schema (Table (..), table) import Harness.Test.Schema (Table (..), table)
import Harness.Test.Schema qualified as Schema import Harness.Test.Schema qualified as Schema
import Harness.Test.SetupAction (permitTeardownFail) import Harness.Test.SetupAction (permitTeardownFail)
import Harness.Test.SetupAction qualified as SetupAction
import Harness.TestEnvironment (GlobalTestEnvironment, Server (..), TestEnvironment, getServer) import Harness.TestEnvironment (GlobalTestEnvironment, Server (..), TestEnvironment, getServer)
import Harness.Webhook qualified as Webhook import Harness.Webhook qualified as Webhook
import Harness.Yaml (shouldBeYaml, shouldReturnYaml) import Harness.Yaml (shouldBeYaml, shouldReturnYaml)
@ -41,7 +40,10 @@ spec =
Fixture.mkLocalTestEnvironment = const Webhook.run, Fixture.mkLocalTestEnvironment = const Webhook.run,
Fixture.setupTeardown = \(testEnvironment, (webhookServer, _)) -> Fixture.setupTeardown = \(testEnvironment, (webhookServer, _)) ->
[ permitTeardownFail (Postgres.setupTablesAction (schema "authors") testEnvironment), [ 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 - name
- created_at - 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.Fixture qualified as Fixture
import Harness.Test.Schema (Table (..), table) import Harness.Test.Schema (Table (..), table)
import Harness.Test.Schema qualified as Schema import Harness.Test.Schema qualified as Schema
import Harness.Test.SetupAction qualified as SetupAction
import Harness.TestEnvironment (GlobalTestEnvironment, TestEnvironment) import Harness.TestEnvironment (GlobalTestEnvironment, TestEnvironment)
import Harness.Webhook qualified as Webhook import Harness.Webhook qualified as Webhook
import Harness.Yaml (shouldBeYaml, shouldReturnYaml) import Harness.Yaml (shouldBeYaml, shouldReturnYaml)
@ -35,7 +34,10 @@ spec =
Fixture.mkLocalTestEnvironment = const Webhook.run, Fixture.mkLocalTestEnvironment = const Webhook.run,
Fixture.setupTeardown = \(testEnvironment, (webhookServer, _)) -> Fixture.setupTeardown = \(testEnvironment, (webhookServer, _)) ->
[ Postgres.setupTablesAction (schema "authors" "articles") testEnvironment, [ 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 timeout_sec: 60
webhook: #{webhookServerEchoEndpoint} 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.Fixture qualified as Fixture
import Harness.Test.Schema (Table (..), table) import Harness.Test.Schema (Table (..), table)
import Harness.Test.Schema qualified as Schema import Harness.Test.Schema qualified as Schema
import Harness.Test.SetupAction qualified as SetupAction
import Harness.TestEnvironment (GlobalTestEnvironment, TestEnvironment) import Harness.TestEnvironment (GlobalTestEnvironment, TestEnvironment)
import Harness.Yaml (shouldReturnOneOfYaml, shouldReturnYaml) import Harness.Yaml (shouldReturnOneOfYaml, shouldReturnYaml)
import Hasura.Prelude import Hasura.Prelude
@ -293,4 +292,18 @@ setupMetadata testEnvironment = do
author_name: name 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.Fixture qualified as Fixture
import Harness.Test.Schema (Table (..), table) import Harness.Test.Schema (Table (..), table)
import Harness.Test.Schema qualified as Schema import Harness.Test.Schema qualified as Schema
import Harness.Test.SetupAction qualified as SetupAction
import Harness.TestEnvironment (GlobalTestEnvironment, TestEnvironment) import Harness.TestEnvironment (GlobalTestEnvironment, TestEnvironment)
import Harness.Yaml (shouldReturnYaml) import Harness.Yaml (shouldReturnYaml)
import Hasura.Prelude import Hasura.Prelude
@ -151,4 +150,18 @@ setupMetadata backendTypeMetadata testEnvironment = do
subscription_root_fields: ["select_stream"] 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.Fixture qualified as Fixture
import Harness.Test.Schema (Table (..), table) import Harness.Test.Schema (Table (..), table)
import Harness.Test.Schema qualified as Schema import Harness.Test.Schema qualified as Schema
import Harness.Test.SetupAction qualified as SetupAction
import Harness.TestEnvironment (GlobalTestEnvironment, TestEnvironment) import Harness.TestEnvironment (GlobalTestEnvironment, TestEnvironment)
import Harness.Yaml (shouldReturnYaml) import Harness.Yaml (shouldReturnYaml)
import Hasura.Prelude import Hasura.Prelude
@ -276,4 +275,34 @@ setupMetadata backendTypeMetadata testEnvironment = do
- name - 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.Fixture qualified as Fixture
import Harness.Test.Schema (Table (..), table) import Harness.Test.Schema (Table (..), table)
import Harness.Test.Schema qualified as Schema import Harness.Test.Schema qualified as Schema
import Harness.Test.SetupAction qualified as SetupAction
import Harness.TestEnvironment (GlobalTestEnvironment, TestEnvironment) import Harness.TestEnvironment (GlobalTestEnvironment, TestEnvironment)
import Harness.Yaml (shouldReturnYaml) import Harness.Yaml (shouldReturnYaml)
import Hasura.Prelude import Hasura.Prelude
@ -257,4 +256,27 @@ setupMetadata backendTypeMetadata testEnvironment = do
columns: "*" 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.Fixture qualified as Fixture
import Harness.Test.Schema (Table (..), table) import Harness.Test.Schema (Table (..), table)
import Harness.Test.Schema qualified as Schema import Harness.Test.Schema qualified as Schema
import Harness.Test.SetupAction qualified as SetupAction
import Harness.TestEnvironment (GlobalTestEnvironment, TestEnvironment) import Harness.TestEnvironment (GlobalTestEnvironment, TestEnvironment)
import Harness.Yaml (shouldReturnYaml) import Harness.Yaml (shouldReturnYaml)
import Hasura.Prelude import Hasura.Prelude
@ -259,4 +258,27 @@ setupMetadata backendTypeMetadata testEnvironment = do
columns: '*' 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 Control.Concurrent.Extended
import Data.List qualified as List import Data.List qualified as List
import Data.String import Data.String
@ -265,7 +266,9 @@ setup tables' (testEnvironment, _) = do
teardown :: [Schema.Table] -> (TestEnvironment, ()) -> IO () teardown :: [Schema.Table] -> (TestEnvironment, ()) -> IO ()
teardown _ (testEnvironment, _) = do teardown _ (testEnvironment, _) = do
let schemaName = Schema.getSchemaName testEnvironment let schemaName = Schema.getSchemaName testEnvironment
removeDataset schemaName concurrently_
(GraphqlEngine.setSources testEnvironment mempty Nothing)
(removeDataset schemaName)
setupTablesAction :: HasCallStack => [Schema.Table] -> TestEnvironment -> SetupAction setupTablesAction :: HasCallStack => [Schema.Table] -> TestEnvironment -> SetupAction
setupTablesAction ts env = setupTablesAction ts env =

View File

@ -320,7 +320,8 @@ setupPermissionsAction permissions env =
-- | Teardown the schema and tracking in the most expected way. -- | Teardown the schema and tracking in the most expected way.
-- NOTE: Certain test modules may warrant having their own version. -- NOTE: Certain test modules may warrant having their own version.
teardown :: HasCallStack => [Schema.Table] -> (TestEnvironment, ()) -> IO () 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. -- | Setup the given permissions to the graphql engine in a TestEnvironment.
setupPermissions :: [Permissions.Permission] -> TestEnvironment -> IO () 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 -- Because the Fixture takes care of dropping the DB, all we do here is
-- clear the metadata with `replace_metadata`. -- clear the metadata with `replace_metadata`.
teardown :: HasCallStack => [Schema.Table] -> (TestEnvironment, ()) -> IO () teardown :: HasCallStack => [Schema.Table] -> (TestEnvironment, ()) -> IO ()
teardown _ _ = pure () teardown _ (testEnvironment, _) =
GraphqlEngine.setSources testEnvironment mempty Nothing
setupTablesAction :: [Schema.Table] -> TestEnvironment -> SetupAction setupTablesAction :: [Schema.Table] -> TestEnvironment -> SetupAction
setupTablesAction ts env = setupTablesAction ts env =

View File

@ -12,9 +12,7 @@ module Harness.Backend.Postgres
runSQL, runSQL,
defaultSourceMetadata, defaultSourceMetadata,
defaultSourceConfiguration, defaultSourceConfiguration,
createMetadataDatabase,
createDatabase, createDatabase,
dropMetadataDatabase,
dropDatabase, dropDatabase,
createTable, createTable,
insertTable, insertTable,
@ -69,7 +67,7 @@ import Harness.Test.Schema
) )
import Harness.Test.Schema qualified as Schema import Harness.Test.Schema qualified as Schema
import Harness.Test.SetupAction (SetupAction (..)) import Harness.Test.SetupAction (SetupAction (..))
import Harness.TestEnvironment (GlobalTestEnvironment (..), TestEnvironment (..), TestingMode (..), UniqueTestId, testLogMessage) import Harness.TestEnvironment (GlobalTestEnvironment (..), TestEnvironment (..), TestingMode (..), testLogMessage)
import Hasura.Prelude import Hasura.Prelude
import System.Process.Typed import System.Process.Typed
import Text.Pretty.Simple (pShow) import Text.Pretty.Simple (pShow)
@ -133,20 +131,18 @@ makeFreshDbConnectionString testEnvironment =
{ Postgres.connectDatabase = uniqueDbName (uniqueTestId testEnvironment) { Postgres.connectDatabase = uniqueDbName (uniqueTestId testEnvironment)
} }
metadataLivenessCheck :: HasCallStack => TestEnvironment -> IO () metadataLivenessCheck :: HasCallStack => IO ()
metadataLivenessCheck = metadataLivenessCheck =
doLivenessCheck doLivenessCheck $
. fromString fromString postgresqlMetadataConnectionString
. postgresqlMetadataConnectionString
. uniqueTestId
livenessCheck :: HasCallStack => TestEnvironment -> IO () livenessCheck :: HasCallStack => TestEnvironment -> IO ()
livenessCheck = doLivenessCheck . makeFreshDbConnectionString livenessCheck = doLivenessCheck . makeFreshDbConnectionString
-- PostgreSQL 15.1 on x86_64-pc-linux-musl, com .... -- PostgreSQL 15.1 on x86_64-pc-linux-musl, com ....
-- forgive me, padre -- forgive me, padre
parsePostgresVersion :: String -> Maybe Int _parsePostgresVersion :: String -> Maybe Int
parsePostgresVersion = _parsePostgresVersion =
readMaybe readMaybe
. takeWhile (not . (==) '.') . takeWhile (not . (==) '.')
. drop (length @[] "PostgreSQL ") . drop (length @[] "PostgreSQL ")
@ -401,17 +397,6 @@ untrackTable :: TestEnvironment -> Schema.Table -> IO ()
untrackTable testEnvironment table = untrackTable testEnvironment table =
Schema.untrackTable (BackendType.backendSourceName backendTypeMetadata) table testEnvironment 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 -- | create a database to use and later drop for these tests
-- note we use the 'initial' connection string here, ie, the one we started -- note we use the 'initial' connection string here, ie, the one we started
-- with. -- with.
@ -431,22 +416,6 @@ dropDatabase testEnvironment = do
-- up. -- up.
dropDatabaseInternal :: String -> TestEnvironment -> IO () dropDatabaseInternal :: String -> TestEnvironment -> IO ()
dropDatabaseInternal dbName testEnvironment = do dropDatabaseInternal dbName testEnvironment = do
([Postgres.Only version]) <-
queryWithInitialDb @(Postgres.Only String)
testEnvironment
"SELECT version();"
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 $ void $
queryWithInitialDb @(Postgres.Only Bool) queryWithInitialDb @(Postgres.Only Bool)
testEnvironment testEnvironment
@ -502,7 +471,8 @@ setup tables (testEnvironment, _) = do
-- NOTE: Certain test modules may warrant having their own version. -- NOTE: Certain test modules may warrant having their own version.
-- we replace metadata with nothing. -- we replace metadata with nothing.
teardown :: [Schema.Table] -> (TestEnvironment, ()) -> IO () teardown :: [Schema.Table] -> (TestEnvironment, ()) -> IO ()
teardown _ _ = pure () teardown _ (testEnvironment, _) =
GraphqlEngine.setSources testEnvironment mempty Nothing
setupTablesAction :: [Schema.Table] -> TestEnvironment -> SetupAction setupTablesAction :: [Schema.Table] -> TestEnvironment -> SetupAction
setupTablesAction ts env = setupTablesAction ts env =

View File

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

View File

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

View File

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

View File

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

View File

@ -35,7 +35,8 @@ import Data.Map.Strict qualified as Map
import Harness.Exceptions (throw, withFrozenCallStack) import Harness.Exceptions (throw, withFrozenCallStack)
import Harness.Logging.Messages import Harness.Logging.Messages
import Harness.TestEnvironment import Harness.TestEnvironment
( Server (..), ( GlobalTestEnvironment (..),
Server (..),
TestEnvironment (..), TestEnvironment (..),
testLogMessage, testLogMessage,
) )
@ -96,7 +97,7 @@ newtype SubscriptionHandle = SubscriptionHandle {unSubscriptionHandle :: MVar Va
-- > actual `shouldBe` expected -- > actual `shouldBe` expected
withSubscriptions :: SpecWith (Value -> [Pair] -> IO SubscriptionHandle, TestEnvironment) -> SpecWith TestEnvironment withSubscriptions :: SpecWith (Value -> [Pair] -> IO SubscriptionHandle, TestEnvironment) -> SpecWith TestEnvironment
withSubscriptions = aroundAllWith \actionWithSubAndTest testEnvironment -> do 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: -- CAVE: loads of stuff still outstanding:
-- * trimming threads, NDAT-228 -- * trimming threads, NDAT-228
-- * multiplexing handles, NDAT-229 -- * multiplexing handles, NDAT-229

View File

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

View File

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