Back to index

apport  2.4
test_recoverable_problem.py
Go to the documentation of this file.
00001 '''Test recoverable_problem'''
00002 
00003 # Copyright (C) 2012 Canonical Ltd.
00004 # Author: Evan Dandrea <ev@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
00013 import sys
00014 import os
00015 import subprocess
00016 import tempfile
00017 import time
00018 import shutil
00019 import apport.report
00020 
00021 
00022 class T(unittest.TestCase):
00023     def setUp(self):
00024         self.report_dir = tempfile.mkdtemp()
00025         self.addCleanup(shutil.rmtree, self.report_dir)
00026         os.environ['APPORT_REPORT_DIR'] = self.report_dir
00027         self.datadir = os.environ.get('APPORT_DATA_DIR', '/usr/share/apport')
00028 
00029     def wait_for_report(self):
00030         cwd = os.getcwd().replace('/', '_')
00031         base = sys.argv[0]
00032         if base.startswith('./'):
00033             base = base[2:]
00034         base = base.replace('/', '_')
00035         path = '%s_%s.%d.crash' % (cwd, base, os.getuid())
00036         path = os.path.join(self.report_dir, path)
00037         seconds = 0
00038         while not os.path.exists(path):
00039             time.sleep(1)
00040             seconds += 1
00041             self.assertTrue(seconds < 10, 'timeout while waiting for %s to be created.' % path)
00042         return path
00043 
00044     def call_recoverable_problem(self, data):
00045         cmd = ['%s/recoverable_problem' % self.datadir]
00046         proc = subprocess.Popen(cmd, stdin=subprocess.PIPE,
00047                                 stderr=subprocess.PIPE)
00048         err = proc.communicate(data.encode('UTF-8'))[1]
00049         if proc.returncode != 0:
00050             self.assertNotEqual(err, b'')  # we expect some error message
00051             raise subprocess.CalledProcessError(proc.returncode, cmd[0])
00052         self.assertEqual(err, b'')
00053 
00054     def test_recoverable_problem(self):
00055         '''recoverable_problem with valid data'''
00056 
00057         self.call_recoverable_problem('hello\0there')
00058         path = self.wait_for_report()
00059         with open(path, 'rb') as report_path:
00060             report = apport.report.Report()
00061             report.load(report_path)
00062             self.assertEqual(report['hello'], 'there')
00063             self.assertTrue('Pid:\t%d' % os.getpid() in report['ProcStatus'])
00064 
00065     def test_invalid_data(self):
00066         '''recoverable_problem with invalid data'''
00067 
00068         self.assertRaises(subprocess.CalledProcessError,
00069                           self.call_recoverable_problem, 'hello')
00070 
00071         self.assertRaises(subprocess.CalledProcessError,
00072                           self.call_recoverable_problem,
00073                           'hello\0there\0extraneous')
00074 
00075         self.assertRaises(subprocess.CalledProcessError,
00076                           self.call_recoverable_problem,
00077                           'hello\0\0there')
00078 
00079 
00080 unittest.main()