Debian Codegraph @

test/hs/Test/Ganeti/Query/Instance.hs

raw
1 {-# LANGUAGE TupleSections, TemplateHaskell #-}
2
3 {-| Unittests for Instance Queries.
4
5 -}
6
7 {-
8
9 Copyright (C) 2013 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.Query.Instance
38 ( testQuery_Instance
39 ) where
40
41 import qualified Data.ByteString.UTF8 as UTF8
42 import qualified Data.Map as Map
43 import System.Time (ClockTime(..))
44
45 import Ganeti.JSON
46 import Ganeti.Objects
47 import Ganeti.Query.Instance
48 import Ganeti.Rpc
49 import Ganeti.Types
50
51 import Test.Ganeti.TestHelper
52 import Test.HUnit
53
54 {-# ANN module "HLint: ignore Use camelCase" #-}
55
56 -- | Creates an instance with the desired name, pnode uuid,
57 -- 'AdminState', and 'AdminStateSource'. All other fields are
58 -- placeholders.
59 createInstance :: String -> String -> AdminState -> AdminStateSource -> Instance
60 createInstance name pnodeUuid adminState adminStateSource =
61 RealInstance $ RealInstanceData name pnodeUuid "" Kvm
62 (GenericContainer Map.empty)
63 (PartialBeParams Nothing Nothing Nothing Nothing Nothing Nothing)
64 (GenericContainer Map.empty) (GenericContainer Map.empty)
65 adminState adminStateSource [] [] False Nothing epochTime epochTime
66 (UTF8.fromString "") 0 emptyTagSet
67 where epochTime = TOD 0 0
68
69 -- | A fake InstanceInfo to be used to check values.
70 fakeInstanceInfo :: InstanceInfo
71 fakeInstanceInfo = InstanceInfo 0 InstanceStateRunning 0 0
72
73 -- | Erroneous node response - the exact error does not matter.
74 responseError :: String -> (String, ERpcError a)
75 responseError name = (name, Left . RpcResultError $ "Insignificant error")
76
77 -- | Successful response - the error does not really matter.
78 responseSuccess :: String
79 -> [String]
80 -> (String, ERpcError RpcResultAllInstancesInfo)
81 responseSuccess name instNames = (name, Right .
82 RpcResultAllInstancesInfo . map (, fakeInstanceInfo) $ instNames)
83
84 -- | The instance used for testing. Called Waldo as test cases involve trouble
85 -- finding information related to it.
86 waldoInstance :: Instance
87 waldoInstance = createInstance "Waldo" "prim" AdminUp AdminSource
88
89 -- | Check that an error is thrown when the node is offline
90 case_nodeOffline :: Assertion
91 case_nodeOffline =
92 let responses = [ responseError "prim"
93 , responseError "second"
94 , responseSuccess "node" ["NotWaldo", "DefinitelyNotWaldo"]
95 ]
96 in case getInstanceInfo responses waldoInstance of
97 Left _ -> return ()
98 Right _ -> assertFailure
99 "Error occurred when instance info is missing and node is offline"
100
101 -- | Check that a Right Nothing is returned when the node is online, yet no info
102 -- is present anywhere in the system.
103 case_nodeOnlineNoInfo :: Assertion
104 case_nodeOnlineNoInfo =
105 let responses = [ responseSuccess "prim" ["NotWaldo1"]
106 , responseSuccess "second" ["NotWaldo2"]
107 , responseError "node"
108 ]
109 in case getInstanceInfo responses waldoInstance of
110 Left _ -> assertFailure
111 "Error occurred when instance info could be found on primary"
112 Right Nothing -> return ()
113 Right _ -> assertFailure
114 "Some instance info found when none should be"
115
116 -- | Check the case when the info is on the primary node
117 case_infoOnPrimary :: Assertion
118 case_infoOnPrimary =
119 let responses = [ responseSuccess "prim" ["NotWaldo1", "Waldo"]
120 , responseSuccess "second" ["NotWaldo2"]
121 , responseSuccess "node" ["NotWaldo3"]
122 ]
123 in case getInstanceInfo responses waldoInstance of
124 Left _ -> assertFailure
125 "Cannot retrieve instance info when present on primary node"
126 Right (Just (_, True)) -> return ()
127 Right _ -> assertFailure
128 "Instance info not found on primary node, despite being there"
129
130 -- | Check the case when the info is on the primary node
131 case_infoOnSecondary :: Assertion
132 case_infoOnSecondary =
133 let responses = [ responseSuccess "prim" ["NotWaldo1"]
134 , responseSuccess "second" ["Waldo", "NotWaldo2"]
135 , responseError "node"
136 ]
137 in case getInstanceInfo responses waldoInstance of
138 Left _ -> assertFailure
139 "Cannot retrieve instance info when present on secondary node"
140 Right (Just (_, False)) -> return ()
141 Right _ -> assertFailure
142 "Instance info not found on secondary node, despite being there"
143
144 testSuite "Query_Instance"
145 [ 'case_nodeOffline
146 , 'case_nodeOnlineNoInfo
147 , 'case_infoOnPrimary
148 , 'case_infoOnSecondary
149 ]