Statistics
| Branch: | Tag: | Revision:

root / test / hs / Test / Ganeti / Types.hs @ 2dcb5a26

History | View | Annotate | Download (14.6 kB)

1
{-# LANGUAGE TemplateHaskell #-}
2
{-# OPTIONS_GHC -fno-warn-orphans #-}
3

    
4
{-| Unittests for 'Ganeti.Types'.
5

    
6
-}
7

    
8
{-
9

    
10
Copyright (C) 2012, 2013 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.Types
30
  ( testTypes
31
  , AllocPolicy(..)
32
  , DiskTemplate(..)
33
  , allDiskTemplates
34
  , InstanceStatus(..)
35
  , NonEmpty(..)
36
  , Hypervisor(..)
37
  , JobId(..)
38
  ) where
39

    
40
import Data.List (delete, sort)
41
import Test.QuickCheck as QuickCheck hiding (Result)
42
import Test.HUnit
43
import qualified Text.JSON as J
44

    
45
import Test.Ganeti.TestHelper
46
import Test.Ganeti.TestCommon
47

    
48
import Ganeti.BasicTypes
49
import qualified Ganeti.Constants as C
50
import Ganeti.Types as Types
51
import Ganeti.JSON
52

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

    
55
-- * Arbitrary instance
56

    
57
instance (Arbitrary a, Ord a, Num a, Show a) =>
58
  Arbitrary (Types.Positive a) where
59
  arbitrary = do
60
    (QuickCheck.Positive i) <- arbitrary
61
    Types.mkPositive i
62

    
63
instance (Arbitrary a, Ord a, Num a, Show a) =>
64
  Arbitrary (Types.NonNegative a) where
65
  arbitrary = do
66
    (QuickCheck.NonNegative i) <- arbitrary
67
    Types.mkNonNegative i
68

    
69
instance (Arbitrary a, Ord a, Num a, Show a) =>
70
  Arbitrary (Types.Negative a) where
71
  arbitrary = do
72
    (QuickCheck.Positive i) <- arbitrary
73
    Types.mkNegative $ negate i
74

    
75
instance (Arbitrary a) => Arbitrary (Types.NonEmpty a) where
76
  arbitrary = do
77
    QuickCheck.NonEmpty lst <- arbitrary
78
    Types.mkNonEmpty lst
79

    
80
$(genArbitrary ''AllocPolicy)
81

    
82
-- | Valid disk templates (depending on configure options).
83
allDiskTemplates :: [DiskTemplate]
84
allDiskTemplates =
85
  let all_vals = [minBound..maxBound]::[DiskTemplate]
86
      sel1 = if C.enableSharedFileStorage
87
               then all_vals
88
               else delete DTSharedFile all_vals
89
  in sel1
90

    
91
-- | Custom 'Arbitrary' instance for 'DiskTemplate', which needs to
92
-- handle the case of file storage being disabled at configure time.
93
instance Arbitrary DiskTemplate where
94
  arbitrary = elements allDiskTemplates
95

    
96
$(genArbitrary ''InstanceStatus)
97

    
98
$(genArbitrary ''MigrationMode)
99

    
100
$(genArbitrary ''VerifyOptionalChecks)
101

    
102
$(genArbitrary ''DdmSimple)
103

    
104
$(genArbitrary ''DdmFull)
105

    
106
$(genArbitrary ''CVErrorCode)
107

    
108
$(genArbitrary ''Hypervisor)
109

    
110
$(genArbitrary ''OobCommand)
111

    
112
-- | Valid storage types.
113
allStorageTypes :: [StorageType]
114
allStorageTypes = [minBound..maxBound]::[StorageType]
115

    
116
-- | Custom 'Arbitrary' instance for 'StorageType', which needs to
117
-- handle the case of file storage being disabled at configure time.
118
instance Arbitrary StorageType where
119
  arbitrary = elements allStorageTypes
120

    
121
$(genArbitrary ''NodeEvacMode)
122

    
123
$(genArbitrary ''FileDriver)
124

    
125
$(genArbitrary ''InstCreateMode)
126

    
127
$(genArbitrary ''RebootType)
128

    
129
$(genArbitrary ''ExportMode)
130

    
131
$(genArbitrary ''IAllocatorTestDir)
132

    
133
$(genArbitrary ''IAllocatorMode)
134

    
135
$(genArbitrary ''NICMode)
136

    
137
$(genArbitrary ''JobStatus)
138

    
139
$(genArbitrary ''FinalizedJobStatus)
140

    
141
instance Arbitrary JobId where
142
  arbitrary = do
143
    (Positive i) <- arbitrary
144
    makeJobId i
145

    
146
$(genArbitrary ''JobIdDep)
147

    
148
$(genArbitrary ''JobDependency)
149

    
150
$(genArbitrary ''OpSubmitPriority)
151

    
152
$(genArbitrary ''OpStatus)
153

    
154
$(genArbitrary ''ELogType)
155

    
156
-- * Properties
157

    
158
prop_AllocPolicy_serialisation :: AllocPolicy -> Property
159
prop_AllocPolicy_serialisation = testSerialisation
160

    
161
-- | Test 'AllocPolicy' ordering is as expected.
162
case_AllocPolicy_order :: Assertion
163
case_AllocPolicy_order =
164
  assertEqual "sort order" [ Types.AllocPreferred
165
                           , Types.AllocLastResort
166
                           , Types.AllocUnallocable
167
                           ] [minBound..maxBound]
168

    
169
prop_DiskTemplate_serialisation :: DiskTemplate -> Property
170
prop_DiskTemplate_serialisation = testSerialisation
171

    
172
prop_InstanceStatus_serialisation :: InstanceStatus -> Property
173
prop_InstanceStatus_serialisation = testSerialisation
174

    
175
-- | Tests building non-negative numbers.
176
prop_NonNeg_pass :: QuickCheck.NonNegative Int -> Property
177
prop_NonNeg_pass (QuickCheck.NonNegative i) =
178
  case mkNonNegative i of
179
    Bad msg -> failTest $ "Fail to build non-negative: " ++ msg
180
    Ok nn -> fromNonNegative nn ==? i
181

    
182
-- | Tests building non-negative numbers.
183
prop_NonNeg_fail :: QuickCheck.Positive Int -> Property
184
prop_NonNeg_fail (QuickCheck.Positive i) =
185
  case mkNonNegative (negate i)::Result (Types.NonNegative Int) of
186
    Bad _ -> passTest
187
    Ok nn -> failTest $ "Built non-negative number '" ++ show nn ++
188
             "' from negative value " ++ show i
189

    
190
-- | Tests building positive numbers.
191
prop_Positive_pass :: QuickCheck.Positive Int -> Property
192
prop_Positive_pass (QuickCheck.Positive i) =
193
  case mkPositive i of
194
    Bad msg -> failTest $ "Fail to build positive: " ++ msg
195
    Ok nn -> fromPositive nn ==? i
196

    
197
-- | Tests building positive numbers.
198
prop_Positive_fail :: QuickCheck.NonNegative Int -> Property
199
prop_Positive_fail (QuickCheck.NonNegative i) =
200
  case mkPositive (negate i)::Result (Types.Positive Int) of
201
    Bad _ -> passTest
202
    Ok nn -> failTest $ "Built positive number '" ++ show nn ++
203
             "' from negative or zero value " ++ show i
204

    
205
-- | Tests building negative numbers.
206
prop_Neg_pass :: QuickCheck.Positive Int -> Property
207
prop_Neg_pass (QuickCheck.Positive i) =
208
  case mkNegative i' of
209
    Bad msg -> failTest $ "Fail to build negative: " ++ msg
210
    Ok nn -> fromNegative nn ==? i'
211
  where i' = negate i
212

    
213
-- | Tests building negative numbers.
214
prop_Neg_fail :: QuickCheck.NonNegative Int -> Property
215
prop_Neg_fail (QuickCheck.NonNegative i) =
216
  case mkNegative i::Result (Types.Negative Int) of
217
    Bad _ -> passTest
218
    Ok nn -> failTest $ "Built negative number '" ++ show nn ++
219
             "' from non-negative value " ++ show i
220

    
221
-- | Tests building non-empty lists.
222
prop_NonEmpty_pass :: QuickCheck.NonEmptyList String -> Property
223
prop_NonEmpty_pass (QuickCheck.NonEmpty xs) =
224
  case mkNonEmpty xs of
225
    Bad msg -> failTest $ "Fail to build non-empty list: " ++ msg
226
    Ok nn -> fromNonEmpty nn ==? xs
227

    
228
-- | Tests building positive numbers.
229
case_NonEmpty_fail :: Assertion
230
case_NonEmpty_fail =
231
  assertEqual "building non-empty list from an empty list"
232
    (Bad "Received empty value for non-empty list") (mkNonEmpty ([]::[Int]))
233

    
234
-- | Tests migration mode serialisation.
235
prop_MigrationMode_serialisation :: MigrationMode -> Property
236
prop_MigrationMode_serialisation = testSerialisation
237

    
238
-- | Tests verify optional checks serialisation.
239
prop_VerifyOptionalChecks_serialisation :: VerifyOptionalChecks -> Property
240
prop_VerifyOptionalChecks_serialisation = testSerialisation
241

    
242
-- | Tests 'DdmSimple' serialisation.
243
prop_DdmSimple_serialisation :: DdmSimple -> Property
244
prop_DdmSimple_serialisation = testSerialisation
245

    
246
-- | Tests 'DdmFull' serialisation.
247
prop_DdmFull_serialisation :: DdmFull -> Property
248
prop_DdmFull_serialisation = testSerialisation
249

    
250
-- | Tests 'CVErrorCode' serialisation.
251
prop_CVErrorCode_serialisation :: CVErrorCode -> Property
252
prop_CVErrorCode_serialisation = testSerialisation
253

    
254
-- | Tests equivalence with Python, based on Constants.hs code.
255
case_CVErrorCode_pyequiv :: Assertion
256
case_CVErrorCode_pyequiv = do
257
  let all_py_codes = sort C.cvAllEcodesStrings
258
      all_hs_codes = sort $ map Types.cVErrorCodeToRaw [minBound..maxBound]
259
  assertEqual "for CVErrorCode equivalence" all_py_codes all_hs_codes
260

    
261
-- | Test 'Hypervisor' serialisation.
262
prop_Hypervisor_serialisation :: Hypervisor -> Property
263
prop_Hypervisor_serialisation = testSerialisation
264

    
265
-- | Test 'OobCommand' serialisation.
266
prop_OobCommand_serialisation :: OobCommand -> Property
267
prop_OobCommand_serialisation = testSerialisation
268

    
269
-- | Test 'StorageType' serialisation.
270
prop_StorageType_serialisation :: StorageType -> Property
271
prop_StorageType_serialisation = testSerialisation
272

    
273
-- | Test 'NodeEvacMode' serialisation.
274
prop_NodeEvacMode_serialisation :: NodeEvacMode -> Property
275
prop_NodeEvacMode_serialisation = testSerialisation
276

    
277
-- | Test 'FileDriver' serialisation.
278
prop_FileDriver_serialisation :: FileDriver -> Property
279
prop_FileDriver_serialisation = testSerialisation
280

    
281
-- | Test 'InstCreate' serialisation.
282
prop_InstCreateMode_serialisation :: InstCreateMode -> Property
283
prop_InstCreateMode_serialisation = testSerialisation
284

    
285
-- | Test 'RebootType' serialisation.
286
prop_RebootType_serialisation :: RebootType -> Property
287
prop_RebootType_serialisation = testSerialisation
288

    
289
-- | Test 'ExportMode' serialisation.
290
prop_ExportMode_serialisation :: ExportMode -> Property
291
prop_ExportMode_serialisation = testSerialisation
292

    
293
-- | Test 'IAllocatorTestDir' serialisation.
294
prop_IAllocatorTestDir_serialisation :: IAllocatorTestDir -> Property
295
prop_IAllocatorTestDir_serialisation = testSerialisation
296

    
297
-- | Test 'IAllocatorMode' serialisation.
298
prop_IAllocatorMode_serialisation :: IAllocatorMode -> Property
299
prop_IAllocatorMode_serialisation = testSerialisation
300

    
301
-- | Tests equivalence with Python, based on Constants.hs code.
302
case_IAllocatorMode_pyequiv :: Assertion
303
case_IAllocatorMode_pyequiv = do
304
  let all_py_codes = sort C.validIallocatorModes
305
      all_hs_codes = sort $ map Types.iAllocatorModeToRaw [minBound..maxBound]
306
  assertEqual "for IAllocatorMode equivalence" all_py_codes all_hs_codes
307

    
308
-- | Test 'NICMode' serialisation.
309
prop_NICMode_serialisation :: NICMode -> Property
310
prop_NICMode_serialisation = testSerialisation
311

    
312
-- | Test 'OpStatus' serialisation.
313
prop_OpStatus_serialization :: OpStatus -> Property
314
prop_OpStatus_serialization = testSerialisation
315

    
316
-- | Test 'JobStatus' serialisation.
317
prop_JobStatus_serialization :: JobStatus -> Property
318
prop_JobStatus_serialization = testSerialisation
319

    
320
-- | Test 'JobStatus' ordering is as expected.
321
case_JobStatus_order :: Assertion
322
case_JobStatus_order =
323
  assertEqual "sort order" [ Types.JOB_STATUS_QUEUED
324
                           , Types.JOB_STATUS_WAITING
325
                           , Types.JOB_STATUS_CANCELING
326
                           , Types.JOB_STATUS_RUNNING
327
                           , Types.JOB_STATUS_CANCELED
328
                           , Types.JOB_STATUS_SUCCESS
329
                           , Types.JOB_STATUS_ERROR
330
                           ] [minBound..maxBound]
331

    
332
-- | Tests equivalence with Python, based on Constants.hs code.
333
case_NICMode_pyequiv :: Assertion
334
case_NICMode_pyequiv = do
335
  let all_py_codes = sort C.nicValidModes
336
      all_hs_codes = sort $ map Types.nICModeToRaw [minBound..maxBound]
337
  assertEqual "for NICMode equivalence" all_py_codes all_hs_codes
338

    
339
-- | Test 'FinalizedJobStatus' serialisation.
340
prop_FinalizedJobStatus_serialisation :: FinalizedJobStatus -> Property
341
prop_FinalizedJobStatus_serialisation = testSerialisation
342

    
343
-- | Tests equivalence with Python, based on Constants.hs code.
344
case_FinalizedJobStatus_pyequiv :: Assertion
345
case_FinalizedJobStatus_pyequiv = do
346
  let all_py_codes = sort C.jobsFinalized
347
      all_hs_codes = sort $ map Types.finalizedJobStatusToRaw
348
                            [minBound..maxBound]
349
  assertEqual "for FinalizedJobStatus equivalence" all_py_codes all_hs_codes
350

    
351
-- | Tests JobId serialisation (both from string and ints).
352
prop_JobId_serialisation :: JobId -> Property
353
prop_JobId_serialisation jid =
354
  conjoin [ testSerialisation jid
355
          , (J.readJSON . J.showJSON . show $ fromJobId jid) ==? J.Ok jid
356
          , case (fromJVal . J.showJSON . negate $
357
                  fromJobId jid)::Result JobId of
358
              Bad _ -> passTest
359
              Ok jid' -> failTest $ "Parsed negative job id as id " ++
360
                         show (fromJobId jid')
361
          ]
362

    
363
-- | Tests that fractional job IDs are not accepted.
364
prop_JobId_fractional :: Property
365
prop_JobId_fractional =
366
  forAll (arbitrary `suchThat`
367
          (\d -> fromIntegral (truncate d::Int) /= d)) $ \d ->
368
  case J.readJSON (J.showJSON (d::Double)) of
369
    J.Error _ -> passTest
370
    J.Ok jid -> failTest $ "Parsed fractional value " ++ show d ++
371
                " as job id " ++ show (fromJobId jid)
372

    
373
-- | Tests that a job ID is not parseable from \"bad\" JSON values.
374
case_JobId_BadTypes :: Assertion
375
case_JobId_BadTypes = do
376
  let helper jsval = case J.readJSON jsval of
377
                       J.Error _ -> return ()
378
                       J.Ok jid -> assertFailure $ "Parsed " ++ show jsval
379
                                   ++ " as job id " ++ show (fromJobId jid)
380
  helper J.JSNull
381
  helper (J.JSBool True)
382
  helper (J.JSBool False)
383
  helper (J.JSArray [])
384

    
385
-- | Test 'JobDependency' serialisation.
386
prop_JobDependency_serialisation :: JobDependency -> Property
387
prop_JobDependency_serialisation = testSerialisation
388

    
389
-- | Test 'OpSubmitPriority' serialisation.
390
prop_OpSubmitPriority_serialisation :: OpSubmitPriority -> Property
391
prop_OpSubmitPriority_serialisation = testSerialisation
392

    
393
-- | Tests string formatting for 'OpSubmitPriority'.
394
prop_OpSubmitPriority_string :: OpSubmitPriority -> Property
395
prop_OpSubmitPriority_string prio =
396
  parseSubmitPriority (fmtSubmitPriority prio) ==? Just prio
397

    
398
-- | Test 'ELogType' serialisation.
399
prop_ELogType_serialisation :: ELogType -> Property
400
prop_ELogType_serialisation = testSerialisation
401

    
402
testSuite "Types"
403
  [ 'prop_AllocPolicy_serialisation
404
  , 'case_AllocPolicy_order
405
  , 'prop_DiskTemplate_serialisation
406
  , 'prop_InstanceStatus_serialisation
407
  , 'prop_NonNeg_pass
408
  , 'prop_NonNeg_fail
409
  , 'prop_Positive_pass
410
  , 'prop_Positive_fail
411
  , 'prop_Neg_pass
412
  , 'prop_Neg_fail
413
  , 'prop_NonEmpty_pass
414
  , 'case_NonEmpty_fail
415
  , 'prop_MigrationMode_serialisation
416
  , 'prop_VerifyOptionalChecks_serialisation
417
  , 'prop_DdmSimple_serialisation
418
  , 'prop_DdmFull_serialisation
419
  , 'prop_CVErrorCode_serialisation
420
  , 'case_CVErrorCode_pyequiv
421
  , 'prop_Hypervisor_serialisation
422
  , 'prop_OobCommand_serialisation
423
  , 'prop_StorageType_serialisation
424
  , 'prop_NodeEvacMode_serialisation
425
  , 'prop_FileDriver_serialisation
426
  , 'prop_InstCreateMode_serialisation
427
  , 'prop_RebootType_serialisation
428
  , 'prop_ExportMode_serialisation
429
  , 'prop_IAllocatorTestDir_serialisation
430
  , 'prop_IAllocatorMode_serialisation
431
  , 'case_IAllocatorMode_pyequiv
432
  , 'prop_NICMode_serialisation
433
  , 'prop_OpStatus_serialization
434
  , 'prop_JobStatus_serialization
435
  , 'case_JobStatus_order
436
  , 'case_NICMode_pyequiv
437
  , 'prop_FinalizedJobStatus_serialisation
438
  , 'case_FinalizedJobStatus_pyequiv
439
  , 'prop_JobId_serialisation
440
  , 'prop_JobId_fractional
441
  , 'case_JobId_BadTypes
442
  , 'prop_JobDependency_serialisation
443
  , 'prop_OpSubmitPriority_serialisation
444
  , 'prop_OpSubmitPriority_string
445
  , 'prop_ELogType_serialisation
446
  ]