Add unit tests for the Diskstats parser
authorMichele Tartara <mtartara@google.com>
Tue, 28 May 2013 21:16:55 +0000 (17:16 -0400)
committerMichele Tartara <mtartara@google.com>
Mon, 3 Jun 2013 15:09:36 +0000 (11:09 -0400)
Add one test parsing an actual /proc/diskstats file and one QuickCheck
test.

Signed-off-by: Michele Tartara <mtartara@google.com>
Reviewed-by: Klaus Aehlig <aehlig@google.com>

Makefile.am
test/data/proc_diskstats.txt [new file with mode: 0644]
test/hs/Test/Ganeti/Block/Diskstats/Parser.hs [new file with mode: 0644]
test/hs/htest.hs

index 035f17b..76d4846 100644 (file)
@@ -80,6 +80,7 @@ HS_DIRS = \
        test/hs/Test \
        test/hs/Test/Ganeti \
        test/hs/Test/Ganeti/Block \
+       test/hs/Test/Ganeti/Block/Diskstats \
        test/hs/Test/Ganeti/Block/Drbd \
        test/hs/Test/Ganeti/Confd \
        test/hs/Test/Ganeti/HTools \
@@ -615,6 +616,7 @@ HS_LIB_SRCS = \
 HS_TEST_SRCS = \
        test/hs/Test/Ganeti/Attoparsec.hs \
        test/hs/Test/Ganeti/BasicTypes.hs \
+       test/hs/Test/Ganeti/Block/Diskstats/Parser.hs \
        test/hs/Test/Ganeti/Block/Drbd/Parser.hs \
        test/hs/Test/Ganeti/Block/Drbd/Types.hs \
        test/hs/Test/Ganeti/Common.hs \
@@ -1145,6 +1147,7 @@ TEST_FILES = \
        test/data/ovfdata/wrong_manifest.ovf \
        test/data/ovfdata/wrong_ova.ova \
        test/data/ovfdata/wrong_xml.ovf \
+       test/data/proc_diskstats.txt \
        test/data/proc_drbd8.txt \
        test/data/proc_drbd80-emptyline.txt \
        test/data/proc_drbd80-emptyversion.txt \
diff --git a/test/data/proc_diskstats.txt b/test/data/proc_diskstats.txt
new file mode 100644 (file)
index 0000000..04d7c56
--- /dev/null
@@ -0,0 +1,32 @@
+   1       0 ram0 0 0 0 0 0 0 0 0 0 0 0
+   1       1 ram1 0 0 0 0 0 0 0 0 0 0 0
+   1       2 ram2 0 0 0 0 0 0 0 0 0 0 0
+   1       3 ram3 0 0 0 0 0 0 0 0 0 0 0
+   1       4 ram4 0 0 0 0 0 0 0 0 0 0 0
+   1       5 ram5 0 0 0 0 0 0 0 0 0 0 0
+   1       6 ram6 0 0 0 0 0 0 0 0 0 0 0
+   1       7 ram7 0 0 0 0 0 0 0 0 0 0 0
+   1       8 ram8 0 0 0 0 0 0 0 0 0 0 0
+   1       9 ram9 0 0 0 0 0 0 0 0 0 0 0
+   1      10 ram10 0 0 0 0 0 0 0 0 0 0 0
+   1      11 ram11 0 0 0 0 0 0 0 0 0 0 0
+   1      12 ram12 0 0 0 0 0 0 0 0 0 0 0
+   1      13 ram13 0 0 0 0 0 0 0 0 0 0 0
+   1      14 ram14 0 0 0 0 0 0 0 0 0 0 0
+   1      15 ram15 0 0 0 0 0 0 0 0 0 0 0
+   7       0 loop0 0 0 0 0 0 0 0 0 0 0 0
+   7       1 loop1 0 0 0 0 0 0 0 0 0 0 0
+   7       2 loop2 0 0 0 0 0 0 0 0 0 0 0
+   7       3 loop3 0 0 0 0 0 0 0 0 0 0 0
+   7       4 loop4 0 0 0 0 0 0 0 0 0 0 0
+   7       5 loop5 0 0 0 0 0 0 0 0 0 0 0
+   7       6 loop6 0 0 0 0 0 0 0 0 0 0 0
+   7       7 loop7 0 0 0 0 0 0 0 0 0 0 0
+   8       0 sda 89502 4833 4433387 89244 519115 62738 16059726 465120 0 149148 554564
+   8       1 sda1 505 2431 8526 132 478 174 124358 8500 0 340 8632
+   8       2 sda2 2 0 4 4 0 0 0 0 0 4 4
+   8       5 sda5 88802 2269 4422249 89032 453703 62564 15935368 396244 0 90064 485500
+ 252       0 dm-0 90978 0 4420002 158632 582226 0 15935368 5592012 0 167688 5750652
+ 252       1 dm-1 88775 0 4402378 157204 469594 0 15136008 4910424 0 164556 5067640
+ 252       2 dm-2 1956 0 15648 1052 99920 0 799360 682492 0 4516 683552
+   8      16 sdb 0 0 0 0 0 0 0 0 0 0 0
diff --git a/test/hs/Test/Ganeti/Block/Diskstats/Parser.hs b/test/hs/Test/Ganeti/Block/Diskstats/Parser.hs
new file mode 100644 (file)
index 0000000..c468879
--- /dev/null
@@ -0,0 +1,118 @@
+{-# LANGUAGE TemplateHaskell #-}
+{-# OPTIONS_GHC -fno-warn-orphans #-}
+
+{-| Unittests for the @/proc/diskstats@ parser -}
+
+{-
+
+Copyright (C) 2013 Google Inc.
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301, USA.
+
+-}
+
+module Test.Ganeti.Block.Diskstats.Parser (testBlock_Diskstats_Parser) where
+
+import Test.QuickCheck as QuickCheck hiding (Result)
+import Test.HUnit
+
+import Test.Ganeti.TestHelper
+import Test.Ganeti.TestCommon
+
+import Control.Applicative ((<*>), (<$>))
+import qualified Data.Attoparsec.Text as A
+import Data.Text (pack)
+import Text.Printf
+
+import Ganeti.Block.Diskstats.Parser (diskstatsParser)
+import Ganeti.Block.Diskstats.Types
+
+{-# ANN module "HLint: ignore Use camelCase" #-}
+
+
+-- | Test a diskstats.
+case_diskstats :: Assertion
+case_diskstats = testParser diskstatsParser "proc_diskstats.txt"
+  [ Diskstats 1 0 "ram0" 0 0 0 0 0 0 0 0 0 0 0
+  , Diskstats 1 1 "ram1" 0 0 0 0 0 0 0 0 0 0 0
+  , Diskstats 1 2 "ram2" 0 0 0 0 0 0 0 0 0 0 0
+  , Diskstats 1 3 "ram3" 0 0 0 0 0 0 0 0 0 0 0
+  , Diskstats 1 4 "ram4" 0 0 0 0 0 0 0 0 0 0 0
+  , Diskstats 1 5 "ram5" 0 0 0 0 0 0 0 0 0 0 0
+  , Diskstats 1 6 "ram6" 0 0 0 0 0 0 0 0 0 0 0
+  , Diskstats 1 7 "ram7" 0 0 0 0 0 0 0 0 0 0 0
+  , Diskstats 1 8 "ram8" 0 0 0 0 0 0 0 0 0 0 0
+  , Diskstats 1 9 "ram9" 0 0 0 0 0 0 0 0 0 0 0
+  , Diskstats 1 10 "ram10" 0 0 0 0 0 0 0 0 0 0 0
+  , Diskstats 1 11 "ram11" 0 0 0 0 0 0 0 0 0 0 0
+  , Diskstats 1 12 "ram12" 0 0 0 0 0 0 0 0 0 0 0
+  , Diskstats 1 13 "ram13" 0 0 0 0 0 0 0 0 0 0 0
+  , Diskstats 1 14 "ram14" 0 0 0 0 0 0 0 0 0 0 0
+  , Diskstats 1 15 "ram15" 0 0 0 0 0 0 0 0 0 0 0
+  , Diskstats 7 0 "loop0" 0 0 0 0 0 0 0 0 0 0 0
+  , Diskstats 7 1 "loop1" 0 0 0 0 0 0 0 0 0 0 0
+  , Diskstats 7 2 "loop2" 0 0 0 0 0 0 0 0 0 0 0
+  , Diskstats 7 3 "loop3" 0 0 0 0 0 0 0 0 0 0 0
+  , Diskstats 7 4 "loop4" 0 0 0 0 0 0 0 0 0 0 0
+  , Diskstats 7 5 "loop5" 0 0 0 0 0 0 0 0 0 0 0
+  , Diskstats 7 6 "loop6" 0 0 0 0 0 0 0 0 0 0 0
+  , Diskstats 7 7 "loop7" 0 0 0 0 0 0 0 0 0 0 0
+  , Diskstats 8 0 "sda" 89502 4833 4433387 89244 519115 62738 16059726 465120 0
+    149148 554564
+  , Diskstats 8 1 "sda1" 505 2431 8526 132 478 174 124358 8500 0 340 8632
+  , Diskstats 8 2 "sda2" 2 0 4 4 0 0 0 0 0 4 4
+  , Diskstats 8 5 "sda5" 88802 2269 4422249 89032 453703 62564 15935368 396244 0
+    90064 485500
+  , Diskstats 252 0 "dm-0" 90978 0 4420002 158632 582226 0 15935368 5592012 0
+  167688 5750652
+  , Diskstats 252 1 "dm-1" 88775 0 4402378 157204 469594 0 15136008 4910424 0
+  164556 5067640
+  , Diskstats 252 2 "dm-2" 1956 0 15648 1052 99920 0 799360 682492 0 4516 683552
+  , Diskstats 8 16 "sdb" 0 0 0 0 0 0 0 0 0 0 0
+  ]
+
+-- | The instance for generating arbitrary Diskstats
+instance Arbitrary Diskstats where
+  arbitrary =
+    Diskstats <$> genNonNegative <*> genNonNegative <*> genName
+              <*> genNonNegative <*> genNonNegative <*> genNonNegative
+              <*> genNonNegative <*> genNonNegative <*> genNonNegative
+              <*> genNonNegative <*> genNonNegative <*> genNonNegative
+              <*> genNonNegative <*> genNonNegative
+
+-- | Serialize a list of Diskstats in a parsable way
+serializeDiskstatsList :: [Diskstats] -> String
+serializeDiskstatsList = concatMap serializeDiskstats
+
+-- | Serialize a Diskstats in a parsable way
+serializeDiskstats :: Diskstats -> String
+serializeDiskstats ds =
+  printf "\t%d\t%d %s %d %d %d %d %d %d %d %d %d %d %d\n"
+    (dsMajor ds) (dsMinor ds) (dsName ds) (dsReads ds) (dsMergedReads ds)
+    (dsSecRead ds) (dsTimeRead ds) (dsWrites ds) (dsMergedWrites ds)
+    (dsSecWritten ds) (dsTimeWrite ds) (dsIos ds) (dsTimeIO ds) (dsWIOmillis ds)
+
+-- | Test whether an arbitrary Diskstats is parsed correctly
+prop_diskstats :: [Diskstats] -> Property
+prop_diskstats dsList =
+    case A.parseOnly diskstatsParser $ pack (serializeDiskstatsList dsList) of
+      Left msg -> failTest $ "Parsing failed: " ++ msg
+      Right obtained -> dsList ==? obtained
+
+testSuite "Block/Diskstats/Parser"
+          [ 'case_diskstats,
+            'prop_diskstats
+          ]
index b93ba81..1d72571 100644 (file)
@@ -33,6 +33,7 @@ import System.Log.Logger
 import Test.Ganeti.TestImports ()
 import Test.Ganeti.Attoparsec
 import Test.Ganeti.BasicTypes
+import Test.Ganeti.Block.Diskstats.Parser
 import Test.Ganeti.Block.Drbd.Parser
 import Test.Ganeti.Block.Drbd.Types
 import Test.Ganeti.Common
@@ -91,6 +92,7 @@ allTests =
   , testConfd_Types
   , testConfd_Utils
   , testDaemon
+  , testBlock_Diskstats_Parser
   , testBlock_Drbd_Parser
   , testBlock_Drbd_Types
   , testErrors