Debian Codegraph @

test/hs/Test/Ganeti/Runtime.hs

raw
1 {-# LANGUAGE TemplateHaskell #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3
4 {-| Unittests for "Ganeti.Runtime".
5
6 -}
7
8 {-
9
10 Copyright (C) 2013 Google Inc.
11 All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are
15 met:
16
17 1. Redistributions of source code must retain the above copyright notice,
18 this list of conditions and the following disclaimer.
19
20 2. Redistributions in binary form must reproduce the above copyright
21 notice, this list of conditions and the following disclaimer in the
22 documentation and/or other materials provided with the distribution.
23
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
28 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
36 -}
37
38 module Test.Ganeti.Runtime (testRuntime) where
39
40 import Test.HUnit
41 import qualified Text.JSON as J
42
43 import Test.Ganeti.TestHelper
44 import Test.Ganeti.TestCommon
45
46 import Ganeti.Runtime
47
48 {-# ANN module "HLint: ignore Use camelCase" #-}
49
50 -- | Tests the compatibility between Haskell and Python log files.
51 case_LogFiles :: Assertion
52 case_LogFiles = do
53 let daemons = [minBound..maxBound]::[GanetiDaemon]
54 dnames = map daemonName daemons
55 dfiles <- mapM daemonLogFile daemons
56 let serialized = J.encode dnames
57 py_stdout <-
58 runPython "from ganeti import constants\n\
59 \from ganeti import serializer\n\
60 \import sys\n\
61 \daemons = serializer.Load(sys.stdin.read())\n\
62 \logfiles = [constants.DAEMONS_LOGFILES[d] for d in daemons]\n\
63 \sys.stdout.buffer.write(serializer.Dump(logfiles))" serialized
64 >>= checkPythonResult
65 let deserialised = J.decode py_stdout::J.Result [String]
66 decoded <- case deserialised of
67 J.Ok ops -> return ops
68 J.Error msg ->
69 assertFailure ("Unable to decode log files: " ++ msg)
70 -- this already raised an expection, but we need it
71 -- for proper types
72 >> fail "Unable to decode log files"
73 assertEqual "Mismatch in number of returned log files"
74 (length decoded) (length daemons)
75 mapM_ (uncurry (assertEqual "Different result after encoding/decoding")
76 ) $ zip dfiles decoded
77
78 -- | Tests the compatibility between Haskell and Python users.
79 case_UsersGroups :: Assertion
80 case_UsersGroups = do
81 -- note: we don't have here a programatic way to list all users, so
82 -- we harcode some parts of the two (hs/py) lists
83 let daemons = [minBound..maxBound]::[GanetiDaemon]
84 users = map daemonUser daemons
85 groups = map daemonGroup $
86 map DaemonGroup daemons ++ map ExtraGroup [minBound..maxBound]
87 py_stdout <-
88 runPython "from ganeti import constants\n\
89 \from ganeti import serializer\n\
90 \import sys\n\
91 \users = [constants.MASTERD_USER,\n\
92 \ constants.METAD_USER,\n\
93 \ constants.NODED_USER,\n\
94 \ constants.RAPI_USER,\n\
95 \ constants.CONFD_USER,\n\
96 \ constants.WCONFD_USER,\n\
97 \ constants.KVMD_USER,\n\
98 \ constants.LUXID_USER,\n\
99 \ constants.MOND_USER,\n\
100 \ ]\n\
101 \groups = [constants.MASTERD_GROUP,\n\
102 \ constants.METAD_GROUP,\n\
103 \ constants.NODED_GROUP,\n\
104 \ constants.RAPI_GROUP,\n\
105 \ constants.CONFD_GROUP,\n\
106 \ constants.WCONFD_GROUP,\n\
107 \ constants.KVMD_GROUP,\n\
108 \ constants.LUXID_GROUP,\n\
109 \ constants.MOND_GROUP,\n\
110 \ constants.DAEMONS_GROUP,\n\
111 \ constants.ADMIN_GROUP,\n\
112 \ ]\n\
113 \encoded = (users, groups)\n\
114 \sys.stdout.buffer.write(serializer.Dump(encoded))" ""
115 >>= checkPythonResult
116 let deserialised = J.decode py_stdout::J.Result ([String], [String])
117 (py_users, py_groups) <-
118 case deserialised of
119 J.Ok ops -> return ops
120 J.Error msg ->
121 assertFailure ("Unable to decode users/groups: " ++ msg)
122 -- this already raised an expection, but we need it for proper
123 -- types
124 >> fail "Unable to decode users/groups"
125 assertEqual "Mismatch in number of returned users"
126 (length py_users) (length users)
127 assertEqual "Mismatch in number of returned users"
128 (length py_groups) (length groups)
129 mapM_ (uncurry (assertEqual "Different result for users")
130 ) $ zip users py_users
131 mapM_ (uncurry (assertEqual "Different result for groups")
132 ) $ zip groups py_groups
133
134 testSuite "Runtime"
135 [ 'case_LogFiles
136 , 'case_UsersGroups
137 ]