Revision 80a0546b

b/htest/Test/Ganeti/Utils.hs
1
{-# LANGUAGE TemplateHaskell #-}
1
{-# LANGUAGE TemplateHaskell, CPP #-}
2 2
{-# OPTIONS_GHC -fno-warn-orphans #-}
3 3

  
4 4
{-| Unittests for ganeti-htools.
......
34 34
import Data.Char (isSpace)
35 35
import Data.List
36 36
import qualified Text.JSON as J
37
#ifndef NO_REGEX_PCRE
38
import Text.Regex.PCRE
39
#endif
37 40

  
38 41
import Test.Ganeti.TestHelper
39 42
import Test.Ganeti.TestCommon
40 43

  
41 44
import Ganeti.BasicTypes
45
import qualified Ganeti.Constants as C
42 46
import qualified Ganeti.JSON as JSON
43 47
import Ganeti.Utils
44 48

  
......
219 223
              rStripSpace "" ==? ""
220 224
          ]
221 225

  
226
#ifndef NO_REGEX_PCRE
227
-- | Tests that the newUUID function produces valid UUIDs.
228
case_new_uuid :: Assertion
229
case_new_uuid = do
230
  uuid <- newUUID
231
  assertBool "newUUID" $ uuid =~ C.uuidRegex
232
#endif
233

  
222 234
-- | Test list for the Utils module.
223 235
testSuite "Utils"
224 236
            [ 'prop_commaJoinSplit
......
235 247
            , 'prop_niceSort_numbers
236 248
            , 'prop_niceSortKey_equiv
237 249
            , 'prop_rStripSpace
250
#ifndef NO_REGEX_PCRE
251
            , 'case_new_uuid
252
#endif
238 253
            ]
b/htools/Ganeti/Utils.hs
44 44
  , exitWhen
45 45
  , exitUnless
46 46
  , rStripSpace
47
  , newUUID
47 48
  ) where
48 49

  
49 50
import Data.Char (toUpper, isAlphaNum, isDigit, isSpace)
......
53 54
import Debug.Trace
54 55

  
55 56
import Ganeti.BasicTypes
57
import qualified Ganeti.Constants as C
56 58
import System.IO
57 59
import System.Exit
58 60

  
......
277 279
-- expensive, should only be run on small strings.
278 280
rStripSpace :: String -> String
279 281
rStripSpace = reverse . dropWhile isSpace . reverse
282

  
283
-- | Returns a random UUID.
284
-- This is a Linux-specific method as it uses the /proc filesystem.
285
newUUID :: IO String
286
newUUID = do
287
  contents <- readFile C.randomUuidFile
288
  let stripNewlines = reverse . dropWhile (=='\n') . reverse
289
  return $! stripNewlines $ take 128 contents
b/lib/constants.py
2210 2210
NDS_SSCONF = "ssconf"
2211 2211
NDS_START_NODE_DAEMON = "start_node_daemon"
2212 2212

  
2213
# Path generating random UUID
2214
RANDOM_UUID_FILE = "/proc/sys/kernel/random/uuid"
2215

  
2216
# Regex string for verifying a UUID
2217
UUID_REGEX = "^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$"
2218

  
2213 2219
# Do not re-export imported modules
2214 2220
del re, _vcsversion, _autoconf, socket, pathutils, compat
b/lib/utils/__init__.py
59 59

  
60 60
_VALID_SERVICE_NAME_RE = re.compile("^[-_.a-zA-Z0-9]{1,128}$")
61 61

  
62
UUID_RE = re.compile("^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-"
63
                     "[a-f0-9]{4}-[a-f0-9]{12}$")
62
UUID_RE = re.compile(constants.UUID_REGEX)
64 63

  
65 64

  
66 65
def ForceDictType(target, key_types, allowed_values=None):
b/lib/utils/io.py
35 35
from ganeti import pathutils
36 36
from ganeti.utils import filelock
37 37

  
38

  
39
#: Path generating random UUID
40
_RANDOM_UUID_FILE = "/proc/sys/kernel/random/uuid"
41

  
42 38
#: Directory used by fsck(8) to store recovered data, usually at a file
43 39
#: system's root directory
44 40
_LOST_AND_FOUND = "lost+found"
......
1029 1025
  @rtype: str
1030 1026

  
1031 1027
  """
1032
  return ReadFile(_RANDOM_UUID_FILE, size=128).rstrip("\n")
1028
  return ReadFile(constants.RANDOM_UUID_FILE, size=128).rstrip("\n")
1033 1029

  
1034 1030

  
1035 1031
class TemporaryFileManager(object):

Also available in: Unified diff