Back to index

apport  2.4
Public Member Functions | Public Attributes | Private Member Functions
test_fileutils.T Class Reference

List of all members.

Public Member Functions

def setUp
def tearDown
def test_find_package_desktopfile
def test_likely_packaged
def test_find_file_package
def test_seen
def test_mark_hanging_process
def test_mark_report_upload
def test_get_all_reports
def test_get_system_reports
def test_delete_report
def test_get_recent_crashes
def test_make_report_path
def test_check_files_md5
def test_get_config

Public Attributes

 orig_report_dir
 orig_config_file

Private Member Functions

def _create_reports

Detailed Description

Definition at line 10 of file test_fileutils.py.


Member Function Documentation

def test_fileutils.T._create_reports (   self,
  create_inaccessible = False 
) [private]
Create some test reports

Definition at line 22 of file test_fileutils.py.

00022 
00023     def _create_reports(self, create_inaccessible=False):
00024         '''Create some test reports'''
00025 
00026         r1 = os.path.join(apport.fileutils.report_dir, 'rep1.crash')
00027         r2 = os.path.join(apport.fileutils.report_dir, 'rep2.crash')
00028 
00029         with open(r1, 'w') as fd:
00030             fd.write('report 1')
00031         with open(r2, 'w') as fd:
00032             fd.write('report 2')
00033         os.chmod(r1, 0o600)
00034         os.chmod(r2, 0o600)
00035         if create_inaccessible:
00036             ri = os.path.join(apport.fileutils.report_dir, 'inaccessible.crash')
00037             with open(ri, 'w') as fd:
00038                 fd.write('inaccessible')
00039             os.chmod(ri, 0)
00040             return [r1, r2, ri]
00041         else:
00042             return [r1, r2]

Here is the caller graph for this function:

def test_fileutils.T.setUp (   self)

Definition at line 11 of file test_fileutils.py.

00011 
00012     def setUp(self):
00013         self.orig_report_dir = apport.fileutils.report_dir
00014         apport.fileutils.report_dir = tempfile.mkdtemp()
00015         self.orig_config_file = apport.fileutils._config_file

def test_fileutils.T.tearDown (   self)

Definition at line 16 of file test_fileutils.py.

00016 
00017     def tearDown(self):
00018         shutil.rmtree(apport.fileutils.report_dir)
00019         apport.fileutils.report_dir = self.orig_report_dir
00020         self.orig_report_dir = None
00021         apport.fileutils._config_file = self.orig_config_file

check_files_md5()

Definition at line 221 of file test_fileutils.py.

00221 
00222     def test_check_files_md5(self):
00223         '''check_files_md5()'''
00224 
00225         f1 = os.path.join(apport.fileutils.report_dir, 'test 1.txt')
00226         f2 = os.path.join(apport.fileutils.report_dir, 'test:2.txt')
00227         sumfile = os.path.join(apport.fileutils.report_dir, 'sums.txt')
00228         with open(f1, 'w') as fd:
00229             fd.write('Some stuff')
00230         with open(f2, 'w') as fd:
00231             fd.write('More stuff')
00232         # use one relative and one absolute path in checksums file
00233         with open(sumfile, 'w') as fd:
00234             fd.write('''2e41290da2fa3f68bd3313174467e3b5  %s
00235 f6423dfbc4faf022e58b4d3f5ff71a70  %s
00236 ''' % (f1[1:], f2))
00237         self.assertEqual(apport.fileutils.check_files_md5(sumfile), [], 'correct md5sums')
00238 
00239         with open(f1, 'w') as fd:
00240             fd.write('Some stuff!')
00241         self.assertEqual(apport.fileutils.check_files_md5(sumfile), [f1[1:]], 'file 1 wrong')
00242         with open(f2, 'w') as fd:
00243             fd.write('More stuff!')
00244         self.assertEqual(apport.fileutils.check_files_md5(sumfile), [f1[1:], f2], 'files 1 and 2 wrong')
00245         with open(f1, 'w') as fd:
00246             fd.write('Some stuff')
00247         self.assertEqual(apport.fileutils.check_files_md5(sumfile), [f2], 'file 2 wrong')

Here is the call graph for this function:

delete_report()

Definition at line 170 of file test_fileutils.py.

00170 
00171     def test_delete_report(self):
00172         '''delete_report()'''
00173 
00174         tr = self._create_reports()
00175 
00176         while tr:
00177             self.assertEqual(set(apport.fileutils.get_all_reports()), set(tr))
00178             apport.fileutils.delete_report(tr.pop())

Here is the call graph for this function:

find_file_package()

Definition at line 90 of file test_fileutils.py.

00090 
00091     def test_find_file_package(self):
00092         '''find_file_package()'''
00093 
00094         self.assertEqual(apport.fileutils.find_file_package('/bin/bash'), 'bash')
00095         self.assertEqual(apport.fileutils.find_file_package('/bin/cat'), 'coreutils')
00096         self.assertEqual(apport.fileutils.find_file_package('/nonexisting'), None)

Here is the call graph for this function:

find_package_desktopfile()

Definition at line 43 of file test_fileutils.py.

00043 
00044     def test_find_package_desktopfile(self):
00045         '''find_package_desktopfile()'''
00046 
00047         # package without any .desktop file
00048         nodesktop = 'bash'
00049         assert len([f for f in apport.packaging.get_files(nodesktop)
00050                     if f.endswith('.desktop')]) == 0
00051 
00052         # find a package with one and a package with multiple .desktop files
00053         onedesktop = None
00054         multidesktop = None
00055         for d in os.listdir('/usr/share/applications/'):
00056             if not d.endswith('.desktop'):
00057                 continue
00058             pkg = apport.packaging.get_file_package(
00059                 os.path.join('/usr/share/applications/', d))
00060             num = len([f for f in apport.packaging.get_files(pkg)
00061                        if f.endswith('.desktop')])
00062             if not onedesktop and num == 1:
00063                 onedesktop = pkg
00064             elif not multidesktop and num > 1:
00065                 multidesktop = pkg
00066 
00067             if onedesktop and multidesktop:
00068                 break
00069 
00070         if nodesktop:
00071             self.assertEqual(apport.fileutils.find_package_desktopfile(nodesktop), None, 'no-desktop package %s' % nodesktop)
00072         if multidesktop:
00073             self.assertEqual(apport.fileutils.find_package_desktopfile(multidesktop), None, 'multi-desktop package %s' % multidesktop)
00074         if onedesktop:
00075             d = apport.fileutils.find_package_desktopfile(onedesktop)
00076             self.assertNotEqual(d, None, 'one-desktop package %s' % onedesktop)
00077             self.assertTrue(os.path.exists(d))
00078             self.assertTrue(d.endswith('.desktop'))

Here is the call graph for this function:

get_all_reports()

Definition at line 133 of file test_fileutils.py.

00133 
00134     def test_get_all_reports(self):
00135         '''get_all_reports()'''
00136 
00137         self.assertEqual(apport.fileutils.get_all_reports(), [])
00138         if os.getuid() == 0:
00139             tr = self._create_reports(True)
00140         else:
00141             tr = [r for r in self._create_reports(True) if not 'inaccessible' in r]
00142         self.assertEqual(set(apport.fileutils.get_all_reports()), set(tr))
00143 
00144         # now mark them as seen and check again
00145         for r in tr:
00146             apport.fileutils.mark_report_seen(r)
00147 
00148         self.assertEqual(set(apport.fileutils.get_all_reports()), set(tr))

Here is the call graph for this function:

get_config()

Definition at line 248 of file test_fileutils.py.

00248 
00249     def test_get_config(self):
00250         '''get_config()'''
00251 
00252         # nonexisting
00253         apport.fileutils._config_file = '/nonexisting'
00254         self.assertEqual(apport.fileutils.get_config('main', 'foo'), None)
00255         self.assertEqual(apport.fileutils.get_config('main', 'foo', 'moo'), 'moo')
00256         apport.fileutils.get_config.config = None  # trash cache
00257 
00258         # empty
00259         f = tempfile.NamedTemporaryFile()
00260         apport.fileutils._config_file = f.name
00261         self.assertEqual(apport.fileutils.get_config('main', 'foo'), None)
00262         self.assertEqual(apport.fileutils.get_config('main', 'foo', 'moo'), 'moo')
00263         apport.fileutils.get_config.config = None  # trash cache
00264 
00265         # nonempty
00266         f.write(b'[main]\none=1\ntwo = TWO\nb1 = 1\nb2=False\n[spethial]\none= 99\n')
00267         f.flush()
00268         self.assertEqual(apport.fileutils.get_config('main', 'foo'), None)
00269         self.assertEqual(apport.fileutils.get_config('main', 'foo', 'moo'), 'moo')
00270         self.assertEqual(apport.fileutils.get_config('main', 'one'), '1')
00271         self.assertEqual(apport.fileutils.get_config('main', 'one', default='moo'), '1')
00272         self.assertEqual(apport.fileutils.get_config('main', 'two'), 'TWO')
00273         self.assertEqual(apport.fileutils.get_config('main', 'b1', bool=True), True)
00274         self.assertEqual(apport.fileutils.get_config('main', 'b2', bool=True), False)
00275         self.assertEqual(apport.fileutils.get_config('main', 'b3', bool=True), None)
00276         self.assertEqual(apport.fileutils.get_config('main', 'b3', default=False, bool=True), False)
00277         self.assertEqual(apport.fileutils.get_config('spethial', 'one'), '99')
00278         self.assertEqual(apport.fileutils.get_config('spethial', 'two'), None)
00279         self.assertEqual(apport.fileutils.get_config('spethial', 'one', 'moo'), '99')
00280         self.assertEqual(apport.fileutils.get_config('spethial', 'nope', 'moo'), 'moo')
00281         apport.fileutils.get_config.config = None  # trash cache
00282 
00283         f.close()

Here is the call graph for this function:

get_recent_crashes()

Definition at line 179 of file test_fileutils.py.

00179 
00180     def test_get_recent_crashes(self):
00181         '''get_recent_crashes()'''
00182 
00183         # incomplete fields
00184         r = BytesIO(b'''ProblemType: Crash''')
00185         self.assertEqual(apport.fileutils.get_recent_crashes(r), 0)
00186 
00187         r = BytesIO(b'''ProblemType: Crash
00188 Date: Wed Aug 01 00:00:01 1990''')
00189         self.assertEqual(apport.fileutils.get_recent_crashes(r), 0)
00190 
00191         # ancient report
00192         r = BytesIO(b'''ProblemType: Crash
00193 Date: Wed Aug 01 00:00:01 1990
00194 CrashCounter: 3''')
00195         self.assertEqual(apport.fileutils.get_recent_crashes(r), 0)
00196 
00197         # old report (one day + one hour ago)
00198         date = time.ctime(time.mktime(time.localtime()) - 25 * 3600)
00199         r = BytesIO(b'''ProblemType: Crash
00200 Date: ''' + date.encode() + b'''
00201 CrashCounter: 3''')
00202         self.assertEqual(apport.fileutils.get_recent_crashes(r), 0)
00203 
00204         # current report (one hour ago)
00205         date = time.ctime(time.mktime(time.localtime()) - 3600)
00206         r = BytesIO(b'''ProblemType: Crash
00207 Date: ''' + date.encode() + b'''
00208 CrashCounter: 3''')
00209         self.assertEqual(apport.fileutils.get_recent_crashes(r), 3)

Here is the call graph for this function:

get_all_system_reports() and get_new_system_reports()

Definition at line 149 of file test_fileutils.py.

00149 
00150     def test_get_system_reports(self):
00151         '''get_all_system_reports() and get_new_system_reports()'''
00152 
00153         self.assertEqual(apport.fileutils.get_all_reports(), [])
00154         self.assertEqual(apport.fileutils.get_all_system_reports(), [])
00155         if os.getuid() == 0:
00156             tr = self._create_reports(True)
00157             self.assertEqual(set(apport.fileutils.get_all_system_reports()), set(tr))
00158             self.assertEqual(set(apport.fileutils.get_new_system_reports()), set(tr))
00159 
00160             # now mark them as seen and check again
00161             for r in tr:
00162                 apport.fileutils.mark_report_seen(r)
00163 
00164             self.assertEqual(set(apport.fileutils.get_all_system_reports()), set(tr))
00165             self.assertEqual(set(apport.fileutils.get_new_system_reports()), set([]))
00166         else:
00167             tr = [r for r in self._create_reports(True) if not 'inaccessible' in r]
00168             self.assertEqual(set(apport.fileutils.get_all_system_reports()), set([]))
00169             self.assertEqual(set(apport.fileutils.get_new_system_reports()), set([]))

Here is the call graph for this function:

likely_packaged()

Definition at line 79 of file test_fileutils.py.

00079 
00080     def test_likely_packaged(self):
00081         '''likely_packaged()'''
00082 
00083         self.assertEqual(apport.fileutils.likely_packaged('/bin/bash'), True)
00084         self.assertEqual(apport.fileutils.likely_packaged('/usr/bin/foo'), True)
00085         self.assertEqual(apport.fileutils.likely_packaged('/usr/local/bin/foo'), False)
00086         self.assertEqual(apport.fileutils.likely_packaged('/home/test/bin/foo'), False)
00087         self.assertEqual(apport.fileutils.likely_packaged('/tmp/foo'), False)
00088         # ignore crashes in /var/lib (LP#122859, LP#414368)
00089         self.assertEqual(apport.fileutils.likely_packaged('/var/lib/foo'), False)

Here is the call graph for this function:

make_report_path()

Definition at line 210 of file test_fileutils.py.

00210 
00211     def test_make_report_path(self):
00212         '''make_report_path()'''
00213 
00214         pr = problem_report.ProblemReport()
00215         self.assertRaises(ValueError, apport.fileutils.make_report_path, pr)
00216 
00217         pr['Package'] = 'bash 1'
00218         self.assertTrue(apport.fileutils.make_report_path(pr).startswith('%s/bash' % apport.fileutils.report_dir))
00219         pr['ExecutablePath'] = '/bin/bash'
00220         self.assertTrue(apport.fileutils.make_report_path(pr).startswith('%s/_bin_bash' % apport.fileutils.report_dir))

Here is the call graph for this function:

mark_hanging_process()

Definition at line 116 of file test_fileutils.py.

00116 
00117     def test_mark_hanging_process(self):
00118         '''mark_hanging_process()'''
00119         pr = problem_report.ProblemReport()
00120         pr['ExecutablePath'] = '/bin/bash'
00121         apport.fileutils.mark_hanging_process(pr, '1')
00122         uid = str(os.getuid())
00123         base = '_bin_bash.%s.1.hanging' % uid
00124         expected = os.path.join(apport.fileutils.report_dir, base)
00125         self.assertTrue(os.path.exists(expected))

Here is the call graph for this function:

mark_report_upload()

Definition at line 126 of file test_fileutils.py.

00126 
00127     def test_mark_report_upload(self):
00128         '''mark_report_upload()'''
00129         report = os.path.join(apport.fileutils.report_dir, 'report.crash')
00130         apport.fileutils.mark_report_upload(report)
00131         expected = os.path.join(apport.fileutils.report_dir, 'report.upload')
00132         self.assertTrue(os.path.exists(expected))

Here is the call graph for this function:

get_new_reports() and seen_report()

Definition at line 97 of file test_fileutils.py.

00097 
00098     def test_seen(self):
00099         '''get_new_reports() and seen_report()'''
00100 
00101         self.assertEqual(apport.fileutils.get_new_reports(), [])
00102         if os.getuid() == 0:
00103             tr = self._create_reports(True)
00104         else:
00105             tr = [r for r in self._create_reports(True) if not 'inaccessible' in r]
00106         self.assertEqual(set(apport.fileutils.get_new_reports()), set(tr))
00107 
00108         # now mark them as seen and check again
00109         nr = set(tr)
00110         for r in tr:
00111             self.assertEqual(apport.fileutils.seen_report(r), False)
00112             nr.remove(r)
00113             apport.fileutils.mark_report_seen(r)
00114             self.assertEqual(apport.fileutils.seen_report(r), True)
00115             self.assertEqual(set(apport.fileutils.get_new_reports()), nr)

Here is the call graph for this function:


Member Data Documentation

Definition at line 14 of file test_fileutils.py.

Definition at line 12 of file test_fileutils.py.


The documentation for this class was generated from the following file: