4 # Copyright (C) 2010, 2011, 2012 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 """QA tests for node groups.
26 from ganeti import constants
27 from ganeti import query
28 from ganeti import utils
33 from qa_utils import AssertCommand, AssertEqual, GetCommandOutput
36 def GetDefaultGroup():
37 """Returns the default node group.
40 groups = qa_config.get("groups", {})
41 return groups.get("group-with-nodes", constants.INITIAL_NODE_GROUP_NAME)
44 def TestGroupAddRemoveRename():
45 """gnt-group add/remove/rename"""
46 existing_group_with_nodes = GetDefaultGroup()
48 (group1, group2, group3) = qa_utils.GetNonexistentGroups(3)
50 AssertCommand(["gnt-group", "add", group1])
51 AssertCommand(["gnt-group", "add", group2])
52 AssertCommand(["gnt-group", "add", group2], fail=True)
53 AssertCommand(["gnt-group", "add", existing_group_with_nodes], fail=True)
55 AssertCommand(["gnt-group", "rename", group1, group2], fail=True)
56 AssertCommand(["gnt-group", "rename", group1, group3])
59 AssertCommand(["gnt-group", "rename", existing_group_with_nodes, group1])
61 AssertCommand(["gnt-group", "remove", group2])
62 AssertCommand(["gnt-group", "remove", group3])
63 AssertCommand(["gnt-group", "remove", group1], fail=True)
65 # Try to ensure idempotency re groups that already existed.
66 AssertCommand(["gnt-group", "rename", group1, existing_group_with_nodes])
69 def TestGroupAddWithOptions():
70 """gnt-group add with options"""
71 (group1, ) = qa_utils.GetNonexistentGroups(1)
73 AssertCommand(["gnt-group", "add", "--alloc-policy", "notvalid", group1],
76 AssertCommand(["gnt-group", "add", "--alloc-policy", "last_resort",
77 "--node-parameters", "oob_program=/bin/true", group1])
79 AssertCommand(["gnt-group", "remove", group1])
82 def _TestGroupModifyISpecs(groupname):
83 std_defaults = constants.IPOLICY_DEFAULTS[constants.ISPECS_STD]
84 min_v = std_defaults[constants.ISPEC_MEM_SIZE] * 10
87 # Get the ipolicy command (from the cluster config)
88 mnode = qa_config.GetMasterNode()
89 addcmd = GetCommandOutput(mnode.primary, utils.ShellQuoteArgs([
90 "gnt-group", "show-ispecs-cmd", "--include-defaults", groupname,
92 modcmd = ["gnt-group", "modify"]
94 assert opts[0:2] == ["gnt-group", "add"]
95 for k in range(2, len(opts) - 1):
96 if opts[k].startswith("--ipolicy-"):
97 assert k + 2 <= len(opts)
98 modcmd.extend(opts[k:k + 2])
99 modcmd.append(groupname)
100 # Apply the ipolicy to the group and verify the result
101 AssertCommand(modcmd)
102 new_addcmd = GetCommandOutput(mnode.primary, utils.ShellQuoteArgs([
103 "gnt-group", "show-ispecs-cmd", groupname,
105 AssertEqual(addcmd, new_addcmd)
107 AssertCommand(["gnt-group", "modify", "--specs-mem-size",
108 "min=%s,max=%s,std=0" % (min_v, max_v), groupname], fail=True)
109 AssertCommand(["gnt-group", "modify", "--specs-mem-size",
110 "min=%s,max=%s" % (min_v, max_v), groupname])
111 AssertCommand(["gnt-group", "modify", "--specs-mem-size",
112 "min=default,max=default", groupname])
115 def _TestGroupModifyIPolicy(groupname):
116 _TestGroupModifyISpecs(groupname)
117 AssertCommand(["gnt-group", "modify", "--ipolicy-vcpu-ratio",
119 AssertCommand(["gnt-group", "modify", "--ipolicy-vcpu-ratio",
120 "default", groupname])
123 def TestGroupModify():
124 """gnt-group modify"""
125 (group1, ) = qa_utils.GetNonexistentGroups(1)
127 AssertCommand(["gnt-group", "add", group1])
130 _TestGroupModifyIPolicy(group1)
131 AssertCommand(["gnt-group", "modify", "--alloc-policy", "unallocable",
132 "--node-parameters", "oob_program=/bin/false", group1])
133 AssertCommand(["gnt-group", "modify",
134 "--alloc-policy", "notvalid", group1], fail=True)
135 AssertCommand(["gnt-group", "modify",
136 "--node-parameters", "spindle_count=10", group1])
137 if qa_config.TestEnabled("htools"):
138 AssertCommand(["hbal", "-L", "-G", group1])
139 AssertCommand(["gnt-group", "modify",
140 "--node-parameters", "spindle_count=default", group1])
142 AssertCommand(["gnt-group", "remove", group1])
147 qa_utils.GenericQueryTest("gnt-group", query.GROUP_FIELDS.keys())
150 def TestGroupListFields():
151 """gnt-group list-fields"""
152 qa_utils.GenericQueryFieldsTest("gnt-group", query.GROUP_FIELDS.keys())
155 def TestAssignNodesIncludingSplit(orig_group, node1, node2):
156 """gnt-group assign-nodes --force
158 Expects node1 and node2 to be primary and secondary for a common instance.
161 assert node1 != node2
163 (other_group, ) = qa_utils.GetNonexistentGroups(1)
165 master_node = qa_config.GetMasterNode().primary
167 def AssertInGroup(group, nodes):
168 real_output = GetCommandOutput(master_node,
169 "gnt-node list --no-headers -o group " +
170 utils.ShellQuoteArgs(nodes))
171 AssertEqual(real_output.splitlines(), [group] * len(nodes))
173 AssertInGroup(orig_group, [node1, node2])
174 AssertCommand(["gnt-group", "add", other_group])
177 AssertCommand(["gnt-group", "assign-nodes", other_group, node1, node2])
178 AssertInGroup(other_group, [node1, node2])
180 # This should fail because moving node1 to orig_group would leave their
181 # common instance split between orig_group and other_group.
182 AssertCommand(["gnt-group", "assign-nodes", orig_group, node1], fail=True)
183 AssertInGroup(other_group, [node1, node2])
185 AssertCommand(["gnt-group", "assign-nodes", "--force", orig_group, node1])
186 AssertInGroup(orig_group, [node1])
187 AssertInGroup(other_group, [node2])
189 AssertCommand(["gnt-group", "assign-nodes", orig_group, node2])
190 AssertInGroup(orig_group, [node1, node2])
192 AssertCommand(["gnt-group", "remove", other_group])