Add an example "ethers" hook
[ganeti-local] / qa / qa_node.py
1 #
2 #
3
4 # Copyright (C) 2007 Google Inc.
5 #
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.
10 #
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.
15 #
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
19 # 02110-1301, USA.
20
21
22 from ganeti import utils
23
24 import qa_config
25 import qa_error
26 import qa_utils
27
28 from qa_utils import AssertEqual, StartSSH
29
30
31 def _NodeAdd(node, readd=False):
32   master = qa_config.GetMasterNode()
33
34   if not readd and node.get('_added', False):
35     raise qa_error.Error("Node %s already in cluster" % node['primary'])
36   elif readd and not node.get('_added', False):
37     raise qa_error.Error("Node %s not yet in cluster" % node['primary'])
38
39   cmd = ['gnt-node', 'add', "--no-ssh-key-check"]
40   if node.get('secondary', None):
41     cmd.append('--secondary-ip=%s' % node['secondary'])
42   if readd:
43     cmd.append('--readd')
44   cmd.append(node['primary'])
45   AssertEqual(StartSSH(master['primary'],
46                        utils.ShellQuoteArgs(cmd)).wait(), 0)
47
48   node['_added'] = True
49
50
51 def _NodeRemove(node):
52   master = qa_config.GetMasterNode()
53
54   cmd = ['gnt-node', 'remove', node['primary']]
55   AssertEqual(StartSSH(master['primary'],
56                        utils.ShellQuoteArgs(cmd)).wait(), 0)
57   node['_added'] = False
58
59
60 def TestNodeAddAll():
61   """Adding all nodes to cluster."""
62   master = qa_config.GetMasterNode()
63   for node in qa_config.get('nodes'):
64     if node != master:
65       _NodeAdd(node, readd=False)
66
67
68 def TestNodeRemoveAll():
69   """Removing all nodes from cluster."""
70   master = qa_config.GetMasterNode()
71   for node in qa_config.get('nodes'):
72     if node != master:
73       _NodeRemove(node)
74
75
76 def TestNodeReadd(node):
77   """gnt-node add --readd"""
78   _NodeAdd(node, readd=True)
79
80
81 def TestNodeInfo():
82   """gnt-node info"""
83   master = qa_config.GetMasterNode()
84
85   cmd = ['gnt-node', 'info']
86   AssertEqual(StartSSH(master['primary'],
87                        utils.ShellQuoteArgs(cmd)).wait(), 0)
88
89
90 def TestNodeVolumes():
91   """gnt-node volumes"""
92   master = qa_config.GetMasterNode()
93
94   cmd = ['gnt-node', 'volumes']
95   AssertEqual(StartSSH(master['primary'],
96                        utils.ShellQuoteArgs(cmd)).wait(), 0)
97
98
99 def TestNodeFailover(node, node2):
100   """gnt-node failover"""
101   master = qa_config.GetMasterNode()
102
103   if qa_utils.GetNodeInstances(node2, secondaries=False):
104     raise qa_error.UnusableNodeError("Secondary node has at least one"
105                                      " primary instance. This test requires"
106                                      " it to have no primary instances.")
107
108   # Fail over to secondary node
109   cmd = ['gnt-node', 'failover', '-f', node['primary']]
110   AssertEqual(StartSSH(master['primary'],
111                        utils.ShellQuoteArgs(cmd)).wait(), 0)
112
113   # ... and back again.
114   cmd = ['gnt-node', 'failover', '-f', node2['primary']]
115   AssertEqual(StartSSH(master['primary'],
116                        utils.ShellQuoteArgs(cmd)).wait(), 0)
117
118
119 def TestNodeEvacuate(node, node2):
120   """gnt-node evacuate"""
121   master = qa_config.GetMasterNode()
122
123   node3 = qa_config.AcquireNode(exclude=[node, node2])
124   try:
125     if qa_utils.GetNodeInstances(node3, secondaries=True):
126       raise qa_error.UnusableNodeError("Evacuation node has at least one"
127                                        " secondary instance. This test requires"
128                                        " it to have no secondary instances.")
129
130     # Evacuate all secondary instances
131     cmd = ['gnt-node', 'evacuate', '-f', node2['primary'], node3['primary']]
132     AssertEqual(StartSSH(master['primary'],
133                          utils.ShellQuoteArgs(cmd)).wait(), 0)
134
135     # ... and back again.
136     cmd = ['gnt-node', 'evacuate', '-f', node3['primary'], node2['primary']]
137     AssertEqual(StartSSH(master['primary'],
138                          utils.ShellQuoteArgs(cmd)).wait(), 0)
139   finally:
140     qa_config.ReleaseNode(node3)