1 {-# LANGUAGE TemplateHaskell #-}
3 {-| Unittests for Attoparsec support for unicode -}
7 Copyright (C) 2012 Google Inc.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 module Test.Ganeti.Block.Drbd.Parser (testBlock_Drbd_Parser) where
28 import Test.QuickCheck as QuickCheck hiding (Result)
31 import Test.Ganeti.TestHelper
32 import Test.Ganeti.TestCommon
34 import qualified Data.Attoparsec.Text as A
35 import Data.List (intercalate)
36 import Data.Text (pack)
38 import Ganeti.Block.Drbd.Parser (drbdStatusParser, commaIntParser)
39 import Ganeti.Block.Drbd.Types
41 {-# ANN module "HLint: ignore Use camelCase" #-}
43 -- | Function for testing whether a file is parsed correctly.
44 testFile :: String -> DRBDStatus -> Assertion
45 testFile fileName expectedContent = do
46 fileContent <- readTestData fileName
47 case A.parseOnly (drbdStatusParser []) $ pack fileContent of
48 Left msg -> assertFailure $ "Parsing failed: " ++ msg
49 Right obtained -> assertEqual fileName expectedContent obtained
51 -- | Test a DRBD 8.0 file with an empty line inside.
52 case_drbd80_emptyline :: Assertion
53 case_drbd80_emptyline = testFile "proc_drbd80-emptyline.txt" $
55 ( VersionInfo (Just "8.0.12") (Just "86") (Just "86") Nothing
56 (Just "5c9f89594553e32adb87d9638dce591782f947e3")
57 (Just "root@node1.example.com, 2009-05-22 12:47:52")
59 [ DeviceInfo 0 Connected (LocalRemote Primary Secondary)
60 (LocalRemote UpToDate UpToDate) 'C' "r---"
61 (PerfIndicators 78728316 0 77675644 1277039 254 270 0 0 0 0
62 Nothing Nothing Nothing)
64 (Just $ AdditionalInfo 0 61 65657 135 0 0 135)
65 (Just $ AdditionalInfo 0 257 11378843 254 0 0 254)
73 -- | Test a DRBD 8.3 file with a NULL caracter inside.
74 case_drbd83_sync_krnl2_6_39 :: Assertion
75 case_drbd83_sync_krnl2_6_39 = testFile "proc_drbd83_sync_krnl2.6.39.txt" $
77 ( VersionInfo (Just "8.3.1") (Just "88") (Just "86-89") Nothing
78 (Just "fd40f4a8f9104941537d1afc8521e584a6d3003c")
79 (Just "phil@fat-tyre, 2009-03-27 12:19:49")
81 [ DeviceInfo 0 Connected (LocalRemote Primary Secondary)
82 (LocalRemote UpToDate UpToDate) 'C' "r----"
83 (PerfIndicators 140978 0 9906 131533 27 8 0 0 0 0 (Just 1)
89 DeviceInfo 1 Connected (LocalRemote Secondary Primary)
90 (LocalRemote UpToDate UpToDate) 'C' "r---"
91 (PerfIndicators 0 140980 140980 0 0 8 0 0 0 0 (Just 1) (Just 'f')
98 DeviceInfo 3 SyncSource (LocalRemote Primary Secondary)
99 (LocalRemote UpToDate Inconsistent) 'A' "r-----"
100 (PerfIndicators 373888 0 0 374088 0 22 7 27 7 0 (Just 1)
101 (Just 'f') (Just 15358208))
102 (Just $ SyncStatus 2.4 14996 15360 MegaByte (Time 0 4 8) 61736 Nothing
107 DeviceInfo 4 WFConnection (LocalRemote Primary Unknown)
108 (LocalRemote UpToDate DUnknown) 'C' "r----"
109 (PerfIndicators 140978 0 9906 131534 27 8 0 0 0 0 (Just 1)
117 -- | Test a DRBD 8.3 file with an ongoing synchronization.
118 case_drbd83_sync :: Assertion
119 case_drbd83_sync = testFile "proc_drbd83_sync.txt" $
121 ( VersionInfo (Just "8.3.1") (Just "88") (Just "86-89") Nothing
122 (Just "fd40f4a8f9104941537d1afc8521e584a6d3003c")
123 (Just "phil@fat-tyre, 2009-03-27 12:19:49")
125 [ DeviceInfo 0 Connected (LocalRemote Primary Secondary)
126 (LocalRemote UpToDate UpToDate) 'C' "r----"
127 (PerfIndicators 140978 0 9906 131533 27 8 0 0 0 0 (Just 1)
133 DeviceInfo 1 Connected (LocalRemote Secondary Primary)
134 (LocalRemote UpToDate UpToDate) 'C' "r---"
135 (PerfIndicators 0 140980 140980 0 0 8 0 0 0 0 (Just 1) (Just 'f')
141 UnconfiguredDevice 2,
142 DeviceInfo 3 SyncTarget (LocalRemote Primary Secondary)
143 (LocalRemote Inconsistent UpToDate) 'C' "r----"
144 (PerfIndicators 0 178176 178176 0 104 42 0 0 0 0 (Just 1)
145 (Just 'b') (Just 346112))
146 (Just $ SyncStatus 34.9 346112 524288 MegaByte (Time 0 0 5) 59392
147 Nothing KiloByte Second)
151 DeviceInfo 4 WFConnection (LocalRemote Primary Unknown)
152 (LocalRemote UpToDate DUnknown) 'C' "r----"
153 (PerfIndicators 140978 0 9906 131534 27 8 0 0 0 0 (Just 1)
161 -- | Test a DRBD 8.3 file not from git sources, with an ongoing synchronization
162 -- and the "want" field
163 case_drbd83_sync_want :: Assertion
164 case_drbd83_sync_want = testFile "proc_drbd83_sync_want.txt" $
166 ( VersionInfo (Just "8.3.11") (Just "88") (Just "86-96")
167 (Just "2D876214BAAD53B31ADC1D6")
170 [ DeviceInfo 0 SyncTarget (LocalRemote Secondary Primary)
171 (LocalRemote Inconsistent UpToDate) 'C' "r-----"
172 (PerfIndicators 0 460288 460160 0 0 28 2 4 1 0 (Just 1)
173 (Just 'f') (Just 588416))
174 (Just $ SyncStatus 44.4 588416 1048576 KiloByte (Time 0 0 8) 65736
175 (Just 61440) KiloByte Second)
179 UnconfiguredDevice 1,
180 UnconfiguredDevice 2,
184 -- | Test a DRBD 8.3 file.
185 case_drbd83 :: Assertion
186 case_drbd83 = testFile "proc_drbd83.txt" $
188 ( VersionInfo (Just "8.3.1") (Just "88") (Just "86-89")
190 (Just "fd40f4a8f9104941537d1afc8521e584a6d3003c")
191 (Just "phil@fat-tyre, 2009-03-27 12:19:49")
193 [ DeviceInfo 0 Connected (LocalRemote Primary Secondary)
194 (LocalRemote UpToDate UpToDate) 'C' "r----"
195 (PerfIndicators 140978 0 9906 131533 27 8 0 0 0 0 (Just 1)
201 DeviceInfo 1 Connected (LocalRemote Secondary Primary)
202 (LocalRemote UpToDate UpToDate) 'C' "r---"
203 (PerfIndicators 0 140980 140980 0 0 8 0 0 0 0 (Just 1) (Just 'f')
209 UnconfiguredDevice 2,
210 DeviceInfo 4 WFConnection (LocalRemote Primary Unknown)
211 (LocalRemote UpToDate DUnknown) 'C' "r----"
212 (PerfIndicators 140978 0 9906 131534 27 8 0 0 0 0 (Just 1)
218 DeviceInfo 5 Connected (LocalRemote Primary Secondary)
219 (LocalRemote UpToDate Diskless) 'C' "r----"
220 (PerfIndicators 140978 0 9906 131533 19 8 0 0 0 0 (Just 1)
226 DeviceInfo 6 Connected (LocalRemote Secondary Primary)
227 (LocalRemote Diskless UpToDate) 'C' "r---"
228 (PerfIndicators 0 140978 140978 0 0 8 0 0 0 0 (Just 1) (Just 'f')
234 DeviceInfo 7 WFConnection (LocalRemote Secondary Unknown)
235 (LocalRemote UpToDate DUnknown) 'C' "r---"
236 (PerfIndicators 0 140978 140978 0 0 8 0 0 0 0 (Just 1) (Just 'f')
242 DeviceInfo 8 StandAlone (LocalRemote Secondary Unknown)
243 (LocalRemote UpToDate DUnknown) ' ' "r---"
244 (PerfIndicators 0 140978 140978 0 0 8 0 0 0 0 (Just 1)
252 -- | Test a DRBD 8.0 file with a missing device.
253 case_drbd8 :: Assertion
254 case_drbd8 = testFile "proc_drbd8.txt" $
256 ( VersionInfo (Just "8.0.12") (Just "86") (Just "86") Nothing
257 (Just "5c9f89594553e32adb87d9638dce591782f947e3")
260 [ DeviceInfo 0 Connected (LocalRemote Primary Secondary)
261 (LocalRemote UpToDate UpToDate) 'C' "r---"
262 (PerfIndicators 4375577 0 4446279 674 1067 69 0 0 0 0 Nothing
265 (Just $ AdditionalInfo 0 61 0 0 0 0 0)
266 (Just $ AdditionalInfo 0 257 793749 1067 0 0 1067)
268 DeviceInfo 1 Connected (LocalRemote Secondary Primary)
269 (LocalRemote UpToDate UpToDate) 'C' "r---"
270 (PerfIndicators 738320 0 738320 554400 67 0 0 0 0 0 Nothing
273 (Just $ AdditionalInfo 0 61 0 0 0 0 0)
274 (Just $ AdditionalInfo 0 257 92464 67 0 0 67)
276 UnconfiguredDevice 2,
277 DeviceInfo 4 WFConnection (LocalRemote Primary Unknown)
278 (LocalRemote UpToDate DUnknown) 'C' "r---"
279 (PerfIndicators 738320 0 738320 554400 67 0 0 0 0 0 Nothing
282 (Just $ AdditionalInfo 0 61 0 0 0 0 0)
283 (Just $ AdditionalInfo 0 257 92464 67 0 0 67)
285 DeviceInfo 5 Connected (LocalRemote Primary Secondary)
286 (LocalRemote UpToDate Diskless) 'C' "r---"
287 (PerfIndicators 4375581 0 4446283 674 1069 69 0 0 0 0 Nothing
290 (Just $ AdditionalInfo 0 61 0 0 0 0 0)
291 (Just $ AdditionalInfo 0 257 793750 1069 0 0 1069)
293 DeviceInfo 6 Connected (LocalRemote Secondary Primary)
294 (LocalRemote Diskless UpToDate) 'C' "r---"
295 (PerfIndicators 0 4375581 5186925 327 75 214 0 0 0 0 Nothing
301 DeviceInfo 7 WFConnection (LocalRemote Secondary Unknown)
302 (LocalRemote UpToDate DUnknown) 'C' "r---"
303 (PerfIndicators 0 0 0 0 0 0 0 0 0 0 Nothing Nothing Nothing)
305 (Just $ AdditionalInfo 0 61 0 0 0 0 0)
306 (Just $ AdditionalInfo 0 257 0 0 0 0 0)
308 DeviceInfo 8 StandAlone (LocalRemote Secondary Unknown)
309 (LocalRemote UpToDate DUnknown) ' ' "r---"
310 (PerfIndicators 0 0 0 0 0 0 0 0 0 0 Nothing Nothing Nothing)
312 (Just $ AdditionalInfo 0 61 0 0 0 0 0)
313 (Just $ AdditionalInfo 0 257 0 0 0 0 0)
317 -- | Function for splitting a list in chunks of a given size.
318 -- FIXME: an equivalent function exists in Data.List.Split, but it seems
319 -- pointless to add this package as a dependence just for this single
320 -- use. In case it is ever added, just remove this function definition
321 -- and use the one from the package.
322 splitEvery :: Int -> [e] -> [[e]]
323 splitEvery i l = map (take i) (splitter l (:) []) where
325 splitter li c n = li `c` splitter (drop i li) c n
327 -- | Function for testing whether a single comma-separated integer is
329 testCommaInt :: String -> Int -> Assertion
330 testCommaInt numString expectedResult =
331 case A.parseOnly commaIntParser $ pack numString of
332 Left msg -> assertFailure $ "Parsing failed: " ++ msg
333 Right obtained -> assertEqual numString expectedResult obtained
335 -- | Generate a property test for CommaInt numbers in a given range.
336 gen_prop_CommaInt :: Int -> Int -> Property
337 gen_prop_CommaInt minVal maxVal =
338 forAll (choose (minVal, maxVal)) $ \i ->
339 case A.parseOnly commaIntParser $ pack (generateCommaInt i) of
340 Left msg -> failTest $ "Parsing failed: " ++ msg
341 Right obtained -> i ==? obtained
342 where generateCommaInt x =
343 ((reverse . intercalate ",") . splitEvery 3) . reverse $ show x
345 -- | Test if <4 digit integers are recognized correctly.
346 prop_commaInt_noCommas :: Property
347 prop_commaInt_noCommas = gen_prop_CommaInt 0 999
349 -- | Test if integers with 1 comma are recognized correctly.
350 prop_commaInt_1Comma :: Property
351 prop_commaInt_1Comma = gen_prop_CommaInt 1000 999999
353 -- | Test if integers with multiple commas are recognized correctly.
354 prop_commaInt_multipleCommas :: Property
355 prop_commaInt_multipleCommas = gen_prop_CommaInt 1000000 (maxBound ::
358 -- | Test whether the parser is actually able to behave as intended with
359 -- numbers without commas. That is, if a number with more than 3 digits
360 -- is parsed, only up to the first 3 digits are considered (because they
361 -- are a valid commaInt), and the rest is ignored.
362 -- e.g.: parse "1234" = 123
363 prop_commaInt_max3WithoutComma :: Property
364 prop_commaInt_max3WithoutComma =
365 forAll (choose (0, maxBound :: Int)) $ \i ->
366 case A.parseOnly commaIntParser $ pack (show i) of
367 Left msg -> failTest $ "Parsing failed: " ++ msg
370 getFirst3Digits i ==? obtained
371 where getFirst3Digits x =
373 then getFirst3Digits $ x `div` 10
376 -- | Test if non-triplets are handled correctly (they are assumed NOT being part
378 case_commaInt_non_triplet :: Assertion
379 case_commaInt_non_triplet = testCommaInt "61,736,12" 61736
382 testSuite "Block/Drbd/Parser"
383 [ 'case_drbd80_emptyline,
384 'case_drbd83_sync_krnl2_6_39,
386 'case_drbd83_sync_want,
389 'case_commaInt_non_triplet,
390 'prop_commaInt_noCommas,
391 'prop_commaInt_1Comma,
392 'prop_commaInt_multipleCommas,
393 'prop_commaInt_max3WithoutComma