Revision 3fd38382

b/Makefile.am
756 756
	test/hs/Test/Ganeti/Network.hs \
757 757
	test/hs/Test/Ganeti/Objects.hs \
758 758
	test/hs/Test/Ganeti/OpCodes.hs \
759
	test/hs/Test/Ganeti/Query/Aliases.hs \
759 760
	test/hs/Test/Ganeti/Query/Filter.hs \
760 761
	test/hs/Test/Ganeti/Query/Language.hs \
761 762
	test/hs/Test/Ganeti/Query/Network.hs \
b/src/Ganeti/Query/Common.hs
44 44
  , buildHvParamField
45 45
  , getDefaultHypervisorSpec
46 46
  , getHvParamsFromCluster
47
  , aliasFields
47 48
  ) where
48 49

  
50
import Control.Monad (guard)
49 51
import qualified Data.Map as Map
50 52
import Data.Maybe (fromMaybe)
51 53
import Text.JSON (JSON, showJSON)
......
238 240
  fromMaybe (GenericContainer Map.empty) .
239 241
    Map.lookup (hypervisorToRaw hv) .
240 242
      fromContainer . clusterHvparams $ configCluster cfg
243

  
244
-- | Given an alias list and a field list, copies field definitions under a
245
-- new field name. Aliases should be tested - see the test module
246
-- 'Test.Ganeti.Query.Aliases'!
247
aliasFields :: [(FieldName, FieldName)] -> FieldList a b -> FieldList a b
248
aliasFields aliases fieldList = fieldList ++ do
249
  alias <- aliases
250
  (FieldDefinition name d1 d2 d3, v1, v2) <- fieldList
251
  guard (snd alias == name)
252
  return (FieldDefinition (fst alias) d1 d2 d3, v1, v2)
b/src/Ganeti/Query/Instance.hs
27 27
  ( Runtime
28 28
  , fieldsMap
29 29
  , collectLiveData
30
  , instanceFields
31
  , instanceAliases
30 32
  ) where
31 33

  
32 34
import Control.Applicative
......
64 66

  
65 67
-- | The instance fields map.
66 68
fieldsMap :: FieldMap Instance Runtime
67
fieldsMap = Map.fromList [(fdefName f, v) | v@(f, _, _) <- instanceFields]
69
fieldsMap = Map.fromList [(fdefName f, v) | v@(f, _, _) <- aliasedFields]
70

  
71
-- | The instance aliases.
72
instanceAliases :: [(FieldName, FieldName)]
73
instanceAliases =
74
  [ ("vcpus", "be/vcpus")
75
  , ("be/memory", "be/maxmem")
76
  , ("sda_size", "disk.size/0")
77
  , ("sdb_size", "disk.size/1")
78
  , ("ip", "nic.ip/0")
79
  , ("mac", "nic.mac/0")
80
  , ("bridge", "nic.bridge/0")
81
  , ("nic_mode", "nic.mode/0")
82
  , ("nic_link", "nic.link/0")
83
  , ("nic_network", "nic.network/0")
84
  ]
85

  
86
-- | The aliased instance fields.
87
aliasedFields :: FieldList Instance Runtime
88
aliasedFields = aliasFields instanceAliases instanceFields
68 89

  
69
-- | The instance fields
90
-- | The instance fields.
70 91
instanceFields :: FieldList Instance Runtime
71 92
instanceFields =
72 93
  -- Simple fields
b/test/hs/Test/Ganeti/Query/Aliases.hs
1
{-# LANGUAGE TemplateHaskell #-}
2
{-# OPTIONS_GHC -fno-warn-orphans #-}
3

  
4
{-| Unittests for query aliases.
5

  
6
-}
7

  
8
{-
9

  
10
Copyright (C) 2013 Google Inc.
11

  
12
This program is free software; you can redistribute it and/or modify
13
it under the terms of the GNU General Public License as published by
14
the Free Software Foundation; either version 2 of the License, or
15
(at your option) any later version.
16

  
17
This program is distributed in the hope that it will be useful, but
18
WITHOUT ANY WARRANTY; without even the implied warranty of
19
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20
General Public License for more details.
21

  
22
You should have received a copy of the GNU General Public License
23
along with this program; if not, write to the Free Software
24
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25
02110-1301, USA.
26

  
27
-}
28

  
29
module Test.Ganeti.Query.Aliases
30
  ( testQuery_Aliases
31
  ) where
32

  
33
import Data.List
34

  
35
import Test.Ganeti.TestHelper
36
import Test.HUnit
37

  
38
import Ganeti.Query.Common ()
39
import qualified Ganeti.Query.Instance as I
40
import Ganeti.Query.Language
41
import Ganeti.Query.Types
42

  
43
{-# ANN module "HLint: ignore Use camelCase" #-}
44

  
45
-- | Converts field list to field name list
46
toFieldNameList :: FieldList a b -> [FieldName]
47
toFieldNameList = map (\(x,_,_) -> fdefName x)
48

  
49
-- | Converts alias list to alias name list
50
toAliasNameList :: [(FieldName, FieldName)] -> [FieldName]
51
toAliasNameList = map fst
52

  
53
-- | Converts alias list to alias target list
54
toAliasTargetList :: [(FieldName, FieldName)] -> [FieldName]
55
toAliasTargetList = map snd
56

  
57
-- | Checks for shadowing
58
checkShadowing :: String
59
               -> FieldList a b
60
               -> [(FieldName, FieldName)]
61
               -> Assertion
62
checkShadowing name fields aliases =
63
  assertBool (name ++ " aliases do not shadow fields") .
64
    null $ toFieldNameList fields `intersect` toAliasNameList aliases
65

  
66
-- | Checks for target existence
67
checkTargets :: String
68
             -> FieldList a b
69
             -> [(FieldName, FieldName)]
70
             -> Assertion
71
checkTargets name fields aliases =
72
  assertBool (name ++ " alias targets exist") .
73
    null $ toAliasTargetList aliases \\ toFieldNameList fields
74

  
75
-- | Check that instance aliases do not shadow existing fields
76
case_instanceAliasesNoShadowing :: Assertion
77
case_instanceAliasesNoShadowing =
78
  checkShadowing "Instance" I.instanceFields I.instanceAliases
79

  
80
-- | Check that instance alias targets exist
81
case_instanceAliasesTargetsExist :: Assertion
82
case_instanceAliasesTargetsExist =
83
  checkTargets "Instance" I.instanceFields I.instanceAliases
84

  
85
testSuite "Query/Aliases"
86
  [ 'case_instanceAliasesNoShadowing,
87
    'case_instanceAliasesTargetsExist
88
  ]
b/test/hs/htest.hs
59 59
import Test.Ganeti.Network
60 60
import Test.Ganeti.Objects
61 61
import Test.Ganeti.OpCodes
62
import Test.Ganeti.Query.Aliases
62 63
import Test.Ganeti.Query.Filter
63 64
import Test.Ganeti.Query.Language
64 65
import Test.Ganeti.Query.Network
......
120 121
  , testNetwork
121 122
  , testObjects
122 123
  , testOpCodes
124
  , testQuery_Aliases
123 125
  , testQuery_Filter
124 126
  , testQuery_Language
125 127
  , testQuery_Network

Also available in: Unified diff