@@ -3,6 +3,7 @@
3{-# LANGUAGE FlexibleContexts #-}
3{-# LANGUAGE FlexibleContexts #-}
4{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5{-# LANGUAGE OverloadedStrings #-}
5{-# LANGUAGE OverloadedStrings #-}
6+{-# LANGUAGE RecordWildCards #-}
6{-# LANGUAGE ScopedTypeVariables #-}
7{-# LANGUAGE ScopedTypeVariables #-}
7{-# LANGUAGE TemplateHaskell #-}
8{-# LANGUAGE TemplateHaskell #-}
8{-# LANGUAGE TypeOperators #-}
9{-# LANGUAGE TypeOperators #-}
@@ -11,10 +12,10 @@ module Main where
12import Control.Applicative
13import Control.Applicative
13import Control.Error
14import Control.Error
14−import Control.Exception
15+import Control.Exception (evaluate)
15import Control.Monad
16import Control.Monad
17+import Control.Monad.Catch
16import Control.Monad.Reader
18import Control.Monad.Reader
17−import Control.Monad.Trans.Resource
18import Data.Aeson
19import Data.Aeson
19import Data.Aeson.Types (parseEither)
20import Data.Aeson.Types (parseEither)
20import qualified Data.ByteString.Lazy.Char8 as BL8
21import qualified Data.ByteString.Lazy.Char8 as BL8
@@ -26,6 +27,7 @@ import Data.List.NonEmpty (NonEmpty (..))
26import qualified Data.List.NonEmpty as NE
27import qualified Data.List.NonEmpty as NE
27import qualified Data.Map.Strict as M
28import qualified Data.Map.Strict as M
28import Data.Monoid
29import Data.Monoid
30+import Data.Ord (comparing)
29import Data.Proxy
31import Data.Proxy
30import Data.Text (Text)
32import Data.Text (Text)
31import qualified Data.Text as T
33import qualified Data.Text as T
@@ -40,6 +42,7 @@ import Network.HTTP.Client hiding (Proxy)
40import qualified Network.HTTP.Types.Status as NHTS
42import qualified Network.HTTP.Types.Status as NHTS
41import Prelude hiding (filter)
43import Prelude hiding (filter)
42import System.IO.Temp
44import System.IO.Temp
45+import System.Posix.Files
43import Test.Hspec
46import Test.Hspec
44import Test.QuickCheck.Property.Monoid (T (..), eq, prop_Monoid)
47import Test.QuickCheck.Property.Monoid (T (..), eq, prop_Monoid)
@@ -56,10 +59,14 @@ testMapping = MappingName "tweet"
56withTestEnv :: BH IO a -> IO a
59withTestEnv :: BH IO a -> IO a
57withTestEnv = withBH defaultManagerSettings testServer
60withTestEnv = withBH defaultManagerSettings testServer
59−validateStatus :: Response body -> Int -> Expectation
62+validateStatus :: Show body => Response body -> Int -> Expectation
60validateStatus resp expected =
63validateStatus resp expected =
64+ if actual == expected
66+ else expectationFailure ("Expected " <> show expected <> " but got " <> show actual <> ": " <> show body)
61− (NHTS.statusCode $ responseStatus resp)
68+ actual = NHTS.statusCode (responseStatus resp)
62− `shouldBe` (expected :: Int)
69+ body = responseBody resp
64createExampleIndex :: BH IO Reply
71createExampleIndex :: BH IO Reply
65createExampleIndex = createIndex (IndexSettings (ShardCount 1) (ReplicaCount 0)) testIndex
72createExampleIndex = createIndex (IndexSettings (ShardCount 1) (ReplicaCount 0)) testIndex
@@ -314,6 +321,28 @@ searchExpectSource src expected = do
315 value `shouldBe` expected
322 value `shouldBe` expected
329+ -> (GenericSnapshotRepo -> m a)
331+withSnapshotRepo srn@(SnapshotRepoName n) f =
332+ withSystemTempDirectory (T.unpack n) $ \dir -> bracket (alloc dir) free f
335+ liftIO (setFileMode dir mode)
336+ let repo = FsSnapshotRepo srn dir True Nothing Nothing Nothing
337+ resp <- updateSnapshotRepo defaultSnapshotRepoUpdateSettings repo
338+ liftIO (validateStatus resp 200)
339+ return (toGSnapshotRepo repo)
340+ mode = ownerModes `unionFileModes` groupModes `unionFileModes` otherModes
341+ free GenericSnapshotRepo {..} = do
342+ resp <- deleteSnapshotRepo gSnapshotRepoName
343+ liftIO (validateStatus resp 200)
317data BulkTest = BulkTest { name :: Text } deriving (Eq, Generic, Show)
346data BulkTest = BulkTest { name :: Text } deriving (Eq, Generic, Show)
318instance FromJSON BulkTest where
347instance FromJSON BulkTest where
319 parseJSON = genericParseJSON defaultOptions
348 parseJSON = genericParseJSON defaultOptions
@@ -1379,6 +1408,38 @@ main = hspec $ do
1379 Left e -> expectationFailure ("Expected a right but got Left " <> show e)
1408 Left e -> expectationFailure ("Expected a right but got Left " <> show e)
1380 Right _ -> return ()
1409 Right _ -> return ()
1411+ it "finds an existing list of repos" $ withTestEnv $ do
1412+ let r1n = SnapshotRepoName "bloodhound-repo1"
1413+ let r2n = SnapshotRepoName "bloodhound-repo2"
1414+ withSnapshotRepo r1n $ \r1 ->
1415+ withSnapshotRepo r2n $ \r2 -> do
1416+ repos <- getSnapshotRepos (SnapshotRepoList (ExactRepo r1n :| [ExactRepo r2n]))
1417+ liftIO $ case repos of
1419+ let srt = L.sortBy (comparing gSnapshotRepoName)
1420+ srt xs `shouldBe` srt [r1, r2]
1421+ Left e -> expectationFailure (show e)
1423+ it "creates and updates with updateSnapshotRepo" $ withTestEnv $ do
1424+ let r1n = SnapshotRepoName "bloodhound-repo1"
1425+ withSnapshotRepo r1n $ \r1 -> do
1426+ let Just (String dir) = HM.lookup "location" (gSnapshotRepoSettingsObject (gSnapshotRepoSettings r1))
1427+ let noCompression = FsSnapshotRepo r1n (T.unpack dir) False Nothing Nothing Nothing
1428+ resp <- updateSnapshotRepo defaultSnapshotRepoUpdateSettings noCompression
1429+ liftIO (validateStatus resp 200)
1430+ Right [roundtrippedNoCompression] <- getSnapshotRepos (SnapshotRepoList (ExactRepo r1n :| []))
1431+ liftIO (roundtrippedNoCompression `shouldBe` toGSnapshotRepo noCompression)
1433+ it "can verify existing repos" $ withTestEnv $ do
1434+ let r1n = SnapshotRepoName "bloodhound-repo1"
1435+ withSnapshotRepo r1n $ \_ -> do
1436+ res <- verifySnapshotRepo r1n
1437+ liftIO $ case res of
1438+ Right (SnapshotVerification vs)
1439+ | null vs -> expectationFailure "Expected nonempty set of verifying nodes"
1440+ | otherwise -> return ()
1441+ Left e -> expectationFailure (show e)
1382 describe "Enum DocVersion" $ do
1443 describe "Enum DocVersion" $ do
1383 it "follows the laws of Enum, Bounded" $ do
1444 it "follows the laws of Enum, Bounded" $ do
1384 evaluate (succ maxBound :: DocVersion) `shouldThrow` anyErrorCall
1445 evaluate (succ maxBound :: DocVersion) `shouldThrow` anyErrorCall