Statistics
| Branch: | Tag: | Revision:

root / htest / Test / Ganeti / Types.hs @ 497beee2

History | View | Annotate | Download (8.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 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 ''DdmFull)
83

    
84
$(genArbitrary ''CVErrorCode)
85

    
86
$(genArbitrary ''Hypervisor)
87

    
88
$(genArbitrary ''OobCommand)
89

    
90
$(genArbitrary ''StorageType)
91

    
92
$(genArbitrary ''NodeEvacMode)
93

    
94
$(genArbitrary ''FileDriver)
95

    
96
$(genArbitrary ''InstCreateMode)
97

    
98
$(genArbitrary ''RebootType)
99

    
100
$(genArbitrary ''ExportMode)
101

    
102
$(genArbitrary ''IAllocatorTestDir)
103

    
104
$(genArbitrary ''IAllocatorMode)
105

    
106
$(genArbitrary ''NICMode)
107

    
108
-- * Properties
109

    
110
prop_AllocPolicy_serialisation :: AllocPolicy -> Property
111
prop_AllocPolicy_serialisation = testSerialisation
112

    
113
prop_DiskTemplate_serialisation :: DiskTemplate -> Property
114
prop_DiskTemplate_serialisation = testSerialisation
115

    
116
prop_InstanceStatus_serialisation :: InstanceStatus -> Property
117
prop_InstanceStatus_serialisation = testSerialisation
118

    
119
-- | Tests building non-negative numbers.
120
prop_NonNeg_pass :: QuickCheck.NonNegative Int -> Property
121
prop_NonNeg_pass (QuickCheck.NonNegative i) =
122
  case mkNonNegative i of
123
    Bad msg -> failTest $ "Fail to build non-negative: " ++ msg
124
    Ok nn -> fromNonNegative nn ==? i
125

    
126
-- | Tests building non-negative numbers.
127
prop_NonNeg_fail :: QuickCheck.Positive Int -> Property
128
prop_NonNeg_fail (QuickCheck.Positive i) =
129
  case mkNonNegative (negate i)::Result (Types.NonNegative Int) of
130
    Bad _ -> passTest
131
    Ok nn -> failTest $ "Built non-negative number '" ++ show nn ++
132
             "' from negative value " ++ show i
133

    
134
-- | Tests building positive numbers.
135
prop_Positive_pass :: QuickCheck.Positive Int -> Property
136
prop_Positive_pass (QuickCheck.Positive i) =
137
  case mkPositive i of
138
    Bad msg -> failTest $ "Fail to build positive: " ++ msg
139
    Ok nn -> fromPositive nn ==? i
140

    
141
-- | Tests building positive numbers.
142
prop_Positive_fail :: QuickCheck.NonNegative Int -> Property
143
prop_Positive_fail (QuickCheck.NonNegative i) =
144
  case mkPositive (negate i)::Result (Types.Positive Int) of
145
    Bad _ -> passTest
146
    Ok nn -> failTest $ "Built positive number '" ++ show nn ++
147
             "' from negative or zero value " ++ show i
148

    
149
-- | Tests building non-empty lists.
150
prop_NonEmpty_pass :: QuickCheck.NonEmptyList String -> Property
151
prop_NonEmpty_pass (QuickCheck.NonEmpty xs) =
152
  case mkNonEmpty xs of
153
    Bad msg -> failTest $ "Fail to build non-empty list: " ++ msg
154
    Ok nn -> fromNonEmpty nn ==? xs
155

    
156
-- | Tests building positive numbers.
157
case_NonEmpty_fail :: Assertion
158
case_NonEmpty_fail =
159
  assertEqual "building non-empty list from an empty list"
160
    (Bad "Received empty value for non-empty list") (mkNonEmpty ([]::[Int]))
161

    
162
-- | Tests migration mode serialisation.
163
prop_MigrationMode_serialisation :: MigrationMode -> Property
164
prop_MigrationMode_serialisation = testSerialisation
165

    
166
-- | Tests verify optional checks serialisation.
167
prop_VerifyOptionalChecks_serialisation :: VerifyOptionalChecks -> Property
168
prop_VerifyOptionalChecks_serialisation = testSerialisation
169

    
170
-- | Tests 'DdmSimple' serialisation.
171
prop_DdmSimple_serialisation :: DdmSimple -> Property
172
prop_DdmSimple_serialisation = testSerialisation
173

    
174
-- | Tests 'DdmFull' serialisation.
175
prop_DdmFull_serialisation :: DdmFull -> Property
176
prop_DdmFull_serialisation = testSerialisation
177

    
178
-- | Tests 'CVErrorCode' serialisation.
179
prop_CVErrorCode_serialisation :: CVErrorCode -> Property
180
prop_CVErrorCode_serialisation = testSerialisation
181

    
182
-- | Tests equivalence with Python, based on Constants.hs code.
183
case_CVErrorCode_pyequiv :: Assertion
184
case_CVErrorCode_pyequiv = do
185
  let all_py_codes = sort C.cvAllEcodesStrings
186
      all_hs_codes = sort $ map Types.cVErrorCodeToRaw [minBound..maxBound]
187
  assertEqual "for CVErrorCode equivalence" all_py_codes all_hs_codes
188

    
189
-- | Test 'Hypervisor' serialisation.
190
prop_Hypervisor_serialisation :: Hypervisor -> Property
191
prop_Hypervisor_serialisation = testSerialisation
192

    
193
-- | Test 'OobCommand' serialisation.
194
prop_OobCommand_serialisation :: OobCommand -> Property
195
prop_OobCommand_serialisation = testSerialisation
196

    
197
-- | Test 'StorageType' serialisation.
198
prop_StorageType_serialisation :: StorageType -> Property
199
prop_StorageType_serialisation = testSerialisation
200

    
201
-- | Test 'NodeEvacMode' serialisation.
202
prop_NodeEvacMode_serialisation :: NodeEvacMode -> Property
203
prop_NodeEvacMode_serialisation = testSerialisation
204

    
205
-- | Test 'FileDriver' serialisation.
206
prop_FileDriver_serialisation :: FileDriver -> Property
207
prop_FileDriver_serialisation = testSerialisation
208

    
209
-- | Test 'InstCreate' serialisation.
210
prop_InstCreateMode_serialisation :: InstCreateMode -> Property
211
prop_InstCreateMode_serialisation = testSerialisation
212

    
213
-- | Test 'RebootType' serialisation.
214
prop_RebootType_serialisation :: RebootType -> Property
215
prop_RebootType_serialisation = testSerialisation
216

    
217
-- | Test 'ExportMode' serialisation.
218
prop_ExportMode_serialisation :: ExportMode -> Property
219
prop_ExportMode_serialisation = testSerialisation
220

    
221
-- | Test 'IAllocatorTestDir' serialisation.
222
prop_IAllocatorTestDir_serialisation :: IAllocatorTestDir -> Property
223
prop_IAllocatorTestDir_serialisation = testSerialisation
224

    
225
-- | Test 'IAllocatorMode' serialisation.
226
prop_IAllocatorMode_serialisation :: IAllocatorMode -> Property
227
prop_IAllocatorMode_serialisation = testSerialisation
228

    
229
-- | Tests equivalence with Python, based on Constants.hs code.
230
case_IAllocatorMode_pyequiv :: Assertion
231
case_IAllocatorMode_pyequiv = do
232
  let all_py_codes = sort C.validIallocatorModes
233
      all_hs_codes = sort $ map Types.iAllocatorModeToRaw [minBound..maxBound]
234
  assertEqual "for IAllocatorMode equivalence" all_py_codes all_hs_codes
235

    
236
-- | Test 'NICMode' serialisation.
237
prop_NICMode_serialisation :: NICMode -> Property
238
prop_NICMode_serialisation = testSerialisation
239

    
240
-- | Tests equivalence with Python, based on Constants.hs code.
241
case_NICMode_pyequiv :: Assertion
242
case_NICMode_pyequiv = do
243
  let all_py_codes = sort C.nicValidModes
244
      all_hs_codes = sort $ map Types.nICModeToRaw [minBound..maxBound]
245
  assertEqual "for NICMode equivalence" all_py_codes all_hs_codes
246

    
247
testSuite "Types"
248
  [ 'prop_AllocPolicy_serialisation
249
  , 'prop_DiskTemplate_serialisation
250
  , 'prop_InstanceStatus_serialisation
251
  , 'prop_NonNeg_pass
252
  , 'prop_NonNeg_fail
253
  , 'prop_Positive_pass
254
  , 'prop_Positive_fail
255
  , 'prop_NonEmpty_pass
256
  , 'case_NonEmpty_fail
257
  , 'prop_MigrationMode_serialisation
258
  , 'prop_VerifyOptionalChecks_serialisation
259
  , 'prop_DdmSimple_serialisation
260
  , 'prop_DdmFull_serialisation
261
  , 'prop_CVErrorCode_serialisation
262
  , 'case_CVErrorCode_pyequiv
263
  , 'prop_Hypervisor_serialisation
264
  , 'prop_OobCommand_serialisation
265
  , 'prop_StorageType_serialisation
266
  , 'prop_NodeEvacMode_serialisation
267
  , 'prop_FileDriver_serialisation
268
  , 'prop_InstCreateMode_serialisation
269
  , 'prop_RebootType_serialisation
270
  , 'prop_ExportMode_serialisation
271
  , 'prop_IAllocatorTestDir_serialisation
272
  , 'prop_IAllocatorMode_serialisation
273
  , 'case_IAllocatorMode_pyequiv
274
  , 'prop_NICMode_serialisation
275
  , 'case_NICMode_pyequiv
276
  ]