@@ -0,0 +1,93 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+{-# LANGUAGE TemplateHaskell #-}
3+module Main
4+ ( main
5+ ) where
6+
7+
8+-------------------------------------------------------------------------------
9+import Control.Monad
10+import Control.Monad.IO.Class
11+import qualified Data.Map as M
12+import Data.Monoid
13+import Database.Bloodhound
14+import Network.HTTP.Client (defaultManagerSettings)
15+import Test.Tasty
16+import Test.Tasty.HUnit
17+-------------------------------------------------------------------------------
18+import Katip
19+import Katip.Scribes.ElasticSearch
20+-------------------------------------------------------------------------------
21+
22+
23+main :: IO ()
24+main = defaultMain $ testGroup "katip-elasticsearch"
25+ [
26+ withResource setupSearch teardownSearch esTests
27+ ]
28+
29+
30+-------------------------------------------------------------------------------
31+setupSearch :: IO (Scribe, IO ())
32+setupSearch = do
33+ bh recreateESSchema
34+ mkEsScribe defaultEsScribeCfg svr ixn mn DebugS V3
35+
36+
37+-------------------------------------------------------------------------------
38+teardownSearch :: (Scribe, IO ()) -> IO ()
39+teardownSearch (_, finalizer) = do
40+ finalizer
41+ bh dropESSchema
42+
43+
44+-------------------------------------------------------------------------------
45+esTests :: IO (Scribe, IO ()) -> TestTree
46+esTests setup = testGroup "elasticsearch scribe"
47+ [
48+ testCase "it works" $ withTestLogging setup $ \done -> do
49+ $(logT) () mempty InfoS "A test message"
50+ liftIO $ do
51+ done
52+ print =<< getLogs
53+ True @?= False
54+ ]
55+
56+
57+-------------------------------------------------------------------------------
58+getLogs = bh $ searchByIndex ixn $ mkSearch Nothing Nothing
59+
60+
61+-------------------------------------------------------------------------------
62+bh = withBH defaultManagerSettings svr
63+
64+
65+-------------------------------------------------------------------------------
66+withTestLogging setup f = do
67+ (scribe, done) <- setup
68+ le <- initLogEnv ns env
69+ runKatipT le { _logEnvScribes = M.singleton "es" scribe} (f done)
70+ where
71+ ns = Namespace ["katip-test"]
72+ env = Environment "test"
73+
74+-------------------------------------------------------------------------------
75+svr = Server "http://localhost:9200"
76+ixn = IndexName "katip-elasticsearch-tests"
77+ixs = defaultIndexSettings
78+mn = MappingName "logs"
79+mapping = () -- ehhh
80+
81+
82+-------------------------------------------------------------------------------
83+recreateESSchema = dropESSchema >> createESSchema
84+
85+
86+-------------------------------------------------------------------------------
87+dropESSchema = void $ deleteIndex ixn
88+
89+
90+-------------------------------------------------------------------------------
91+createESSchema = do
92+ createIndex ixs ixn
93+ putMapping ixn mn mapping