-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
nix(test): add test suite for observability tests #4732
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
steve-chavez
merged 1 commit into
PostgREST:main
from
taimoorzaeem:nix/postgrest-test-obs
Mar 19, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| module Main where | ||
|
|
||
| import qualified Hasql.Pool as P | ||
| import qualified Hasql.Pool.Config as P | ||
| import qualified Hasql.Transaction.Sessions as HT | ||
|
|
||
| import Data.Function (id) | ||
|
|
||
| import PostgREST.App (postgrest) | ||
| import qualified PostgREST.AppState as AppState | ||
| import PostgREST.Config (AppConfig (..)) | ||
| import PostgREST.Config.Database (queryPgVersion) | ||
| import qualified PostgREST.Logger as Logger | ||
| import qualified PostgREST.Metrics as Metrics | ||
| import PostgREST.SchemaCache (querySchemaCache) | ||
|
|
||
| import qualified Observation.JwtCache | ||
|
|
||
| import ObsHelper | ||
| import Protolude hiding (toList, toS) | ||
| import Test.Hspec | ||
|
|
||
| main :: IO () | ||
| main = do | ||
| pool <- P.acquire $ P.settings | ||
| [ P.size 3 | ||
| , P.acquisitionTimeout 10 | ||
| , P.agingTimeout 60 | ||
| , P.idlenessTimeout 60 | ||
| , P.staticConnectionSettings (toUtf8 $ configDbUri testCfg) | ||
| ] | ||
|
|
||
| actualPgVersion <- either (panic . show) id <$> P.use pool (queryPgVersion False) | ||
|
|
||
| -- cached schema cache so most tests run fast | ||
| baseSchemaCache <- loadSCache pool testCfg | ||
| loggerState <- Logger.init | ||
| metricsState <- Metrics.init (configDbPoolSize testCfg) | ||
|
|
||
| let | ||
| initApp sCache st config = do | ||
| appState <- AppState.initWithPool pool config loggerState metricsState (Metrics.observationMetrics metricsState) | ||
| AppState.putPgVersion appState actualPgVersion | ||
| AppState.putSchemaCache appState (Just sCache) | ||
| return (st, postgrest (configLogLevel config) appState (pure ())) | ||
|
|
||
| -- Run all test modules | ||
| hspec $ do | ||
| before (initApp baseSchemaCache metricsState testCfgJwtCache) $ | ||
| describe "Observation.JwtCacheObs" Observation.JwtCache.spec | ||
|
|
||
| where | ||
| loadSCache pool conf = | ||
| either (panic.show) id <$> P.use pool (HT.transaction HT.ReadCommitted HT.Read $ querySchemaCache conf) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| {-# LANGUAGE AllowAmbiguousTypes #-} | ||
| {-# LANGUAGE ExistentialQuantification #-} | ||
| {-# LANGUAGE FlexibleContexts #-} | ||
| {-# LANGUAGE ScopedTypeVariables #-} | ||
| {-# LANGUAGE TupleSections #-} | ||
| {-# LANGUAGE TypeApplications #-} | ||
| module ObsHelper where | ||
|
|
||
| import qualified Data.ByteString.Base64 as B64 (decodeLenient) | ||
| import qualified Data.ByteString.Char8 as BS | ||
| import qualified Data.ByteString.Lazy as BL | ||
| import qualified Jose.Jwa as JWT | ||
| import qualified Jose.Jws as JWT | ||
| import qualified Jose.Jwt as JWT | ||
|
|
||
| import PostgREST.Config (AppConfig (..), JSPathExp (..), | ||
| LogLevel (..), OpenAPIMode (..), | ||
| Verbosity (..), parseSecret) | ||
|
|
||
| import Data.List.NonEmpty (fromList) | ||
| import Data.String (String) | ||
| import Prometheus (Counter, getCounter) | ||
| import Test.Hspec.Expectations.Contrib (annotate) | ||
|
|
||
| import Network.HTTP.Types | ||
| import Protolude | ||
| import Test.Hspec | ||
| import Test.Hspec.Wai | ||
|
|
||
|
|
||
| baseCfg :: AppConfig | ||
| baseCfg = let secret = encodeUtf8 "reallyreallyreallyreallyverysafe" in | ||
| AppConfig { | ||
| configAppSettings = [] | ||
| , configClientErrorVerbosity = Verbose | ||
| , configDbAggregates = False | ||
| , configDbAnonRole = Just "postgrest_test_anonymous" | ||
| , configDbChannel = mempty | ||
| , configDbChannelEnabled = True | ||
| , configDbExtraSearchPath = [] | ||
| , configDbHoistedTxSettings = ["default_transaction_isolation","plan_filter.statement_cost_limit","statement_timeout"] | ||
| , configDbMaxRows = Nothing | ||
| , configDbPlanEnabled = False | ||
| , configDbPoolSize = 10 | ||
| , configDbPoolAcquisitionTimeout = 10 | ||
| , configDbPoolMaxLifetime = 1800 | ||
| , configDbPoolMaxIdletime = 600 | ||
| , configDbPoolAutomaticRecovery = True | ||
| , configDbPreRequest = Nothing | ||
| , configDbPreparedStatements = True | ||
| , configDbRootSpec = Nothing | ||
| , configDbSchemas = fromList ["test"] | ||
| , configDbConfig = False | ||
| , configDbPreConfig = Nothing | ||
| , configDbUri = "postgresql://" | ||
| , configFilePath = Nothing | ||
| , configJWKS = rightToMaybe $ parseSecret secret | ||
| , configJwtAudience = Nothing | ||
| , configJwtRoleClaimKey = [JSPKey "role"] | ||
| , configJwtSecret = Just secret | ||
| , configJwtSecretIsBase64 = False | ||
| , configJwtCacheMaxEntries = 10 | ||
| , configLogLevel = LogCrit | ||
| , configLogQuery = False | ||
| , configOpenApiMode = OAFollowPriv | ||
| , configOpenApiSecurityActive = False | ||
| , configOpenApiServerProxyUri = Nothing | ||
| , configServerCorsAllowedOrigins = Nothing | ||
| , configServerHost = "localhost" | ||
| , configServerPort = 3000 | ||
| , configServerTraceHeader = Nothing | ||
| , configServerUnixSocket = Nothing | ||
| , configServerUnixSocketMode = 432 | ||
| , configDbTxAllowOverride = True | ||
| , configDbTxRollbackAll = True | ||
| , configAdminServerHost = "localhost" | ||
| , configAdminServerPort = Nothing | ||
| , configRoleSettings = mempty | ||
| , configRoleIsoLvl = mempty | ||
| , configInternalSCQuerySleep = Nothing | ||
| , configInternalSCLoadSleep = Nothing | ||
| , configInternalSCRelLoadSleep = Nothing | ||
| , configServerTimingEnabled = True | ||
| } | ||
|
|
||
| testCfg :: AppConfig | ||
| testCfg = baseCfg | ||
|
|
||
| testCfgJwtCache :: AppConfig | ||
| testCfgJwtCache = | ||
| baseCfg { | ||
| configJwtSecret = Just generateSecret | ||
| , configJWKS = rightToMaybe $ parseSecret generateSecret | ||
| , configJwtCacheMaxEntries = 2 | ||
| } | ||
|
|
||
| authHeader :: BS.ByteString -> BS.ByteString -> Header | ||
| authHeader typ creds = | ||
| (hAuthorization, typ <> " " <> creds) | ||
|
|
||
| authHeaderJWT :: BS.ByteString -> Header | ||
| authHeaderJWT = authHeader "Bearer" | ||
|
|
||
| generateSecret :: ByteString | ||
| generateSecret = B64.decodeLenient "cmVhbGx5cmVhbGx5cmVhbGx5cmVhbGx5dmVyeXNhZmU=" | ||
|
|
||
| generateJWT :: BL.ByteString -> ByteString | ||
| generateJWT claims = | ||
| either mempty JWT.unJwt $ JWT.hmacEncode JWT.HS256 generateSecret (BL.toStrict claims) | ||
|
|
||
| -- state check helpers | ||
|
|
||
| data StateCheck st m = forall a. StateCheck (st -> (String, m a)) (a -> a -> Expectation) | ||
|
|
||
| stateCheck :: (Show a, Eq a) => (c -> m a) -> (st -> (String, c)) -> (a -> a) -> StateCheck st m | ||
| stateCheck extractValue extractComponent expect = StateCheck (second extractValue . extractComponent) (flip shouldBe . expect) | ||
|
|
||
| expectField :: forall s st a c m. (KnownSymbol s, Show a, Eq a, HasField s st c) => (c -> m a) -> (a -> a) -> StateCheck st m | ||
| expectField extractValue = stateCheck extractValue ((symbolVal (Proxy @s),) . getField @s) | ||
|
|
||
| checkState :: (Traversable t) => t (StateCheck st (WaiSession st)) -> WaiSession st b -> WaiSession st () | ||
| checkState checks act = getState >>= flip (`checkState'` checks) act | ||
|
|
||
| checkState' :: (Traversable t, MonadIO m) => st -> t (StateCheck st m) -> m b -> m () | ||
| checkState' initialState checks act = do | ||
| expectations <- traverse (\(StateCheck g expect) -> let (msg, m) = g initialState in m >>= createExpectation msg m . expect) checks | ||
| void act | ||
| sequenceA_ expectations | ||
| where | ||
| createExpectation msg metrics expect = pure $ metrics >>= liftIO . annotate msg . expect | ||
|
|
||
| expectCounter :: forall s st m. (KnownSymbol s, HasField s st Counter, MonadIO m) => (Int -> Int) -> StateCheck st m | ||
| expectCounter = expectField @s intCounter | ||
| where | ||
| intCounter = ((round @Double @Int) <$>) . getCounter | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looked at our current
It is indeed purely tested in terms of metrics, so I agree in that it fits here. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| -- Suppress NOTICE: ... messages | ||
| SET client_min_messages TO warning; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| -- Loads all fixtures for the PostgREST observability tests | ||
|
|
||
| \set ON_ERROR_STOP on | ||
|
|
||
| \ir database.sql | ||
| \ir roles.sql | ||
| \ir schema.sql | ||
| \ir privileges.sql |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| -- Schema test objects | ||
| SET search_path = test, pg_catalog; | ||
|
|
||
| GRANT USAGE ON SCHEMA test TO postgrest_test_anonymous; | ||
| GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA test TO postgrest_test_anonymous; | ||
| REVOKE ALL PRIVILEGES ON TABLE authors_only FROM postgrest_test_anonymous; | ||
|
|
||
| GRANT USAGE ON SCHEMA test TO postgrest_test_author; | ||
| GRANT ALL ON TABLE authors_only TO postgrest_test_author; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| DROP ROLE IF EXISTS postgrest_test_anonymous, postgrest_test_author; | ||
| CREATE ROLE postgrest_test_anonymous; | ||
| CREATE ROLE postgrest_test_author; | ||
|
|
||
| GRANT postgrest_test_anonymous, postgrest_test_author TO :PGUSER; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see there's duplication here but don't have a good idea on how to solve right now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, I feel the same way. I believe the long term solution would be to come with some sort of internal helper library of functions, but that would be too much for now.