Statistics
| Branch: | Tag: | Revision:

root / htest / Test / Ganeti / Types.hs @ 22381768

History | View | Annotate | Download (5.4 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
$(genArbitrary ''AllocPolicy)
60

    
61
$(genArbitrary ''DiskTemplate)
62

    
63
$(genArbitrary ''InstanceStatus)
64

    
65
$(genArbitrary ''MigrationMode)
66

    
67
$(genArbitrary ''VerifyOptionalChecks)
68

    
69
$(genArbitrary ''DdmSimple)
70

    
71
$(genArbitrary ''CVErrorCode)
72

    
73
$(genArbitrary ''Hypervisor)
74

    
75
instance (Arbitrary a) => Arbitrary (Types.NonEmpty a) where
76
  arbitrary = do
77
    QuickCheck.NonEmpty lst <- arbitrary
78
    Types.mkNonEmpty lst
79

    
80
-- * Properties
81

    
82
prop_AllocPolicy_serialisation :: AllocPolicy -> Property
83
prop_AllocPolicy_serialisation = testSerialisation
84

    
85
prop_DiskTemplate_serialisation :: DiskTemplate -> Property
86
prop_DiskTemplate_serialisation = testSerialisation
87

    
88
prop_InstanceStatus_serialisation :: InstanceStatus -> Property
89
prop_InstanceStatus_serialisation = testSerialisation
90

    
91
-- | Tests building non-negative numbers.
92
prop_NonNeg_pass :: QuickCheck.NonNegative Int -> Property
93
prop_NonNeg_pass (QuickCheck.NonNegative i) =
94
  case mkNonNegative i of
95
    Bad msg -> failTest $ "Fail to build non-negative: " ++ msg
96
    Ok nn -> fromNonNegative nn ==? i
97

    
98
-- | Tests building non-negative numbers.
99
prop_NonNeg_fail :: QuickCheck.Positive Int -> Property
100
prop_NonNeg_fail (QuickCheck.Positive i) =
101
  case mkNonNegative (negate i)::Result (Types.NonNegative Int) of
102
    Bad _ -> passTest
103
    Ok nn -> failTest $ "Built non-negative number '" ++ show nn ++
104
             "' from negative value " ++ show i
105

    
106
-- | Tests building positive numbers.
107
prop_Positive_pass :: QuickCheck.Positive Int -> Property
108
prop_Positive_pass (QuickCheck.Positive i) =
109
  case mkPositive i of
110
    Bad msg -> failTest $ "Fail to build positive: " ++ msg
111
    Ok nn -> fromPositive nn ==? i
112

    
113
-- | Tests building positive numbers.
114
prop_Positive_fail :: QuickCheck.NonNegative Int -> Property
115
prop_Positive_fail (QuickCheck.NonNegative i) =
116
  case mkPositive (negate i)::Result (Types.Positive Int) of
117
    Bad _ -> passTest
118
    Ok nn -> failTest $ "Built positive number '" ++ show nn ++
119
             "' from negative or zero value " ++ show i
120

    
121
-- | Tests building non-empty lists.
122
prop_NonEmpty_pass :: QuickCheck.NonEmptyList String -> Property
123
prop_NonEmpty_pass (QuickCheck.NonEmpty xs) =
124
  case mkNonEmpty xs of
125
    Bad msg -> failTest $ "Fail to build non-empty list: " ++ msg
126
    Ok nn -> fromNonEmpty nn ==? xs
127

    
128
-- | Tests building positive numbers.
129
case_NonEmpty_fail :: Assertion
130
case_NonEmpty_fail =
131
  assertEqual "building non-empty list from an empty list"
132
    (Bad "Received empty value for non-empty list") (mkNonEmpty ([]::[Int]))
133

    
134
-- | Tests migration mode serialisation.
135
prop_MigrationMode_serialisation :: MigrationMode -> Property
136
prop_MigrationMode_serialisation = testSerialisation
137

    
138
-- | Tests verify optional checks serialisation.
139
prop_VerifyOptionalChecks_serialisation :: VerifyOptionalChecks -> Property
140
prop_VerifyOptionalChecks_serialisation = testSerialisation
141

    
142
-- | Tests 'DdmSimple' serialisation.
143
prop_DdmSimple_serialisation :: DdmSimple -> Property
144
prop_DdmSimple_serialisation = testSerialisation
145

    
146
-- | Tests 'CVErrorCode' serialisation.
147
prop_CVErrorCode_serialisation :: CVErrorCode -> Property
148
prop_CVErrorCode_serialisation = testSerialisation
149

    
150
-- | Tests equivalence with Python, based on Constants.hs code.
151
case_CVErrorCode_pyequiv :: Assertion
152
case_CVErrorCode_pyequiv = do
153
  let all_py_codes = sort C.cvAllEcodesStrings
154
      all_hs_codes = sort $ map Types.cVErrorCodeToRaw [minBound..maxBound]
155
  assertEqual "for CVErrorCode equivalence" all_py_codes all_hs_codes
156

    
157
-- | Test 'Hypervisor' serialisation.
158
prop_Hypervisor_serialisation :: Hypervisor -> Property
159
prop_Hypervisor_serialisation = testSerialisation
160

    
161
testSuite "Types"
162
  [ 'prop_AllocPolicy_serialisation
163
  , 'prop_DiskTemplate_serialisation
164
  , 'prop_InstanceStatus_serialisation
165
  , 'prop_NonNeg_pass
166
  , 'prop_NonNeg_fail
167
  , 'prop_Positive_pass
168
  , 'prop_Positive_fail
169
  , 'prop_NonEmpty_pass
170
  , 'case_NonEmpty_fail
171
  , 'prop_MigrationMode_serialisation
172
  , 'prop_VerifyOptionalChecks_serialisation
173
  , 'prop_DdmSimple_serialisation
174
  , 'prop_CVErrorCode_serialisation
175
  , 'case_CVErrorCode_pyequiv
176
  , 'prop_Hypervisor_serialisation
177
  ]