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