Back to index

apport  2.4
test_apport_unpack.py
Go to the documentation of this file.
00001 '''Test apport-unpack'''
00002 
00003 # Copyright (C) 2012 Canonical Ltd.
00004 # Author: Martin Pitt <martin.pitt@ubuntu.com>
00005 #
00006 # This program is free software; you can redistribute it and/or modify it
00007 # under the terms of the GNU General Public License as published by the
00008 # Free Software Foundation; either version 2 of the License, or (at your
00009 # option) any later version.  See http://www.gnu.org/copyleft/gpl.html for
00010 # the full text of the license.
00011 
00012 import unittest, subprocess, tempfile, shutil, os, os.path, sys
00013 import problem_report
00014 
00015 
00016 class T(unittest.TestCase):
00017     @classmethod
00018     def setUpClass(klass):
00019         klass.workdir = tempfile.mkdtemp()
00020 
00021         # create problem report file with all possible data types
00022         r = problem_report.ProblemReport()
00023         klass.utf8_str = b'a\xe2\x99\xa5b'
00024         klass.bindata = b'\x00\x01\xFF\x40'
00025         r['utf8'] = klass.utf8_str
00026         r['unicode'] = klass.utf8_str.decode('UTF-8')
00027         r['binary'] = klass.bindata
00028         r['compressed'] = problem_report.CompressedValue(b'FooFoo!')
00029 
00030         klass.report_file = os.path.join(klass.workdir, 'test.apport')
00031         with open(klass.report_file, 'wb') as f:
00032             r.write(f)
00033 
00034         klass.unpack_dir = os.path.join(klass.workdir, 'un pack')
00035 
00036     @classmethod
00037     def tearDownClass(klass):
00038         shutil.rmtree(klass.workdir)
00039 
00040     def tearDown(self):
00041         if os.path.isdir(self.unpack_dir):
00042             shutil.rmtree(self.unpack_dir)
00043 
00044     def test_unpack(self):
00045         '''apport-unpack for all possible data types'''
00046 
00047         self.assertEqual(self._call(['apport-unpack', self.report_file, self.unpack_dir]),
00048                          (0, '', ''))
00049 
00050         self.assertEqual(self._get_unpack('utf8'), self.utf8_str)
00051         self.assertEqual(self._get_unpack('unicode'), self.utf8_str)
00052         self.assertEqual(self._get_unpack('binary'), self.bindata)
00053         self.assertEqual(self._get_unpack('compressed'), b'FooFoo!')
00054 
00055     def test_unpack_python(self):
00056         '''apport-unpack with explicity Python interpreter
00057 
00058         This will catch Python 2/3 specific errors when running the tests with
00059         a different $PYTHON than apport-unpacks' hashbang.
00060         '''
00061         apport_unpack = os.path.join(
00062             os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'bin',
00063             'apport-unpack')
00064         if not os.path.exists(apport_unpack):
00065             apport_unpack = '/usr/bin/apport-unpack'
00066         if not os.path.exists(apport_unpack):
00067             sys.stderr.write('[skip: apport-unpack not found] ')
00068             return
00069 
00070         self.assertEqual(self._call([os.getenv('PYTHON', 'python3'),
00071                                      apport_unpack,
00072                                      self.report_file,
00073                                      self.unpack_dir]),
00074                          (0, '', ''))
00075 
00076         self.assertEqual(self._get_unpack('utf8'), self.utf8_str)
00077         self.assertEqual(self._get_unpack('unicode'), self.utf8_str)
00078         self.assertEqual(self._get_unpack('binary'), self.bindata)
00079         self.assertEqual(self._get_unpack('compressed'), b'FooFoo!')
00080 
00081     def test_help(self):
00082         '''calling apport-unpack with help'''
00083 
00084         (ret, out, err) = self._call(['apport-unpack', '--help'])
00085         self.assertEqual(ret, 0)
00086         self.assertEqual(err, '')
00087         self.assertTrue(out.startswith('Usage:'), out)
00088 
00089     def test_error(self):
00090         '''calling apport-unpack with wrong arguments'''
00091 
00092         (ret, out, err) = self._call(['apport-unpack'])
00093         self.assertEqual(ret, 1)
00094         self.assertEqual(err, '')
00095         self.assertTrue(out.startswith('Usage:'), out)
00096 
00097         (ret, out, err) = self._call(['apport-unpack', self.report_file])
00098         self.assertEqual(ret, 1)
00099         self.assertEqual(err, '')
00100         self.assertTrue(out.startswith('Usage:'), out)
00101 
00102         (ret, out, err) = self._call(['apport-unpack', '/nonexisting.crash', self.unpack_dir])
00103         self.assertEqual(ret, 1)
00104         self.assertTrue('/nonexisting.crash' in err)
00105         self.assertEqual(out, '')
00106 
00107     def _call(self, argv):
00108         a = subprocess.Popen(argv, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
00109         (out, err) = a.communicate()
00110         return (a.returncode, out.decode('UTF-8'), err.decode('UTF-8'))
00111 
00112     def _get_unpack(self, fname):
00113         with open(os.path.join(self.unpack_dir, fname), 'rb') as f:
00114             return f.read()
00115 
00116 unittest.main()