Debian Codegraph @

test/hs/Test/Ganeti/HTools/Backend/MonD.hs

raw
1 {-# LANGUAGE TemplateHaskell #-}
2
3 {-| Unittests for htools' ganeti-mond backend
4
5 -}
6
7 {-
8
9 Copyright (C) 2015 Google Inc.
10 All rights reserved.
11
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are
14 met:
15
16 1. Redistributions of source code must retain the above copyright notice,
17 this list of conditions and the following disclaimer.
18
19 2. Redistributions in binary form must reproduce the above copyright
20 notice, this list of conditions and the following disclaimer in the
21 documentation and/or other materials provided with the distribution.
22
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
27 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
35 -}
36
37 module Test.Ganeti.HTools.Backend.MonD
38 (testHTools_Backend_MonD
39 ) where
40
41 import qualified Test.HUnit as HUnit
42 import qualified Text.JSON as J
43
44 import qualified Ganeti.BasicTypes as BT
45 import qualified Ganeti.DataCollectors.CPUload as CPUload
46
47 import Ganeti.Cpu.Types (CPUavgload(..))
48 import Ganeti.DataCollectors.Types (DCReport(..))
49 import Ganeti.HTools.Backend.MonD
50 import Ganeti.JSON
51 import Test.Ganeti.TestCommon
52 import Test.Ganeti.TestHelper
53
54 {-# ANN module "HLint: ignore Use camelCase" #-}
55
56 -- | Test a MonD data file.
57 case_parseMonDData :: HUnit.Assertion
58 case_parseMonDData = do
59 let mond_data_file = "mond-data.txt"
60 n1 = "node1.example.com"
61 n2 = "node2.example.com"
62 t1 = 1379507272000000000
63 t2 = 1379507280000000000
64 cpu_number1 = 4
65 cpu_number2 = 2
66 cpus1 = [ 0.04108859597350646,0.04456554528165781
67 , 0.06203619909502262,0.05595448881893895]
68 cpus2 = [0.004155409618511363,0.0034586452012150787]
69 cpu_total1 = 0.203643517607712
70 cpu_total2 = 0.007614031289927129
71 dcr1 = DCReport CPUload.dcName CPUload.dcVersion CPUload.dcFormatVersion
72 t1 CPUload.dcCategory CPUload.dcKind
73 (J.showJSON (CPUavgload cpu_number1 cpus1 cpu_total1))
74 dcr2 = DCReport CPUload.dcName CPUload.dcVersion CPUload.dcFormatVersion
75 t2 CPUload.dcCategory CPUload.dcKind
76 (J.showJSON (CPUavgload cpu_number2 cpus2 cpu_total2))
77 expected_list = [(n1,[dcr1]),(n2,[dcr2])]
78 ans <- readTestData mond_data_file
79 case pMonDData ans of
80 BT.Ok l -> HUnit.assertBool ("Parsing " ++ mond_data_file ++ " failed")
81 (isAlEqual expected_list l)
82 BT.Bad s -> HUnit.assertFailure $ "Parsing failed: " ++ s
83
84 -- | Check for quality two list of tuples.
85 isAlEqual :: [(String, [DCReport])] -> [(String, [DCReport])] -> Bool
86 isAlEqual a b = and (zipWith tupleIsAlEqual a b)
87
88 -- | Check a tuple for quality.
89 tupleIsAlEqual :: (String, [DCReport]) -> (String, [DCReport]) -> Bool
90 tupleIsAlEqual (na, a) (nb, b) =
91 na == nb
92 && and (zipWith dcReportIsAlmostEqual a b)
93
94 -- | Check if two DCReports are equal. Only reports from CPUload Data
95 -- Collectors are supported.
96 dcReportIsAlmostEqual :: DCReport -> DCReport -> Bool
97 dcReportIsAlmostEqual a b =
98 dcReportName a == dcReportName b
99 && dcReportVersion a == dcReportVersion b
100 && dcReportFormatVersion a == dcReportFormatVersion b
101 && dcReportTimestamp a == dcReportTimestamp b
102 && dcReportCategory a == dcReportCategory b
103 && dcReportKind a == dcReportKind b
104 && case () of
105 _ | CPUload.dcName == dcReportName a ->
106 cpuavgloadDataIsAlmostEq (dcReportData a) (dcReportData b)
107 | otherwise -> False
108
109 -- | Converts two JSValue objects and compares them.
110 cpuavgloadDataIsAlmostEq :: J.JSValue -> J.JSValue -> Bool
111 cpuavgloadDataIsAlmostEq a b =
112 case fromJVal a :: BT.Result CPUavgload of
113 BT.Bad _ -> False
114 BT.Ok cavA ->
115 case fromJVal b :: BT.Result CPUavgload of
116 BT.Bad _ -> False
117 BT.Ok cavB -> compareCPUavgload cavA cavB
118
119 -- | Compares two CPuavgload objects.
120 compareCPUavgload :: CPUavgload -> CPUavgload -> Bool
121 compareCPUavgload a b =
122 let relError x y = relativeError x y <= 1e-9
123 in cavCpuNumber a == cavCpuNumber b
124 && relError (cavCpuTotal a) (cavCpuTotal b)
125 && length (cavCpus a) == length (cavCpus b)
126 && and (zipWith relError (cavCpus a) (cavCpus b))
127
128 testSuite "HTools/Backend/MonD"
129 [ 'case_parseMonDData
130 ]