Query.hs 6.12 KB
Newer Older
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
{-# LANGUAGE TemplateHaskell #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}

{-| Unittests for ganeti-htools.

-}

{-

Copyright (C) 2009, 2010, 2011, 2012 Google Inc.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.

-}

module Test.Ganeti.Query.Query (testQuery_Query) where

import Test.HUnit (Assertion, assertEqual)
import Test.QuickCheck hiding (Result)
import Test.QuickCheck.Monadic

import Data.Function (on)
import Data.List
import qualified Data.Map as Map
import Data.Maybe
import Text.JSON (JSValue(..))

import Test.Ganeti.TestHelper
import Test.Ganeti.TestCommon
import Test.Ganeti.Objects (genEmptyCluster)

import Ganeti.BasicTypes
import Ganeti.Query.Language
import Ganeti.Query.Node
import Ganeti.Query.Query

-- * Helpers

-- | Checks if a list of field definitions contains unknown fields.
hasUnknownFields :: [FieldDefinition] -> Bool
hasUnknownFields = (QFTUnknown `notElem`) . map fdefKind

-- * Test cases

-- | Tests that querying any existing fields, via either query or
-- queryFields, will not return unknown fields.
prop_queryNode_noUnknown :: Property
prop_queryNode_noUnknown =
  forAll (choose (0, maxNodes) >>= genEmptyCluster) $ \cluster ->
  forAll (elements (Map.keys nodeFieldsMap)) $ \field -> monadicIO $ do
  QueryResult fdefs fdata <-
    run (query cluster (Query QRNode [field] EmptyFilter)) >>= resultProp
  QueryFieldsResult fdefs' <-
    resultProp $ queryFields (QueryFields QRNode [field])
  stop $ printTestCase ("Got unknown fields via query (" ++ show fdefs ++ ")")
         (hasUnknownFields fdefs) .&&.
         printTestCase ("Got unknown result status via query (" ++
                        show fdata ++ ")")
           (all (all ((/= RSUnknown) . rentryStatus)) fdata) .&&.
         printTestCase ("Got unknown fields via query fields (" ++ show fdefs'
                        ++ ")") (hasUnknownFields fdefs')

-- | Tests that an unknown field is returned as such.
prop_queryNode_Unknown :: Property
prop_queryNode_Unknown =
  forAll (choose (0, maxNodes) >>= genEmptyCluster) $ \cluster ->
  forAll (arbitrary `suchThat` (`notElem` (Map.keys nodeFieldsMap)))
    $ \field -> monadicIO $ do
  QueryResult fdefs fdata <-
    run (query cluster (Query QRNode [field] EmptyFilter)) >>= resultProp
  QueryFieldsResult fdefs' <-
    resultProp $ queryFields (QueryFields QRNode [field])
  stop $ printTestCase ("Got known fields via query (" ++ show fdefs ++ ")")
         (not $ hasUnknownFields fdefs) .&&.
         printTestCase ("Got /= ResultUnknown result status via query (" ++
                        show fdata ++ ")")
           (all (all ((== RSUnknown) . rentryStatus)) fdata) .&&.
         printTestCase ("Got a Just in a result value (" ++
                        show fdata ++ ")")
           (all (all (isNothing . rentryValue)) fdata) .&&.
         printTestCase ("Got known fields via query fields (" ++ show fdefs'
                        ++ ")") (not $ hasUnknownFields fdefs')

-- | Checks that a result type is conforming to a field definition.
checkResultType :: FieldDefinition -> ResultEntry -> Property
checkResultType _ (ResultEntry RSNormal Nothing) =
  failTest "Nothing result in RSNormal field"
checkResultType _ (ResultEntry _ Nothing) = passTest
checkResultType fdef (ResultEntry RSNormal (Just v)) =
  case (fdefKind fdef, v) of
    (QFTText      , JSString {})   -> passTest
    (QFTBool      , JSBool {})     -> passTest
    (QFTNumber    , JSRational {}) -> passTest
    (QFTTimestamp , JSRational {}) -> passTest
    (QFTUnit      , JSRational {}) -> passTest
    (QFTOther     , _)             -> passTest -- meh, QFT not precise...
    (kind, _) -> failTest $ "Type mismatch, field definition says " ++
                  show kind ++ " but returned value is " ++ show v ++
                  " for field '" ++ fdefName fdef ++ "'"
checkResultType _ (ResultEntry r (Just _)) =
  failTest $ "Just result in " ++ show r ++ " field"

-- | Tests that querying any existing fields, the following three
-- properties hold: RSNormal corresponds to a Just value, any other
-- value corresponds to Nothing, and for a RSNormal and value field,
-- the type of the value corresponds to the type of the field as
-- declared in the FieldDefinition.
prop_queryNode_types :: Property
prop_queryNode_types =
  forAll (choose (0, maxNodes)) $ \numnodes ->
  forAll (genEmptyCluster numnodes) $ \cfg ->
  forAll (elements (Map.keys nodeFieldsMap)) $ \field -> monadicIO $ do
  QueryResult fdefs fdata <-
    run (query cfg (Query QRNode [field] EmptyFilter)) >>= resultProp
  stop $ printTestCase ("Inconsistent result entries (" ++ show fdata ++ ")")
         (conjoin $ map (conjoin . zipWith checkResultType fdefs) fdata) .&&.
         printTestCase "Wrong field definitions length"
           (length fdefs ==? 1) .&&.
         printTestCase "Wrong field result rows length"
           (all ((== 1) . length) fdata) .&&.
         printTestCase "Wrong number of result rows"
           (length fdata ==? numnodes)

-- | Test that queryFields with empty fields list returns all node fields.
case_queryNode_allfields :: Assertion
case_queryNode_allfields = do
   fdefs <- case queryFields (QueryFields QRNode []) of
              Bad msg -> fail $ "Error in query all fields: " ++ msg
              Ok (QueryFieldsResult v) -> return v
   let field_sort = compare `on` fdefName
   assertEqual "Mismatch in all fields list"
     (sortBy field_sort . map fst $ Map.elems nodeFieldsMap)
     (sortBy field_sort fdefs)

testSuite "Query/Query"
  [ 'prop_queryNode_noUnknown
  , 'prop_queryNode_Unknown
  , 'prop_queryNode_types
  , 'case_queryNode_allfields
  ]