Statistics
| Branch: | Tag: | Revision:

root / htest / Test / Ganeti / Types.hs @ 6a28e02c

History | View | Annotate | Download (6.1 kB)

1 5e9deac0 Iustin Pop
{-# LANGUAGE TemplateHaskell #-}
2 5e9deac0 Iustin Pop
{-# OPTIONS_GHC -fno-warn-orphans #-}
3 5e9deac0 Iustin Pop
4 5e9deac0 Iustin Pop
{-| Unittests for 'Ganeti.Types'.
5 5e9deac0 Iustin Pop
6 5e9deac0 Iustin Pop
-}
7 5e9deac0 Iustin Pop
8 5e9deac0 Iustin Pop
{-
9 5e9deac0 Iustin Pop
10 5e9deac0 Iustin Pop
Copyright (C) 2012 Google Inc.
11 5e9deac0 Iustin Pop
12 5e9deac0 Iustin Pop
This program is free software; you can redistribute it and/or modify
13 5e9deac0 Iustin Pop
it under the terms of the GNU General Public License as published by
14 5e9deac0 Iustin Pop
the Free Software Foundation; either version 2 of the License, or
15 5e9deac0 Iustin Pop
(at your option) any later version.
16 5e9deac0 Iustin Pop
17 5e9deac0 Iustin Pop
This program is distributed in the hope that it will be useful, but
18 5e9deac0 Iustin Pop
WITHOUT ANY WARRANTY; without even the implied warranty of
19 5e9deac0 Iustin Pop
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 5e9deac0 Iustin Pop
General Public License for more details.
21 5e9deac0 Iustin Pop
22 5e9deac0 Iustin Pop
You should have received a copy of the GNU General Public License
23 5e9deac0 Iustin Pop
along with this program; if not, write to the Free Software
24 5e9deac0 Iustin Pop
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 5e9deac0 Iustin Pop
02110-1301, USA.
26 5e9deac0 Iustin Pop
27 5e9deac0 Iustin Pop
-}
28 5e9deac0 Iustin Pop
29 5e9deac0 Iustin Pop
module Test.Ganeti.Types
30 5e9deac0 Iustin Pop
  ( testTypes
31 5e9deac0 Iustin Pop
  , AllocPolicy(..)
32 5e9deac0 Iustin Pop
  , DiskTemplate(..)
33 5e9deac0 Iustin Pop
  , InstanceStatus(..)
34 edb5a1c8 Iustin Pop
  , NonEmpty(..)
35 22381768 Iustin Pop
  , Hypervisor(..)
36 5e9deac0 Iustin Pop
  ) where
37 5e9deac0 Iustin Pop
38 d696bbef Iustin Pop
import Data.List (sort)
39 edb5a1c8 Iustin Pop
import Test.QuickCheck as QuickCheck hiding (Result)
40 edb5a1c8 Iustin Pop
import Test.HUnit
41 5e9deac0 Iustin Pop
42 5e9deac0 Iustin Pop
import Test.Ganeti.TestHelper
43 5e9deac0 Iustin Pop
import Test.Ganeti.TestCommon
44 5e9deac0 Iustin Pop
45 edb5a1c8 Iustin Pop
import Ganeti.BasicTypes
46 d696bbef Iustin Pop
import qualified Ganeti.Constants as C
47 edb5a1c8 Iustin Pop
import Ganeti.Types as Types
48 5e9deac0 Iustin Pop
49 39573352 Iustin Pop
{-# ANN module "HLint: ignore Use camelCase" #-}
50 39573352 Iustin Pop
51 5e9deac0 Iustin Pop
-- * Arbitrary instance
52 5e9deac0 Iustin Pop
53 edb5a1c8 Iustin Pop
instance (Arbitrary a, Ord a, Num a, Show a) =>
54 edb5a1c8 Iustin Pop
  Arbitrary (Types.Positive a) where
55 edb5a1c8 Iustin Pop
  arbitrary = do
56 edb5a1c8 Iustin Pop
    (QuickCheck.Positive i) <- arbitrary
57 edb5a1c8 Iustin Pop
    Types.mkPositive i
58 edb5a1c8 Iustin Pop
59 5e9deac0 Iustin Pop
$(genArbitrary ''AllocPolicy)
60 5e9deac0 Iustin Pop
61 5e9deac0 Iustin Pop
$(genArbitrary ''DiskTemplate)
62 5e9deac0 Iustin Pop
63 5e9deac0 Iustin Pop
$(genArbitrary ''InstanceStatus)
64 5e9deac0 Iustin Pop
65 d696bbef Iustin Pop
$(genArbitrary ''MigrationMode)
66 d696bbef Iustin Pop
67 d696bbef Iustin Pop
$(genArbitrary ''VerifyOptionalChecks)
68 d696bbef Iustin Pop
69 d696bbef Iustin Pop
$(genArbitrary ''DdmSimple)
70 d696bbef Iustin Pop
71 d696bbef Iustin Pop
$(genArbitrary ''CVErrorCode)
72 d696bbef Iustin Pop
73 22381768 Iustin Pop
$(genArbitrary ''Hypervisor)
74 22381768 Iustin Pop
75 6a28e02c Iustin Pop
$(genArbitrary ''OobCommand)
76 6a28e02c Iustin Pop
77 48755fac Iustin Pop
$(genArbitrary ''StorageType)
78 48755fac Iustin Pop
79 6a28e02c Iustin Pop
$(genArbitrary ''NodeEvacMode)
80 6a28e02c Iustin Pop
81 edb5a1c8 Iustin Pop
instance (Arbitrary a) => Arbitrary (Types.NonEmpty a) where
82 edb5a1c8 Iustin Pop
  arbitrary = do
83 edb5a1c8 Iustin Pop
    QuickCheck.NonEmpty lst <- arbitrary
84 edb5a1c8 Iustin Pop
    Types.mkNonEmpty lst
85 edb5a1c8 Iustin Pop
86 d696bbef Iustin Pop
-- * Properties
87 d696bbef Iustin Pop
88 5e9deac0 Iustin Pop
prop_AllocPolicy_serialisation :: AllocPolicy -> Property
89 5e9deac0 Iustin Pop
prop_AllocPolicy_serialisation = testSerialisation
90 5e9deac0 Iustin Pop
91 5e9deac0 Iustin Pop
prop_DiskTemplate_serialisation :: DiskTemplate -> Property
92 5e9deac0 Iustin Pop
prop_DiskTemplate_serialisation = testSerialisation
93 5e9deac0 Iustin Pop
94 5e9deac0 Iustin Pop
prop_InstanceStatus_serialisation :: InstanceStatus -> Property
95 5e9deac0 Iustin Pop
prop_InstanceStatus_serialisation = testSerialisation
96 5e9deac0 Iustin Pop
97 edb5a1c8 Iustin Pop
-- | Tests building non-negative numbers.
98 edb5a1c8 Iustin Pop
prop_NonNeg_pass :: QuickCheck.NonNegative Int -> Property
99 edb5a1c8 Iustin Pop
prop_NonNeg_pass (QuickCheck.NonNegative i) =
100 edb5a1c8 Iustin Pop
  case mkNonNegative i of
101 edb5a1c8 Iustin Pop
    Bad msg -> failTest $ "Fail to build non-negative: " ++ msg
102 edb5a1c8 Iustin Pop
    Ok nn -> fromNonNegative nn ==? i
103 edb5a1c8 Iustin Pop
104 edb5a1c8 Iustin Pop
-- | Tests building non-negative numbers.
105 edb5a1c8 Iustin Pop
prop_NonNeg_fail :: QuickCheck.Positive Int -> Property
106 edb5a1c8 Iustin Pop
prop_NonNeg_fail (QuickCheck.Positive i) =
107 edb5a1c8 Iustin Pop
  case mkNonNegative (negate i)::Result (Types.NonNegative Int) of
108 edb5a1c8 Iustin Pop
    Bad _ -> passTest
109 edb5a1c8 Iustin Pop
    Ok nn -> failTest $ "Built non-negative number '" ++ show nn ++
110 edb5a1c8 Iustin Pop
             "' from negative value " ++ show i
111 edb5a1c8 Iustin Pop
112 edb5a1c8 Iustin Pop
-- | Tests building positive numbers.
113 edb5a1c8 Iustin Pop
prop_Positive_pass :: QuickCheck.Positive Int -> Property
114 edb5a1c8 Iustin Pop
prop_Positive_pass (QuickCheck.Positive i) =
115 edb5a1c8 Iustin Pop
  case mkPositive i of
116 edb5a1c8 Iustin Pop
    Bad msg -> failTest $ "Fail to build positive: " ++ msg
117 edb5a1c8 Iustin Pop
    Ok nn -> fromPositive nn ==? i
118 edb5a1c8 Iustin Pop
119 edb5a1c8 Iustin Pop
-- | Tests building positive numbers.
120 edb5a1c8 Iustin Pop
prop_Positive_fail :: QuickCheck.NonNegative Int -> Property
121 edb5a1c8 Iustin Pop
prop_Positive_fail (QuickCheck.NonNegative i) =
122 edb5a1c8 Iustin Pop
  case mkPositive (negate i)::Result (Types.Positive Int) of
123 edb5a1c8 Iustin Pop
    Bad _ -> passTest
124 edb5a1c8 Iustin Pop
    Ok nn -> failTest $ "Built positive number '" ++ show nn ++
125 edb5a1c8 Iustin Pop
             "' from negative or zero value " ++ show i
126 edb5a1c8 Iustin Pop
127 edb5a1c8 Iustin Pop
-- | Tests building non-empty lists.
128 39573352 Iustin Pop
prop_NonEmpty_pass :: QuickCheck.NonEmptyList String -> Property
129 edb5a1c8 Iustin Pop
prop_NonEmpty_pass (QuickCheck.NonEmpty xs) =
130 edb5a1c8 Iustin Pop
  case mkNonEmpty xs of
131 edb5a1c8 Iustin Pop
    Bad msg -> failTest $ "Fail to build non-empty list: " ++ msg
132 edb5a1c8 Iustin Pop
    Ok nn -> fromNonEmpty nn ==? xs
133 edb5a1c8 Iustin Pop
134 edb5a1c8 Iustin Pop
-- | Tests building positive numbers.
135 edb5a1c8 Iustin Pop
case_NonEmpty_fail :: Assertion
136 39573352 Iustin Pop
case_NonEmpty_fail =
137 edb5a1c8 Iustin Pop
  assertEqual "building non-empty list from an empty list"
138 edb5a1c8 Iustin Pop
    (Bad "Received empty value for non-empty list") (mkNonEmpty ([]::[Int]))
139 edb5a1c8 Iustin Pop
140 d696bbef Iustin Pop
-- | Tests migration mode serialisation.
141 d696bbef Iustin Pop
prop_MigrationMode_serialisation :: MigrationMode -> Property
142 d696bbef Iustin Pop
prop_MigrationMode_serialisation = testSerialisation
143 d696bbef Iustin Pop
144 d696bbef Iustin Pop
-- | Tests verify optional checks serialisation.
145 d696bbef Iustin Pop
prop_VerifyOptionalChecks_serialisation :: VerifyOptionalChecks -> Property
146 d696bbef Iustin Pop
prop_VerifyOptionalChecks_serialisation = testSerialisation
147 d696bbef Iustin Pop
148 d696bbef Iustin Pop
-- | Tests 'DdmSimple' serialisation.
149 d696bbef Iustin Pop
prop_DdmSimple_serialisation :: DdmSimple -> Property
150 d696bbef Iustin Pop
prop_DdmSimple_serialisation = testSerialisation
151 d696bbef Iustin Pop
152 d696bbef Iustin Pop
-- | Tests 'CVErrorCode' serialisation.
153 d696bbef Iustin Pop
prop_CVErrorCode_serialisation :: CVErrorCode -> Property
154 d696bbef Iustin Pop
prop_CVErrorCode_serialisation = testSerialisation
155 d696bbef Iustin Pop
156 d696bbef Iustin Pop
-- | Tests equivalence with Python, based on Constants.hs code.
157 d696bbef Iustin Pop
case_CVErrorCode_pyequiv :: Assertion
158 d696bbef Iustin Pop
case_CVErrorCode_pyequiv = do
159 d696bbef Iustin Pop
  let all_py_codes = sort C.cvAllEcodesStrings
160 d696bbef Iustin Pop
      all_hs_codes = sort $ map Types.cVErrorCodeToRaw [minBound..maxBound]
161 d696bbef Iustin Pop
  assertEqual "for CVErrorCode equivalence" all_py_codes all_hs_codes
162 d696bbef Iustin Pop
163 22381768 Iustin Pop
-- | Test 'Hypervisor' serialisation.
164 22381768 Iustin Pop
prop_Hypervisor_serialisation :: Hypervisor -> Property
165 22381768 Iustin Pop
prop_Hypervisor_serialisation = testSerialisation
166 22381768 Iustin Pop
167 6a28e02c Iustin Pop
-- | Test 'OobCommand' serialisation.
168 6a28e02c Iustin Pop
prop_OobCommand_serialisation :: OobCommand -> Property
169 6a28e02c Iustin Pop
prop_OobCommand_serialisation = testSerialisation
170 6a28e02c Iustin Pop
171 48755fac Iustin Pop
-- | Test 'StorageType' serialisation.
172 48755fac Iustin Pop
prop_StorageType_serialisation :: StorageType -> Property
173 48755fac Iustin Pop
prop_StorageType_serialisation = testSerialisation
174 48755fac Iustin Pop
175 6a28e02c Iustin Pop
-- | Test 'NodeEvacMode' serialisation.
176 6a28e02c Iustin Pop
prop_NodeEvacMode_serialisation :: NodeEvacMode -> Property
177 6a28e02c Iustin Pop
prop_NodeEvacMode_serialisation = testSerialisation
178 6a28e02c Iustin Pop
179 5e9deac0 Iustin Pop
testSuite "Types"
180 5e9deac0 Iustin Pop
  [ 'prop_AllocPolicy_serialisation
181 5e9deac0 Iustin Pop
  , 'prop_DiskTemplate_serialisation
182 5e9deac0 Iustin Pop
  , 'prop_InstanceStatus_serialisation
183 edb5a1c8 Iustin Pop
  , 'prop_NonNeg_pass
184 edb5a1c8 Iustin Pop
  , 'prop_NonNeg_fail
185 edb5a1c8 Iustin Pop
  , 'prop_Positive_pass
186 edb5a1c8 Iustin Pop
  , 'prop_Positive_fail
187 edb5a1c8 Iustin Pop
  , 'prop_NonEmpty_pass
188 edb5a1c8 Iustin Pop
  , 'case_NonEmpty_fail
189 d696bbef Iustin Pop
  , 'prop_MigrationMode_serialisation
190 d696bbef Iustin Pop
  , 'prop_VerifyOptionalChecks_serialisation
191 d696bbef Iustin Pop
  , 'prop_DdmSimple_serialisation
192 d696bbef Iustin Pop
  , 'prop_CVErrorCode_serialisation
193 d696bbef Iustin Pop
  , 'case_CVErrorCode_pyequiv
194 22381768 Iustin Pop
  , 'prop_Hypervisor_serialisation
195 6a28e02c Iustin Pop
  , 'prop_OobCommand_serialisation
196 48755fac Iustin Pop
  , 'prop_StorageType_serialisation
197 6a28e02c Iustin Pop
  , 'prop_NodeEvacMode_serialisation
198 5e9deac0 Iustin Pop
  ]