Statistics
| Branch: | Tag: | Revision:

root / qa / qa_utils.py @ 5d640672

History | View | Annotate | Download (2.9 kB)

1
# Copyright (C) 2007 Google Inc.
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful, but
9
# WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11
# General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16
# 02110-1301, USA.
17

    
18

    
19
"""Utilities for QA tests.
20

21
"""
22

    
23
import os
24
import subprocess
25

    
26
from ganeti import utils
27

    
28
import qa_config
29
import qa_error
30

    
31

    
32
def AssertEqual(first, second, msg=None):
33
  """Raises an error when values aren't equal.
34

35
  """
36
  if not first == second:
37
    raise qa_error.Error(msg or '%r == %r' % (first, second))
38

    
39

    
40
def GetSSHCommand(node, cmd, strict=True):
41
  """Builds SSH command to be executed.
42

43
  """
44
  args = [ 'ssh', '-oEscapeChar=none', '-oBatchMode=yes', '-l', 'root' ]
45

    
46
  if strict:
47
    tmp = 'yes'
48
  else:
49
    tmp = 'no'
50
  args.append('-oStrictHostKeyChecking=%s' % tmp)
51
  args.append('-oClearAllForwardings=yes')
52
  args.append('-oForwardAgent=yes')
53
  args.append(node)
54

    
55
  if qa_config.options.dry_run:
56
    prefix = 'exit 0; '
57
  else:
58
    prefix = ''
59

    
60
  args.append(prefix + cmd)
61

    
62
  print 'SSH:', utils.ShellQuoteArgs(args)
63

    
64
  return args
65

    
66

    
67
def StartSSH(node, cmd, strict=True):
68
  """Starts SSH.
69

70
  """
71
  args = GetSSHCommand(node, cmd, strict=strict)
72
  return subprocess.Popen(args, shell=False)
73

    
74

    
75
def UploadFile(node, src):
76
  """Uploads a file to a node and returns the filename.
77

78
  Caller needs to remove the returned file on the node when it's not needed
79
  anymore.
80
  """
81
  # Make sure nobody else has access to it while preserving local permissions
82
  mode = os.stat(src).st_mode & 0700
83

    
84
  cmd = ('tmp=$(tempfile --mode %o --prefix gnt) && '
85
         '[[ -f "${tmp}" ]] && '
86
         'cat > "${tmp}" && '
87
         'echo "${tmp}"') % mode
88

    
89
  f = open(src, 'r')
90
  try:
91
    p = subprocess.Popen(GetSSHCommand(node, cmd), shell=False, stdin=f,
92
                         stdout=subprocess.PIPE)
93
    AssertEqual(p.wait(), 0)
94

    
95
    # Return temporary filename
96
    return p.stdout.read().strip()
97
  finally:
98
    f.close()
99

    
100

    
101
def ResolveInstanceName(instance):
102
  """Gets the full name of an instance.
103

104
  """
105
  master = qa_config.GetMasterNode()
106

    
107
  info_cmd = utils.ShellQuoteArgs(['gnt-instance', 'info', instance['name']])
108
  sed_cmd = utils.ShellQuoteArgs(['sed', '-n', '-e', 's/^Instance name: *//p'])
109

    
110
  cmd = '%s | %s' % (info_cmd, sed_cmd)
111
  ssh_cmd = GetSSHCommand(master['primary'], cmd)
112
  p = subprocess.Popen(ssh_cmd, shell=False, stdout=subprocess.PIPE)
113
  AssertEqual(p.wait(), 0)
114

    
115
  return p.stdout.read().strip()