Statistics
| Branch: | Tag: | Revision:

root / htest / Test / Ganeti / OpCodes.hs @ c66f09f5

History | View | Annotate | Download (10.8 kB)

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

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

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

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

    
27
-}
28

    
29
module Test.Ganeti.OpCodes
30
  ( testOpCodes
31
  , OpCodes.OpCode(..)
32
  ) where
33

    
34
import qualified Test.HUnit as HUnit
35
import Test.QuickCheck
36

    
37
import Control.Applicative
38
import Data.List
39
import qualified Data.Map as Map
40
import qualified Text.JSON as J
41

    
42
import Test.Ganeti.TestHelper
43
import Test.Ganeti.TestCommon
44
import Test.Ganeti.Types ()
45
import Test.Ganeti.Query.Language
46

    
47
import qualified Ganeti.Constants as C
48
import qualified Ganeti.OpCodes as OpCodes
49
import Ganeti.Types
50
import Ganeti.OpParams
51
import Ganeti.JSON
52

    
53
{-# ANN module "HLint: ignore Use camelCase" #-}
54

    
55
-- * Arbitrary instances
56

    
57
instance Arbitrary OpCodes.TagObject where
58
  arbitrary = oneof [ OpCodes.TagInstance <$> getFQDN
59
                    , OpCodes.TagNode     <$> getFQDN
60
                    , OpCodes.TagGroup    <$> getFQDN
61
                    , pure OpCodes.TagCluster
62
                    ]
63

    
64
$(genArbitrary ''OpCodes.ReplaceDisksMode)
65

    
66
instance Arbitrary OpCodes.DiskIndex where
67
  arbitrary = choose (0, C.maxDisks - 1) >>= OpCodes.mkDiskIndex
68

    
69
$(genArbitrary ''INicParams)
70

    
71
instance Arbitrary OpCodes.OpCode where
72
  arbitrary = do
73
    op_id <- elements OpCodes.allOpIDs
74
    case op_id of
75
      "OP_TEST_DELAY" ->
76
        OpCodes.OpTestDelay <$> arbitrary <*> arbitrary
77
                 <*> genNodeNames
78
      "OP_INSTANCE_REPLACE_DISKS" ->
79
        OpCodes.OpInstanceReplaceDisks <$> getFQDN <*>
80
          getMaybe genNodeNameNE <*> arbitrary <*> genDiskIndices <*> arbitrary
81
      "OP_INSTANCE_FAILOVER" ->
82
        OpCodes.OpInstanceFailover <$> getFQDN <*> arbitrary <*>
83
          getMaybe genNodeNameNE
84
      "OP_INSTANCE_MIGRATE" ->
85
        OpCodes.OpInstanceMigrate <$> getFQDN <*> arbitrary <*>
86
          arbitrary <*> arbitrary <*> getMaybe genNodeNameNE
87
      "OP_TAGS_SET" ->
88
        OpCodes.OpTagsSet <$> arbitrary <*> genTags
89
      "OP_TAGS_DEL" ->
90
        OpCodes.OpTagsSet <$> arbitrary <*> genTags
91
      "OP_CLUSTER_POST_INIT" -> pure OpCodes.OpClusterPostInit
92
      "OP_CLUSTER_DESTROY" -> pure OpCodes.OpClusterDestroy
93
      "OP_CLUSTER_QUERY" -> pure OpCodes.OpClusterQuery
94
      "OP_CLUSTER_VERIFY" ->
95
        OpCodes.OpClusterVerify <$> arbitrary <*> arbitrary <*>
96
          genSet Nothing <*> genSet Nothing <*> arbitrary <*> arbitrary
97
      "OP_CLUSTER_VERIFY_CONFIG" ->
98
        OpCodes.OpClusterVerifyConfig <$> arbitrary <*> arbitrary <*>
99
          genSet Nothing <*> arbitrary
100
      "OP_CLUSTER_VERIFY_GROUP" ->
101
        OpCodes.OpClusterVerifyGroup <$> arbitrary <*> arbitrary <*>
102
          arbitrary <*> genSet Nothing <*> genSet Nothing <*> arbitrary
103
      "OP_CLUSTER_VERIFY_DISKS" -> pure OpCodes.OpClusterVerifyDisks
104
      "OP_GROUP_VERIFY_DISKS" ->
105
        OpCodes.OpGroupVerifyDisks <$> arbitrary
106
      "OP_CLUSTER_REPAIR_DISK_SIZES" ->
107
        OpCodes.OpClusterRepairDiskSizes <$>
108
          resize maxNodes (listOf (getFQDN >>= mkNonEmpty))
109
      "OP_CLUSTER_CONFIG_QUERY" ->
110
        OpCodes.OpClusterConfigQuery <$> resize maxNodes arbitrary
111
      "OP_CLUSTER_RENAME" ->
112
        OpCodes.OpClusterRename <$> (getName >>= mkNonEmpty)
113
      "OP_CLUSTER_SET_PARAMS" ->
114
        OpCodes.OpClusterSetParams <$> emptyMUD <*> emptyMUD <*>
115
          arbitrary <*> getMaybe (listOf1 arbitrary >>= mkNonEmpty) <*>
116
          getMaybe genEmptyContainer <*> emptyMUD <*>
117
          getMaybe genEmptyContainer <*> getMaybe genEmptyContainer <*>
118
          getMaybe genEmptyContainer <*> getMaybe arbitrary <*>
119
          arbitrary <*> arbitrary <*> arbitrary <*>
120
          arbitrary <*> arbitrary <*> arbitrary <*>
121
          emptyMUD <*> emptyMUD <*> arbitrary <*>
122
          arbitrary <*> arbitrary <*> arbitrary <*>
123
          arbitrary <*> arbitrary <*> arbitrary
124
      "OP_CLUSTER_REDIST_CONF" -> pure OpCodes.OpClusterRedistConf
125
      "OP_CLUSTER_ACTIVATE_MASTER_IP" ->
126
        pure OpCodes.OpClusterActivateMasterIp
127
      "OP_CLUSTER_DEACTIVATE_MASTER_IP" ->
128
        pure OpCodes.OpClusterDeactivateMasterIp
129
      "OP_QUERY" ->
130
        OpCodes.OpQuery <$> arbitrary <*> arbitrary <*> arbitrary <*> genFilter
131
      "OP_QUERY_FIELDS" ->
132
        OpCodes.OpQueryFields <$> arbitrary <*> arbitrary
133
      "OP_OOB_COMMAND" ->
134
        OpCodes.OpOobCommand <$> genNodeNamesNE <*> arbitrary <*>
135
          arbitrary <*> arbitrary <*> (arbitrary `suchThat` (>0))
136
      "OP_NODE_REMOVE" -> OpCodes.OpNodeRemove <$> (getFQDN >>= mkNonEmpty)
137
      "OP_NODE_ADD" ->
138
        OpCodes.OpNodeAdd <$> (getFQDN >>= mkNonEmpty) <*>
139
          emptyMUD <*> emptyMUD <*>
140
          arbitrary <*> arbitrary <*> arbitrary <*> arbitrary <*>
141
          arbitrary <*> arbitrary <*> emptyMUD
142
      "OP_NODE_QUERY" ->
143
        OpCodes.OpNodeQuery <$> arbitrary <*> arbitrary <*> arbitrary
144
      "OP_NODE_QUERYVOLS" ->
145
        OpCodes.OpNodeQueryvols <$> arbitrary <*> genNodeNamesNE
146
      "OP_NODE_QUERY_STORAGE" ->
147
        OpCodes.OpNodeQueryStorage <$> arbitrary <*> arbitrary <*>
148
          genNodeNamesNE <*> arbitrary
149
      "OP_NODE_MODIFY_STORAGE" ->
150
        OpCodes.OpNodeModifyStorage <$> genNodeNameNE <*> arbitrary <*>
151
          arbitrary <*> pure emptyJSObject
152
      "OP_REPAIR_NODE_STORAGE" ->
153
        OpCodes.OpRepairNodeStorage <$> genNodeNameNE <*> arbitrary <*>
154
          arbitrary <*> arbitrary
155
      "OP_NODE_SET_PARAMS" ->
156
        OpCodes.OpNodeSetParams <$> genNodeNameNE <*> arbitrary <*>
157
          emptyMUD <*> emptyMUD <*> arbitrary <*> arbitrary <*> arbitrary <*>
158
          arbitrary <*> arbitrary <*> arbitrary <*> arbitrary <*>
159
          emptyMUD
160
      "OP_NODE_POWERCYCLE" ->
161
        OpCodes.OpNodePowercycle <$> genNodeNameNE <*> arbitrary
162
      "OP_NODE_MIGRATE" ->
163
        OpCodes.OpNodeMigrate <$> genNodeNameNE <*> arbitrary <*>
164
          arbitrary <*> arbitrary <*> arbitrary <*> arbitrary <*>
165
          arbitrary
166
      "OP_NODE_EVACUATE" ->
167
        OpCodes.OpNodeEvacuate <$> arbitrary <*> genNodeNameNE <*>
168
          getMaybe genNodeNameNE <*> arbitrary <*> arbitrary
169
      _ -> fail $ "Undefined arbitrary for opcode " ++ op_id
170

    
171
-- * Helper functions
172

    
173
-- | Empty JSObject.
174
emptyJSObject :: J.JSObject J.JSValue
175
emptyJSObject = J.toJSObject []
176

    
177
-- | Empty maybe unchecked dictionary.
178
emptyMUD :: Gen (Maybe (J.JSObject J.JSValue))
179
emptyMUD = getMaybe $ pure emptyJSObject
180

    
181
-- | Generates an empty container.
182
genEmptyContainer :: (Ord a) => Gen (GenericContainer a b)
183
genEmptyContainer = pure . GenericContainer $ Map.fromList []
184

    
185
-- | Generates list of disk indices.
186
genDiskIndices :: Gen [DiskIndex]
187
genDiskIndices = do
188
  cnt <- choose (0, C.maxDisks)
189
  genUniquesList cnt
190

    
191
-- | Generates a list of node names.
192
genNodeNames :: Gen [String]
193
genNodeNames = resize maxNodes (listOf getFQDN)
194

    
195
-- | Generates a list of node names in non-empty string type.
196
genNodeNamesNE :: Gen [NonEmptyString]
197
genNodeNamesNE = genNodeNames >>= mapM (mkNonEmpty)
198

    
199
-- | Gets a node name in non-empty type.
200
genNodeNameNE :: Gen NonEmptyString
201
genNodeNameNE = getFQDN >>= mkNonEmpty
202

    
203
-- * Test cases
204

    
205
-- | Check that opcode serialization is idempotent.
206
prop_serialization :: OpCodes.OpCode -> Property
207
prop_serialization = testSerialisation
208

    
209
-- | Check that Python and Haskell defined the same opcode list.
210
case_AllDefined :: HUnit.Assertion
211
case_AllDefined = do
212
  py_stdout <- runPython "from ganeti import opcodes\n\
213
                         \print '\\n'.join(opcodes.OP_MAPPING.keys())" "" >>=
214
               checkPythonResult
215
  let py_ops = sort $ lines py_stdout
216
      hs_ops = OpCodes.allOpIDs
217
      -- extra_py = py_ops \\ hs_ops
218
      extra_hs = hs_ops \\ py_ops
219
  -- FIXME: uncomment when we have parity
220
  -- HUnit.assertBool ("OpCodes missing from Haskell code:\n" ++
221
  --                  unlines extra_py) (null extra_py)
222
  HUnit.assertBool ("Extra OpCodes in the Haskell code code:\n" ++
223
                    unlines extra_hs) (null extra_hs)
224

    
225
-- | Custom HUnit test case that forks a Python process and checks
226
-- correspondence between Haskell-generated OpCodes and their Python
227
-- decoded, validated and re-encoded version.
228
--
229
-- Note that we have a strange beast here: since launching Python is
230
-- expensive, we don't do this via a usual QuickProperty, since that's
231
-- slow (I've tested it, and it's indeed quite slow). Rather, we use a
232
-- single HUnit assertion, and in it we manually use QuickCheck to
233
-- generate 500 opcodes times the number of defined opcodes, which
234
-- then we pass in bulk to Python. The drawbacks to this method are
235
-- two fold: we cannot control the number of generated opcodes, since
236
-- HUnit assertions don't get access to the test options, and for the
237
-- same reason we can't run a repeatable seed. We should probably find
238
-- a better way to do this, for example by having a
239
-- separately-launched Python process (if not running the tests would
240
-- be skipped).
241
case_py_compat :: HUnit.Assertion
242
case_py_compat = do
243
  let num_opcodes = length OpCodes.allOpIDs * 500
244
  sample_opcodes <- sample' (vectorOf num_opcodes
245
                             (arbitrary::Gen OpCodes.OpCode))
246
  let opcodes = head sample_opcodes
247
      serialized = J.encode opcodes
248
  py_stdout <-
249
     runPython "from ganeti import opcodes\n\
250
               \import sys\n\
251
               \from ganeti import serializer\n\
252
               \op_data = serializer.Load(sys.stdin.read())\n\
253
               \decoded = [opcodes.OpCode.LoadOpCode(o) for o in op_data]\n\
254
               \for op in decoded:\n\
255
               \  op.Validate(True)\n\
256
               \encoded = [op.__getstate__() for op in decoded]\n\
257
               \print serializer.Dump(encoded)" serialized
258
     >>= checkPythonResult
259
  let deserialised = J.decode py_stdout::J.Result [OpCodes.OpCode]
260
  decoded <- case deserialised of
261
               J.Ok ops -> return ops
262
               J.Error msg ->
263
                 HUnit.assertFailure ("Unable to decode opcodes: " ++ msg)
264
                 -- this already raised an expection, but we need it
265
                 -- for proper types
266
                 >> fail "Unable to decode opcodes"
267
  HUnit.assertEqual "Mismatch in number of returned opcodes"
268
    (length opcodes) (length decoded)
269
  mapM_ (uncurry (HUnit.assertEqual "Different result after encoding/decoding")
270
        ) $ zip opcodes decoded
271

    
272
testSuite "OpCodes"
273
            [ 'prop_serialization
274
            , 'case_AllDefined
275
            , 'case_py_compat
276
            ]