| 1 |
{-# LANGUAGE TemplateHaskell #-} |
| 2 |
{-# OPTIONS_GHC -fno-warn-orphans #-} |
| 3 |
|
| 4 |
{-| Unittests for ganeti-htools. |
| 5 |
|
| 6 |
-} |
| 7 |
|
| 8 |
{- |
| 9 |
|
| 10 |
Copyright (C) 2009, 2010, 2011, 2012 Google Inc. |
| 11 |
All rights reserved. |
| 12 |
|
| 13 |
Redistribution and use in source and binary forms, with or without |
| 14 |
modification, are permitted provided that the following conditions are |
| 15 |
met: |
| 16 |
|
| 17 |
1. Redistributions of source code must retain the above copyright notice, |
| 18 |
this list of conditions and the following disclaimer. |
| 19 |
|
| 20 |
2. Redistributions in binary form must reproduce the above copyright |
| 21 |
notice, this list of conditions and the following disclaimer in the |
| 22 |
documentation and/or other materials provided with the distribution. |
| 23 |
|
| 24 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 25 |
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 26 |
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 27 |
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 28 |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 29 |
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 30 |
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 31 |
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 32 |
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 33 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 34 |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 35 |
|
| 36 |
-} |
| 37 |
|
| 38 |
module Test.Ganeti.Confd.Types |
| 39 |
( testConfd_Types |
| 40 |
, ConfdRequestType(..) |
| 41 |
, ConfdReqField(..) |
| 42 |
, ConfdReqQ(..) |
| 43 |
) where |
| 44 |
|
| 45 |
import Test.QuickCheck |
| 46 |
import Test.HUnit |
| 47 |
import qualified Text.JSON as J |
| 48 |
|
| 49 |
import Test.Ganeti.TestHelper |
| 50 |
import Test.Ganeti.TestCommon |
| 51 |
|
| 52 |
import Ganeti.Confd.Types as Confd |
| 53 |
|
| 54 |
{-# ANN module "HLint: ignore Use camelCase" #-} |
| 55 |
|
| 56 |
-- * Arbitrary instances |
| 57 |
|
| 58 |
$(genArbitrary ''ConfdRequestType) |
| 59 |
|
| 60 |
$(genArbitrary ''ConfdReqField) |
| 61 |
|
| 62 |
$(genArbitrary ''ConfdReqQ) |
| 63 |
|
| 64 |
instance Arbitrary ConfdQuery where |
| 65 |
arbitrary = oneof [ pure EmptyQuery |
| 66 |
, PlainQuery <$> genName |
| 67 |
, DictQuery <$> arbitrary |
| 68 |
] |
| 69 |
|
| 70 |
$(genArbitrary ''ConfdRequest) |
| 71 |
|
| 72 |
$(genArbitrary ''ConfdReplyStatus) |
| 73 |
|
| 74 |
instance Arbitrary ConfdReply where |
| 75 |
arbitrary = ConfdReply <$> arbitrary <*> arbitrary <*> |
| 76 |
pure J.JSNull <*> arbitrary |
| 77 |
|
| 78 |
$(genArbitrary ''ConfdErrorType) |
| 79 |
|
| 80 |
$(genArbitrary ''ConfdNodeRole) |
| 81 |
|
| 82 |
-- * Test cases |
| 83 |
|
| 84 |
-- | Test 'ConfdQuery' serialisation. |
| 85 |
prop_ConfdQuery_serialisation :: ConfdQuery -> Property |
| 86 |
prop_ConfdQuery_serialisation = testSerialisation |
| 87 |
|
| 88 |
-- | Test bad types deserialisation for 'ConfdQuery'. |
| 89 |
case_ConfdQuery_BadTypes :: Assertion |
| 90 |
case_ConfdQuery_BadTypes = do |
| 91 |
let helper jsval = case J.readJSON jsval of |
| 92 |
J.Error _ -> return () |
| 93 |
J.Ok cq -> assertFailure $ "Parsed " ++ show jsval |
| 94 |
++ " as query " ++ show (cq::ConfdQuery) |
| 95 |
helper $ J.showJSON (1::Int) |
| 96 |
helper $ J.JSBool True |
| 97 |
helper $ J.JSBool False |
| 98 |
helper $ J.JSArray [] |
| 99 |
|
| 100 |
|
| 101 |
-- | Test 'ConfdReplyStatus' serialisation. |
| 102 |
prop_ConfdReplyStatus_serialisation :: ConfdReplyStatus -> Property |
| 103 |
prop_ConfdReplyStatus_serialisation = testSerialisation |
| 104 |
|
| 105 |
-- | Test 'ConfdReply' serialisation. |
| 106 |
prop_ConfdReply_serialisation :: ConfdReply -> Property |
| 107 |
prop_ConfdReply_serialisation = testSerialisation |
| 108 |
|
| 109 |
-- | Test 'ConfdErrorType' serialisation. |
| 110 |
prop_ConfdErrorType_serialisation :: ConfdErrorType -> Property |
| 111 |
prop_ConfdErrorType_serialisation = testSerialisation |
| 112 |
|
| 113 |
-- | Test 'ConfdNodeRole' serialisation. |
| 114 |
prop_ConfdNodeRole_serialisation :: ConfdNodeRole -> Property |
| 115 |
prop_ConfdNodeRole_serialisation = testSerialisation |
| 116 |
|
| 117 |
testSuite "Confd/Types" |
| 118 |
[ 'prop_ConfdQuery_serialisation |
| 119 |
, 'case_ConfdQuery_BadTypes |
| 120 |
, 'prop_ConfdReplyStatus_serialisation |
| 121 |
, 'prop_ConfdReply_serialisation |
| 122 |
, 'prop_ConfdErrorType_serialisation |
| 123 |
, 'prop_ConfdNodeRole_serialisation |
| 124 |
] |