-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathMain.hs
820 lines (686 loc) · 30.4 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
import Control.Concurrent
import qualified Control.Concurrent.Async as Async
import Control.Exception
import Control.Monad ((<=<), void, unless)
import Data.Function
import qualified Data.Map.Strict as Map
import Data.Maybe
import Data.Monoid
import Data.Monoid.Generic
import Data.List
import qualified Data.Set as Set
import Data.String
import qualified Database.PostgreSQL.Simple as PG
import qualified Database.PostgreSQL.Simple.SqlQQ as PG
import qualified Database.PostgreSQL.Simple.Options as Client
import Database.Postgres.Temp.Internal
import Database.Postgres.Temp.Internal.Config
import Database.Postgres.Temp.Internal.Core
import GHC.Generics (Generic)
-- import qualified Network.Socket as N
import Network.Socket.Free
import System.Directory
import System.Environment
import System.Exit
import System.IO.Error
import System.IO.Temp
import System.Posix.Files
import System.Process
import System.Timeout
import Test.Hspec
import Data.Either
withConn :: DB -> (PG.Connection -> IO a) -> IO a
withConn db f = do
let connStr = toConnectionString db
bracket (PG.connectPostgreSQL connStr) PG.close f
withConfig' :: Config -> (DB -> IO a) -> IO a
withConfig' config = either throwIO pure <=< withConfig config
countPostgresProcesses :: IO Int
countPostgresProcesses = countProcesses "postgres"
countInitdbProcesses :: IO Int
countInitdbProcesses = countProcesses "initdb"
countCreatedbProcesses :: IO Int
countCreatedbProcesses = countProcesses "createdb"
countProcesses :: String -> IO Int
countProcesses processName = do
-- TODO we should restrict to child process
(exitCode, xs, _) <- readProcessWithExitCode "pgrep" [processName] []
unless (exitCode == ExitSuccess || exitCode == ExitFailure 1) $ throwIO exitCode
pure $ length $ lines xs
assertConnection :: DB -> IO ()
assertConnection db = do
one <- fmap (PG.fromOnly . head) $
withConn db $ \conn -> PG.query_ conn "SELECT 1"
one `shouldBe` (1 :: Int)
testSuccessfulConfigNoTmp :: ConfigAndAssertion -> IO ()
testSuccessfulConfigNoTmp ConfigAndAssertion {..} = do
initialPostgresCount <- countPostgresProcesses
initialInitdbCount <- countInitdbProcesses
initialCreatedbCount <- countCreatedbProcesses
withConfig' cConfig $ \db -> do
cAssert db
assertConnection db
countPostgresProcesses `shouldReturn` initialPostgresCount
countInitdbProcesses `shouldReturn` initialInitdbCount
countCreatedbProcesses `shouldReturn` initialCreatedbCount
testSuccessfulConfig :: ConfigAndAssertion -> IO ()
testSuccessfulConfig configAssert@ConfigAndAssertion{..} = do
-- get the temp listing before
let tmpDir = fromMaybe (error "test is bad")
$ getLast $ temporaryDirectory cConfig
initialContents <- listDirectory tmpDir
testSuccessfulConfigNoTmp configAssert
-- get the temp listing after
listDirectory tmpDir `shouldReturn` initialContents
testWithTemporaryDirectory :: ConfigAndAssertion -> (ConfigAndAssertion -> IO a) -> IO a
testWithTemporaryDirectory x f =
withTempDirectory "/tmp" "tmp-postgres-spec" $ \directoryPath ->
f x
{ cConfig = (cConfig x) { temporaryDirectory = pure directoryPath }
, cAssert = cAssert x <> (\db -> toTemporaryDirectory db `shouldBe` directoryPath)
}
data ConfigAndAssertion = ConfigAndAssertion
{ cConfig :: Config
, cAssert :: DB -> IO ()
}
deriving (Generic)
deriving Semigroup via GenericSemigroup ConfigAndAssertion
deriving Monoid via GenericMonoid ConfigAndAssertion
-- Set all the things we support
memptyConfigAndAssertion :: ConfigAndAssertion
memptyConfigAndAssertion = memptyConfigAndAssertion' "postgres"
countDbs :: PG.Connection -> IO Int
countDbs conn = PG.fromOnly . head <$> PG.query_ conn [PG.sql|
SELECT COUNT(*)
FROM pg_catalog.pg_database
|]
memptyConfigAndAssertion' :: String -> ConfigAndAssertion
memptyConfigAndAssertion' expectedDbName =
let
cConfig = mempty
cAssert db = do
let
Client.Options {..} = toConnectionOptions db
Last (Just hostString) = host
-- I'm assuming here that I'm going to test
-- with this prefix
hostString `shouldStartWith` "/tmp/tmp-postgres-"
-- Ephemeral range
let Just thePort = getLast port
thePort `shouldSatisfy` (>32768)
toDataDirectory db `shouldStartWith` "/tmp/tmp-postgres-"
dbname `shouldBe` pure expectedDbName
in ConfigAndAssertion {..}
optionsToDefaultConfigMempty :: ConfigAndAssertion
optionsToDefaultConfigMempty = memptyConfigAndAssertion
{ cConfig = optionsToDefaultConfig mempty
}
-- can't be combined
optionsToDefaultConfigSocket :: FilePath -> ConfigAndAssertion
optionsToDefaultConfigSocket socketPath =
let
cConfig = optionsToDefaultConfig mempty
{ Client.host = pure socketPath
}
cAssert db = do
let
Client.Options {..} = toConnectionOptions db
Last (Just hostString) = host
-- I'm assuming here that I'm going to test
-- with this prefix
hostString `shouldStartWith` "/tmp/tmp-postgres-"
in ConfigAndAssertion {..}
optionsToDefaultConfigFilledOutConfigAssert :: Int -> ConfigAndAssertion
optionsToDefaultConfigFilledOutConfigAssert expectedPort =
let
expectedDbName = "foobar"
expectedUser = "some_user"
expectedPassword = "password"
expectedHost = "localhost"
cConfig = optionsToDefaultConfig mempty
{ Client.port = pure expectedPort
, Client.dbname = pure expectedDbName
, Client.user = pure expectedUser
, Client.password = pure expectedPassword
, Client.host = pure expectedHost
}
cAssert db = do
let Client.Options {..} = toConnectionOptions db
port `shouldBe` pure expectedPort
user `shouldBe` pure expectedUser
dbname `shouldBe` pure expectedDbName
password `shouldBe` pure expectedPassword
host `shouldBe` pure expectedHost
in ConfigAndAssertion {..}
extraConfigAssert :: ConfigAndAssertion
extraConfigAssert =
let
cConfig = mempty
{ postgresConfigFile = [("log_min_duration_statement","'100ms'")]
}
cAssert db = withConn db $ \conn -> do
[PG.Only actualDuration] <- PG.query_ conn "SHOW log_min_duration_statement"
actualDuration `shouldBe` ("100ms" :: String)
in ConfigAndAssertion {..}
defaultIpConfig :: ConfigAndAssertion
defaultIpConfig =
let
cConfig = mempty
{ connectionOptions = mempty
{ Client.host = pure "127.0.0.1"
}
}
cAssert db = do
let Client.Options {..} = toConnectionOptions db
host `shouldBe` pure "127.0.0.1"
in ConfigAndAssertion {..}
specificUnixSocket :: FilePath -> ConfigAndAssertion
specificUnixSocket filePath =
let
cConfig = mempty
{ socketDirectory = Permanent filePath
}
cAssert db = do
let
Client.Options {..} = toConnectionOptions db
Last (Just hostString) = host
-- I'm assuming here that I'm going to test
-- with this prefix
hostString `shouldStartWith` filePath
in ConfigAndAssertion {..}
defaultConfigAssert :: ConfigAndAssertion
defaultConfigAssert = ConfigAndAssertion defaultConfig mempty
createdbAndDescription :: ConfigAndAssertion
createdbAndDescription =
let
expectedDescription = "newdb description"
cConfig = mempty
{ createDbConfig = pure silentProcessConfig
{ commandLine = mempty
{ indexBased = Map.fromList
[ (0, "newdb")
, (1, expectedDescription)
]
}
}
}
cAssert db = withConn db $ \conn -> do
[PG.Only actualDescription] <- PG.query_ conn $ fromString $ unlines
[ "SELECT description FROM pg_shdescription"
, "JOIN pg_database ON objoid = pg_database.oid"
, "WHERE datname = 'newdb'"
]
actualDescription `shouldBe` expectedDescription
in ConfigAndAssertion {..}
happyPaths :: Spec
happyPaths = describe "succeeds with" $ do
it "mempty and extra postgresql.conf" $
testWithTemporaryDirectory
(defaultConfigAssert <> memptyConfigAndAssertion <> extraConfigAssert)
testSuccessfulConfig
it "optionsToDefaultConfig mempty is the same as mempty Config" $
testWithTemporaryDirectory
optionsToDefaultConfigMempty
testSuccessfulConfig
it "postgres db name does not cause createdb failure" $ do
testWithTemporaryDirectory
( defaultConfigAssert
<> memptyConfigAndAssertion
<> ConfigAndAssertion (optionsToDefaultConfig mempty { Client.dbname = pure "postgres" }) mempty
)
testSuccessfulConfig
it "template1 db name does not cause createdb failure" $ do
testWithTemporaryDirectory
( defaultConfigAssert
<> memptyConfigAndAssertion' "template1"
<> ConfigAndAssertion (optionsToDefaultConfig mempty { Client.dbname = pure "template1" }) mempty
)
testSuccessfulConfig
it "specific socket works with optionsToDefaultConfig" $
withTempDirectory "/tmp" "tmp-postgres-spec-socket" $ \socketFilePath ->
testWithTemporaryDirectory
(optionsToDefaultConfigSocket socketFilePath)
testSuccessfulConfig
it "filled out optionsToDefaultConfig" $ do
thePort <- getFreePort
testWithTemporaryDirectory
(optionsToDefaultConfigFilledOutConfigAssert thePort)
testSuccessfulConfig
it "default ip option works" $
testWithTemporaryDirectory
(defaultConfigAssert <> defaultIpConfig)
testSuccessfulConfig
it "specific unix socket works" $
withTempDirectory "/tmp" "tmp-postgres-spec-socket" $ \socketFilePath ->
testWithTemporaryDirectory
(defaultConfigAssert <> specificUnixSocket socketFilePath)
testSuccessfulConfig
it "works with the default temporary directory to some degree at least" $
testSuccessfulConfigNoTmp $ defaultConfigAssert <>
memptyConfigAndAssertion <> createdbAndDescription
it "works if on non-empty if initdb is disabled" $
withTempDirectory "/tmp" "tmp-postgres-preinitdb" $ \dirPath -> do
throwIfNotSuccess id . (\(x, _, _) -> x) =<<
readProcessWithExitCode "initdb" [dirPath] ""
let nonEmptyFolderConfig = memptyConfigAndAssertion
{ cConfig = defaultConfig
{ dataDirectory = Permanent dirPath
, initDbConfig = Zlich
}
}
testWithTemporaryDirectory nonEmptyFolderConfig testSuccessfulConfig
it "makeResourcesDataDirPermanent works" $
withTempDirectory "/tmp" "tmp-postgres-make-premanent" $ \dirPath -> do
let config = defaultConfig { temporaryDirectory = pure dirPath }
pathToCheck <- bracket (either throwIO (pure . makeDataDirectoryPermanent) =<< startConfig config) stop $
pure . toDataDirectory
doesDirectoryExist pathToCheck >>= \case
True -> pure ()
False -> fail "temporary file was not made permanent"
it "withDbCacheConfig actually caches the config and cleans up" $
withTempDirectory "/tmp" "tmp-postgres-cache-test" $ \dirPath -> do
let config = defaultConfig { temporaryDirectory = pure dirPath }
theCacheConfig = CacheConfig
{ cacheTemporaryDirectory = dirPath
, cacheDirectoryType = Temporary
, cacheUseCopyOnWrite = True
}
withDbCacheConfig theCacheConfig $ \cacheInfo -> do
withConfig' (config <> cacheConfig cacheInfo <> verboseConfig) $ const $ pure ()
-- see if there is a cache
tmpFiles <- listDirectory dirPath
cacheDir <- case filter ("tmp-postgres-cache" `isPrefixOf`) tmpFiles of
[x] -> pure x
xs -> fail $ "expected single cache dir got: " <> show xs
listDirectory (dirPath <> "/" <> cacheDir) >>= \case
[versionDir] -> listDirectory (dirPath <> "/" <> cacheDir <> "/" <> versionDir) >>= \case
[initDbCacheDir] ->
writeFile
( dirPath
<> "/"
<> cacheDir
<> "/"
<> versionDir
<> "/"
<> initDbCacheDir
<> "/data/cacheCheck.txt"
)
"test"
xs -> fail $ "expected a single initdb cache directory but got " <> show xs
xs -> fail $ "expected a single version directory but got " <> show xs
-- add a file to look for later
withConfig' (config <> cacheConfig cacheInfo) $ \db -> do
-- see if the file is in the data directory
let theDataDirectory = toDataDirectory db
xs <- listDirectory theDataDirectory
xs `shouldContain` ["cacheCheck.txt"]
assertConnection db
-- See if cache has been cleaned up
tmpFiles <- listDirectory dirPath
case filter ("tmp-postgres-cache" `isPrefixOf`) tmpFiles of
[] -> pure ()
xs -> fail $ "unexpected cache dirs. Should be cleaned up. Got: " <> show xs
it "withDbCache seems to work" $
withDbCache $ \cacheInfo ->
either throwIO pure <=< withConfig (cacheConfig cacheInfo <> autoExplainConfig 0) $ \db -> do
assertConnection db
withConn db $ \conn -> countDbs conn `shouldReturn` 3
--
-- Error Plans. Can't be combined. Just list them out inline since they can't be combined
--
errorPaths :: Spec
errorPaths = describe "fails when" $ do
-- Should this test ensure that atleast a single connection
-- attempt was made? probably.
it "timesout if the connection parameters are wrong" $ do
let invalidConfig = mempty
{ connectionTimeout = pure 0
, connectionOptions = mempty
{ Client.dbname = pure "doesnotexist"
}
}
withConfig (defaultConfig <> invalidConfig) (const $ pure ())
`shouldReturn` Left ConnectionTimedOut
it "does not timeout quickly with an invalid connection and large timeout" $ do
let invalidConfig = mempty
{ connectionTimeout = pure maxBound
, connectionOptions = mempty
{ Client.dbname = pure "doesnotexist"
}
}
timeout 2000000 (withConfig (defaultConfig <> invalidConfig) (const $ threadDelay 10000000))
`shouldReturn` Nothing
{-
it "throws StartPostgresFailed if the port is taken" $
bracket openFreePort (N.close . snd) $ \(thePort, _) -> do
let invalidConfig' = optionsToDefaultConfig mempty
{ Client.port = pure thePort
, Client.host = pure "127.0.0.1"
} <> verboseConfig
invalidConfig = invalidConfig'
{ plan = (plan invalidConfig')
{ connectionTimeout = pure 1000000
}
}
withConfig invalidConfig (const $ pure ())
`shouldReturn` Left (StartPostgresFailed $ ExitFailure 1)
-}
it "throws StartPostgresFailed if the host does not exist" $ do
let invalidConfig = optionsToDefaultConfig mempty
{ Client.host = pure "focalhost"
}
invalidConfig' = invalidConfig
{ connectionTimeout = pure 100000
}
withConfig invalidConfig' (const $ pure ())
`shouldReturn` Left ConnectionTimedOut
it "throws StartPostgresFailed if the host does not resolve to ip that is local" $ do
let invalidConfig = optionsToDefaultConfig mempty
{ Client.host = pure "yahoo.com"
}
invalidConfig' = invalidConfig
{ connectionTimeout = pure 100000
}
withConfig invalidConfig' (const $ pure ())
`shouldReturn` Left ConnectionTimedOut
it "throws StartPostgresFailed if the host path does not exist" $ do
let invalidConfig = optionsToDefaultConfig mempty
{ Client.host = pure "/focalhost"
}
withConfig invalidConfig (const $ pure ())
`shouldReturn` Left (StartPostgresFailed $ ExitFailure 1)
it "No initdb plan causes failure" $ do
let dontTimeout = defaultConfig
{ connectionTimeout = pure maxBound
, initDbConfig = Zlich
}
withConfig dontTimeout (const $ pure ())
`shouldReturn` Left EmptyDataDirectory
it "initdb with non-empty data directory fails with InitDbFailed" $
withTempDirectory "/tmp" "tmp-postgres-test" $ \dirPath -> do
writeFile (dirPath <> "/PG_VERSION") "1 million"
let nonEmptyFolderPlan = defaultConfig
{ dataDirectory = Permanent dirPath
}
withConfig nonEmptyFolderPlan mempty >>= \case
Right () -> fail "Should not succeed"
Left ((InitDbFailed _ _ code)) ->
code `shouldBe` ExitFailure 1
Left err -> fail $ "Wrong type of error " <> show err
it "invalid initdb options cause an error" $ do
let invalidConfig = defaultConfig
{ initDbConfig = pure silentProcessConfig
{ commandLine = mempty
{ keyBased = Map.singleton "--super-sync" Nothing
}
}
}
withConfig invalidConfig (const $ pure ()) >>= \case
Right () -> fail "Should not succeed"
Left (InitDbFailed {}) -> pure ()
Left err -> fail $ "Wrong type of error " <> show err
it "invalid createdb plan causes an error" $ do
let invalidConfig = defaultConfig
{ createDbConfig = pure silentProcessConfig
{ commandLine = mempty
{ indexBased =
Map.singleton 0 "template1"
}
}
}
withConfig invalidConfig (const $ pure ()) >>= \case
Right () -> fail "Should not succeed"
Left (CreateDbFailed {}) -> pure ()
Left err -> fail $ "Wrong type of error " <> show err
it "throws if initdb is not on the path" $ do
path <- getEnv "PATH"
bracket (setEnv "PATH" "/bin") (const $ setEnv "PATH" path) $ \_ ->
withConfig defaultConfig (const $ pure ())
`shouldThrow` isDoesNotExistError
it "throws if createdb is not on the path" $
withTempDirectory "/tmp" "createdb-not-on-path-test" $ \dir -> do
Just initDbPath <- findExecutable "initdb"
Just postgresPath <- findExecutable "postgres"
Just rmPath <- findExecutable "rm"
Just chmod <- findExecutable "chmod"
-- create symlinks
createSymbolicLink initDbPath $ dir <> "/initdb"
createSymbolicLink postgresPath $ dir <> "/postgres"
createSymbolicLink rmPath $ dir <> "/rm"
createSymbolicLink chmod $ dir <> "/chmod"
path <- getEnv "PATH"
let config = defaultConfig
{ createDbConfig = pure mempty
}
bracket (setEnv "PATH" dir) (const $ setEnv "PATH" path) $ \_ ->
withConfig config (const $ pure ())
`shouldThrow` isDoesNotExistError
withConfigSpecs :: Spec
withConfigSpecs = describe "withConfig" $ do
happyPaths
errorPaths
withSnapshotSpecs :: Spec
withSnapshotSpecs = describe "withSnapshot" $ do
it "works" $ withConfig' defaultConfig $ \db -> do
withConn db $ \conn -> do
_ <- PG.execute_ conn "BEGIN; CREATE TABLE foo ( id int );"
void $ PG.execute_ conn "INSERT INTO foo (id) VALUES (1); END;"
either throwIO pure <=< withSnapshot db $ \snapshotDir -> do
let theSnapshotConfig = defaultConfig <> snapshotConfig snapshotDir
snapshotConfigAndAssert = ConfigAndAssertion theSnapshotConfig $ flip withConn $ \conn -> do
oneAgain <- fmap (PG.fromOnly . head) $ PG.query_ conn "SELECT id FROM foo"
oneAgain `shouldBe` (1 :: Int)
testWithTemporaryDirectory
snapshotConfigAndAssert
testSuccessfulConfig
cacheActionSpecs :: Spec
cacheActionSpecs = describe "cacheAction" $ do
let configTest config = testWithTemporaryDirectory
(ConfigAndAssertion config mempty)
testSuccessfulConfig
it "creates the cache if it does not exist" $ do
let action db = withConn db $ \conn -> do
_ <- PG.execute_ conn "BEGIN; CREATE TABLE foo ( id int );"
void $ PG.execute_ conn "INSERT INTO foo (id) VALUES (1); END;"
withTempDirectory "/tmp" "tmp-postgres-cache-action" $ \cachePath -> do
let theFinalCachePath = cachePath <> "/cached"
cacheAction theFinalCachePath action defaultConfig >>= \case
Left err -> fail $ "cacheAction failed with:" <> show err
Right newCache -> do
nonEmpty <- doesFileExist $ theFinalCachePath <> "/PG_VERSION"
nonEmpty `shouldBe` True
-- Write a file and make sure it shows up in the data directory
writeFile (theFinalCachePath <> "/newFile.txt") "yes"
let
asserts db = do
doesFileExist (toDataDirectory db <> "/newFile.txt") `shouldReturn` True
withConn db $ \conn -> do
oneAgain <- fmap (PG.fromOnly . head) $ PG.query_ conn "SELECT id FROM foo"
oneAgain `shouldBe` (1 :: Int)
snapshotConfigAndAssert = ConfigAndAssertion newCache asserts
testWithTemporaryDirectory
snapshotConfigAndAssert
testSuccessfulConfig
it "doesnt create the cache if it exists" $ do
let action db = withConn db $ \conn -> do
_ <- PG.execute_ conn "BEGIN; CREATE TABLE foo ( id int );"
void $ PG.execute_ conn "INSERT INTO foo (id) VALUES (1); END;"
withTempDirectory "/tmp" "tmp-postgres-cache-action" $ \cachePath -> do
let theFinalCachePath = cachePath <> "/cached"
cacheAction theFinalCachePath action defaultConfig >>= \case
Left err -> fail $ "cacheAction failed with:" <> show err
Right newCache -> do
let nextAction db = withConn db $ \conn ->
void $ PG.execute_ conn "INSERT INTO foo (id) VALUES (2); END;"
cacheAction theFinalCachePath nextAction newCache >>= \case
Left err -> fail $ "cacheAction failed with:" <> show err
Right newCache1 -> do
let
asserts db = do
withConn db $ \conn -> do
oneAgain <- fmap (PG.fromOnly . head) $ PG.query_ conn "SELECT COUNT(*) FROM foo"
oneAgain `shouldBe` (1 :: Int)
snapshotConfigAndAssert = ConfigAndAssertion newCache1 asserts
testWithTemporaryDirectory
snapshotConfigAndAssert
testSuccessfulConfig
it "fails if the cache director and data directory are the same" $ do
let action db = withConn db $ \conn -> do
_ <- PG.execute_ conn "BEGIN; CREATE TABLE foo ( id int );"
void $ PG.execute_ conn "INSERT INTO foo (id) VALUES (1); END;"
withTempDirectory "/tmp" "tmp-postgres-cache-action" $ \cachePath -> do
let theFinalCachePath = cachePath <> "/cached"
cacheAction theFinalCachePath action (defaultConfig { dataDirectory = Permanent theFinalCachePath } ) >>= \case
Left (SnapshotCopyFailed {}) -> pure ()
_ -> fail $ "cacheAction should have failed with SnapshotCopyFailed"
it "works if two threads try to create a cache at the same time" $ do
withTempDirectory "/tmp" "tmp-postgres-parallel-cache-test" $ \dirPath -> do
withDbCache $ \cacheInfo -> do
lock <- newEmptyMVar
let
theConfig = defaultConfig <> cacheConfig cacheInfo
waitIfSecond _ = do
tryPutMVar lock () >>= \case
True -> pure ()
False -> threadDelay 100000
theCacheAction = cacheAction dirPath waitIfSecond theConfig
res <- Async.replicateConcurrently 3 theCacheAction
if all isRight res then pure () else fail "Failed to create caches concurrently"
it "nested calls don't deadlock" $ do
withTempDirectory "/tmp" "tmp-postgres-cache-action" $ \cachePath -> do
let
action _ = do
let differentCachePath = cachePath <> "/cached1"
cacheAction differentCachePath (const $ pure ()) defaultConfig >>= \case
Left err -> fail $ "Second call should succeed but failed with " <> show err
Right config -> configTest config
theFinalCachePath = cachePath <> "/cached"
cacheAction theFinalCachePath action defaultConfig >>= \case
Left err -> fail $ "First call should succeed but failed with " <> show err
Right config -> configTest config
it "doesnt deadlock if the parent directory is missing" $ do
withTempDirectory "/tmp" "tmp-postgres-cache-action" $ \cachePath -> do
let theFinalCachePath = cachePath <> "/parent/child"
cacheAction theFinalCachePath (const $ pure ()) defaultConfig >>= \case
Left err -> fail $ "First call should succeed but failed with " <> show err
Right config -> configTest config
it "doesnt deadlock if the parent directory is missing multithreaded version" $ do
withTempDirectory "/tmp" "tmp-postgres-cache-action" $ \cachePath -> do
let theFinalCachePath = cachePath <> "/parent/child"
threadBody = cacheAction theFinalCachePath mempty defaultConfig
thread1 <- Async.async threadBody
thread2 <- Async.async threadBody
Async.wait thread1 >>= \case
Left err -> fail $ "First call should succeed but failed with " <> show err
Right config -> configTest config
Async.wait thread2 >>= \case
Left err -> fail $ "Second call should succeed but failed with " <> show err
Right config -> configTest config
spec :: Spec
spec = do
withConfigSpecs
withSnapshotSpecs
cacheActionSpecs
it "stopPostgres cannot be connected to" $ withConfig' defaultConfig $ \db -> do
stopPostgres db `shouldReturn` ExitSuccess
PG.connectPostgreSQL (toConnectionString db) `shouldThrow`
(\(_ :: IOError) -> True)
-- Not a great test but don't want to be too rigid
let createdbPlan = optionsToDefaultConfig mempty { Client.dbname = pure "newdb" }
it "prettyPrintConfig seems to work" $ do
let configString = prettyPrintConfig createdbPlan
wordsToSearchFor = Set.fromList
[ "commandLine:"
, "connectionOptions:"
, "connectionTimeout:"
, "dataDirectory:"
, "environmentVariables:"
, "inherit:"
, "initDbConfig:"
, "port:"
, "postgresConfig:"
, "postgresConfigFile:"
, "socketDirectory:"
, "specific:"
, "stdErr:"
, "stdIn:"
, "stdOut:"
]
shouldSatisfy (Set.fromList $ words configString) $
Set.isSubsetOf wordsToSearchFor
it "prettyPrintDB seems to work" $ withConfig' (createdbPlan <> defaultConfig) $ \db -> do
let dbString = prettyPrintDB db
wordsToSearchFor = Set.fromList
[ "completePlanInitDb:"
, "completePlanCreateDb:"
, "completePlanPostgres:"
, "completePlanConfig:"
, "completePlanDataDirectory:"
]
shouldSatisfy (Set.fromList $ words dbString) $
Set.isSubsetOf wordsToSearchFor
let justBackupResources = mempty { postgresConfigFile =
[ ("wal_level", "replica")
, ("archive_mode","on")
, ("max_wal_senders","2")
]
}
backupResources = justBackupResources
it "can support backup and restore" $ withConfig' backupResources $ \db@DB {..} -> do
let dataDir = toFilePath (resourcesDataDir dbResources)
appendFile (dataDir ++ "/pg_hba.conf") $ "local replication all trust"
withTempDirectory "/tmp" "tmp-postgres-backup" $ \tempDir -> do
let walArchiveDir = tempDir ++ "/archive"
baseBackupFile = tempDir ++ "/backup"
archiveLine = "archive_command = " ++
"'test ! -f " ++ walArchiveDir ++ "/%f && cp %p " ++ walArchiveDir ++ "/%f'\n"
appendFile (dataDir ++ "/postgresql.conf") $ archiveLine
createDirectory walArchiveDir
bracket (PG.connectPostgreSQL $ toConnectionString db) PG.close $ \conn ->
(void :: IO [PG.Only Bool] -> IO ()) $
PG.query_ conn "SELECT pg_reload_conf()"
let Just port = getLast $ Client.port $ postgresProcessClientOptions dbPostgresProcess
Just host = getLast $ Client.host $ postgresProcessClientOptions dbPostgresProcess
backupCommandFlags =
[ "-D" ++ baseBackupFile
, "--format=tar"
, "-p" ++ show port
, "-h" ++ host
]
(exitCode, stdouts, errs) <- readProcessWithExitCode "pg_basebackup" backupCommandFlags []
case exitCode of
ExitSuccess -> pure ()
ExitFailure _ -> do
putStrLn stdouts
putStrLn errs
fail $ "pg_basebackup failed with flags " ++ unwords backupCommandFlags
bracket (PG.connectPostgreSQL $ toConnectionString db ) PG.close $ \conn -> do
_ <- PG.execute_ conn "CREATE TABLE foo(id int PRIMARY KEY);"
_ <- PG.execute_ conn "BEGIN ISOLATION LEVEL READ COMMITTED READ WRITE; INSERT INTO foo (id) VALUES (1); COMMIT"
_ :: [PG.Only String] <- PG.query_ conn "SELECT pg_walfile_name(pg_switch_wal())"
_ :: [PG.Only String] <- PG.query_ conn "SELECT pg_walfile_name(pg_create_restore_point('pitr'))"
_ <- PG.execute_ conn "BEGIN ISOLATION LEVEL READ COMMITTED READ WRITE; INSERT INTO foo (id) VALUES (2); COMMIT"
PG.query_ conn "SELECT id FROM foo ORDER BY id ASC"
`shouldReturn` [PG.Only (1 :: Int), PG.Only 2]
stopPostgresGracefully db `shouldReturn` ExitSuccess
removeDirectoryRecursive dataDir
createDirectory dataDir
let untarCommand = "tar -C" ++ dataDir ++ " -xf " ++ baseBackupFile ++ "/base.tar"
system untarCommand `shouldReturn` ExitSuccess
system ("chmod -R 700 " ++ dataDir) `shouldReturn` ExitSuccess
writeFile (dataDir ++ "/recovery.conf") $ "recovery_target_name='pitr'\nrecovery_target_action='promote'\nrecovery_target_inclusive=true\nrestore_command='"
++ "cp " ++ walArchiveDir ++ "/%f %p'"
either throwIO pure <=< withRestart db $ \newDb -> do
bracket (PG.connectPostgreSQL $ toConnectionString newDb) PG.close $ \conn -> do
fix $ \next -> do
fmap (PG.fromOnly . head) (PG.query_ conn "SELECT pg_is_in_recovery()") >>= \case
True -> threadDelay 100000 >> next
False -> pure ()
PG.query_ conn "SELECT id FROM foo ORDER BY id ASC"
`shouldReturn` [PG.Only (1 :: Int)]
it "executeProcessAndTee doesn't lose stderr" do
let config = CompleteProcessConfig [] ["--bogus-argument"] devNull devNull devNull
let expected = "could not find a \"initdb\" to executeinitdb: unrecognized option '--bogus-argument'Try \"initdb --help\" for more information."
executeProcessAndTee "initdb" config `shouldReturn` (ExitFailure 1, "", expected)
main :: IO ()
main = hspec spec