Back to index

apport  2.3
Public Member Functions | Public Attributes | Private Member Functions
test_hookutils.T Class Reference

List of all members.

Public Member Functions

def setUp
def tearDown
def test_module_license_evaluation
def test_attach_dmesg
def test_dmesg_overwrite
def test_attach_file
def test_attach_file_binary
def test_attach_file_if_exists
def test_recent_logfile
def test_recent_logfile_overflow
def test_in_session_of_problem
def test_xsession_errors
def test_no_crashes
def test_command_output

Public Attributes

 workdir

Private Member Functions

def _get_mem_usage

Detailed Description

Definition at line 7 of file test_hookutils.py.


Member Function Documentation

def test_hookutils.T._get_mem_usage (   klass) [private]
Get current memory usage in kB

Definition at line 312 of file test_hookutils.py.

00312 
00313     def _get_mem_usage(klass):
00314         '''Get current memory usage in kB'''
00315 
00316         with open('/proc/self/status') as f:
00317             for line in f:
00318                 if line.startswith('VmSize:'):
00319                     return int(line.split()[1])
00320             else:
00321                 raise SystemError('did not find VmSize: in /proc/self/status')
00322 
00323 unittest.main()

Here is the caller graph for this function:

def test_hookutils.T.setUp (   self)

Definition at line 8 of file test_hookutils.py.

00008 
00009     def setUp(self):
00010         self.workdir = tempfile.mkdtemp()

def test_hookutils.T.tearDown (   self)

Definition at line 11 of file test_hookutils.py.

00011 
00012     def tearDown(self):
00013         shutil.rmtree(self.workdir)

attach_dmesg() does not overwrite already existing data

Definition at line 53 of file test_hookutils.py.

00053 
00054     def test_attach_dmesg(self):
00055         '''attach_dmesg() does not overwrite already existing data'''
00056 
00057         report = {}
00058 
00059         apport.hookutils.attach_dmesg(report)
00060         self.assertTrue(report['BootDmesg'].startswith('['))
00061         self.assertTrue(len(report['BootDmesg']) > 500)
00062         self.assertTrue(report['CurrentDmesg'].startswith('['))

Here is the call graph for this function:

attach_file()

Definition at line 78 of file test_hookutils.py.

00078 
00079     def test_attach_file(self):
00080         '''attach_file()'''
00081 
00082         with open('/etc/motd') as f:
00083             motd_contents = f.read().strip()
00084         with open('/etc/issue') as f:
00085             issue_contents = f.read().strip()
00086 
00087         # default key name
00088         report = {}
00089         apport.hookutils.attach_file(report, '/etc/motd')
00090         self.assertEqual(list(report), ['.etc.motd'])
00091         self.assertEqual(report['.etc.motd'], motd_contents)
00092 
00093         # custom key name
00094         report = {}
00095         apport.hookutils.attach_file(report, '/etc/motd', 'Motd')
00096         self.assertEqual(list(report), ['Motd'])
00097         self.assertEqual(report['Motd'], motd_contents)
00098 
00099         # nonexisting file
00100         report = {}
00101         apport.hookutils.attach_file(report, '/nonexisting')
00102         self.assertEqual(list(report), ['.nonexisting'])
00103         self.assertTrue(report['.nonexisting'].startswith('Error: '))
00104 
00105         # existing key
00106         report = {}
00107         apport.hookutils.attach_file(report, '/etc/motd')
00108         apport.hookutils.attach_file(report, '/etc/motd')
00109         self.assertEqual(list(report), ['.etc.motd'])
00110         self.assertEqual(report['.etc.motd'], motd_contents)
00111 
00112         apport.hookutils.attach_file(report, '/etc/issue', '.etc.motd', overwrite=False)
00113         self.assertEqual(sorted(report.keys()), ['.etc.motd', '.etc.motd_'])
00114         self.assertEqual(report['.etc.motd'], motd_contents)
00115         self.assertEqual(report['.etc.motd_'], issue_contents)

Here is the call graph for this function:

attach_file() for binary files

Definition at line 116 of file test_hookutils.py.

00116 
00117     def test_attach_file_binary(self):
00118         '''attach_file() for binary files'''
00119 
00120         myfile = os.path.join(self.workdir, 'data')
00121         with open(myfile, 'wb') as f:
00122             f.write(b'a\xc3\xb6b\xffx')
00123 
00124         report = {}
00125         apport.hookutils.attach_file(report, myfile, key='data')
00126         self.assertEqual(report['data'], b'a\xc3\xb6b\xffx')
00127 
00128         apport.hookutils.attach_file(report, myfile, key='data', force_unicode=True)
00129         self.assertEqual(report['data'], b'a\xc3\xb6b\xef\xbf\xbdx'.decode('UTF-8'))

Here is the call graph for this function:

attach_file_if_exists()

Definition at line 130 of file test_hookutils.py.

00130 
00131     def test_attach_file_if_exists(self):
00132         '''attach_file_if_exists()'''
00133 
00134         with open('/etc/motd') as f:
00135             motd_contents = f.read().strip()
00136 
00137         # default key name
00138         report = {}
00139         apport.hookutils.attach_file_if_exists(report, '/etc/motd')
00140         self.assertEqual(list(report), ['.etc.motd'])
00141         self.assertEqual(report['.etc.motd'], motd_contents)
00142 
00143         # custom key name
00144         report = {}
00145         apport.hookutils.attach_file_if_exists(report, '/etc/motd', 'Motd')
00146         self.assertEqual(list(report), ['Motd'])
00147         self.assertEqual(report['Motd'], motd_contents)
00148 
00149         # nonexisting file
00150         report = {}
00151         apport.hookutils.attach_file_if_exists(report, '/nonexisting')
00152         self.assertEqual(list(report), [])

Here is the call graph for this function:

Definition at line 286 of file test_hookutils.py.

00286 
00287     def test_command_output(self):
00288         orig_lcm = os.environ.get('LC_MESSAGES')
00289         os.environ['LC_MESSAGES'] = 'en_US.UTF-8'
00290         try:
00291             # default mode: disable translations
00292             out = apport.hookutils.command_output(['env'])
00293             self.assertTrue('LC_MESSAGES=C' in out)
00294 
00295             # keep locale
00296             out = apport.hookutils.command_output(['env'], keep_locale=True)
00297             self.assertFalse('LC_MESSAGES=C' in out, out)
00298         finally:
00299             if orig_lcm is not None:
00300                 os.environ['LC_MESSAGES'] = orig_lcm
00301             else:
00302                 os.unsetenv('LC_MESSAGES')
00303 
00304         # nonexisting binary
00305         out = apport.hookutils.command_output(['/non existing'])
00306         self.assertTrue(out.startswith('Error: [Errno 2]'))
00307 
00308         # stdin
00309         out = apport.hookutils.command_output(['cat'], input=b'hello')
00310         self.assertEqual(out, 'hello')

Here is the call graph for this function:

attach_dmesg() does not overwrite already existing data

Definition at line 63 of file test_hookutils.py.

00063 
00064     def test_dmesg_overwrite(self):
00065         '''attach_dmesg() does not overwrite already existing data'''
00066 
00067         report = {'BootDmesg': 'existingboot'}
00068 
00069         apport.hookutils.attach_dmesg(report)
00070         self.assertEqual(report['BootDmesg'][:50], 'existingboot')
00071         self.assertTrue(report['CurrentDmesg'].startswith('['))
00072 
00073         report = {'BootDmesg': 'existingboot', 'CurrentDmesg': 'existingcurrent'}
00074 
00075         apport.hookutils.attach_dmesg(report)
00076         self.assertEqual(report['BootDmesg'], 'existingboot')
00077         self.assertEqual(report['CurrentDmesg'], 'existingcurrent')

Here is the call graph for this function:

in_session_of_problem()

Definition at line 182 of file test_hookutils.py.

00182 
00183     def test_in_session_of_problem(self):
00184         '''in_session_of_problem()'''
00185 
00186         old_ctime = locale.getlocale(locale.LC_TIME)
00187         locale.setlocale(locale.LC_TIME, 'C')
00188 
00189         report = {'Date': 'Sat Jan  1 12:00:00 2011'}
00190         self.assertFalse(apport.hookutils.in_session_of_problem(report))
00191 
00192         report = {'Date': 'Mon Oct 10 21:06:03 2009'}
00193         self.assertFalse(apport.hookutils.in_session_of_problem(report))
00194 
00195         report = {'Date': 'Tue Jan  1 12:00:00 2211'}
00196         self.assertTrue(apport.hookutils.in_session_of_problem(report))
00197 
00198         locale.setlocale(locale.LC_TIME, '')
00199 
00200         report = {'Date': 'Sat Jan  1 12:00:00 2011'}
00201         self.assertFalse(apport.hookutils.in_session_of_problem(report))
00202 
00203         report = {'Date': 'Mon Oct 10 21:06:03 2009'}
00204         self.assertFalse(apport.hookutils.in_session_of_problem(report))
00205 
00206         report = apport.Report()
00207         self.assertTrue(apport.hookutils.in_session_of_problem(report))
00208 
00209         self.assertEqual(apport.hookutils.in_session_of_problem({}), None)
00210 
00211         locale.setlocale(locale.LC_TIME, old_ctime)

Here is the call graph for this function:

module licenses can be validated correctly

Definition at line 14 of file test_hookutils.py.

00014 
00015     def test_module_license_evaluation(self):
00016         '''module licenses can be validated correctly'''
00017 
00018         def _build_ko(license):
00019             asm = tempfile.NamedTemporaryFile(prefix='%s-' % (license),
00020                                               suffix='.S')
00021             asm.write(('.section .modinfo\n.string "license=%s"\n' % (license)).encode())
00022             asm.flush()
00023             ko = tempfile.NamedTemporaryFile(prefix='%s-' % (license),
00024                                              suffix='.ko')
00025             subprocess.call(['/usr/bin/as', asm.name, '-o', ko.name])
00026             return ko
00027 
00028         good_ko = _build_ko('GPL')
00029         bad_ko = _build_ko('BAD')
00030 
00031         # test:
00032         #  - loaded real module
00033         #  - unfindable module
00034         #  - fake GPL module
00035         #  - fake BAD module
00036 
00037         # direct license check
00038         self.assertTrue('GPL' in apport.hookutils._get_module_license('isofs'))
00039         self.assertEqual(apport.hookutils._get_module_license('does-not-exist'), 'invalid')
00040         self.assertTrue('GPL' in apport.hookutils._get_module_license(good_ko.name))
00041         self.assertTrue('BAD' in apport.hookutils._get_module_license(bad_ko.name))
00042 
00043         # check via nonfree_kernel_modules logic
00044         f = tempfile.NamedTemporaryFile()
00045         f.write(('isofs\ndoes-not-exist\n%s\n%s\n' %
00046                 (good_ko.name, bad_ko.name)).encode())
00047         f.flush()
00048         nonfree = apport.hookutils.nonfree_kernel_modules(f.name)
00049         self.assertFalse('isofs' in nonfree)
00050         self.assertTrue('does-not-exist' in nonfree)
00051         self.assertFalse(good_ko.name in nonfree)
00052         self.assertTrue(bad_ko.name in nonfree)

Here is the call graph for this function:

functions do not crash (very shallow)

Definition at line 270 of file test_hookutils.py.

00270 
00271     def test_no_crashes(self):
00272         '''functions do not crash (very shallow)'''
00273 
00274         report = {}
00275         apport.hookutils.attach_hardware(report)
00276         apport.hookutils.attach_alsa(report)
00277         apport.hookutils.attach_network(report)
00278         apport.hookutils.attach_wifi(report)
00279         apport.hookutils.attach_printing(report)
00280         apport.hookutils.attach_conffiles(report, 'bash')
00281         apport.hookutils.attach_conffiles(report, 'apport')
00282         apport.hookutils.attach_conffiles(report, 'nonexisting')
00283         apport.hookutils.attach_upstart_overrides(report, 'apport')
00284         apport.hookutils.attach_upstart_overrides(report, 'nonexisting')
00285         apport.hookutils.attach_default_grub(report)

Here is the call graph for this function:

recent_logfile

Definition at line 153 of file test_hookutils.py.

00153 
00154     def test_recent_logfile(self):
00155         '''recent_logfile'''
00156 
00157         self.assertEqual(apport.hookutils.recent_logfile('/nonexisting', re.compile('.')), '')
00158         self.assertEqual(apport.hookutils.recent_syslog(re.compile('ThisCantPossiblyHitAnything')), '')
00159         self.assertNotEqual(len(apport.hookutils.recent_syslog(re.compile('.'))), 0)

Here is the call graph for this function:

recent_logfile on a huge file

Definition at line 160 of file test_hookutils.py.

00160 
00161     def test_recent_logfile_overflow(self):
00162         '''recent_logfile on a huge file'''
00163 
00164         log = os.path.join(self.workdir, 'syslog')
00165         with open(log, 'w') as f:
00166             lines = 1000000
00167             while lines >= 0:
00168                 f.write('Apr 20 11:30:00 komputer kernel: bogus message\n')
00169                 lines -= 1
00170 
00171         mem_before = self._get_mem_usage()
00172         data = apport.hookutils.recent_logfile(log, re.compile('kernel'))
00173         mem_after = self._get_mem_usage()
00174         delta_kb = mem_after - mem_before
00175         sys.stderr.write('[Δ %i kB] ' % delta_kb)
00176         self.assertLess(delta_kb, 5000)
00177 
00178         self.assertTrue(data.startswith('Apr 20 11:30:00 komputer kernel: bogus message\n'))
00179         self.assertGreater(len(data), 100000)
00180         self.assertLess(len(data), 1000000)

Here is the call graph for this function:

xsession_errors()

Definition at line 212 of file test_hookutils.py.

00212 
00213     def test_xsession_errors(self):
00214         '''xsession_errors()'''
00215 
00216         with open(os.path.join(self.workdir, '.xsession-errors'), 'w', encoding='UTF-8') as f:
00217             f.write('''Loading profile from /etc/profile
00218 gnome-session[1948]: WARNING: standard glib warning
00219 EggSMClient-CRITICAL **: egg_sm_client_set_mode: standard glib assertion
00220 24/02/2012 11:14:46 Sending credentials s3kr1t
00221 
00222 ** WARNING **: nonstandard warning
00223 
00224 WARN  2012-02-24 11:23:47 unity <unknown>:0 some unicode ♥ ♪
00225 
00226 GNOME_KEYRING_CONTROL=/tmp/keyring-u7hrD6
00227 
00228 (gnome-settings-daemon:5115): Gdk-WARNING **: The program 'gnome-settings-daemon' received an X Window System error.
00229 This probably reflects a bug in the program.
00230 The error was 'BadMatch (invalid parameter attributes)'.
00231   (Details: serial 723 error_code 8 request_code 143 minor_code 22)
00232   (Note to programmers: normally, X errors are reported asynchronously;
00233    that is, you will receive the error a while after causing it.
00234    To debug your program, run it with the --sync command line
00235    option to change this behavior. You can then get a meaningful
00236    backtrace from your debugger if you break on the gdk_x_error() function.)"
00237 
00238 GdkPixbuf-CRITICAL **: gdk_pixbuf_scale_simple: another standard glib assertion
00239 ''')
00240         orig_home = os.environ.get('HOME')
00241         try:
00242             os.environ['HOME'] = self.workdir
00243 
00244             # explicit pattern
00245             pattern = re.compile('notfound')
00246             self.assertEqual(apport.hookutils.xsession_errors(pattern), '')
00247 
00248             pattern = re.compile('^\w+-CRITICAL')
00249             res = apport.hookutils.xsession_errors(pattern).splitlines()
00250             self.assertEqual(len(res), 2)
00251             self.assertTrue(res[0].startswith('EggSMClient-CRITICAL'))
00252             self.assertTrue(res[1].startswith('GdkPixbuf-CRITICAL'))
00253 
00254             # default pattern includes glib assertions and X Errors
00255             res = apport.hookutils.xsession_errors()
00256             self.assertFalse('nonstandard warning' in res)
00257             self.assertFalse('keyring' in res)
00258             self.assertFalse('credentials' in res)
00259             self.assertTrue('WARNING: standard glib warning' in res, res)
00260             self.assertTrue('GdkPixbuf-CRITICAL' in res, res)
00261             self.assertTrue("'gnome-settings-daemon' received an X Window" in res, res)
00262             self.assertTrue('BadMatch' in res, res)
00263             self.assertTrue('serial 723' in res, res)
00264 
00265         finally:
00266             if orig_home is not None:
00267                 os.environ['HOME'] = orig_home
00268             else:
00269                 os.unsetenv('HOME')

Here is the call graph for this function:


Member Data Documentation

Definition at line 9 of file test_hookutils.py.


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