Debian Codegraph @

test/hs/Test/Ganeti/Types.hs

raw
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 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.Types
39 ( testTypes
40 , AllocPolicy(..)
41 , DiskTemplate(..)
42 , allDiskTemplates
43 , InstanceStatus(..)
44 , NonEmpty(..)
45 , Hypervisor(..)
46 , JobId(..)
47 , genReasonTrail
48 ) where
49
50 import System.Time (ClockTime(..))
51
52 import Test.QuickCheck as QuickCheck hiding (Result)
53 import Test.HUnit
54 import qualified Text.JSON as J
55
56 import Test.Ganeti.TestHelper
57 import Test.Ganeti.TestCommon
58
59 import Ganeti.BasicTypes
60 import qualified Ganeti.Constants as C
61 import qualified Ganeti.ConstantUtils as ConstantUtils
62 import Ganeti.Types as Types
63 import Ganeti.JSON
64
65 {-# ANN module "HLint: ignore Use camelCase" #-}
66
67 -- * Arbitrary instance
68
69 instance Arbitrary ClockTime where
70 arbitrary = TOD <$> arbitrary <*> fmap (`mod` (10^(12::Int))) arbitrary
71
72 instance (Arbitrary a, Ord a, Num a, Show a) =>
73 Arbitrary (Types.Positive a) where
74 arbitrary = do
75 (QuickCheck.Positive i) <- arbitrary
76 Types.mkPositive i
77
78 instance (Arbitrary a, Ord a, Num a, Show a) =>
79 Arbitrary (Types.NonNegative a) where
80 arbitrary = do
81 (QuickCheck.NonNegative i) <- arbitrary
82 Types.mkNonNegative i
83
84 instance (Arbitrary a, Ord a, Num a, Show a) =>
85 Arbitrary (Types.Negative a) where
86 arbitrary = do
87 (QuickCheck.Positive i) <- arbitrary
88 Types.mkNegative $ negate i
89
90 instance (Arbitrary a) => Arbitrary (Types.NonEmpty a) where
91 arbitrary = do
92 QuickCheck.NonEmpty lst <- arbitrary
93 Types.mkNonEmpty lst
94
95 $(genArbitrary ''AllocPolicy)
96
97 -- | Valid disk templates (depending on configure options).
98 allDiskTemplates :: [DiskTemplate]
99 allDiskTemplates = [minBound..maxBound]::[DiskTemplate]
100
101 -- | Custom 'Arbitrary' instance for 'DiskTemplate', which needs to
102 -- handle the case of file storage being disabled at configure time.
103 instance Arbitrary DiskTemplate where
104 arbitrary = elements allDiskTemplates
105
106 $(genArbitrary ''InstanceStatus)
107
108 $(genArbitrary ''MigrationMode)
109
110 $(genArbitrary ''VerifyOptionalChecks)
111
112 $(genArbitrary ''DdmSimple)
113
114 $(genArbitrary ''DdmFull)
115
116 $(genArbitrary ''CVErrorCode)
117
118 $(genArbitrary ''Hypervisor)
119
120 $(genArbitrary ''TagKind)
121
122 $(genArbitrary ''OobCommand)
123
124 -- | Valid storage types.
125 allStorageTypes :: [StorageType]
126 allStorageTypes = [minBound..maxBound]::[StorageType]
127
128 -- | Custom 'Arbitrary' instance for 'StorageType', which needs to
129 -- handle the case of file storage being disabled at configure time.
130 instance Arbitrary StorageType where
131 arbitrary = elements allStorageTypes
132
133 $(genArbitrary ''EvacMode)
134
135 $(genArbitrary ''FileDriver)
136
137 $(genArbitrary ''InstCreateMode)
138
139 $(genArbitrary ''RebootType)
140
141 $(genArbitrary ''ExportMode)
142
143 $(genArbitrary ''IAllocatorTestDir)
144
145 $(genArbitrary ''IAllocatorMode)
146
147 $(genArbitrary ''NICMode)
148
149 $(genArbitrary ''JobStatus)
150
151 $(genArbitrary ''FinalizedJobStatus)
152
153 instance Arbitrary JobId where
154 arbitrary = do
155 (Positive i) <- arbitrary
156 makeJobId i
157
158 $(genArbitrary ''JobIdDep)
159
160 $(genArbitrary ''JobDependency)
161
162 $(genArbitrary ''OpSubmitPriority)
163
164 $(genArbitrary ''OpStatus)
165
166 $(genArbitrary ''ELogType)
167
168 -- | Generates one element of a reason trail
169 genReasonElem :: Gen ReasonElem
170 genReasonElem = (,,) <$> genFQDN <*> genFQDN <*> arbitrary
171
172 -- | Generates a reason trail
173 genReasonTrail :: Gen ReasonTrail
174 genReasonTrail = do
175 size <- choose (0, 10)
176 vectorOf size genReasonElem
177
178 -- * Properties
179
180 prop_AllocPolicy_serialisation :: AllocPolicy -> Property
181 prop_AllocPolicy_serialisation = testSerialisation
182
183 -- | Test 'AllocPolicy' ordering is as expected.
184 case_AllocPolicy_order :: Assertion
185 case_AllocPolicy_order =
186 assertEqual "sort order" [ Types.AllocPreferred
187 , Types.AllocLastResort
188 , Types.AllocUnallocable
189 ] [minBound..maxBound]
190
191 prop_DiskTemplate_serialisation :: DiskTemplate -> Property
192 prop_DiskTemplate_serialisation = testSerialisation
193
194 prop_InstanceStatus_serialisation :: InstanceStatus -> Property
195 prop_InstanceStatus_serialisation = testSerialisation
196
197 -- | Tests building non-negative numbers.
198 prop_NonNeg_pass :: QuickCheck.NonNegative Int -> Property
199 prop_NonNeg_pass (QuickCheck.NonNegative i) =
200 case mkNonNegative i of
201 Bad msg -> failTest $ "Fail to build non-negative: " ++ msg
202 Ok nn -> fromNonNegative nn ==? i
203
204 -- | Tests building non-negative numbers.
205 prop_NonNeg_fail :: QuickCheck.Positive Int -> Property
206 prop_NonNeg_fail (QuickCheck.Positive i) =
207 case mkNonNegative (negate i)::Result (Types.NonNegative Int) of
208 Bad _ -> passTest
209 Ok nn -> failTest $ "Built non-negative number '" ++ show nn ++
210 "' from negative value " ++ show i
211
212 -- | Tests building positive numbers.
213 prop_Positive_pass :: QuickCheck.Positive Int -> Property
214 prop_Positive_pass (QuickCheck.Positive i) =
215 case mkPositive i of
216 Bad msg -> failTest $ "Fail to build positive: " ++ msg
217 Ok nn -> fromPositive nn ==? i
218
219 -- | Tests building positive numbers.
220 prop_Positive_fail :: QuickCheck.NonNegative Int -> Property
221 prop_Positive_fail (QuickCheck.NonNegative i) =
222 case mkPositive (negate i)::Result (Types.Positive Int) of
223 Bad _ -> passTest
224 Ok nn -> failTest $ "Built positive number '" ++ show nn ++
225 "' from negative or zero value " ++ show i
226
227 -- | Tests building negative numbers.
228 prop_Neg_pass :: QuickCheck.Positive Int -> Property
229 prop_Neg_pass (QuickCheck.Positive i) =
230 case mkNegative i' of
231 Bad msg -> failTest $ "Fail to build negative: " ++ msg
232 Ok nn -> fromNegative nn ==? i'
233 where i' = negate i
234
235 -- | Tests building negative numbers.
236 prop_Neg_fail :: QuickCheck.NonNegative Int -> Property
237 prop_Neg_fail (QuickCheck.NonNegative i) =
238 case mkNegative i::Result (Types.Negative Int) of
239 Bad _ -> passTest
240 Ok nn -> failTest $ "Built negative number '" ++ show nn ++
241 "' from non-negative value " ++ show i
242
243 -- | Tests building non-empty lists.
244 prop_NonEmpty_pass :: QuickCheck.NonEmptyList String -> Property
245 prop_NonEmpty_pass (QuickCheck.NonEmpty xs) =
246 case mkNonEmpty xs of
247 Bad msg -> failTest $ "Fail to build non-empty list: " ++ msg
248 Ok nn -> fromNonEmpty nn ==? xs
249
250 -- | Tests building positive numbers.
251 case_NonEmpty_fail :: Assertion
252 case_NonEmpty_fail =
253 assertEqual "building non-empty list from an empty list"
254 (Bad "Received empty value for non-empty list") (mkNonEmpty ([]::[Int]))
255
256 -- | Tests migration mode serialisation.
257 prop_MigrationMode_serialisation :: MigrationMode -> Property
258 prop_MigrationMode_serialisation = testSerialisation
259
260 -- | Tests verify optional checks serialisation.
261 prop_VerifyOptionalChecks_serialisation :: VerifyOptionalChecks -> Property
262 prop_VerifyOptionalChecks_serialisation = testSerialisation
263
264 -- | Tests 'DdmSimple' serialisation.
265 prop_DdmSimple_serialisation :: DdmSimple -> Property
266 prop_DdmSimple_serialisation = testSerialisation
267
268 -- | Tests 'DdmFull' serialisation.
269 prop_DdmFull_serialisation :: DdmFull -> Property
270 prop_DdmFull_serialisation = testSerialisation
271
272 -- | Tests 'CVErrorCode' serialisation.
273 prop_CVErrorCode_serialisation :: CVErrorCode -> Property
274 prop_CVErrorCode_serialisation = testSerialisation
275
276 -- | Tests equivalence with Python, based on Constants.hs code.
277 case_CVErrorCode_pyequiv :: Assertion
278 case_CVErrorCode_pyequiv = do
279 let all_py_codes = C.cvAllEcodesStrings
280 all_hs_codes = ConstantUtils.mkSet $
281 map Types.cVErrorCodeToRaw [minBound..maxBound]
282 assertEqual "for CVErrorCode equivalence" all_py_codes all_hs_codes
283
284 -- | Test 'Hypervisor' serialisation.
285 prop_Hypervisor_serialisation :: Hypervisor -> Property
286 prop_Hypervisor_serialisation = testSerialisation
287
288 -- | Test 'OobCommand' serialisation.
289 prop_OobCommand_serialisation :: OobCommand -> Property
290 prop_OobCommand_serialisation = testSerialisation
291
292 -- | Test 'StorageType' serialisation.
293 prop_StorageType_serialisation :: StorageType -> Property
294 prop_StorageType_serialisation = testSerialisation
295
296 -- | Test 'NodeEvacMode' serialisation.
297 prop_NodeEvacMode_serialisation :: EvacMode -> Property
298 prop_NodeEvacMode_serialisation = testSerialisation
299
300 -- | Test 'FileDriver' serialisation.
301 prop_FileDriver_serialisation :: FileDriver -> Property
302 prop_FileDriver_serialisation = testSerialisation
303
304 -- | Test 'InstCreate' serialisation.
305 prop_InstCreateMode_serialisation :: InstCreateMode -> Property
306 prop_InstCreateMode_serialisation = testSerialisation
307
308 -- | Test 'RebootType' serialisation.
309 prop_RebootType_serialisation :: RebootType -> Property
310 prop_RebootType_serialisation = testSerialisation
311
312 -- | Test 'ExportMode' serialisation.
313 prop_ExportMode_serialisation :: ExportMode -> Property
314 prop_ExportMode_serialisation = testSerialisation
315
316 -- | Test 'IAllocatorTestDir' serialisation.
317 prop_IAllocatorTestDir_serialisation :: IAllocatorTestDir -> Property
318 prop_IAllocatorTestDir_serialisation = testSerialisation
319
320 -- | Test 'IAllocatorMode' serialisation.
321 prop_IAllocatorMode_serialisation :: IAllocatorMode -> Property
322 prop_IAllocatorMode_serialisation = testSerialisation
323
324 -- | Tests equivalence with Python, based on Constants.hs code.
325 case_IAllocatorMode_pyequiv :: Assertion
326 case_IAllocatorMode_pyequiv = do
327 let all_py_codes = C.validIallocatorModes
328 all_hs_codes = ConstantUtils.mkSet $
329 map Types.iAllocatorModeToRaw [minBound..maxBound]
330 assertEqual "for IAllocatorMode equivalence" all_py_codes all_hs_codes
331
332 -- | Test 'NICMode' serialisation.
333 prop_NICMode_serialisation :: NICMode -> Property
334 prop_NICMode_serialisation = testSerialisation
335
336 -- | Test 'OpStatus' serialisation.
337 prop_OpStatus_serialization :: OpStatus -> Property
338 prop_OpStatus_serialization = testSerialisation
339
340 -- | Test 'JobStatus' serialisation.
341 prop_JobStatus_serialization :: JobStatus -> Property
342 prop_JobStatus_serialization = testSerialisation
343
344 -- | Test 'JobStatus' ordering is as expected.
345 case_JobStatus_order :: Assertion
346 case_JobStatus_order =
347 assertEqual "sort order" [ Types.JOB_STATUS_QUEUED
348 , Types.JOB_STATUS_WAITING
349 , Types.JOB_STATUS_CANCELING
350 , Types.JOB_STATUS_RUNNING
351 , Types.JOB_STATUS_CANCELED
352 , Types.JOB_STATUS_SUCCESS
353 , Types.JOB_STATUS_ERROR
354 ] [minBound..maxBound]
355
356 -- | Tests equivalence with Python, based on Constants.hs code.
357 case_NICMode_pyequiv :: Assertion
358 case_NICMode_pyequiv = do
359 let all_py_codes = C.nicValidModes
360 all_hs_codes = ConstantUtils.mkSet $
361 map Types.nICModeToRaw [minBound..maxBound]
362 assertEqual "for NICMode equivalence" all_py_codes all_hs_codes
363
364 -- | Test 'FinalizedJobStatus' serialisation.
365 prop_FinalizedJobStatus_serialisation :: FinalizedJobStatus -> Property
366 prop_FinalizedJobStatus_serialisation = testSerialisation
367
368 -- | Tests equivalence with Python, based on Constants.hs code.
369 case_FinalizedJobStatus_pyequiv :: Assertion
370 case_FinalizedJobStatus_pyequiv = do
371 let all_py_codes = C.jobsFinalized
372 all_hs_codes = ConstantUtils.mkSet $
373 map Types.finalizedJobStatusToRaw [minBound..maxBound]
374 assertEqual "for FinalizedJobStatus equivalence" all_py_codes all_hs_codes
375
376 -- | Tests JobId serialisation (both from string and ints).
377 prop_JobId_serialisation :: JobId -> Property
378 prop_JobId_serialisation jid =
379 conjoin [ testSerialisation jid
380 , (J.readJSON . J.showJSON . show $ fromJobId jid) ==? J.Ok jid
381 , case (fromJVal . J.showJSON . negate $
382 fromJobId jid)::Result JobId of
383 Bad _ -> passTest
384 Ok jid' -> failTest $ "Parsed negative job id as id " ++
385 show (fromJobId jid')
386 ]
387
388 -- | Tests that fractional job IDs are not accepted.
389 prop_JobId_fractional :: Property
390 prop_JobId_fractional =
391 forAll (arbitrary `suchThat`
392 (\d -> fromIntegral (truncate d::Int) /= d)) $ \d ->
393 case J.readJSON (J.showJSON (d::Double)) of
394 J.Error _ -> passTest
395 J.Ok jid -> failTest $ "Parsed fractional value " ++ show d ++
396 " as job id " ++ show (fromJobId jid)
397
398 -- | Tests that a job ID is not parseable from \"bad\" JSON values.
399 case_JobId_BadTypes :: Assertion
400 case_JobId_BadTypes = do
401 let helper jsval = case J.readJSON jsval of
402 J.Error _ -> return ()
403 J.Ok jid -> assertFailure $ "Parsed " ++ show jsval
404 ++ " as job id " ++ show (fromJobId jid)
405 helper J.JSNull
406 helper (J.JSBool True)
407 helper (J.JSBool False)
408 helper (J.JSArray [])
409
410 -- | Test 'JobDependency' serialisation.
411 prop_JobDependency_serialisation :: JobDependency -> Property
412 prop_JobDependency_serialisation = testSerialisation
413
414 -- | Test 'OpSubmitPriority' serialisation.
415 prop_OpSubmitPriority_serialisation :: OpSubmitPriority -> Property
416 prop_OpSubmitPriority_serialisation = testSerialisation
417
418 -- | Tests string formatting for 'OpSubmitPriority'.
419 prop_OpSubmitPriority_string :: OpSubmitPriority -> Property
420 prop_OpSubmitPriority_string prio =
421 parseSubmitPriority (fmtSubmitPriority prio) ==? Just prio
422
423 -- | Test 'ELogType' serialisation.
424 prop_ELogType_serialisation :: ELogType -> Property
425 prop_ELogType_serialisation = testSerialisation
426
427 testSuite "Types"
428 [ 'prop_AllocPolicy_serialisation
429 , 'case_AllocPolicy_order
430 , 'prop_DiskTemplate_serialisation
431 , 'prop_InstanceStatus_serialisation
432 , 'prop_NonNeg_pass
433 , 'prop_NonNeg_fail
434 , 'prop_Positive_pass
435 , 'prop_Positive_fail
436 , 'prop_Neg_pass
437 , 'prop_Neg_fail
438 , 'prop_NonEmpty_pass
439 , 'case_NonEmpty_fail
440 , 'prop_MigrationMode_serialisation
441 , 'prop_VerifyOptionalChecks_serialisation
442 , 'prop_DdmSimple_serialisation
443 , 'prop_DdmFull_serialisation
444 , 'prop_CVErrorCode_serialisation
445 , 'case_CVErrorCode_pyequiv
446 , 'prop_Hypervisor_serialisation
447 , 'prop_OobCommand_serialisation
448 , 'prop_StorageType_serialisation
449 , 'prop_NodeEvacMode_serialisation
450 , 'prop_FileDriver_serialisation
451 , 'prop_InstCreateMode_serialisation
452 , 'prop_RebootType_serialisation
453 , 'prop_ExportMode_serialisation
454 , 'prop_IAllocatorTestDir_serialisation
455 , 'prop_IAllocatorMode_serialisation
456 , 'case_IAllocatorMode_pyequiv
457 , 'prop_NICMode_serialisation
458 , 'prop_OpStatus_serialization
459 , 'prop_JobStatus_serialization
460 , 'case_JobStatus_order
461 , 'case_NICMode_pyequiv
462 , 'prop_FinalizedJobStatus_serialisation
463 , 'case_FinalizedJobStatus_pyequiv
464 , 'prop_JobId_serialisation
465 , 'prop_JobId_fractional
466 , 'case_JobId_BadTypes
467 , 'prop_JobDependency_serialisation
468 , 'prop_OpSubmitPriority_serialisation
469 , 'prop_OpSubmitPriority_string
470 , 'prop_ELogType_serialisation
471 ]