| 1 |
{-# LANGUAGE TemplateHaskell #-} |
| 2 |
{-# OPTIONS_GHC -fno-warn-orphans #-} |
| 3 |
|
| 4 |
{-| Unittests for the LV Parser -} |
| 5 |
|
| 6 |
{- |
| 7 |
|
| 8 |
Copyright (C) 2013 Google Inc. |
| 9 |
All rights reserved. |
| 10 |
|
| 11 |
Redistribution and use in source and binary forms, with or without |
| 12 |
modification, are permitted provided that the following conditions are |
| 13 |
met: |
| 14 |
|
| 15 |
1. Redistributions of source code must retain the above copyright notice, |
| 16 |
this list of conditions and the following disclaimer. |
| 17 |
|
| 18 |
2. Redistributions in binary form must reproduce the above copyright |
| 19 |
notice, this list of conditions and the following disclaimer in the |
| 20 |
documentation and/or other materials provided with the distribution. |
| 21 |
|
| 22 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 23 |
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 24 |
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 25 |
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 26 |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 27 |
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 28 |
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 29 |
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 30 |
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 31 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 32 |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 |
|
| 34 |
-} |
| 35 |
|
| 36 |
module Test.Ganeti.Storage.Lvm.LVParser (testStorage_Lvm_LVParser) where |
| 37 |
|
| 38 |
import Test.QuickCheck as QuickCheck hiding (Result) |
| 39 |
import Test.HUnit |
| 40 |
|
| 41 |
import Test.Ganeti.TestHelper |
| 42 |
import Test.Ganeti.TestCommon |
| 43 |
|
| 44 |
import Data.List (intercalate) |
| 45 |
|
| 46 |
import Ganeti.Storage.Lvm.LVParser |
| 47 |
import Ganeti.Storage.Lvm.Types |
| 48 |
|
| 49 |
{-# ANN module "HLint: ignore Use camelCase" #-} |
| 50 |
|
| 51 |
|
| 52 |
-- | Test parsing a LV @lvs@ output. |
| 53 |
case_lvs_lv :: Assertion |
| 54 |
case_lvs_lv = testParser lvParser "lvs_lv.txt" |
| 55 |
[ LVInfo "nhasjL-cnZi-uqLS-WRLj-tkXI-nvCB-n0o2lj" |
| 56 |
"df9ff3f6-a833-48ff-8bd5-bff2eaeab759.disk0_data" "-wi-ao" (negate 1) |
| 57 |
(negate 1) 253 0 1073741824 1 |
| 58 |
"originstname+instance1.example.com" "" |
| 59 |
"uZgXit-eiRr-vRqe-xpEo-e9nU-mTuR-9nfVIU" "xenvg" "linear" 0 0 1073741824 |
| 60 |
"" "/dev/sda5:0-15" "/dev/sda5(0)" Nothing |
| 61 |
, LVInfo "5fW5mE-SBSs-GSU0-KZDg-hnwb-sZOC-zZt736" |
| 62 |
"df9ff3f6-a833-48ff-8bd5-bff2eaeab759.disk0_meta" "-wi-ao" (negate 1) |
| 63 |
(negate 1) 253 1 134217728 1 |
| 64 |
"originstname+instance1.example.com" "" |
| 65 |
"uZgXit-eiRr-vRqe-xpEo-e9nU-mTuR-9nfVIU" "xenvg" "linear" 0 0 134217728 "" |
| 66 |
"/dev/sda5:16-17" "/dev/sda5(16)" Nothing |
| 67 |
] |
| 68 |
|
| 69 |
-- | Serialize a LVInfo in the same format that is output by @lvs@. |
| 70 |
-- The "instance" field is not serialized because it's not provided by @lvs@ |
| 71 |
-- so it is not part of this test. |
| 72 |
serializeLVInfo :: LVInfo -> String |
| 73 |
serializeLVInfo l = intercalate ";" |
| 74 |
[ lviUuid l |
| 75 |
, lviName l |
| 76 |
, lviAttr l |
| 77 |
, show $ lviMajor l |
| 78 |
, show $ lviMinor l |
| 79 |
, show $ lviKernelMajor l |
| 80 |
, show $ lviKernelMinor l |
| 81 |
, show (lviSize l) ++ "B" |
| 82 |
, show $ lviSegCount l |
| 83 |
, lviTags l |
| 84 |
, lviModules l |
| 85 |
, lviVgUuid l |
| 86 |
, lviVgName l |
| 87 |
, lviSegtype l |
| 88 |
, show (lviSegStart l) ++ "B" |
| 89 |
, show $ lviSegStartPe l |
| 90 |
, show (lviSegSize l) ++ "B" |
| 91 |
, lviSegTags l |
| 92 |
, lviSegPeRanges l |
| 93 |
, lviDevices l |
| 94 |
] ++ "\n" |
| 95 |
|
| 96 |
-- | Serialize a list of LVInfo in the same format that is output by @lvs@. |
| 97 |
serializeLVInfos :: [LVInfo] -> String |
| 98 |
serializeLVInfos = concatMap serializeLVInfo |
| 99 |
|
| 100 |
-- | Arbitrary instance for LVInfo. |
| 101 |
-- The instance is always Nothing because it is not part of the parsed data: |
| 102 |
-- it is added afterwards from a different source. |
| 103 |
instance Arbitrary LVInfo where |
| 104 |
arbitrary = |
| 105 |
LVInfo |
| 106 |
<$> genUUID -- uuid |
| 107 |
<*> genName -- name |
| 108 |
<*> genName -- attr |
| 109 |
<*> arbitrary -- major |
| 110 |
<*> arbitrary -- minor |
| 111 |
<*> arbitrary -- kernel_major |
| 112 |
<*> arbitrary -- kernel_minor |
| 113 |
<*> genNonNegative -- size |
| 114 |
<*> arbitrary -- seg_cont |
| 115 |
<*> genName -- tags |
| 116 |
<*> genName -- modules |
| 117 |
<*> genUUID -- vg_uuid |
| 118 |
<*> genName -- vg_name |
| 119 |
<*> genName -- segtype |
| 120 |
<*> genNonNegative -- seg_start |
| 121 |
<*> arbitrary -- seg_start_pe |
| 122 |
<*> genNonNegative -- seg_size |
| 123 |
<*> genName -- seg_tags |
| 124 |
<*> genName -- seg_pe_ranges |
| 125 |
<*> genName -- devices |
| 126 |
<*> return Nothing -- instance |
| 127 |
|
| 128 |
-- | Test if a randomly generated LV lvs output is properly parsed. |
| 129 |
prop_parse_lvs_lv :: [LVInfo] -> Property |
| 130 |
prop_parse_lvs_lv expected = |
| 131 |
genPropParser lvParser (serializeLVInfos expected) expected |
| 132 |
|
| 133 |
testSuite "Storage/Lvm/LVParser" |
| 134 |
[ 'case_lvs_lv, |
| 135 |
'prop_parse_lvs_lv |
| 136 |
] |