root / test / hs / Test / Ganeti / OpCodes.hs @ 896cc964
History | View | Annotate | Download (25.8 kB)
1 | aed2325f | Iustin Pop | {-# LANGUAGE TemplateHaskell #-} |
---|---|---|---|
2 | aed2325f | Iustin Pop | {-# OPTIONS_GHC -fno-warn-orphans #-} |
3 | aed2325f | Iustin Pop | |
4 | aed2325f | Iustin Pop | {-| Unittests for ganeti-htools. |
5 | aed2325f | Iustin Pop | |
6 | aed2325f | Iustin Pop | -} |
7 | aed2325f | Iustin Pop | |
8 | aed2325f | Iustin Pop | {- |
9 | aed2325f | Iustin Pop | |
10 | 72747d91 | Iustin Pop | Copyright (C) 2009, 2010, 2011, 2012, 2013 Google Inc. |
11 | aed2325f | Iustin Pop | |
12 | aed2325f | Iustin Pop | This program is free software; you can redistribute it and/or modify |
13 | aed2325f | Iustin Pop | it under the terms of the GNU General Public License as published by |
14 | aed2325f | Iustin Pop | the Free Software Foundation; either version 2 of the License, or |
15 | aed2325f | Iustin Pop | (at your option) any later version. |
16 | aed2325f | Iustin Pop | |
17 | aed2325f | Iustin Pop | This program is distributed in the hope that it will be useful, but |
18 | aed2325f | Iustin Pop | WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | aed2325f | Iustin Pop | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
20 | aed2325f | Iustin Pop | General Public License for more details. |
21 | aed2325f | Iustin Pop | |
22 | aed2325f | Iustin Pop | You should have received a copy of the GNU General Public License |
23 | aed2325f | Iustin Pop | along with this program; if not, write to the Free Software |
24 | aed2325f | Iustin Pop | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
25 | aed2325f | Iustin Pop | 02110-1301, USA. |
26 | aed2325f | Iustin Pop | |
27 | aed2325f | Iustin Pop | -} |
28 | aed2325f | Iustin Pop | |
29 | aed2325f | Iustin Pop | module Test.Ganeti.OpCodes |
30 | aed2325f | Iustin Pop | ( testOpCodes |
31 | aed2325f | Iustin Pop | , OpCodes.OpCode(..) |
32 | aed2325f | Iustin Pop | ) where |
33 | aed2325f | Iustin Pop | |
34 | f56013fd | Iustin Pop | import Test.HUnit as HUnit |
35 | f56013fd | Iustin Pop | import Test.QuickCheck as QuickCheck |
36 | aed2325f | Iustin Pop | |
37 | aed2325f | Iustin Pop | import Control.Applicative |
38 | dc4b5c42 | Iustin Pop | import Control.Monad |
39 | dc4b5c42 | Iustin Pop | import Data.Char |
40 | aed2325f | Iustin Pop | import Data.List |
41 | c66f09f5 | Iustin Pop | import qualified Data.Map as Map |
42 | aed2325f | Iustin Pop | import qualified Text.JSON as J |
43 | 8d239fa4 | Iustin Pop | import Text.Printf (printf) |
44 | aed2325f | Iustin Pop | |
45 | aed2325f | Iustin Pop | import Test.Ganeti.TestHelper |
46 | aed2325f | Iustin Pop | import Test.Ganeti.TestCommon |
47 | c7d249d0 | Iustin Pop | import Test.Ganeti.Types () |
48 | 6e94b75c | Jose A. Lopes | import Test.Ganeti.Query.Language () |
49 | aed2325f | Iustin Pop | |
50 | f56013fd | Iustin Pop | import Ganeti.BasicTypes |
51 | aed2325f | Iustin Pop | import qualified Ganeti.Constants as C |
52 | aed2325f | Iustin Pop | import qualified Ganeti.OpCodes as OpCodes |
53 | c7d249d0 | Iustin Pop | import Ganeti.Types |
54 | c7d249d0 | Iustin Pop | import Ganeti.OpParams |
55 | c66f09f5 | Iustin Pop | import Ganeti.JSON |
56 | aed2325f | Iustin Pop | |
57 | 5b11f8db | Iustin Pop | {-# ANN module "HLint: ignore Use camelCase" #-} |
58 | 5b11f8db | Iustin Pop | |
59 | aed2325f | Iustin Pop | -- * Arbitrary instances |
60 | aed2325f | Iustin Pop | |
61 | de36f091 | Jose A. Lopes | instance (Ord k, Arbitrary k, Arbitrary a) => Arbitrary (Map.Map k a) where |
62 | de36f091 | Jose A. Lopes | arbitrary = Map.fromList <$> arbitrary |
63 | 367c4241 | Dato Simó | |
64 | 6e94b75c | Jose A. Lopes | arbitraryOpTagsGet :: Gen OpCodes.OpCode |
65 | 6e94b75c | Jose A. Lopes | arbitraryOpTagsGet = do |
66 | 6e94b75c | Jose A. Lopes | kind <- arbitrary |
67 | 6e94b75c | Jose A. Lopes | OpCodes.OpTagsSet kind <$> arbitrary <*> genOpCodesTagName kind |
68 | 6e94b75c | Jose A. Lopes | |
69 | 6e94b75c | Jose A. Lopes | arbitraryOpTagsSet :: Gen OpCodes.OpCode |
70 | 6e94b75c | Jose A. Lopes | arbitraryOpTagsSet = do |
71 | 6e94b75c | Jose A. Lopes | kind <- arbitrary |
72 | 6e94b75c | Jose A. Lopes | OpCodes.OpTagsSet kind <$> genTags <*> genOpCodesTagName kind |
73 | 6e94b75c | Jose A. Lopes | |
74 | 6e94b75c | Jose A. Lopes | arbitraryOpTagsDel :: Gen OpCodes.OpCode |
75 | 6e94b75c | Jose A. Lopes | arbitraryOpTagsDel = do |
76 | 6e94b75c | Jose A. Lopes | kind <- arbitrary |
77 | 6e94b75c | Jose A. Lopes | OpCodes.OpTagsDel kind <$> genTags <*> genOpCodesTagName kind |
78 | 6e94b75c | Jose A. Lopes | |
79 | 7022db83 | Iustin Pop | $(genArbitrary ''OpCodes.ReplaceDisksMode) |
80 | aed2325f | Iustin Pop | |
81 | 6d558717 | Iustin Pop | $(genArbitrary ''DiskAccess) |
82 | 6d558717 | Iustin Pop | |
83 | f198cf91 | Thomas Thrainer | $(genArbitrary ''ImportExportCompression) |
84 | f198cf91 | Thomas Thrainer | |
85 | aed2325f | Iustin Pop | instance Arbitrary OpCodes.DiskIndex where |
86 | aed2325f | Iustin Pop | arbitrary = choose (0, C.maxDisks - 1) >>= OpCodes.mkDiskIndex |
87 | aed2325f | Iustin Pop | |
88 | 5ef4fbb1 | Iustin Pop | instance Arbitrary INicParams where |
89 | 5006418e | Iustin Pop | arbitrary = INicParams <$> genMaybe genNameNE <*> genMaybe genName <*> |
90 | 6e94b75c | Jose A. Lopes | genMaybe genNameNE <*> genMaybe genNameNE <*> |
91 | 6e94b75c | Jose A. Lopes | genMaybe genNameNE <*> genMaybe genNameNE <*> |
92 | 6e94b75c | Jose A. Lopes | genMaybe genNameNE |
93 | c66f09f5 | Iustin Pop | |
94 | 6d558717 | Iustin Pop | instance Arbitrary IDiskParams where |
95 | 6d558717 | Iustin Pop | arbitrary = IDiskParams <$> arbitrary <*> arbitrary <*> |
96 | 5006418e | Iustin Pop | genMaybe genNameNE <*> genMaybe genNameNE <*> |
97 | 4e4433e8 | Christos Stavrakakis | genMaybe genNameNE <*> genMaybe genNameNE |
98 | 6d558717 | Iustin Pop | |
99 | c2d3219b | Iustin Pop | instance Arbitrary RecreateDisksInfo where |
100 | c2d3219b | Iustin Pop | arbitrary = oneof [ pure RecreateDisksAll |
101 | c2d3219b | Iustin Pop | , RecreateDisksIndices <$> arbitrary |
102 | c2d3219b | Iustin Pop | , RecreateDisksParams <$> arbitrary |
103 | c2d3219b | Iustin Pop | ] |
104 | c2d3219b | Iustin Pop | |
105 | c2d3219b | Iustin Pop | instance Arbitrary DdmOldChanges where |
106 | c2d3219b | Iustin Pop | arbitrary = oneof [ DdmOldIndex <$> arbitrary |
107 | c2d3219b | Iustin Pop | , DdmOldMod <$> arbitrary |
108 | c2d3219b | Iustin Pop | ] |
109 | c2d3219b | Iustin Pop | |
110 | c2d3219b | Iustin Pop | instance (Arbitrary a) => Arbitrary (SetParamsMods a) where |
111 | c2d3219b | Iustin Pop | arbitrary = oneof [ pure SetParamsEmpty |
112 | c2d3219b | Iustin Pop | , SetParamsDeprecated <$> arbitrary |
113 | c2d3219b | Iustin Pop | , SetParamsNew <$> arbitrary |
114 | c2d3219b | Iustin Pop | ] |
115 | c2d3219b | Iustin Pop | |
116 | 398e9066 | Iustin Pop | instance Arbitrary ExportTarget where |
117 | 398e9066 | Iustin Pop | arbitrary = oneof [ ExportTargetLocal <$> genNodeNameNE |
118 | 398e9066 | Iustin Pop | , ExportTargetRemote <$> pure [] |
119 | 398e9066 | Iustin Pop | ] |
120 | 398e9066 | Iustin Pop | |
121 | aed2325f | Iustin Pop | instance Arbitrary OpCodes.OpCode where |
122 | aed2325f | Iustin Pop | arbitrary = do |
123 | aed2325f | Iustin Pop | op_id <- elements OpCodes.allOpIDs |
124 | aed2325f | Iustin Pop | case op_id of |
125 | aed2325f | Iustin Pop | "OP_TEST_DELAY" -> |
126 | 7d421386 | Iustin Pop | OpCodes.OpTestDelay <$> arbitrary <*> arbitrary <*> |
127 | 1c3231aa | Thomas Thrainer | genNodeNamesNE <*> return Nothing <*> arbitrary |
128 | aed2325f | Iustin Pop | "OP_INSTANCE_REPLACE_DISKS" -> |
129 | da4a52a3 | Thomas Thrainer | OpCodes.OpInstanceReplaceDisks <$> genFQDN <*> return Nothing <*> |
130 | da4a52a3 | Thomas Thrainer | arbitrary <*> arbitrary <*> arbitrary <*> genDiskIndices <*> |
131 | 1c3231aa | Thomas Thrainer | genMaybe genNodeNameNE <*> return Nothing <*> genMaybe genNameNE |
132 | aed2325f | Iustin Pop | "OP_INSTANCE_FAILOVER" -> |
133 | da4a52a3 | Thomas Thrainer | OpCodes.OpInstanceFailover <$> genFQDN <*> return Nothing <*> |
134 | da4a52a3 | Thomas Thrainer | arbitrary <*> arbitrary <*> genMaybe genNodeNameNE <*> |
135 | 1ca326c8 | Thomas Thrainer | return Nothing <*> arbitrary <*> arbitrary <*> genMaybe genNameNE |
136 | aed2325f | Iustin Pop | "OP_INSTANCE_MIGRATE" -> |
137 | da4a52a3 | Thomas Thrainer | OpCodes.OpInstanceMigrate <$> genFQDN <*> return Nothing <*> |
138 | da4a52a3 | Thomas Thrainer | arbitrary <*> arbitrary <*> genMaybe genNodeNameNE <*> |
139 | da4a52a3 | Thomas Thrainer | return Nothing <*> arbitrary <*> arbitrary <*> arbitrary <*> |
140 | da4a52a3 | Thomas Thrainer | genMaybe genNameNE <*> arbitrary |
141 | a451dae2 | Iustin Pop | "OP_TAGS_GET" -> |
142 | 6e94b75c | Jose A. Lopes | arbitraryOpTagsGet |
143 | a451dae2 | Iustin Pop | "OP_TAGS_SEARCH" -> |
144 | a451dae2 | Iustin Pop | OpCodes.OpTagsSearch <$> genNameNE |
145 | 3bebda52 | Dato Simó | "OP_TAGS_SET" -> |
146 | 6e94b75c | Jose A. Lopes | arbitraryOpTagsSet |
147 | 3bebda52 | Dato Simó | "OP_TAGS_DEL" -> |
148 | 6e94b75c | Jose A. Lopes | arbitraryOpTagsDel |
149 | c66f09f5 | Iustin Pop | "OP_CLUSTER_POST_INIT" -> pure OpCodes.OpClusterPostInit |
150 | c66f09f5 | Iustin Pop | "OP_CLUSTER_DESTROY" -> pure OpCodes.OpClusterDestroy |
151 | c66f09f5 | Iustin Pop | "OP_CLUSTER_QUERY" -> pure OpCodes.OpClusterQuery |
152 | c66f09f5 | Iustin Pop | "OP_CLUSTER_VERIFY" -> |
153 | c66f09f5 | Iustin Pop | OpCodes.OpClusterVerify <$> arbitrary <*> arbitrary <*> |
154 | 4651c69f | Jose A. Lopes | genListSet Nothing <*> genListSet Nothing <*> arbitrary <*> |
155 | 5006418e | Iustin Pop | genMaybe genNameNE |
156 | c66f09f5 | Iustin Pop | "OP_CLUSTER_VERIFY_CONFIG" -> |
157 | c66f09f5 | Iustin Pop | OpCodes.OpClusterVerifyConfig <$> arbitrary <*> arbitrary <*> |
158 | 4651c69f | Jose A. Lopes | genListSet Nothing <*> arbitrary |
159 | c66f09f5 | Iustin Pop | "OP_CLUSTER_VERIFY_GROUP" -> |
160 | 5ef4fbb1 | Iustin Pop | OpCodes.OpClusterVerifyGroup <$> genNameNE <*> arbitrary <*> |
161 | 4651c69f | Jose A. Lopes | arbitrary <*> genListSet Nothing <*> genListSet Nothing <*> arbitrary |
162 | c66f09f5 | Iustin Pop | "OP_CLUSTER_VERIFY_DISKS" -> pure OpCodes.OpClusterVerifyDisks |
163 | c66f09f5 | Iustin Pop | "OP_GROUP_VERIFY_DISKS" -> |
164 | 5ef4fbb1 | Iustin Pop | OpCodes.OpGroupVerifyDisks <$> genNameNE |
165 | c66f09f5 | Iustin Pop | "OP_CLUSTER_REPAIR_DISK_SIZES" -> |
166 | 5006418e | Iustin Pop | OpCodes.OpClusterRepairDiskSizes <$> genNodeNamesNE |
167 | c66f09f5 | Iustin Pop | "OP_CLUSTER_CONFIG_QUERY" -> |
168 | 5ef4fbb1 | Iustin Pop | OpCodes.OpClusterConfigQuery <$> genFieldsNE |
169 | c66f09f5 | Iustin Pop | "OP_CLUSTER_RENAME" -> |
170 | 5006418e | Iustin Pop | OpCodes.OpClusterRename <$> genNameNE |
171 | c66f09f5 | Iustin Pop | "OP_CLUSTER_SET_PARAMS" -> |
172 | e5c92cfb | Klaus Aehlig | OpCodes.OpClusterSetParams <$> arbitrary <*> emptyMUD <*> emptyMUD <*> |
173 | 6e94b75c | Jose A. Lopes | arbitrary <*> genMaybe arbitrary <*> |
174 | 5006418e | Iustin Pop | genMaybe genEmptyContainer <*> emptyMUD <*> |
175 | 5006418e | Iustin Pop | genMaybe genEmptyContainer <*> genMaybe genEmptyContainer <*> |
176 | 5006418e | Iustin Pop | genMaybe genEmptyContainer <*> genMaybe arbitrary <*> |
177 | c66f09f5 | Iustin Pop | arbitrary <*> arbitrary <*> arbitrary <*> |
178 | c66f09f5 | Iustin Pop | arbitrary <*> arbitrary <*> arbitrary <*> |
179 | c66f09f5 | Iustin Pop | emptyMUD <*> emptyMUD <*> arbitrary <*> |
180 | 67fc4de7 | Iustin Pop | arbitrary <*> arbitrary <*> arbitrary <*> arbitrary <*> |
181 | 3039e2dc | Helga Velroyen | arbitrary <*> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary <*> |
182 | 5ce621ab | Helga Velroyen | genMaybe genName <*> |
183 | 5ce621ab | Helga Velroyen | genMaybe genName |
184 | c66f09f5 | Iustin Pop | "OP_CLUSTER_REDIST_CONF" -> pure OpCodes.OpClusterRedistConf |
185 | c66f09f5 | Iustin Pop | "OP_CLUSTER_ACTIVATE_MASTER_IP" -> |
186 | c66f09f5 | Iustin Pop | pure OpCodes.OpClusterActivateMasterIp |
187 | c66f09f5 | Iustin Pop | "OP_CLUSTER_DEACTIVATE_MASTER_IP" -> |
188 | c66f09f5 | Iustin Pop | pure OpCodes.OpClusterDeactivateMasterIp |
189 | c66f09f5 | Iustin Pop | "OP_QUERY" -> |
190 | 6e94b75c | Jose A. Lopes | OpCodes.OpQuery <$> arbitrary <*> arbitrary <*> arbitrary <*> |
191 | 6e94b75c | Jose A. Lopes | pure Nothing |
192 | c66f09f5 | Iustin Pop | "OP_QUERY_FIELDS" -> |
193 | c66f09f5 | Iustin Pop | OpCodes.OpQueryFields <$> arbitrary <*> arbitrary |
194 | c66f09f5 | Iustin Pop | "OP_OOB_COMMAND" -> |
195 | 1c3231aa | Thomas Thrainer | OpCodes.OpOobCommand <$> genNodeNamesNE <*> return Nothing <*> |
196 | 1c3231aa | Thomas Thrainer | arbitrary <*> arbitrary <*> arbitrary <*> |
197 | 1c3231aa | Thomas Thrainer | (arbitrary `suchThat` (>0)) |
198 | 1c3231aa | Thomas Thrainer | "OP_NODE_REMOVE" -> |
199 | 1c3231aa | Thomas Thrainer | OpCodes.OpNodeRemove <$> genNodeNameNE <*> return Nothing |
200 | c66f09f5 | Iustin Pop | "OP_NODE_ADD" -> |
201 | 5ef4fbb1 | Iustin Pop | OpCodes.OpNodeAdd <$> genNodeNameNE <*> emptyMUD <*> emptyMUD <*> |
202 | 5cbf7832 | Jose A. Lopes | genMaybe genNameNE <*> genMaybe genNameNE <*> arbitrary <*> |
203 | 5006418e | Iustin Pop | genMaybe genNameNE <*> arbitrary <*> arbitrary <*> emptyMUD |
204 | c66f09f5 | Iustin Pop | "OP_NODE_QUERY" -> |
205 | 3131adc7 | Iustin Pop | OpCodes.OpNodeQuery <$> genFieldsNE <*> genNamesNE <*> arbitrary |
206 | c66f09f5 | Iustin Pop | "OP_NODE_QUERYVOLS" -> |
207 | c66f09f5 | Iustin Pop | OpCodes.OpNodeQueryvols <$> arbitrary <*> genNodeNamesNE |
208 | c66f09f5 | Iustin Pop | "OP_NODE_QUERY_STORAGE" -> |
209 | c66f09f5 | Iustin Pop | OpCodes.OpNodeQueryStorage <$> arbitrary <*> arbitrary <*> |
210 | 6e94b75c | Jose A. Lopes | genNodeNamesNE <*> genMaybe genNameNE |
211 | c66f09f5 | Iustin Pop | "OP_NODE_MODIFY_STORAGE" -> |
212 | 1c3231aa | Thomas Thrainer | OpCodes.OpNodeModifyStorage <$> genNodeNameNE <*> return Nothing <*> |
213 | 6e94b75c | Jose A. Lopes | arbitrary <*> genMaybe genNameNE <*> pure emptyJSObject |
214 | c66f09f5 | Iustin Pop | "OP_REPAIR_NODE_STORAGE" -> |
215 | 1c3231aa | Thomas Thrainer | OpCodes.OpRepairNodeStorage <$> genNodeNameNE <*> return Nothing <*> |
216 | 6e94b75c | Jose A. Lopes | arbitrary <*> genMaybe genNameNE <*> arbitrary |
217 | c66f09f5 | Iustin Pop | "OP_NODE_SET_PARAMS" -> |
218 | 1c3231aa | Thomas Thrainer | OpCodes.OpNodeSetParams <$> genNodeNameNE <*> return Nothing <*> |
219 | 1c3231aa | Thomas Thrainer | arbitrary <*> emptyMUD <*> emptyMUD <*> arbitrary <*> arbitrary <*> |
220 | 1c3231aa | Thomas Thrainer | arbitrary <*> arbitrary <*> arbitrary <*> arbitrary <*> |
221 | 1c3231aa | Thomas Thrainer | genMaybe genNameNE <*> emptyMUD <*> arbitrary |
222 | c66f09f5 | Iustin Pop | "OP_NODE_POWERCYCLE" -> |
223 | 1c3231aa | Thomas Thrainer | OpCodes.OpNodePowercycle <$> genNodeNameNE <*> return Nothing <*> |
224 | 1c3231aa | Thomas Thrainer | arbitrary |
225 | c66f09f5 | Iustin Pop | "OP_NODE_MIGRATE" -> |
226 | 1c3231aa | Thomas Thrainer | OpCodes.OpNodeMigrate <$> genNodeNameNE <*> return Nothing <*> |
227 | 1c3231aa | Thomas Thrainer | arbitrary <*> arbitrary <*> genMaybe genNodeNameNE <*> |
228 | 1c3231aa | Thomas Thrainer | return Nothing <*> arbitrary <*> arbitrary <*> genMaybe genNameNE |
229 | c66f09f5 | Iustin Pop | "OP_NODE_EVACUATE" -> |
230 | c66f09f5 | Iustin Pop | OpCodes.OpNodeEvacuate <$> arbitrary <*> genNodeNameNE <*> |
231 | 1c3231aa | Thomas Thrainer | return Nothing <*> genMaybe genNodeNameNE <*> return Nothing <*> |
232 | 1c3231aa | Thomas Thrainer | genMaybe genNameNE <*> arbitrary |
233 | 6d558717 | Iustin Pop | "OP_INSTANCE_CREATE" -> |
234 | 5006418e | Iustin Pop | OpCodes.OpInstanceCreate <$> genFQDN <*> arbitrary <*> |
235 | 6d558717 | Iustin Pop | arbitrary <*> arbitrary <*> arbitrary <*> arbitrary <*> |
236 | 6e94b75c | Jose A. Lopes | pure emptyJSObject <*> arbitrary <*> arbitrary <*> arbitrary <*> |
237 | 6e94b75c | Jose A. Lopes | genMaybe genNameNE <*> pure emptyJSObject <*> arbitrary <*> |
238 | 6e94b75c | Jose A. Lopes | genMaybe genNameNE <*> arbitrary <*> arbitrary <*> arbitrary <*> |
239 | 6e94b75c | Jose A. Lopes | arbitrary <*> arbitrary <*> arbitrary <*> pure emptyJSObject <*> |
240 | 6e94b75c | Jose A. Lopes | genMaybe genNameNE <*> genMaybe genNodeNameNE <*> return Nothing <*> |
241 | 6e94b75c | Jose A. Lopes | genMaybe genNodeNameNE <*> return Nothing <*> genMaybe (pure []) <*> |
242 | 6e94b75c | Jose A. Lopes | genMaybe genNodeNameNE <*> arbitrary <*> genMaybe genNodeNameNE <*> |
243 | 6e94b75c | Jose A. Lopes | return Nothing <*> genMaybe genNodeNameNE <*> genMaybe genNameNE <*> |
244 | 6e94b75c | Jose A. Lopes | arbitrary <*> (genTags >>= mapM mkNonEmpty) |
245 | c2d3219b | Iustin Pop | "OP_INSTANCE_MULTI_ALLOC" -> |
246 | 6e94b75c | Jose A. Lopes | OpCodes.OpInstanceMultiAlloc <$> arbitrary <*> genMaybe genNameNE <*> |
247 | 6e94b75c | Jose A. Lopes | pure [] |
248 | c2d3219b | Iustin Pop | "OP_INSTANCE_REINSTALL" -> |
249 | da4a52a3 | Thomas Thrainer | OpCodes.OpInstanceReinstall <$> genFQDN <*> return Nothing <*> |
250 | da4a52a3 | Thomas Thrainer | arbitrary <*> genMaybe genNameNE <*> genMaybe (pure emptyJSObject) |
251 | c2d3219b | Iustin Pop | "OP_INSTANCE_REMOVE" -> |
252 | da4a52a3 | Thomas Thrainer | OpCodes.OpInstanceRemove <$> genFQDN <*> return Nothing <*> |
253 | c2d3219b | Iustin Pop | arbitrary <*> arbitrary |
254 | da4a52a3 | Thomas Thrainer | "OP_INSTANCE_RENAME" -> |
255 | da4a52a3 | Thomas Thrainer | OpCodes.OpInstanceRename <$> genFQDN <*> return Nothing <*> |
256 | da4a52a3 | Thomas Thrainer | genNodeNameNE <*> arbitrary <*> arbitrary |
257 | c2d3219b | Iustin Pop | "OP_INSTANCE_STARTUP" -> |
258 | da4a52a3 | Thomas Thrainer | OpCodes.OpInstanceStartup <$> genFQDN <*> return Nothing <*> |
259 | da4a52a3 | Thomas Thrainer | arbitrary <*> arbitrary <*> pure emptyJSObject <*> |
260 | da4a52a3 | Thomas Thrainer | pure emptyJSObject <*> arbitrary <*> arbitrary |
261 | c2d3219b | Iustin Pop | "OP_INSTANCE_SHUTDOWN" -> |
262 | da4a52a3 | Thomas Thrainer | OpCodes.OpInstanceShutdown <$> genFQDN <*> return Nothing <*> |
263 | da4a52a3 | Thomas Thrainer | arbitrary <*> arbitrary <*> arbitrary <*> arbitrary |
264 | c2d3219b | Iustin Pop | "OP_INSTANCE_REBOOT" -> |
265 | da4a52a3 | Thomas Thrainer | OpCodes.OpInstanceReboot <$> genFQDN <*> return Nothing <*> |
266 | da4a52a3 | Thomas Thrainer | arbitrary <*> arbitrary <*> arbitrary |
267 | c2d3219b | Iustin Pop | "OP_INSTANCE_MOVE" -> |
268 | da4a52a3 | Thomas Thrainer | OpCodes.OpInstanceMove <$> genFQDN <*> return Nothing <*> |
269 | da4a52a3 | Thomas Thrainer | arbitrary <*> arbitrary <*> genNodeNameNE <*> return Nothing <*> |
270 | f198cf91 | Thomas Thrainer | arbitrary <*> arbitrary |
271 | da4a52a3 | Thomas Thrainer | "OP_INSTANCE_CONSOLE" -> OpCodes.OpInstanceConsole <$> genFQDN <*> |
272 | da4a52a3 | Thomas Thrainer | return Nothing |
273 | c2d3219b | Iustin Pop | "OP_INSTANCE_ACTIVATE_DISKS" -> |
274 | da4a52a3 | Thomas Thrainer | OpCodes.OpInstanceActivateDisks <$> genFQDN <*> return Nothing <*> |
275 | c2d3219b | Iustin Pop | arbitrary <*> arbitrary |
276 | c2d3219b | Iustin Pop | "OP_INSTANCE_DEACTIVATE_DISKS" -> |
277 | da4a52a3 | Thomas Thrainer | OpCodes.OpInstanceDeactivateDisks <$> genFQDN <*> return Nothing <*> |
278 | da4a52a3 | Thomas Thrainer | arbitrary |
279 | c2d3219b | Iustin Pop | "OP_INSTANCE_RECREATE_DISKS" -> |
280 | da4a52a3 | Thomas Thrainer | OpCodes.OpInstanceRecreateDisks <$> genFQDN <*> return Nothing <*> |
281 | da4a52a3 | Thomas Thrainer | arbitrary <*> genNodeNamesNE <*> return Nothing <*> |
282 | da4a52a3 | Thomas Thrainer | genMaybe genNameNE |
283 | 1cd563e2 | Iustin Pop | "OP_INSTANCE_QUERY" -> |
284 | 6e94b75c | Jose A. Lopes | OpCodes.OpInstanceQuery <$> genFieldsNE <*> arbitrary <*> genNamesNE |
285 | c2d3219b | Iustin Pop | "OP_INSTANCE_QUERY_DATA" -> |
286 | c2d3219b | Iustin Pop | OpCodes.OpInstanceQueryData <$> arbitrary <*> |
287 | c2d3219b | Iustin Pop | genNodeNamesNE <*> arbitrary |
288 | c2d3219b | Iustin Pop | "OP_INSTANCE_SET_PARAMS" -> |
289 | da4a52a3 | Thomas Thrainer | OpCodes.OpInstanceSetParams <$> genFQDN <*> return Nothing <*> |
290 | c2d3219b | Iustin Pop | arbitrary <*> arbitrary <*> arbitrary <*> arbitrary <*> |
291 | da4a52a3 | Thomas Thrainer | arbitrary <*> pure emptyJSObject <*> arbitrary <*> |
292 | da4a52a3 | Thomas Thrainer | pure emptyJSObject <*> arbitrary <*> genMaybe genNodeNameNE <*> |
293 | da4a52a3 | Thomas Thrainer | return Nothing <*> genMaybe genNodeNameNE <*> return Nothing <*> |
294 | d2204b1a | Klaus Aehlig | genMaybe genNameNE <*> pure emptyJSObject <*> arbitrary <*> |
295 | d2204b1a | Klaus Aehlig | arbitrary <*> arbitrary |
296 | c2d3219b | Iustin Pop | "OP_INSTANCE_GROW_DISK" -> |
297 | da4a52a3 | Thomas Thrainer | OpCodes.OpInstanceGrowDisk <$> genFQDN <*> return Nothing <*> |
298 | da4a52a3 | Thomas Thrainer | arbitrary <*> arbitrary <*> arbitrary <*> arbitrary |
299 | c2d3219b | Iustin Pop | "OP_INSTANCE_CHANGE_GROUP" -> |
300 | da4a52a3 | Thomas Thrainer | OpCodes.OpInstanceChangeGroup <$> genFQDN <*> return Nothing <*> |
301 | da4a52a3 | Thomas Thrainer | arbitrary <*> genMaybe genNameNE <*> |
302 | da4a52a3 | Thomas Thrainer | genMaybe (resize maxNodes (listOf genNameNE)) |
303 | 398e9066 | Iustin Pop | "OP_GROUP_ADD" -> |
304 | 398e9066 | Iustin Pop | OpCodes.OpGroupAdd <$> genNameNE <*> arbitrary <*> |
305 | 5006418e | Iustin Pop | emptyMUD <*> genMaybe genEmptyContainer <*> |
306 | 398e9066 | Iustin Pop | emptyMUD <*> emptyMUD <*> emptyMUD |
307 | 398e9066 | Iustin Pop | "OP_GROUP_ASSIGN_NODES" -> |
308 | 398e9066 | Iustin Pop | OpCodes.OpGroupAssignNodes <$> genNameNE <*> arbitrary <*> |
309 | 1c3231aa | Thomas Thrainer | genNodeNamesNE <*> return Nothing |
310 | 398e9066 | Iustin Pop | "OP_GROUP_QUERY" -> |
311 | 398e9066 | Iustin Pop | OpCodes.OpGroupQuery <$> genFieldsNE <*> genNamesNE |
312 | 398e9066 | Iustin Pop | "OP_GROUP_SET_PARAMS" -> |
313 | 398e9066 | Iustin Pop | OpCodes.OpGroupSetParams <$> genNameNE <*> arbitrary <*> |
314 | 5006418e | Iustin Pop | emptyMUD <*> genMaybe genEmptyContainer <*> |
315 | 398e9066 | Iustin Pop | emptyMUD <*> emptyMUD <*> emptyMUD |
316 | 398e9066 | Iustin Pop | "OP_GROUP_REMOVE" -> |
317 | 398e9066 | Iustin Pop | OpCodes.OpGroupRemove <$> genNameNE |
318 | 398e9066 | Iustin Pop | "OP_GROUP_RENAME" -> |
319 | 398e9066 | Iustin Pop | OpCodes.OpGroupRename <$> genNameNE <*> genNameNE |
320 | 398e9066 | Iustin Pop | "OP_GROUP_EVACUATE" -> |
321 | 398e9066 | Iustin Pop | OpCodes.OpGroupEvacuate <$> genNameNE <*> arbitrary <*> |
322 | 5006418e | Iustin Pop | genMaybe genNameNE <*> genMaybe genNamesNE |
323 | 398e9066 | Iustin Pop | "OP_OS_DIAGNOSE" -> |
324 | 398e9066 | Iustin Pop | OpCodes.OpOsDiagnose <$> genFieldsNE <*> genNamesNE |
325 | b954f097 | Constantinos Venetsanopoulos | "OP_EXT_STORAGE_DIAGNOSE" -> |
326 | b954f097 | Constantinos Venetsanopoulos | OpCodes.OpOsDiagnose <$> genFieldsNE <*> genNamesNE |
327 | 398e9066 | Iustin Pop | "OP_BACKUP_QUERY" -> |
328 | 398e9066 | Iustin Pop | OpCodes.OpBackupQuery <$> arbitrary <*> genNodeNamesNE |
329 | 398e9066 | Iustin Pop | "OP_BACKUP_PREPARE" -> |
330 | da4a52a3 | Thomas Thrainer | OpCodes.OpBackupPrepare <$> genFQDN <*> return Nothing <*> arbitrary |
331 | 398e9066 | Iustin Pop | "OP_BACKUP_EXPORT" -> |
332 | da4a52a3 | Thomas Thrainer | OpCodes.OpBackupExport <$> genFQDN <*> return Nothing <*> |
333 | 896cc964 | Thomas Thrainer | arbitrary <*> arbitrary <*> arbitrary <*> return Nothing <*> |
334 | 896cc964 | Thomas Thrainer | arbitrary <*> arbitrary <*> arbitrary <*> arbitrary <*> |
335 | 896cc964 | Thomas Thrainer | genMaybe (pure []) <*> genMaybe genNameNE |
336 | 398e9066 | Iustin Pop | "OP_BACKUP_REMOVE" -> |
337 | da4a52a3 | Thomas Thrainer | OpCodes.OpBackupRemove <$> genFQDN <*> return Nothing |
338 | a3f02317 | Iustin Pop | "OP_TEST_ALLOCATOR" -> |
339 | a3f02317 | Iustin Pop | OpCodes.OpTestAllocator <$> arbitrary <*> arbitrary <*> |
340 | 6e94b75c | Jose A. Lopes | genNameNE <*> genMaybe (pure []) <*> genMaybe (pure []) <*> |
341 | 5006418e | Iustin Pop | arbitrary <*> genMaybe genNameNE <*> |
342 | a3f02317 | Iustin Pop | (genTags >>= mapM mkNonEmpty) <*> |
343 | 5006418e | Iustin Pop | arbitrary <*> arbitrary <*> genMaybe genNameNE <*> |
344 | 5006418e | Iustin Pop | arbitrary <*> genMaybe genNodeNamesNE <*> arbitrary <*> |
345 | 5006418e | Iustin Pop | genMaybe genNamesNE <*> arbitrary <*> arbitrary |
346 | a3f02317 | Iustin Pop | "OP_TEST_JQUEUE" -> |
347 | a3f02317 | Iustin Pop | OpCodes.OpTestJqueue <$> arbitrary <*> arbitrary <*> |
348 | 5006418e | Iustin Pop | resize 20 (listOf genFQDN) <*> arbitrary |
349 | a3f02317 | Iustin Pop | "OP_TEST_DUMMY" -> |
350 | a3f02317 | Iustin Pop | OpCodes.OpTestDummy <$> pure J.JSNull <*> pure J.JSNull <*> |
351 | a3f02317 | Iustin Pop | pure J.JSNull <*> pure J.JSNull |
352 | 8d239fa4 | Iustin Pop | "OP_NETWORK_ADD" -> |
353 | 6e94b75c | Jose A. Lopes | OpCodes.OpNetworkAdd <$> genNameNE <*> genIPv4Network <*> |
354 | 6e94b75c | Jose A. Lopes | genMaybe genIPv4Address <*> pure Nothing <*> pure Nothing <*> |
355 | 6e94b75c | Jose A. Lopes | genMaybe genMacPrefix <*> genMaybe (listOf genIPv4Address) <*> |
356 | 1dbceab9 | Iustin Pop | arbitrary <*> (genTags >>= mapM mkNonEmpty) |
357 | 8d239fa4 | Iustin Pop | "OP_NETWORK_REMOVE" -> |
358 | 8d239fa4 | Iustin Pop | OpCodes.OpNetworkRemove <$> genNameNE <*> arbitrary |
359 | 8d239fa4 | Iustin Pop | "OP_NETWORK_SET_PARAMS" -> |
360 | 5cfa6c37 | Dimitris Aragiorgis | OpCodes.OpNetworkSetParams <$> genNameNE <*> |
361 | 6e94b75c | Jose A. Lopes | genMaybe genIPv4Address <*> pure Nothing <*> pure Nothing <*> |
362 | 6e94b75c | Jose A. Lopes | genMaybe genMacPrefix <*> genMaybe (listOf genIPv4Address) <*> |
363 | 6e94b75c | Jose A. Lopes | genMaybe (listOf genIPv4Address) |
364 | 8d239fa4 | Iustin Pop | "OP_NETWORK_CONNECT" -> |
365 | 8d239fa4 | Iustin Pop | OpCodes.OpNetworkConnect <$> genNameNE <*> genNameNE <*> |
366 | 8d239fa4 | Iustin Pop | arbitrary <*> genNameNE <*> arbitrary |
367 | 8d239fa4 | Iustin Pop | "OP_NETWORK_DISCONNECT" -> |
368 | 0ae4b355 | Helga Velroyen | OpCodes.OpNetworkDisconnect <$> genNameNE <*> genNameNE |
369 | 8d239fa4 | Iustin Pop | "OP_NETWORK_QUERY" -> |
370 | 6e94b75c | Jose A. Lopes | OpCodes.OpNetworkQuery <$> genFieldsNE <*> arbitrary <*> genNamesNE |
371 | 1cd563e2 | Iustin Pop | "OP_RESTRICTED_COMMAND" -> |
372 | 1cd563e2 | Iustin Pop | OpCodes.OpRestrictedCommand <$> arbitrary <*> genNodeNamesNE <*> |
373 | 1c3231aa | Thomas Thrainer | return Nothing <*> genNameNE |
374 | c66f09f5 | Iustin Pop | _ -> fail $ "Undefined arbitrary for opcode " ++ op_id |
375 | aed2325f | Iustin Pop | |
376 | 516a0e94 | Michele Tartara | -- | Generates one element of a reason trail |
377 | 516a0e94 | Michele Tartara | genReasonElem :: Gen ReasonElem |
378 | 516a0e94 | Michele Tartara | genReasonElem = (,,) <$> genFQDN <*> genFQDN <*> arbitrary |
379 | 516a0e94 | Michele Tartara | |
380 | 516a0e94 | Michele Tartara | -- | Generates a reason trail |
381 | 516a0e94 | Michele Tartara | genReasonTrail :: Gen ReasonTrail |
382 | 516a0e94 | Michele Tartara | genReasonTrail = do |
383 | 516a0e94 | Michele Tartara | size <- choose (0, 10) |
384 | 516a0e94 | Michele Tartara | vectorOf size genReasonElem |
385 | 516a0e94 | Michele Tartara | |
386 | 4a826364 | Iustin Pop | instance Arbitrary OpCodes.CommonOpParams where |
387 | 4a826364 | Iustin Pop | arbitrary = OpCodes.CommonOpParams <$> arbitrary <*> arbitrary <*> |
388 | 516a0e94 | Michele Tartara | arbitrary <*> resize 5 arbitrary <*> genMaybe genName <*> |
389 | 516a0e94 | Michele Tartara | genReasonTrail |
390 | 4a826364 | Iustin Pop | |
391 | c7d249d0 | Iustin Pop | -- * Helper functions |
392 | c7d249d0 | Iustin Pop | |
393 | c66f09f5 | Iustin Pop | -- | Empty JSObject. |
394 | c66f09f5 | Iustin Pop | emptyJSObject :: J.JSObject J.JSValue |
395 | c66f09f5 | Iustin Pop | emptyJSObject = J.toJSObject [] |
396 | c66f09f5 | Iustin Pop | |
397 | c66f09f5 | Iustin Pop | -- | Empty maybe unchecked dictionary. |
398 | c66f09f5 | Iustin Pop | emptyMUD :: Gen (Maybe (J.JSObject J.JSValue)) |
399 | 5006418e | Iustin Pop | emptyMUD = genMaybe $ pure emptyJSObject |
400 | c66f09f5 | Iustin Pop | |
401 | c66f09f5 | Iustin Pop | -- | Generates an empty container. |
402 | c66f09f5 | Iustin Pop | genEmptyContainer :: (Ord a) => Gen (GenericContainer a b) |
403 | c66f09f5 | Iustin Pop | genEmptyContainer = pure . GenericContainer $ Map.fromList [] |
404 | c66f09f5 | Iustin Pop | |
405 | c7d249d0 | Iustin Pop | -- | Generates list of disk indices. |
406 | c7d249d0 | Iustin Pop | genDiskIndices :: Gen [DiskIndex] |
407 | c7d249d0 | Iustin Pop | genDiskIndices = do |
408 | c7d249d0 | Iustin Pop | cnt <- choose (0, C.maxDisks) |
409 | df8578fb | Iustin Pop | genUniquesList cnt arbitrary |
410 | c7d249d0 | Iustin Pop | |
411 | c7d249d0 | Iustin Pop | -- | Generates a list of node names. |
412 | c7d249d0 | Iustin Pop | genNodeNames :: Gen [String] |
413 | 5006418e | Iustin Pop | genNodeNames = resize maxNodes (listOf genFQDN) |
414 | c7d249d0 | Iustin Pop | |
415 | c66f09f5 | Iustin Pop | -- | Generates a list of node names in non-empty string type. |
416 | c66f09f5 | Iustin Pop | genNodeNamesNE :: Gen [NonEmptyString] |
417 | 417ab39c | Iustin Pop | genNodeNamesNE = genNodeNames >>= mapM mkNonEmpty |
418 | c66f09f5 | Iustin Pop | |
419 | c7d249d0 | Iustin Pop | -- | Gets a node name in non-empty type. |
420 | c7d249d0 | Iustin Pop | genNodeNameNE :: Gen NonEmptyString |
421 | 5006418e | Iustin Pop | genNodeNameNE = genFQDN >>= mkNonEmpty |
422 | c7d249d0 | Iustin Pop | |
423 | 5ef4fbb1 | Iustin Pop | -- | Gets a name (non-fqdn) in non-empty type. |
424 | 5ef4fbb1 | Iustin Pop | genNameNE :: Gen NonEmptyString |
425 | 5006418e | Iustin Pop | genNameNE = genName >>= mkNonEmpty |
426 | 5ef4fbb1 | Iustin Pop | |
427 | 398e9066 | Iustin Pop | -- | Gets a list of names (non-fqdn) in non-empty type. |
428 | 398e9066 | Iustin Pop | genNamesNE :: Gen [NonEmptyString] |
429 | 398e9066 | Iustin Pop | genNamesNE = resize maxNodes (listOf genNameNE) |
430 | 398e9066 | Iustin Pop | |
431 | 5ef4fbb1 | Iustin Pop | -- | Returns a list of non-empty fields. |
432 | 5ef4fbb1 | Iustin Pop | genFieldsNE :: Gen [NonEmptyString] |
433 | 5006418e | Iustin Pop | genFieldsNE = genFields >>= mapM mkNonEmpty |
434 | 5ef4fbb1 | Iustin Pop | |
435 | 8d239fa4 | Iustin Pop | -- | Generate a 3-byte MAC prefix. |
436 | 8d239fa4 | Iustin Pop | genMacPrefix :: Gen NonEmptyString |
437 | 8d239fa4 | Iustin Pop | genMacPrefix = do |
438 | 8d239fa4 | Iustin Pop | octets <- vectorOf 3 $ choose (0::Int, 255) |
439 | 8d239fa4 | Iustin Pop | mkNonEmpty . intercalate ":" $ map (printf "%02x") octets |
440 | 8d239fa4 | Iustin Pop | |
441 | 4a826364 | Iustin Pop | -- | Arbitrary instance for MetaOpCode, defined here due to TH ordering. |
442 | 4a826364 | Iustin Pop | $(genArbitrary ''OpCodes.MetaOpCode) |
443 | 4a826364 | Iustin Pop | |
444 | f56013fd | Iustin Pop | -- | Small helper to check for a failed JSON deserialisation |
445 | f56013fd | Iustin Pop | isJsonError :: J.Result a -> Bool |
446 | f56013fd | Iustin Pop | isJsonError (J.Error _) = True |
447 | f56013fd | Iustin Pop | isJsonError _ = False |
448 | f56013fd | Iustin Pop | |
449 | aed2325f | Iustin Pop | -- * Test cases |
450 | aed2325f | Iustin Pop | |
451 | aed2325f | Iustin Pop | -- | Check that opcode serialization is idempotent. |
452 | 20bc5360 | Iustin Pop | prop_serialization :: OpCodes.OpCode -> Property |
453 | 63b068c1 | Iustin Pop | prop_serialization = testSerialisation |
454 | aed2325f | Iustin Pop | |
455 | aed2325f | Iustin Pop | -- | Check that Python and Haskell defined the same opcode list. |
456 | 20bc5360 | Iustin Pop | case_AllDefined :: HUnit.Assertion |
457 | 20bc5360 | Iustin Pop | case_AllDefined = do |
458 | d31193c3 | Jose A. Lopes | py_stdout <- |
459 | d31193c3 | Jose A. Lopes | runPython "from ganeti import opcodes\n\ |
460 | d31193c3 | Jose A. Lopes | \from ganeti import serializer\n\ |
461 | d31193c3 | Jose A. Lopes | \import sys\n\ |
462 | 6e94b75c | Jose A. Lopes | \print serializer.Dump([opid for opid in opcodes.OP_MAPPING])\n" |
463 | 6e94b75c | Jose A. Lopes | "" |
464 | d31193c3 | Jose A. Lopes | >>= checkPythonResult |
465 | d31193c3 | Jose A. Lopes | py_ops <- case J.decode py_stdout::J.Result [String] of |
466 | d31193c3 | Jose A. Lopes | J.Ok ops -> return ops |
467 | d31193c3 | Jose A. Lopes | J.Error msg -> |
468 | d31193c3 | Jose A. Lopes | HUnit.assertFailure ("Unable to decode opcode names: " ++ msg) |
469 | d31193c3 | Jose A. Lopes | -- this already raised an expection, but we need it |
470 | d31193c3 | Jose A. Lopes | -- for proper types |
471 | d31193c3 | Jose A. Lopes | >> fail "Unable to decode opcode names" |
472 | d31193c3 | Jose A. Lopes | let hs_ops = sort OpCodes.allOpIDs |
473 | 9b773665 | Iustin Pop | extra_py = py_ops \\ hs_ops |
474 | aed2325f | Iustin Pop | extra_hs = hs_ops \\ py_ops |
475 | 9b773665 | Iustin Pop | HUnit.assertBool ("Missing OpCodes from the Haskell code:\n" ++ |
476 | 9b773665 | Iustin Pop | unlines extra_py) (null extra_py) |
477 | aed2325f | Iustin Pop | HUnit.assertBool ("Extra OpCodes in the Haskell code code:\n" ++ |
478 | aed2325f | Iustin Pop | unlines extra_hs) (null extra_hs) |
479 | aed2325f | Iustin Pop | |
480 | aed2325f | Iustin Pop | -- | Custom HUnit test case that forks a Python process and checks |
481 | aed2325f | Iustin Pop | -- correspondence between Haskell-generated OpCodes and their Python |
482 | aed2325f | Iustin Pop | -- decoded, validated and re-encoded version. |
483 | aed2325f | Iustin Pop | -- |
484 | aed2325f | Iustin Pop | -- Note that we have a strange beast here: since launching Python is |
485 | aed2325f | Iustin Pop | -- expensive, we don't do this via a usual QuickProperty, since that's |
486 | aed2325f | Iustin Pop | -- slow (I've tested it, and it's indeed quite slow). Rather, we use a |
487 | aed2325f | Iustin Pop | -- single HUnit assertion, and in it we manually use QuickCheck to |
488 | aed2325f | Iustin Pop | -- generate 500 opcodes times the number of defined opcodes, which |
489 | aed2325f | Iustin Pop | -- then we pass in bulk to Python. The drawbacks to this method are |
490 | aed2325f | Iustin Pop | -- two fold: we cannot control the number of generated opcodes, since |
491 | aed2325f | Iustin Pop | -- HUnit assertions don't get access to the test options, and for the |
492 | aed2325f | Iustin Pop | -- same reason we can't run a repeatable seed. We should probably find |
493 | aed2325f | Iustin Pop | -- a better way to do this, for example by having a |
494 | aed2325f | Iustin Pop | -- separately-launched Python process (if not running the tests would |
495 | aed2325f | Iustin Pop | -- be skipped). |
496 | d1ac695f | Iustin Pop | case_py_compat_types :: HUnit.Assertion |
497 | d1ac695f | Iustin Pop | case_py_compat_types = do |
498 | 086ad4cf | Iustin Pop | let num_opcodes = length OpCodes.allOpIDs * 100 |
499 | 72747d91 | Iustin Pop | opcodes <- genSample (vectorOf num_opcodes |
500 | 72747d91 | Iustin Pop | (arbitrary::Gen OpCodes.MetaOpCode)) |
501 | 72747d91 | Iustin Pop | let with_sum = map (\o -> (OpCodes.opSummary $ |
502 | ad1c1e41 | Iustin Pop | OpCodes.metaOpCode o, o)) opcodes |
503 | aed2325f | Iustin Pop | serialized = J.encode opcodes |
504 | dc4b5c42 | Iustin Pop | -- check for non-ASCII fields, usually due to 'arbitrary :: String' |
505 | dc4b5c42 | Iustin Pop | mapM_ (\op -> when (any (not . isAscii) (J.encode op)) . |
506 | dc4b5c42 | Iustin Pop | HUnit.assertFailure $ |
507 | dc4b5c42 | Iustin Pop | "OpCode has non-ASCII fields: " ++ show op |
508 | dc4b5c42 | Iustin Pop | ) opcodes |
509 | aed2325f | Iustin Pop | py_stdout <- |
510 | aed2325f | Iustin Pop | runPython "from ganeti import opcodes\n\ |
511 | aed2325f | Iustin Pop | \from ganeti import serializer\n\ |
512 | 6e94b75c | Jose A. Lopes | \import sys\n\ |
513 | aed2325f | Iustin Pop | \op_data = serializer.Load(sys.stdin.read())\n\ |
514 | aed2325f | Iustin Pop | \decoded = [opcodes.OpCode.LoadOpCode(o) for o in op_data]\n\ |
515 | aed2325f | Iustin Pop | \for op in decoded:\n\ |
516 | aed2325f | Iustin Pop | \ op.Validate(True)\n\ |
517 | ad1c1e41 | Iustin Pop | \encoded = [(op.Summary(), op.__getstate__())\n\ |
518 | ad1c1e41 | Iustin Pop | \ for op in decoded]\n\ |
519 | aed2325f | Iustin Pop | \print serializer.Dump(encoded)" serialized |
520 | aed2325f | Iustin Pop | >>= checkPythonResult |
521 | ad1c1e41 | Iustin Pop | let deserialised = |
522 | ad1c1e41 | Iustin Pop | J.decode py_stdout::J.Result [(String, OpCodes.MetaOpCode)] |
523 | aed2325f | Iustin Pop | decoded <- case deserialised of |
524 | aed2325f | Iustin Pop | J.Ok ops -> return ops |
525 | aed2325f | Iustin Pop | J.Error msg -> |
526 | aed2325f | Iustin Pop | HUnit.assertFailure ("Unable to decode opcodes: " ++ msg) |
527 | aed2325f | Iustin Pop | -- this already raised an expection, but we need it |
528 | aed2325f | Iustin Pop | -- for proper types |
529 | aed2325f | Iustin Pop | >> fail "Unable to decode opcodes" |
530 | aed2325f | Iustin Pop | HUnit.assertEqual "Mismatch in number of returned opcodes" |
531 | ad1c1e41 | Iustin Pop | (length decoded) (length with_sum) |
532 | aed2325f | Iustin Pop | mapM_ (uncurry (HUnit.assertEqual "Different result after encoding/decoding") |
533 | ad1c1e41 | Iustin Pop | ) $ zip decoded with_sum |
534 | aed2325f | Iustin Pop | |
535 | d1ac695f | Iustin Pop | -- | Custom HUnit test case that forks a Python process and checks |
536 | d1ac695f | Iustin Pop | -- correspondence between Haskell OpCodes fields and their Python |
537 | d1ac695f | Iustin Pop | -- equivalent. |
538 | d1ac695f | Iustin Pop | case_py_compat_fields :: HUnit.Assertion |
539 | d1ac695f | Iustin Pop | case_py_compat_fields = do |
540 | d1ac695f | Iustin Pop | let hs_fields = sort $ map (\op_id -> (op_id, OpCodes.allOpFields op_id)) |
541 | d1ac695f | Iustin Pop | OpCodes.allOpIDs |
542 | d1ac695f | Iustin Pop | py_stdout <- |
543 | d1ac695f | Iustin Pop | runPython "from ganeti import opcodes\n\ |
544 | d1ac695f | Iustin Pop | \import sys\n\ |
545 | d1ac695f | Iustin Pop | \from ganeti import serializer\n\ |
546 | d1ac695f | Iustin Pop | \fields = [(k, sorted([p[0] for p in v.OP_PARAMS]))\n\ |
547 | d1ac695f | Iustin Pop | \ for k, v in opcodes.OP_MAPPING.items()]\n\ |
548 | d1ac695f | Iustin Pop | \print serializer.Dump(fields)" "" |
549 | d1ac695f | Iustin Pop | >>= checkPythonResult |
550 | d1ac695f | Iustin Pop | let deserialised = J.decode py_stdout::J.Result [(String, [String])] |
551 | d1ac695f | Iustin Pop | py_fields <- case deserialised of |
552 | d1ac695f | Iustin Pop | J.Ok v -> return $ sort v |
553 | d1ac695f | Iustin Pop | J.Error msg -> |
554 | d1ac695f | Iustin Pop | HUnit.assertFailure ("Unable to decode op fields: " ++ msg) |
555 | d1ac695f | Iustin Pop | -- this already raised an expection, but we need it |
556 | d1ac695f | Iustin Pop | -- for proper types |
557 | d1ac695f | Iustin Pop | >> fail "Unable to decode op fields" |
558 | d1ac695f | Iustin Pop | HUnit.assertEqual "Mismatch in number of returned opcodes" |
559 | d1ac695f | Iustin Pop | (length hs_fields) (length py_fields) |
560 | d1ac695f | Iustin Pop | HUnit.assertEqual "Mismatch in defined OP_IDs" |
561 | d1ac695f | Iustin Pop | (map fst hs_fields) (map fst py_fields) |
562 | d1ac695f | Iustin Pop | mapM_ (\((py_id, py_flds), (hs_id, hs_flds)) -> do |
563 | d1ac695f | Iustin Pop | HUnit.assertEqual "Mismatch in OP_ID" py_id hs_id |
564 | d1ac695f | Iustin Pop | HUnit.assertEqual ("Mismatch in fields for " ++ hs_id) |
565 | d1ac695f | Iustin Pop | py_flds hs_flds |
566 | d1ac695f | Iustin Pop | ) $ zip py_fields hs_fields |
567 | d1ac695f | Iustin Pop | |
568 | 4a826364 | Iustin Pop | -- | Checks that setOpComment works correctly. |
569 | 4a826364 | Iustin Pop | prop_setOpComment :: OpCodes.MetaOpCode -> String -> Property |
570 | 4a826364 | Iustin Pop | prop_setOpComment op comment = |
571 | 4a826364 | Iustin Pop | let (OpCodes.MetaOpCode common _) = OpCodes.setOpComment comment op |
572 | 4a826364 | Iustin Pop | in OpCodes.opComment common ==? Just comment |
573 | 4a826364 | Iustin Pop | |
574 | f56013fd | Iustin Pop | -- | Tests wrong (negative) disk index. |
575 | f56013fd | Iustin Pop | prop_mkDiskIndex_fail :: QuickCheck.Positive Int -> Property |
576 | f56013fd | Iustin Pop | prop_mkDiskIndex_fail (Positive i) = |
577 | f56013fd | Iustin Pop | case mkDiskIndex (negate i) of |
578 | f56013fd | Iustin Pop | Bad msg -> printTestCase "error message " $ |
579 | f56013fd | Iustin Pop | "Invalid value" `isPrefixOf` msg |
580 | f56013fd | Iustin Pop | Ok v -> failTest $ "Succeeded to build disk index '" ++ show v ++ |
581 | f56013fd | Iustin Pop | "' from negative value " ++ show (negate i) |
582 | f56013fd | Iustin Pop | |
583 | f56013fd | Iustin Pop | -- | Tests a few invalid 'readRecreateDisks' cases. |
584 | f56013fd | Iustin Pop | case_readRecreateDisks_fail :: Assertion |
585 | f56013fd | Iustin Pop | case_readRecreateDisks_fail = do |
586 | f56013fd | Iustin Pop | assertBool "null" $ |
587 | f56013fd | Iustin Pop | isJsonError (J.readJSON J.JSNull::J.Result RecreateDisksInfo) |
588 | f56013fd | Iustin Pop | assertBool "string" $ |
589 | f56013fd | Iustin Pop | isJsonError (J.readJSON (J.showJSON "abc")::J.Result RecreateDisksInfo) |
590 | f56013fd | Iustin Pop | |
591 | f56013fd | Iustin Pop | -- | Tests a few invalid 'readDdmOldChanges' cases. |
592 | f56013fd | Iustin Pop | case_readDdmOldChanges_fail :: Assertion |
593 | f56013fd | Iustin Pop | case_readDdmOldChanges_fail = do |
594 | f56013fd | Iustin Pop | assertBool "null" $ |
595 | f56013fd | Iustin Pop | isJsonError (J.readJSON J.JSNull::J.Result DdmOldChanges) |
596 | f56013fd | Iustin Pop | assertBool "string" $ |
597 | f56013fd | Iustin Pop | isJsonError (J.readJSON (J.showJSON "abc")::J.Result DdmOldChanges) |
598 | f56013fd | Iustin Pop | |
599 | f56013fd | Iustin Pop | -- | Tests a few invalid 'readExportTarget' cases. |
600 | f56013fd | Iustin Pop | case_readExportTarget_fail :: Assertion |
601 | f56013fd | Iustin Pop | case_readExportTarget_fail = do |
602 | f56013fd | Iustin Pop | assertBool "null" $ |
603 | f56013fd | Iustin Pop | isJsonError (J.readJSON J.JSNull::J.Result ExportTarget) |
604 | f56013fd | Iustin Pop | assertBool "int" $ |
605 | f56013fd | Iustin Pop | isJsonError (J.readJSON (J.showJSON (5::Int))::J.Result ExportTarget) |
606 | f56013fd | Iustin Pop | |
607 | aed2325f | Iustin Pop | testSuite "OpCodes" |
608 | 20bc5360 | Iustin Pop | [ 'prop_serialization |
609 | 20bc5360 | Iustin Pop | , 'case_AllDefined |
610 | d1ac695f | Iustin Pop | , 'case_py_compat_types |
611 | d1ac695f | Iustin Pop | , 'case_py_compat_fields |
612 | 4a826364 | Iustin Pop | , 'prop_setOpComment |
613 | f56013fd | Iustin Pop | , 'prop_mkDiskIndex_fail |
614 | f56013fd | Iustin Pop | , 'case_readRecreateDisks_fail |
615 | f56013fd | Iustin Pop | , 'case_readDdmOldChanges_fail |
616 | f56013fd | Iustin Pop | , 'case_readExportTarget_fail |
617 | aed2325f | Iustin Pop | ] |