root / htest / Test / Ganeti / Types.hs @ 6d558717
History | View | Annotate | Download (6.7 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 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 |
, InstanceStatus(..) |
34 |
, NonEmpty(..) |
35 |
, Hypervisor(..) |
36 |
) where |
37 |
|
38 |
import Data.List (sort) |
39 |
import Test.QuickCheck as QuickCheck hiding (Result) |
40 |
import Test.HUnit |
41 |
|
42 |
import Test.Ganeti.TestHelper |
43 |
import Test.Ganeti.TestCommon |
44 |
|
45 |
import Ganeti.BasicTypes |
46 |
import qualified Ganeti.Constants as C |
47 |
import Ganeti.Types as Types |
48 |
|
49 |
{-# ANN module "HLint: ignore Use camelCase" #-} |
50 |
|
51 |
-- * Arbitrary instance |
52 |
|
53 |
instance (Arbitrary a, Ord a, Num a, Show a) => |
54 |
Arbitrary (Types.Positive a) where |
55 |
arbitrary = do |
56 |
(QuickCheck.Positive i) <- arbitrary |
57 |
Types.mkPositive i |
58 |
|
59 |
instance (Arbitrary a, Ord a, Num a, Show a) => |
60 |
Arbitrary (Types.NonNegative a) where |
61 |
arbitrary = do |
62 |
(QuickCheck.NonNegative i) <- arbitrary |
63 |
Types.mkNonNegative i |
64 |
|
65 |
instance (Arbitrary a) => Arbitrary (Types.NonEmpty a) where |
66 |
arbitrary = do |
67 |
QuickCheck.NonEmpty lst <- arbitrary |
68 |
Types.mkNonEmpty lst |
69 |
|
70 |
$(genArbitrary ''AllocPolicy) |
71 |
|
72 |
$(genArbitrary ''DiskTemplate) |
73 |
|
74 |
$(genArbitrary ''InstanceStatus) |
75 |
|
76 |
$(genArbitrary ''MigrationMode) |
77 |
|
78 |
$(genArbitrary ''VerifyOptionalChecks) |
79 |
|
80 |
$(genArbitrary ''DdmSimple) |
81 |
|
82 |
$(genArbitrary ''CVErrorCode) |
83 |
|
84 |
$(genArbitrary ''Hypervisor) |
85 |
|
86 |
$(genArbitrary ''OobCommand) |
87 |
|
88 |
$(genArbitrary ''StorageType) |
89 |
|
90 |
$(genArbitrary ''NodeEvacMode) |
91 |
|
92 |
$(genArbitrary ''FileDriver) |
93 |
|
94 |
$(genArbitrary ''InstCreateMode) |
95 |
|
96 |
-- * Properties |
97 |
|
98 |
prop_AllocPolicy_serialisation :: AllocPolicy -> Property |
99 |
prop_AllocPolicy_serialisation = testSerialisation |
100 |
|
101 |
prop_DiskTemplate_serialisation :: DiskTemplate -> Property |
102 |
prop_DiskTemplate_serialisation = testSerialisation |
103 |
|
104 |
prop_InstanceStatus_serialisation :: InstanceStatus -> Property |
105 |
prop_InstanceStatus_serialisation = testSerialisation |
106 |
|
107 |
-- | Tests building non-negative numbers. |
108 |
prop_NonNeg_pass :: QuickCheck.NonNegative Int -> Property |
109 |
prop_NonNeg_pass (QuickCheck.NonNegative i) = |
110 |
case mkNonNegative i of |
111 |
Bad msg -> failTest $ "Fail to build non-negative: " ++ msg |
112 |
Ok nn -> fromNonNegative nn ==? i |
113 |
|
114 |
-- | Tests building non-negative numbers. |
115 |
prop_NonNeg_fail :: QuickCheck.Positive Int -> Property |
116 |
prop_NonNeg_fail (QuickCheck.Positive i) = |
117 |
case mkNonNegative (negate i)::Result (Types.NonNegative Int) of |
118 |
Bad _ -> passTest |
119 |
Ok nn -> failTest $ "Built non-negative number '" ++ show nn ++ |
120 |
"' from negative value " ++ show i |
121 |
|
122 |
-- | Tests building positive numbers. |
123 |
prop_Positive_pass :: QuickCheck.Positive Int -> Property |
124 |
prop_Positive_pass (QuickCheck.Positive i) = |
125 |
case mkPositive i of |
126 |
Bad msg -> failTest $ "Fail to build positive: " ++ msg |
127 |
Ok nn -> fromPositive nn ==? i |
128 |
|
129 |
-- | Tests building positive numbers. |
130 |
prop_Positive_fail :: QuickCheck.NonNegative Int -> Property |
131 |
prop_Positive_fail (QuickCheck.NonNegative i) = |
132 |
case mkPositive (negate i)::Result (Types.Positive Int) of |
133 |
Bad _ -> passTest |
134 |
Ok nn -> failTest $ "Built positive number '" ++ show nn ++ |
135 |
"' from negative or zero value " ++ show i |
136 |
|
137 |
-- | Tests building non-empty lists. |
138 |
prop_NonEmpty_pass :: QuickCheck.NonEmptyList String -> Property |
139 |
prop_NonEmpty_pass (QuickCheck.NonEmpty xs) = |
140 |
case mkNonEmpty xs of |
141 |
Bad msg -> failTest $ "Fail to build non-empty list: " ++ msg |
142 |
Ok nn -> fromNonEmpty nn ==? xs |
143 |
|
144 |
-- | Tests building positive numbers. |
145 |
case_NonEmpty_fail :: Assertion |
146 |
case_NonEmpty_fail = |
147 |
assertEqual "building non-empty list from an empty list" |
148 |
(Bad "Received empty value for non-empty list") (mkNonEmpty ([]::[Int])) |
149 |
|
150 |
-- | Tests migration mode serialisation. |
151 |
prop_MigrationMode_serialisation :: MigrationMode -> Property |
152 |
prop_MigrationMode_serialisation = testSerialisation |
153 |
|
154 |
-- | Tests verify optional checks serialisation. |
155 |
prop_VerifyOptionalChecks_serialisation :: VerifyOptionalChecks -> Property |
156 |
prop_VerifyOptionalChecks_serialisation = testSerialisation |
157 |
|
158 |
-- | Tests 'DdmSimple' serialisation. |
159 |
prop_DdmSimple_serialisation :: DdmSimple -> Property |
160 |
prop_DdmSimple_serialisation = testSerialisation |
161 |
|
162 |
-- | Tests 'CVErrorCode' serialisation. |
163 |
prop_CVErrorCode_serialisation :: CVErrorCode -> Property |
164 |
prop_CVErrorCode_serialisation = testSerialisation |
165 |
|
166 |
-- | Tests equivalence with Python, based on Constants.hs code. |
167 |
case_CVErrorCode_pyequiv :: Assertion |
168 |
case_CVErrorCode_pyequiv = do |
169 |
let all_py_codes = sort C.cvAllEcodesStrings |
170 |
all_hs_codes = sort $ map Types.cVErrorCodeToRaw [minBound..maxBound] |
171 |
assertEqual "for CVErrorCode equivalence" all_py_codes all_hs_codes |
172 |
|
173 |
-- | Test 'Hypervisor' serialisation. |
174 |
prop_Hypervisor_serialisation :: Hypervisor -> Property |
175 |
prop_Hypervisor_serialisation = testSerialisation |
176 |
|
177 |
-- | Test 'OobCommand' serialisation. |
178 |
prop_OobCommand_serialisation :: OobCommand -> Property |
179 |
prop_OobCommand_serialisation = testSerialisation |
180 |
|
181 |
-- | Test 'StorageType' serialisation. |
182 |
prop_StorageType_serialisation :: StorageType -> Property |
183 |
prop_StorageType_serialisation = testSerialisation |
184 |
|
185 |
-- | Test 'NodeEvacMode' serialisation. |
186 |
prop_NodeEvacMode_serialisation :: NodeEvacMode -> Property |
187 |
prop_NodeEvacMode_serialisation = testSerialisation |
188 |
|
189 |
-- | Test 'FileDriver' serialisation. |
190 |
prop_FileDriver_serialisation :: FileDriver -> Property |
191 |
prop_FileDriver_serialisation = testSerialisation |
192 |
|
193 |
-- | Test 'InstCreate' serialisation. |
194 |
prop_InstCreateMode_serialisation :: InstCreateMode -> Property |
195 |
prop_InstCreateMode_serialisation = testSerialisation |
196 |
|
197 |
testSuite "Types" |
198 |
[ 'prop_AllocPolicy_serialisation |
199 |
, 'prop_DiskTemplate_serialisation |
200 |
, 'prop_InstanceStatus_serialisation |
201 |
, 'prop_NonNeg_pass |
202 |
, 'prop_NonNeg_fail |
203 |
, 'prop_Positive_pass |
204 |
, 'prop_Positive_fail |
205 |
, 'prop_NonEmpty_pass |
206 |
, 'case_NonEmpty_fail |
207 |
, 'prop_MigrationMode_serialisation |
208 |
, 'prop_VerifyOptionalChecks_serialisation |
209 |
, 'prop_DdmSimple_serialisation |
210 |
, 'prop_CVErrorCode_serialisation |
211 |
, 'case_CVErrorCode_pyequiv |
212 |
, 'prop_Hypervisor_serialisation |
213 |
, 'prop_OobCommand_serialisation |
214 |
, 'prop_StorageType_serialisation |
215 |
, 'prop_NodeEvacMode_serialisation |
216 |
, 'prop_FileDriver_serialisation |
217 |
, 'prop_InstCreateMode_serialisation |
218 |
] |