## root / test / hs / Test / Ganeti / Types.hs @ 2fe1e043

History | View | Annotate | Download (14.5 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 (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 = [minBound..maxBound]::[DiskTemplate] |

85 | |

86 |
-- | Custom 'Arbitrary' instance for 'DiskTemplate', which needs to |

87 |
-- handle the case of file storage being disabled at configure time. |

88 |
instance Arbitrary DiskTemplate where |

89 |
arbitrary = elements allDiskTemplates |

90 | |

91 |
$(genArbitrary ''InstanceStatus) |

92 | |

93 |
$(genArbitrary ''MigrationMode) |

94 | |

95 |
$(genArbitrary ''VerifyOptionalChecks) |

96 | |

97 |
$(genArbitrary ''DdmSimple) |

98 | |

99 |
$(genArbitrary ''DdmFull) |

100 | |

101 |
$(genArbitrary ''CVErrorCode) |

102 | |

103 |
$(genArbitrary ''Hypervisor) |

104 | |

105 |
$(genArbitrary ''OobCommand) |

106 | |

107 |
-- | Valid storage types. |

108 |
allStorageTypes :: [StorageType] |

109 |
allStorageTypes = [minBound..maxBound]::[StorageType] |

110 | |

111 |
-- | Custom 'Arbitrary' instance for 'StorageType', which needs to |

112 |
-- handle the case of file storage being disabled at configure time. |

113 |
instance Arbitrary StorageType where |

114 |
arbitrary = elements allStorageTypes |

115 | |

116 |
$(genArbitrary ''NodeEvacMode) |

117 | |

118 |
$(genArbitrary ''FileDriver) |

119 | |

120 |
$(genArbitrary ''InstCreateMode) |

121 | |

122 |
$(genArbitrary ''RebootType) |

123 | |

124 |
$(genArbitrary ''ExportMode) |

125 | |

126 |
$(genArbitrary ''IAllocatorTestDir) |

127 | |

128 |
$(genArbitrary ''IAllocatorMode) |

129 | |

130 |
$(genArbitrary ''NICMode) |

131 | |

132 |
$(genArbitrary ''JobStatus) |

133 | |

134 |
$(genArbitrary ''FinalizedJobStatus) |

135 | |

136 |
instance Arbitrary JobId where |

137 |
arbitrary = do |

138 |
(Positive i) <- arbitrary |

139 |
makeJobId i |

140 | |

141 |
$(genArbitrary ''JobIdDep) |

142 | |

143 |
$(genArbitrary ''JobDependency) |

144 | |

145 |
$(genArbitrary ''OpSubmitPriority) |

146 | |

147 |
$(genArbitrary ''OpStatus) |

148 | |

149 |
$(genArbitrary ''ELogType) |

150 | |

151 |
-- * Properties |

152 | |

153 |
prop_AllocPolicy_serialisation :: AllocPolicy -> Property |

154 |
prop_AllocPolicy_serialisation = testSerialisation |

155 | |

156 |
-- | Test 'AllocPolicy' ordering is as expected. |

157 |
case_AllocPolicy_order :: Assertion |

158 |
case_AllocPolicy_order = |

159 |
assertEqual "sort order" [ Types.AllocPreferred |

160 |
, Types.AllocLastResort |

161 |
, Types.AllocUnallocable |

162 |
] [minBound..maxBound] |

163 | |

164 |
prop_DiskTemplate_serialisation :: DiskTemplate -> Property |

165 |
prop_DiskTemplate_serialisation = testSerialisation |

166 | |

167 |
prop_InstanceStatus_serialisation :: InstanceStatus -> Property |

168 |
prop_InstanceStatus_serialisation = testSerialisation |

169 | |

170 |
-- | Tests building non-negative numbers. |

171 |
prop_NonNeg_pass :: QuickCheck.NonNegative Int -> Property |

172 |
prop_NonNeg_pass (QuickCheck.NonNegative i) = |

173 |
case mkNonNegative i of |

174 |
Bad msg -> failTest $ "Fail to build non-negative: " ++ msg |

175 |
Ok nn -> fromNonNegative nn ==? i |

176 | |

177 |
-- | Tests building non-negative numbers. |

178 |
prop_NonNeg_fail :: QuickCheck.Positive Int -> Property |

179 |
prop_NonNeg_fail (QuickCheck.Positive i) = |

180 |
case mkNonNegative (negate i)::Result (Types.NonNegative Int) of |

181 |
Bad _ -> passTest |

182 |
Ok nn -> failTest $ "Built non-negative number '" ++ show nn ++ |

183 |
"' from negative value " ++ show i |

184 | |

185 |
-- | Tests building positive numbers. |

186 |
prop_Positive_pass :: QuickCheck.Positive Int -> Property |

187 |
prop_Positive_pass (QuickCheck.Positive i) = |

188 |
case mkPositive i of |

189 |
Bad msg -> failTest $ "Fail to build positive: " ++ msg |

190 |
Ok nn -> fromPositive nn ==? i |

191 | |

192 |
-- | Tests building positive numbers. |

193 |
prop_Positive_fail :: QuickCheck.NonNegative Int -> Property |

194 |
prop_Positive_fail (QuickCheck.NonNegative i) = |

195 |
case mkPositive (negate i)::Result (Types.Positive Int) of |

196 |
Bad _ -> passTest |

197 |
Ok nn -> failTest $ "Built positive number '" ++ show nn ++ |

198 |
"' from negative or zero value " ++ show i |

199 | |

200 |
-- | Tests building negative numbers. |

201 |
prop_Neg_pass :: QuickCheck.Positive Int -> Property |

202 |
prop_Neg_pass (QuickCheck.Positive i) = |

203 |
case mkNegative i' of |

204 |
Bad msg -> failTest $ "Fail to build negative: " ++ msg |

205 |
Ok nn -> fromNegative nn ==? i' |

206 |
where i' = negate i |

207 | |

208 |
-- | Tests building negative numbers. |

209 |
prop_Neg_fail :: QuickCheck.NonNegative Int -> Property |

210 |
prop_Neg_fail (QuickCheck.NonNegative i) = |

211 |
case mkNegative i::Result (Types.Negative Int) of |

212 |
Bad _ -> passTest |

213 |
Ok nn -> failTest $ "Built negative number '" ++ show nn ++ |

214 |
"' from non-negative value " ++ show i |

215 | |

216 |
-- | Tests building non-empty lists. |

217 |
prop_NonEmpty_pass :: QuickCheck.NonEmptyList String -> Property |

218 |
prop_NonEmpty_pass (QuickCheck.NonEmpty xs) = |

219 |
case mkNonEmpty xs of |

220 |
Bad msg -> failTest $ "Fail to build non-empty list: " ++ msg |

221 |
Ok nn -> fromNonEmpty nn ==? xs |

222 | |

223 |
-- | Tests building positive numbers. |

224 |
case_NonEmpty_fail :: Assertion |

225 |
case_NonEmpty_fail = |

226 |
assertEqual "building non-empty list from an empty list" |

227 |
(Bad "Received empty value for non-empty list") (mkNonEmpty ([]::[Int])) |

228 | |

229 |
-- | Tests migration mode serialisation. |

230 |
prop_MigrationMode_serialisation :: MigrationMode -> Property |

231 |
prop_MigrationMode_serialisation = testSerialisation |

232 | |

233 |
-- | Tests verify optional checks serialisation. |

234 |
prop_VerifyOptionalChecks_serialisation :: VerifyOptionalChecks -> Property |

235 |
prop_VerifyOptionalChecks_serialisation = testSerialisation |

236 | |

237 |
-- | Tests 'DdmSimple' serialisation. |

238 |
prop_DdmSimple_serialisation :: DdmSimple -> Property |

239 |
prop_DdmSimple_serialisation = testSerialisation |

240 | |

241 |
-- | Tests 'DdmFull' serialisation. |

242 |
prop_DdmFull_serialisation :: DdmFull -> Property |

243 |
prop_DdmFull_serialisation = testSerialisation |

244 | |

245 |
-- | Tests 'CVErrorCode' serialisation. |

246 |
prop_CVErrorCode_serialisation :: CVErrorCode -> Property |

247 |
prop_CVErrorCode_serialisation = testSerialisation |

248 | |

249 |
-- | Tests equivalence with Python, based on Constants.hs code. |

250 |
case_CVErrorCode_pyequiv :: Assertion |

251 |
case_CVErrorCode_pyequiv = do |

252 |
let all_py_codes = sort C.cvAllEcodesStrings |

253 |
all_hs_codes = sort $ map Types.cVErrorCodeToRaw [minBound..maxBound] |

254 |
assertEqual "for CVErrorCode equivalence" all_py_codes all_hs_codes |

255 | |

256 |
-- | Test 'Hypervisor' serialisation. |

257 |
prop_Hypervisor_serialisation :: Hypervisor -> Property |

258 |
prop_Hypervisor_serialisation = testSerialisation |

259 | |

260 |
-- | Test 'OobCommand' serialisation. |

261 |
prop_OobCommand_serialisation :: OobCommand -> Property |

262 |
prop_OobCommand_serialisation = testSerialisation |

263 | |

264 |
-- | Test 'StorageType' serialisation. |

265 |
prop_StorageType_serialisation :: StorageType -> Property |

266 |
prop_StorageType_serialisation = testSerialisation |

267 | |

268 |
-- | Test 'NodeEvacMode' serialisation. |

269 |
prop_NodeEvacMode_serialisation :: NodeEvacMode -> Property |

270 |
prop_NodeEvacMode_serialisation = testSerialisation |

271 | |

272 |
-- | Test 'FileDriver' serialisation. |

273 |
prop_FileDriver_serialisation :: FileDriver -> Property |

274 |
prop_FileDriver_serialisation = testSerialisation |

275 | |

276 |
-- | Test 'InstCreate' serialisation. |

277 |
prop_InstCreateMode_serialisation :: InstCreateMode -> Property |

278 |
prop_InstCreateMode_serialisation = testSerialisation |

279 | |

280 |
-- | Test 'RebootType' serialisation. |

281 |
prop_RebootType_serialisation :: RebootType -> Property |

282 |
prop_RebootType_serialisation = testSerialisation |

283 | |

284 |
-- | Test 'ExportMode' serialisation. |

285 |
prop_ExportMode_serialisation :: ExportMode -> Property |

286 |
prop_ExportMode_serialisation = testSerialisation |

287 | |

288 |
-- | Test 'IAllocatorTestDir' serialisation. |

289 |
prop_IAllocatorTestDir_serialisation :: IAllocatorTestDir -> Property |

290 |
prop_IAllocatorTestDir_serialisation = testSerialisation |

291 | |

292 |
-- | Test 'IAllocatorMode' serialisation. |

293 |
prop_IAllocatorMode_serialisation :: IAllocatorMode -> Property |

294 |
prop_IAllocatorMode_serialisation = testSerialisation |

295 | |

296 |
-- | Tests equivalence with Python, based on Constants.hs code. |

297 |
case_IAllocatorMode_pyequiv :: Assertion |

298 |
case_IAllocatorMode_pyequiv = do |

299 |
let all_py_codes = sort C.validIallocatorModes |

300 |
all_hs_codes = sort $ map Types.iAllocatorModeToRaw [minBound..maxBound] |

301 |
assertEqual "for IAllocatorMode equivalence" all_py_codes all_hs_codes |

302 | |

303 |
-- | Test 'NICMode' serialisation. |

304 |
prop_NICMode_serialisation :: NICMode -> Property |

305 |
prop_NICMode_serialisation = testSerialisation |

306 | |

307 |
-- | Test 'OpStatus' serialisation. |

308 |
prop_OpStatus_serialization :: OpStatus -> Property |

309 |
prop_OpStatus_serialization = testSerialisation |

310 | |

311 |
-- | Test 'JobStatus' serialisation. |

312 |
prop_JobStatus_serialization :: JobStatus -> Property |

313 |
prop_JobStatus_serialization = testSerialisation |

314 | |

315 |
-- | Test 'JobStatus' ordering is as expected. |

316 |
case_JobStatus_order :: Assertion |

317 |
case_JobStatus_order = |

318 |
assertEqual "sort order" [ Types.JOB_STATUS_QUEUED |

319 |
, Types.JOB_STATUS_WAITING |

320 |
, Types.JOB_STATUS_CANCELING |

321 |
, Types.JOB_STATUS_RUNNING |

322 |
, Types.JOB_STATUS_CANCELED |

323 |
, Types.JOB_STATUS_SUCCESS |

324 |
, Types.JOB_STATUS_ERROR |

325 |
] [minBound..maxBound] |

326 | |

327 |
-- | Tests equivalence with Python, based on Constants.hs code. |

328 |
case_NICMode_pyequiv :: Assertion |

329 |
case_NICMode_pyequiv = do |

330 |
let all_py_codes = sort C.nicValidModes |

331 |
all_hs_codes = sort $ map Types.nICModeToRaw [minBound..maxBound] |

332 |
assertEqual "for NICMode equivalence" all_py_codes all_hs_codes |

333 | |

334 |
-- | Test 'FinalizedJobStatus' serialisation. |

335 |
prop_FinalizedJobStatus_serialisation :: FinalizedJobStatus -> Property |

336 |
prop_FinalizedJobStatus_serialisation = testSerialisation |

337 | |

338 |
-- | Tests equivalence with Python, based on Constants.hs code. |

339 |
case_FinalizedJobStatus_pyequiv :: Assertion |

340 |
case_FinalizedJobStatus_pyequiv = do |

341 |
let all_py_codes = sort C.jobsFinalized |

342 |
all_hs_codes = sort $ map Types.finalizedJobStatusToRaw |

343 |
[minBound..maxBound] |

344 |
assertEqual "for FinalizedJobStatus equivalence" all_py_codes all_hs_codes |

345 | |

346 |
-- | Tests JobId serialisation (both from string and ints). |

347 |
prop_JobId_serialisation :: JobId -> Property |

348 |
prop_JobId_serialisation jid = |

349 |
conjoin [ testSerialisation jid |

350 |
, (J.readJSON . J.showJSON . show $ fromJobId jid) ==? J.Ok jid |

351 |
, case (fromJVal . J.showJSON . negate $ |

352 |
fromJobId jid)::Result JobId of |

353 |
Bad _ -> passTest |

354 |
Ok jid' -> failTest $ "Parsed negative job id as id " ++ |

355 |
show (fromJobId jid') |

356 |
] |

357 | |

358 |
-- | Tests that fractional job IDs are not accepted. |

359 |
prop_JobId_fractional :: Property |

360 |
prop_JobId_fractional = |

361 |
forAll (arbitrary `suchThat` |

362 |
(\d -> fromIntegral (truncate d::Int) /= d)) $ \d -> |

363 |
case J.readJSON (J.showJSON (d::Double)) of |

364 |
J.Error _ -> passTest |

365 |
J.Ok jid -> failTest $ "Parsed fractional value " ++ show d ++ |

366 |
" as job id " ++ show (fromJobId jid) |

367 | |

368 |
-- | Tests that a job ID is not parseable from \"bad\" JSON values. |

369 |
case_JobId_BadTypes :: Assertion |

370 |
case_JobId_BadTypes = do |

371 |
let helper jsval = case J.readJSON jsval of |

372 |
J.Error _ -> return () |

373 |
J.Ok jid -> assertFailure $ "Parsed " ++ show jsval |

374 |
++ " as job id " ++ show (fromJobId jid) |

375 |
helper J.JSNull |

376 |
helper (J.JSBool True) |

377 |
helper (J.JSBool False) |

378 |
helper (J.JSArray []) |

379 | |

380 |
-- | Test 'JobDependency' serialisation. |

381 |
prop_JobDependency_serialisation :: JobDependency -> Property |

382 |
prop_JobDependency_serialisation = testSerialisation |

383 | |

384 |
-- | Test 'OpSubmitPriority' serialisation. |

385 |
prop_OpSubmitPriority_serialisation :: OpSubmitPriority -> Property |

386 |
prop_OpSubmitPriority_serialisation = testSerialisation |

387 | |

388 |
-- | Tests string formatting for 'OpSubmitPriority'. |

389 |
prop_OpSubmitPriority_string :: OpSubmitPriority -> Property |

390 |
prop_OpSubmitPriority_string prio = |

391 |
parseSubmitPriority (fmtSubmitPriority prio) ==? Just prio |

392 | |

393 |
-- | Test 'ELogType' serialisation. |

394 |
prop_ELogType_serialisation :: ELogType -> Property |

395 |
prop_ELogType_serialisation = testSerialisation |

396 | |

397 |
testSuite "Types" |

398 |
[ 'prop_AllocPolicy_serialisation |

399 |
, 'case_AllocPolicy_order |

400 |
, 'prop_DiskTemplate_serialisation |

401 |
, 'prop_InstanceStatus_serialisation |

402 |
, 'prop_NonNeg_pass |

403 |
, 'prop_NonNeg_fail |

404 |
, 'prop_Positive_pass |

405 |
, 'prop_Positive_fail |

406 |
, 'prop_Neg_pass |

407 |
, 'prop_Neg_fail |

408 |
, 'prop_NonEmpty_pass |

409 |
, 'case_NonEmpty_fail |

410 |
, 'prop_MigrationMode_serialisation |

411 |
, 'prop_VerifyOptionalChecks_serialisation |

412 |
, 'prop_DdmSimple_serialisation |

413 |
, 'prop_DdmFull_serialisation |

414 |
, 'prop_CVErrorCode_serialisation |

415 |
, 'case_CVErrorCode_pyequiv |

416 |
, 'prop_Hypervisor_serialisation |

417 |
, 'prop_OobCommand_serialisation |

418 |
, 'prop_StorageType_serialisation |

419 |
, 'prop_NodeEvacMode_serialisation |

420 |
, 'prop_FileDriver_serialisation |

421 |
, 'prop_InstCreateMode_serialisation |

422 |
, 'prop_RebootType_serialisation |

423 |
, 'prop_ExportMode_serialisation |

424 |
, 'prop_IAllocatorTestDir_serialisation |

425 |
, 'prop_IAllocatorMode_serialisation |

426 |
, 'case_IAllocatorMode_pyequiv |

427 |
, 'prop_NICMode_serialisation |

428 |
, 'prop_OpStatus_serialization |

429 |
, 'prop_JobStatus_serialization |

430 |
, 'case_JobStatus_order |

431 |
, 'case_NICMode_pyequiv |

432 |
, 'prop_FinalizedJobStatus_serialisation |

433 |
, 'case_FinalizedJobStatus_pyequiv |

434 |
, 'prop_JobId_serialisation |

435 |
, 'prop_JobId_fractional |

436 |
, 'case_JobId_BadTypes |

437 |
, 'prop_JobDependency_serialisation |

438 |
, 'prop_OpSubmitPriority_serialisation |

439 |
, 'prop_OpSubmitPriority_string |

440 |
, 'prop_ELogType_serialisation |

441 |
] |