Debian Codegraph @

test/hs/Test/Ganeti/Query/Filter.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.Query.Filter (testQuery_Filter) where
39
40 import Test.QuickCheck hiding (Result)
41 import Test.QuickCheck.Monadic
42
43 import qualified Data.ByteString.UTF8 as UTF8
44 import qualified Data.Map as Map
45 import Data.List
46 import Text.JSON (showJSON)
47
48 import Test.Ganeti.TestHelper
49 import Test.Ganeti.TestCommon
50 import Test.Ganeti.Objects (genEmptyCluster)
51
52 import Ganeti.BasicTypes
53 import Ganeti.JSON
54 import Ganeti.Objects
55 import Ganeti.Query.Filter
56 import Ganeti.Query.Language
57 import Ganeti.Query.Query
58 import Ganeti.Utils (niceSort)
59
60 -- * Helpers
61
62 -- | Run a query and check that we got a specific response.
63 checkQueryResults :: ConfigData -> Query -> String
64 -> [[ResultEntry]] -> Property
65 checkQueryResults cfg qr descr expected = monadicIO $ do
66 result <- run (query cfg False qr) >>= resultProp
67 _ <- stop $ counterexample ("Inconsistent results in " ++ descr)
68 (qresData result ==? expected)
69 return ()
70
71 -- | Makes a node name query, given a filter.
72 makeNodeQuery :: Filter FilterField -> Query
73 makeNodeQuery = Query (ItemTypeOpCode QRNode) ["name"]
74
75 -- | Checks if a given operation failed.
76 expectBadQuery :: ConfigData -> Query -> String -> Property
77 expectBadQuery cfg qr descr = monadicIO $ do
78 result <- run (query cfg False qr)
79 case result of
80 Bad _ -> return ()
81 Ok a -> stop . failTest $ "Expected failure in " ++ descr ++
82 " but got " ++ show a
83
84 -- | A helper to construct a list of results from an expected names list.
85 namesToResult :: [String] -> [[ResultEntry]]
86 namesToResult = map ((:[]) . ResultEntry RSNormal . Just . showJSON)
87
88 -- | Generates a cluster and returns its node names too.
89 genClusterNames :: Int -> Int -> Gen (ConfigData, [String])
90 genClusterNames min_nodes max_nodes = do
91 numnodes <- choose (min_nodes, max_nodes)
92 cfg <- genEmptyCluster numnodes
93 return (cfg , niceSort . map UTF8.toString . Map.keys . fromContainer
94 $ configNodes cfg)
95
96 -- * Test cases
97
98 -- | Tests single node filtering: eq should return it, and (lt and gt)
99 -- should fail.
100 prop_node_single_filter :: Property
101 prop_node_single_filter =
102 forAll (genClusterNames 1 maxNodes) $ \(cfg, allnodes) ->
103 forAll (elements allnodes) $ \nname ->
104 let fvalue = QuotedString nname
105 buildflt n = n "name" fvalue
106 expsingle = namesToResult [nname]
107 othernodes = nname `delete` allnodes
108 expnot = namesToResult othernodes
109 test_query = checkQueryResults cfg . makeNodeQuery
110 in conjoin
111 [ test_query (buildflt EQFilter) "single-name 'EQ' filter" expsingle
112 , test_query (NotFilter (buildflt EQFilter))
113 "single-name 'NOT EQ' filter" expnot
114 , test_query (AndFilter [buildflt LTFilter, buildflt GTFilter])
115 "single-name 'AND [LT,GT]' filter" []
116 , test_query (AndFilter [buildflt LEFilter, buildflt GEFilter])
117 "single-name 'And [LE,GE]' filter" expsingle
118 ]
119
120 -- | Tests node filtering based on name equality: many 'OrFilter'
121 -- should return all results combined, many 'AndFilter' together
122 -- should return nothing. Note that we need at least 2 nodes so that
123 -- the 'AndFilter' case breaks.
124 prop_node_many_filter :: Property
125 prop_node_many_filter =
126 forAll (genClusterNames 2 maxNodes) $ \(cfg, nnames) ->
127 let eqfilter = map (EQFilter "name" . QuotedString) nnames
128 alln = namesToResult nnames
129 test_query = checkQueryResults cfg . makeNodeQuery
130 num_zero = NumericValue 0
131 in conjoin
132 [ test_query (OrFilter eqfilter) "all nodes 'Or' name filter" alln
133 , test_query (AndFilter eqfilter) "all nodes 'And' name filter" []
134 -- this next test works only because genEmptyCluster generates a
135 -- cluster with no instances
136 , test_query (EQFilter "pinst_cnt" num_zero) "pinst_cnt 'Eq' 0" alln
137 , test_query (GTFilter "sinst_cnt" num_zero) "sinst_cnt 'GT' 0" []
138 ]
139
140 -- | Tests name ordering consistency: requesting a 'simple filter'
141 -- results in identical name ordering as the wanted names, requesting
142 -- a more complex filter results in a niceSort-ed order.
143 prop_node_name_ordering :: Property
144 prop_node_name_ordering =
145 forAll (genClusterNames 2 6) $ \(cfg, nnames) ->
146 forAll (elements (subsequences nnames)) $ \sorted_nodes ->
147 forAll (elements (permutations sorted_nodes)) $ \chosen_nodes ->
148 let orfilter = OrFilter $ map (EQFilter "name" . QuotedString) chosen_nodes
149 alln = namesToResult chosen_nodes
150 all_sorted = namesToResult $ niceSort chosen_nodes
151 test_query = checkQueryResults cfg . makeNodeQuery
152 in conjoin
153 [ test_query orfilter "simple filter/requested" alln
154 , test_query (AndFilter [orfilter]) "complex filter/sorted" all_sorted
155 ]
156
157 -- | Tests node regex filtering. This is a very basic test :(
158 prop_node_regex_filter :: Property
159 prop_node_regex_filter =
160 forAll (genClusterNames 0 maxNodes) $ \(cfg, nnames) ->
161 case mkRegex ".*"::Result FilterRegex of
162 Bad msg -> failTest $ "Can't build regex?! Error: " ++ msg
163 Ok rx ->
164 checkQueryResults cfg (makeNodeQuery (RegexpFilter "name" rx))
165 "rows for all nodes regexp filter" $ namesToResult nnames
166
167 -- | Tests node regex filtering. This is a very basic test :(
168 prop_node_bad_filter :: String -> Int -> Property
169 prop_node_bad_filter rndname rndint =
170 forAll (genClusterNames 1 maxNodes) $ \(cfg, _) ->
171 let test_query = expectBadQuery cfg . makeNodeQuery
172 string_value = QuotedString rndname
173 numeric_value = NumericValue $ fromIntegral rndint
174 in case mkRegex ".*"::Result FilterRegex of
175 Bad msg -> failTest $ "Can't build regex?! Error: " ++ msg
176 Ok rx ->
177 conjoin
178 [ test_query (RegexpFilter "offline" rx)
179 "regex filter against boolean field"
180 , test_query (EQFilter "name" numeric_value)
181 "numeric value eq against string field"
182 , test_query (TrueFilter "name")
183 "true filter against string field"
184 , test_query (EQFilter "offline" string_value)
185 "quoted string eq against boolean field"
186 , test_query (ContainsFilter "name" string_value)
187 "quoted string in non-list field"
188 , test_query (ContainsFilter "name" numeric_value)
189 "numeric value in non-list field"
190 ]
191
192 -- | Tests make simple filter.
193 prop_makeSimpleFilter :: Property
194 prop_makeSimpleFilter =
195 forAll (resize 10 $ listOf1 genName) $ \names ->
196 forAll (resize 10 $ listOf1 arbitrary) $ \ids ->
197 forAll genName $ \namefield ->
198 conjoin [ counterexample "test expected names" $
199 makeSimpleFilter namefield (map Left names) ==?
200 OrFilter (map (EQFilter namefield . QuotedString) names)
201 , counterexample "test expected IDs" $
202 makeSimpleFilter namefield (map Right ids) ==?
203 OrFilter (map (EQFilter namefield . NumericValue) ids)
204 , counterexample "test empty names" $
205 makeSimpleFilter namefield [] ==? EmptyFilter
206 ]
207
208 testSuite "Query/Filter"
209 [ 'prop_node_single_filter
210 , 'prop_node_many_filter
211 , 'prop_node_name_ordering
212 , 'prop_node_regex_filter
213 , 'prop_node_bad_filter
214 , 'prop_makeSimpleFilter
215 ]