assertSoftware.py 10.9 KB
Newer Older
Łukasz Nowak's avatar
Łukasz Nowak committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
##############################################################################
#
# Copyright (c) 2008-2010 Nexedi SA and Contributors. All Rights Reserved.
#                    Lukasz Nowak <luke@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly advised to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################
28

29
import unittest
30
import sys, os, tempfile, stat, subprocess
31

32 33
def getCleanList(s):
  """Converts free form string separated by whitespaces to python list"""
34 35
  return sorted([q.strip() for q in s.split() if len(q.strip()) > 0])

36 37 38 39 40 41 42 43 44 45
def readElfAsDict(f):
  """Reads ELF information from file"""
  popen = subprocess.Popen(['readelf', '-d', f], stdout=subprocess.PIPE,
      stderr=subprocess.STDOUT)
  result = popen.communicate()[0]
  if popen.returncode != 0:
    return False
  library_list = []
  for l in result.split('\n'):
    if '(NEEDED)' in l:
Łukasz Nowak's avatar
Łukasz Nowak committed
46
      library_list.append(l.split(':')[1].strip(' []').split('.so')[0])
47 48 49 50 51
    elif '(RPATH)' in l:
      rpath_list = l.split(':',1)[1].strip(' []').split(':')
    elif '(RUNPATH)' in l:
      runpath_list = l.split(':',1)[1].strip(' []').split(':')
  return dict(
Łukasz Nowak's avatar
Łukasz Nowak committed
52 53 54
    library_list=sorted(library_list),
    rpath_list=sorted(rpath_list),
    runpath_list=sorted(runpath_list)
55 56
  )

57
class AssertPythonSoftware(unittest.TestCase):
Łukasz Nowak's avatar
Łukasz Nowak committed
58
  """Asserts that python related software is in good shape."""
59

60
  def test_python_version(self):
Łukasz Nowak's avatar
Łukasz Nowak committed
61 62
    """Check built python version"""
    self.assertEqual((2,4), sys.version_info[:2])
63

64
  def test_use_generated_python(self):
Łukasz Nowak's avatar
Łukasz Nowak committed
65
    """Checks generated python as python"""
66
    fd, name = tempfile.mkstemp()
67 68 69
    try:
      f = os.fdopen(fd, 'w')
      f.write("""\
70 71 72 73
#!%s
import sys
print sys.version_info[:2]
    """ % sys.executable)
74 75 76
      f.close()
      f_stat = os.stat(name)
      os.chmod(name, f_stat.st_mode | stat.S_IXUSR)
77 78
      result = subprocess.Popen([name], stdout=subprocess.PIPE)\
          .communicate()[0].strip()
79
      self.assertEqual('(2, 4)', result)
80 81 82
    finally:
      os.unlink(name)

83
  def test_use_generated_python_as_normal_interpreter(self):
Łukasz Nowak's avatar
Łukasz Nowak committed
84
    """Checks behaviour of generated python as interpreter"""
Rafael Monnerat's avatar
Rafael Monnerat committed
85
    stdout, stderr = subprocess.Popen(["bin/python2.4", "-V"],
86
        stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
Rafael Monnerat's avatar
Rafael Monnerat committed
87
    self.assertTrue('Python 2.4' in stderr)
88

89
  def test_required_libraries(self):
Łukasz Nowak's avatar
Łukasz Nowak committed
90
    """Checks possibility of importing libraries"""
91
    ignored_library_list = getCleanList("""
92 93
      socks
    """)
94
    required_library_list = getCleanList("""
95 96 97 98 99 100
      ERP5Diff
      MySQLdb
      SOAPpy
      _ssl
      _xmlplus
      bz2
Łukasz Nowak's avatar
Łukasz Nowak committed
101
      cElementTree
102 103 104 105 106 107
      elementtree
      fpconst
      gdbm
      itools
      ldap
      lxml
Łukasz Nowak's avatar
Łukasz Nowak committed
108
      mechanize
109 110
      memcache
      numpy
Łukasz Nowak's avatar
Łukasz Nowak committed
111 112
      paramiko
      ply
113
      pytz
114
      readline
115 116
      simplejson
      threadframe
117
      uuid
Łukasz Nowak's avatar
Łukasz Nowak committed
118
      xml
119 120 121 122 123 124 125 126 127 128
      xml.parsers.expat
      zlib
      """)
    failed_library_list = []
    for lib in required_library_list:
      try:
        __import__(lib)
      except ImportError:
        failed_library_list.append(lib)
    self.assertEqual([], failed_library_list,
Łukasz Nowak's avatar
Łukasz Nowak committed
129
        'Python libraries not found:\n'+'\n'.join(failed_library_list))
130 131

class AssertLddLibs(unittest.TestCase):
Łukasz Nowak's avatar
Łukasz Nowak committed
132
  """Checks for dynamic libraries"""
133

134
  def test_ocropus(self):
Łukasz Nowak's avatar
Łukasz Nowak committed
135
    """Ocropus"""
136 137 138 139 140 141 142
    result = os.system("ldd parts/ocropus/bin/ocropus | grep -q "
        "'parts/ocropus/lib/libocropus.so'")
    self.assertEqual(result, 0)
    result = os.system("ldd parts/ocropus/bin/ocropus | grep -q "
        "'parts/.*/lib/libiulib.so'")
    self.assertEqual(result, 0)

143
  def test_memcached_libevent(self):
Łukasz Nowak's avatar
Łukasz Nowak committed
144
    """Checks proper liunking to libevent from memcached"""
145 146 147
    result = os.system("ldd parts/memcached/bin/memcached | grep -q 'parts/li"
        "bevent/lib/libevent'")

148 149 150 151 152 153 154 155 156 157 158 159 160
class AssertSoftwareRunable(unittest.TestCase):
  def test_HaProxy(self):
    stdout, stderr = subprocess.Popen(["parts/haproxy/sbin/haproxy", "-v"],
        stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
    self.assertEqual(stderr, '')
    self.assertTrue(stdout.startswith('HA-Proxy'))

  def test_Apache(self):
    stdout, stderr = subprocess.Popen(["parts/apache/bin/httpd", "-v"],
        stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
    self.assertEqual(stderr, '')
    self.assertTrue(stdout.startswith('Server version: Apache'))

161 162 163 164 165 166
  def test_Varnish(self):
    stdout, stderr = subprocess.Popen(["parts/varnish/sbin/varnishd", "-V"],
        stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
    self.assertEqual(stdout, '')
    self.assertTrue(stderr.startswith('varnishd ('))

167 168 169 170 171 172
  def test_Ocropus(self):
    stdout, stderr = subprocess.Popen(["parts/ocropus/bin/ocropus"],
        stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
    self.assertEqual(stdout, '')
    self.assertTrue('splitting books' in stderr)

173 174 175 176 177 178 179 180 181 182 183 184 185
  def test_TokyoCabinet(self):
    stdout, stderr = subprocess.Popen(["parts/tokyocabinet/bin/tcamgr",
      "version"],
        stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
    self.assertEqual(stderr, '')
    self.assertTrue(stdout.startswith('Tokyo Cabinet'))

  def test_Flare(self):
    stdout, stderr = subprocess.Popen(["parts/flare/bin/flarei", "-v"],
        stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
    self.assertEqual(stderr, '')
    self.assertTrue(stdout.startswith('flare'))

186
  def test_rdiff_backup(self):
187 188
    stdout, stderr = subprocess.Popen(["bin/rdiff-backup", "-V"],
        stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
189 190 191
    self.assertEqual(stderr, '')
    self.assertEqual(stdout.strip(), 'rdiff-backup 1.0.5')

192 193 194 195 196 197 198 199 200 201 202 203
  def test_imagemagick(self):
    binary_list = [ 'animate', 'composite', 'convert', 'identify', 'mogrify',
        'stream', 'compare', 'conjure', 'display', 'import', 'montage']
    base = os.path.join('parts', 'imagemagick', 'bin')
    error_list = []
    for binary in binary_list:
      stdout, stderr = subprocess.Popen([os.path.join(base, binary), "-version"],
          stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
      if 'Version: ImageMagick' not in stdout:
        error_list.append(binary)
    self.assertEqual([], error_list)

204 205 206 207 208 209
  def test_w3m(self):
    stdout, stderr = subprocess.Popen(["parts/w3m/bin/w3m", "-V"],
        stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
    self.assertEqual(stderr, '')
    self.assertTrue(stdout.startswith('w3m version w3m/0.5.2'))

210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
  def test_MySQLdb(self):
    """Checks proper linking to mysql library from MySQLdb egg"""
    error_list = []
    for d in os.listdir('develop-eggs'):
      if d.startswith('MySQL_python'):
        path = os.path.join('develop-eggs', d, '_mysql.so')
        if os.system("ldd %s | grep -q 'parts/mysql-tritonn-5.0/lib/my"
            "sql/libmysqlclient_r.so'" % path) != 0:
          error_list.append(path)
    self.assertEqual(error_list, [])

class AssertMysql50Tritonn(unittest.TestCase):
  def test_tritonn_senna(self):
    """Senna as an library"""
    result = os.system("ldd parts/mysql-tritonn-5.0/libexec/mysqld | grep -q "
        "'parts/senna/lib/libsenna.so.0'")
    self.assertEqual(result, 0)

228
class AssertApache(unittest.TestCase):
Łukasz Nowak's avatar
Łukasz Nowak committed
229
  """Tests for built apache"""
230

231 232 233
  def test_ld_libaprutil1(self):
    """Checks proper linking"""
    elf_dict = readElfAsDict('parts/apache/lib/libaprutil-1.so')
Łukasz Nowak's avatar
Łukasz Nowak committed
234 235 236 237 238 239 240
    self.assertEqual(sorted(['libexpat', 'libapr-1', 'librt', 'libcrypt',
      'libpthread', 'libdl', 'libc']), elf_dict['library_list'])
    soft_dir = os.path.join(os.path.abspath(os.curdir), 'parts')
    expected_rpath_list = [os.path.join(soft_dir, software, 'lib') for
        software in ['apache', 'zlib', 'openssl']]
    self.assertEqual(sorted(expected_rpath_list), elf_dict['rpath_list'])
    self.assertEqual(sorted(expected_rpath_list), elf_dict['runpath_list'])
241

242
  def test_modules(self):
Łukasz Nowak's avatar
Łukasz Nowak committed
243
    """Checks for availability of apache modules"""
244
    required_module_list = getCleanList("""
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
      authn_default_module
      log_config_module
      proxy_http_module
      authn_alias_module
      authz_dbm_module
      case_filter_in_module
      imagemap_module
      setenvif_module
      include_module
      charset_lite_module
      info_module
      cache_module
      actions_module
      proxy_connect_module
      auth_digest_module
      unique_id_module
      mime_magic_module
      disk_cache_module
      mime_module
      usertrack_module
      asis_module
      optional_hook_import_module
      negotiation_module
      proxy_module
      authz_default_module
      ext_filter_module
      auth_basic_module
      authz_owner_module
      authn_anon_module
      rewrite_module
      proxy_balancer_module
      substitute_module
      filter_module
      expires_module
      autoindex_module
      status_module
      cgid_module
      version_module
      echo_module
      optional_fn_export_module
      optional_fn_import_module
      ident_module
      cgi_module
      bucketeer_module
      optional_hook_export_module
      vhost_alias_module
      ssl_module
      authz_user_module
      env_module
      logio_module
      proxy_ftp_module
      cern_meta_module
      authz_groupfile_module
      dir_module
      log_forensic_module
      alias_module
      deflate_module
      authn_dbm_module
      case_filter_module
      authz_host_module
      headers_module
      dumpio_module
      speling_module
      authn_file_module
309
    """)
310 311 312 313 314
    parts_path_prefix = os.path.join(os.path.dirname(__file__), '../parts')
    result = os.popen("%s/apache/bin/httpd -M" % parts_path_prefix)
    loaded_module_list = [module_name for module_name in result.read().split() 
                          if module_name.endswith('module')]
    result.close()
315 316
    failed_module_list = []
    for module in required_module_list:
317
      if module not in loaded_module_list:
318 319
        failed_module_list.append(module)
    self.assertEqual([], failed_module_list,
Łukasz Nowak's avatar
Łukasz Nowak committed
320
        'Apache modules not found:\n'+'\n'.join(failed_module_list))
321 322 323

if __name__ == '__main__':
  unittest.main()