Debian Codegraph @

test/hs/Test/Ganeti/Luxi.hs

raw
1 {-# LANGUAGE TemplateHaskell #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3
4 {-| Unittests for ganeti-htools.
5
6 -}
7
8 {-
9
10 Copyright (C) 2009, 2010, 2011, 2012 Google Inc.
11 All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are
15 met:
16
17 1. Redistributions of source code must retain the above copyright notice,
18 this list of conditions and the following disclaimer.
19
20 2. Redistributions in binary form must reproduce the above copyright
21 notice, this list of conditions and the following disclaimer in the
22 documentation and/or other materials provided with the distribution.
23
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
28 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
36 -}
37
38 module Test.Ganeti.Luxi (testLuxi) where
39
40 import Test.HUnit
41 import Test.QuickCheck
42 import Test.QuickCheck.Monadic (monadicIO, run, stop)
43
44 import Data.List
45 import Control.Concurrent (forkIO)
46 import Control.Exception (bracket)
47 import qualified Text.JSON as J
48
49 import Test.Ganeti.OpCodes ()
50 import Test.Ganeti.Query.Language (genFilter)
51 import Test.Ganeti.TestCommon
52 import Test.Ganeti.TestHelper
53 import Test.Ganeti.Types (genReasonTrail)
54
55 import Ganeti.BasicTypes
56 import qualified Ganeti.Luxi as Luxi
57 import qualified Ganeti.UDSServer as US
58
59 {-# ANN module "HLint: ignore Use camelCase" #-}
60
61 -- * Luxi tests
62
63 $(genArbitrary ''Luxi.LuxiReq)
64
65 instance Arbitrary Luxi.LuxiOp where
66 arbitrary = do
67 lreq <- arbitrary
68 case lreq of
69 Luxi.ReqQuery -> Luxi.Query <$> arbitrary <*> genFields <*> genFilter
70 Luxi.ReqQueryFields -> Luxi.QueryFields <$> arbitrary <*> genFields
71 Luxi.ReqQueryNodes -> Luxi.QueryNodes <$> listOf genFQDN <*>
72 genFields <*> arbitrary
73 Luxi.ReqQueryGroups -> Luxi.QueryGroups <$> arbitrary <*>
74 arbitrary <*> arbitrary
75 Luxi.ReqQueryNetworks -> Luxi.QueryNetworks <$> arbitrary <*>
76 arbitrary <*> arbitrary
77 Luxi.ReqQueryInstances -> Luxi.QueryInstances <$> listOf genFQDN <*>
78 genFields <*> arbitrary
79 Luxi.ReqQueryFilters -> Luxi.QueryFilters <$> arbitrary <*> genFields
80 Luxi.ReqReplaceFilter -> Luxi.ReplaceFilter <$> genMaybe genUUID <*>
81 arbitrary <*> arbitrary <*> arbitrary <*>
82 genReasonTrail
83 Luxi.ReqDeleteFilter -> Luxi.DeleteFilter <$> genUUID
84 Luxi.ReqQueryJobs -> Luxi.QueryJobs <$> arbitrary <*> genFields
85 Luxi.ReqQueryExports -> Luxi.QueryExports <$>
86 listOf genFQDN <*> arbitrary
87 Luxi.ReqQueryConfigValues -> Luxi.QueryConfigValues <$> genFields
88 Luxi.ReqQueryClusterInfo -> pure Luxi.QueryClusterInfo
89 Luxi.ReqQueryTags -> do
90 kind <- arbitrary
91 Luxi.QueryTags kind <$> genLuxiTagName kind
92 Luxi.ReqSubmitJob -> Luxi.SubmitJob <$> resize maxOpCodes arbitrary
93 Luxi.ReqSubmitJobToDrainedQueue -> Luxi.SubmitJobToDrainedQueue <$>
94 resize maxOpCodes arbitrary
95 Luxi.ReqSubmitManyJobs -> Luxi.SubmitManyJobs <$>
96 resize maxOpCodes arbitrary
97 Luxi.ReqWaitForJobChange -> Luxi.WaitForJobChange <$> arbitrary <*>
98 genFields <*> pure J.JSNull <*>
99 pure J.JSNull <*> arbitrary
100 Luxi.ReqPickupJob -> Luxi.PickupJob <$> arbitrary
101 Luxi.ReqArchiveJob -> Luxi.ArchiveJob <$> arbitrary
102 Luxi.ReqAutoArchiveJobs -> Luxi.AutoArchiveJobs <$> arbitrary <*>
103 arbitrary
104 Luxi.ReqCancelJob -> Luxi.CancelJob <$> arbitrary <*> arbitrary
105 Luxi.ReqChangeJobPriority -> Luxi.ChangeJobPriority <$> arbitrary <*>
106 arbitrary
107 Luxi.ReqSetDrainFlag -> Luxi.SetDrainFlag <$> arbitrary
108 Luxi.ReqSetWatcherPause -> Luxi.SetWatcherPause <$> arbitrary
109
110 -- | Simple check that encoding/decoding of LuxiOp works.
111 prop_CallEncoding :: Luxi.LuxiOp -> Property
112 prop_CallEncoding op =
113 (US.parseCall (US.buildCall (Luxi.strOfOp op) (Luxi.opToArgs op))
114 >>= uncurry Luxi.decodeLuxiCall) ==? Ok op
115
116 -- | Server ping-pong helper.
117 luxiServerPong :: Luxi.Client -> IO ()
118 luxiServerPong c = do
119 msg <- Luxi.recvMsgExt c
120 case msg of
121 Luxi.RecvOk m -> Luxi.sendMsg c m >> luxiServerPong c
122 _ -> return ()
123
124 -- | Client ping-pong helper.
125 luxiClientPong :: Luxi.Client -> [String] -> IO [String]
126 luxiClientPong c =
127 mapM (\m -> Luxi.sendMsg c m >> Luxi.recvMsg c)
128
129 -- | Monadic check that, given a server socket, we can connect via a
130 -- client to it, and that we can send a list of arbitrary messages and
131 -- get back what we sent.
132 prop_ClientServer :: [[DNSChar]] -> Property
133 prop_ClientServer dnschars = monadicIO $ do
134 let msgs = map (map dnsGetChar) dnschars
135 fpath <- run $ getTempFileName "luxitest"
136 -- we need to create the server first, otherwise (if we do it in the
137 -- forked thread) the client could try to connect to it before it's
138 -- ready
139 server <- run $ Luxi.getLuxiServer False fpath
140 -- fork the server responder
141 _ <- run . forkIO $
142 bracket
143 (Luxi.acceptClient server)
144 (\c -> Luxi.closeClient c >> Luxi.closeServer server)
145 luxiServerPong
146 replies <- run $
147 bracket
148 (Luxi.getLuxiClient fpath)
149 Luxi.closeClient
150 (`luxiClientPong` msgs)
151 _ <- stop $ replies ==? msgs
152 return ()
153
154 -- | Check that Python and Haskell define the same Luxi requests list.
155 case_AllDefined :: Assertion
156 case_AllDefined = do
157 py_stdout <- runPython "from ganeti import luxi\n\
158 \print('\\n'.join(luxi.REQ_ALL))" "" >>=
159 checkPythonResult
160 let py_ops = sort $ lines py_stdout
161 hs_ops = Luxi.allLuxiCalls
162 extra_py = py_ops \\ hs_ops
163 extra_hs = hs_ops \\ py_ops
164 assertBool ("Luxi calls missing from Haskell code:\n" ++
165 unlines extra_py) (null extra_py)
166 assertBool ("Extra Luxi calls in the Haskell code:\n" ++
167 unlines extra_hs) (null extra_hs)
168
169
170 testSuite "Luxi"
171 [ 'prop_CallEncoding
172 , 'prop_ClientServer
173 , 'case_AllDefined
174 ]