Back to index

apport  2.4
Public Member Functions
test_problem_report.T Class Reference

List of all members.

Public Member Functions

def test_basic_operations
def test_ctor_arguments
def test_sanity_checks
def test_compressed_values
def test_write
def test_write_append
def test_load
def test_write_file
def test_write_fileobj
def test_write_empty_fileobj
def test_write_delayed_fileobj
def test_read_file
def test_read_file_legacy
def test_big_file
def test_size_limit
def test_iter
def test_modify
def test_add_to_existing
def test_write_mime_text
def test_write_mime_binary
def test_write_mime_extra_headers
def test_write_mime_filter
def test_write_mime_order
def test_updating
def test_import_dict

Detailed Description

Definition at line 14 of file test_problem_report.py.


Member Function Documentation

adding information to an existing report.

Definition at line 567 of file test_problem_report.py.

00567 
00568     def test_add_to_existing(self):
00569         '''adding information to an existing report.'''
00570 
00571         # original report
00572         pr = problem_report.ProblemReport()
00573         pr['old1'] = '11'
00574         pr['old2'] = '22'
00575 
00576         (fd, rep) = tempfile.mkstemp()
00577         os.close(fd)
00578         with open(rep, 'wb') as f:
00579             pr.write(f)
00580 
00581         origstat = os.stat(rep)
00582 
00583         # create a new one and add it
00584         pr = problem_report.ProblemReport()
00585         pr.clear()
00586         pr['new1'] = '33'
00587 
00588         pr.add_to_existing(rep, keep_times=True)
00589 
00590         # check keep_times
00591         newstat = os.stat(rep)
00592         self.assertEqual(origstat.st_mode, newstat.st_mode)
00593         self.assertAlmostEqual(origstat.st_atime, newstat.st_atime, 1)
00594         self.assertAlmostEqual(origstat.st_mtime, newstat.st_mtime, 1)
00595 
00596         # check report contents
00597         newpr = problem_report.ProblemReport()
00598         with open(rep, 'rb') as f:
00599             newpr.load(f)
00600         self.assertEqual(newpr['old1'], '11')
00601         self.assertEqual(newpr['old2'], '22')
00602         self.assertEqual(newpr['new1'], '33')
00603 
00604         # create a another new one and add it, but make sure mtime must be
00605         # different
00606         time.sleep(1)
00607         with open(rep) as f:
00608             f.read()  # bump atime
00609         time.sleep(1)
00610 
00611         pr = problem_report.ProblemReport()
00612         pr.clear()
00613         pr['new2'] = '44'
00614 
00615         pr.add_to_existing(rep)
00616 
00617         # check that timestamps have been updates
00618         newstat = os.stat(rep)
00619         self.assertEqual(origstat.st_mode, newstat.st_mode)
00620         self.assertNotEqual(origstat.st_mtime, newstat.st_mtime)
00621         # skip atime check if filesystem is mounted noatime
00622         skip_atime = False
00623         dir = rep
00624         while len(dir) > 1:
00625             dir, filename = os.path.split(dir)
00626             if os.path.ismount(dir):
00627                 with open('/proc/mounts') as f:
00628                     for line in f:
00629                         mount, fs, options = line.split(' ')[1:4]
00630                         if mount == dir and 'noatime' in options.split(','):
00631                             skip_atime = True
00632                             break
00633                 break
00634         if not skip_atime:
00635             self.assertNotEqual(origstat.st_atime, newstat.st_atime)
00636 
00637         # check report contents
00638         newpr = problem_report.ProblemReport()
00639         with open(rep, 'rb') as f:
00640             newpr.load(f)
00641         self.assertEqual(newpr['old1'], '11')
00642         self.assertEqual(newpr['old2'], '22')
00643         self.assertEqual(newpr['new1'], '33')
00644         self.assertEqual(newpr['new2'], '44')
00645 
00646         os.unlink(rep)

basic creation and operation.

Definition at line 15 of file test_problem_report.py.

00015 
00016     def test_basic_operations(self):
00017         '''basic creation and operation.'''
00018 
00019         pr = problem_report.ProblemReport()
00020         pr['foo'] = 'bar'
00021         pr['bar'] = ' foo   bar\nbaz\n   blip  '
00022         pr['dash-key'] = '1'
00023         pr['dot.key'] = '1'
00024         pr['underscore_key'] = '1'
00025         self.assertEqual(pr['foo'], 'bar')
00026         self.assertEqual(pr['bar'], ' foo   bar\nbaz\n   blip  ')
00027         self.assertEqual(pr['ProblemType'], 'Crash')
00028         self.assertTrue(time.strptime(pr['Date']))
00029         self.assertEqual(pr['dash-key'], '1')
00030         self.assertEqual(pr['dot.key'], '1')
00031         self.assertEqual(pr['underscore_key'], '1')

writing and re-decoding a big random file.

Definition at line 439 of file test_problem_report.py.

00439 
00440     def test_big_file(self):
00441         '''writing and re-decoding a big random file.'''
00442 
00443         # create 1 MB random file
00444         temp = tempfile.NamedTemporaryFile()
00445         data = os.urandom(1048576)
00446         temp.write(data)
00447         temp.flush()
00448 
00449         # write it into problem report
00450         pr = problem_report.ProblemReport()
00451         pr['File'] = (temp.name,)
00452         pr['Before'] = 'xtestx'
00453         pr['ZAfter'] = 'ytesty'
00454         io = BytesIO()
00455         pr.write(io)
00456         temp.close()
00457 
00458         # read it again
00459         io.seek(0)
00460         pr = problem_report.ProblemReport()
00461         pr.load(io)
00462 
00463         self.assertTrue(pr['File'] == data)
00464         self.assertEqual(pr['Before'], 'xtestx')
00465         self.assertEqual(pr['ZAfter'], 'ytesty')
00466 
00467         # write it again
00468         io2 = BytesIO()
00469         pr.write(io2)
00470         self.assertTrue(io.getvalue() == io2.getvalue())
00471 
00472         # check gzip compatibility
00473         io.seek(0)
00474         pr = problem_report.ProblemReport()
00475         pr.load(io, binary='compressed')
00476         self.assertEqual(pr['File'].get_value(), data)

handling of CompressedValue values.

Definition at line 51 of file test_problem_report.py.

00051 
00052     def test_compressed_values(self):
00053         '''handling of CompressedValue values.'''
00054 
00055         large_val = b'A' * 5000000
00056 
00057         pr = problem_report.ProblemReport()
00058         pr['Foo'] = problem_report.CompressedValue(b'FooFoo!')
00059         pr['Bin'] = problem_report.CompressedValue()
00060         pr['Bin'].set_value(bin_data)
00061         pr['Large'] = problem_report.CompressedValue(large_val)
00062 
00063         self.assertTrue(isinstance(pr['Foo'], problem_report.CompressedValue))
00064         self.assertTrue(isinstance(pr['Bin'], problem_report.CompressedValue))
00065         self.assertEqual(pr['Foo'].get_value(), b'FooFoo!')
00066         self.assertEqual(pr['Bin'].get_value(), bin_data)
00067         self.assertEqual(pr['Large'].get_value(), large_val)
00068         self.assertEqual(len(pr['Foo']), 7)
00069         self.assertEqual(len(pr['Bin']), len(bin_data))
00070         self.assertEqual(len(pr['Large']), len(large_val))
00071 
00072         io = BytesIO()
00073         pr['Bin'].write(io)
00074         self.assertEqual(io.getvalue(), bin_data)
00075         io = BytesIO()
00076         pr['Large'].write(io)
00077         self.assertEqual(io.getvalue(), large_val)
00078 
00079         pr['Multiline'] = problem_report.CompressedValue(b'\1\1\1\n\2\2\n\3\3\3')
00080         self.assertEqual(pr['Multiline'].splitlines(),
00081                          [b'\1\1\1', b'\2\2', b'\3\3\3'])
00082 
00083         # test writing of reports with CompressedValues
00084         io = BytesIO()
00085         pr.write(io)
00086         io.seek(0)
00087         pr = problem_report.ProblemReport()
00088         pr.load(io)
00089         self.assertEqual(pr['Foo'], 'FooFoo!')
00090         self.assertEqual(pr['Bin'], bin_data)
00091         self.assertEqual(pr['Large'], large_val.decode('ASCII'))

non-default constructor arguments.

Definition at line 32 of file test_problem_report.py.

00032 
00033     def test_ctor_arguments(self):
00034         '''non-default constructor arguments.'''
00035 
00036         pr = problem_report.ProblemReport('KernelCrash')
00037         self.assertEqual(pr['ProblemType'], 'KernelCrash')
00038         pr = problem_report.ProblemReport(date='19801224 12:34')
00039         self.assertEqual(pr['Date'], '19801224 12:34')

importing a dictionary with update().

Definition at line 964 of file test_problem_report.py.

00964 
00965     def test_import_dict(self):
00966         '''importing a dictionary with update().'''
00967 
00968         pr = problem_report.ProblemReport()
00969         pr['oldtext'] = 'Hello world'
00970         pr['oldbin'] = bin_data
00971         pr['overwrite'] = 'I am crap'
00972 
00973         d = {}
00974         d['newtext'] = 'Goodbye world'
00975         d['newbin'] = '11\000\001\002\xFFZZ'
00976         d['overwrite'] = 'I am good'
00977 
00978         pr.update(d)
00979         self.assertEqual(pr['oldtext'], 'Hello world')
00980         self.assertEqual(pr['oldbin'], bin_data)
00981         self.assertEqual(pr['newtext'], 'Goodbye world')
00982         self.assertEqual(pr['newbin'], '11\000\001\002\xFFZZ')
00983         self.assertEqual(pr['overwrite'], 'I am good')

problem_report.ProblemReport iteration.

Definition at line 512 of file test_problem_report.py.

00512 
00513     def test_iter(self):
00514         '''problem_report.ProblemReport iteration.'''
00515 
00516         pr = problem_report.ProblemReport()
00517         pr['foo'] = 'bar'
00518 
00519         keys = []
00520         for k in pr:
00521             keys.append(k)
00522         keys.sort()
00523         self.assertEqual(' '.join(keys), 'Date ProblemType foo')
00524 
00525         self.assertEqual(len([k for k in pr if k != 'foo']), 2)

load() with various formatting.

Definition at line 178 of file test_problem_report.py.

00178 
00179     def test_load(self):
00180         '''load() with various formatting.'''
00181 
00182         pr = problem_report.ProblemReport()
00183         pr.load(BytesIO(b'''ProblemType: Crash
00184 Date: now!
00185 Simple: bar
00186 WhiteSpace:
00187   foo   bar
00188  baz
00189    blip  
00190 '''))
00191         self.assertEqual(pr['ProblemType'], 'Crash')
00192         self.assertEqual(pr['Date'], 'now!')
00193         self.assertEqual(pr['Simple'], 'bar')
00194         self.assertEqual(pr['WhiteSpace'], ' foo   bar\nbaz\n  blip  ')
00195 
00196         # test last field a bit more
00197         pr.load(BytesIO(b'''ProblemType: Crash
00198 Date: now!
00199 Simple: bar
00200 WhiteSpace:
00201   foo   bar
00202  baz
00203    blip  
00204  
00205 '''))
00206         self.assertEqual(pr['ProblemType'], 'Crash')
00207         self.assertEqual(pr['Date'], 'now!')
00208         self.assertEqual(pr['Simple'], 'bar')
00209         self.assertEqual(pr['WhiteSpace'], ' foo   bar\nbaz\n  blip  \n')
00210 
00211         # last field might not be \n terminated
00212         pr.load(BytesIO(b'''ProblemType: Crash
00213 Date: now!
00214 Simple: bar
00215 WhiteSpace:
00216  foo
00217  bar'''))
00218         self.assertEqual(pr['ProblemType'], 'Crash')
00219         self.assertEqual(pr['Date'], 'now!')
00220         self.assertEqual(pr['Simple'], 'bar')
00221         self.assertEqual(pr['WhiteSpace'], 'foo\nbar')
00222 
00223         pr = problem_report.ProblemReport()
00224         pr.load(BytesIO(b'''ProblemType: Crash
00225 WhiteSpace:
00226   foo   bar
00227  baz
00228  
00229    blip  
00230 Last: foo
00231 '''))
00232         self.assertEqual(pr['WhiteSpace'], ' foo   bar\nbaz\n\n  blip  ')
00233         self.assertEqual(pr['Last'], 'foo')
00234 
00235         pr.load(BytesIO(b'''ProblemType: Crash
00236 WhiteSpace:
00237   foo   bar
00238  baz
00239    blip  
00240 Last: foo
00241  
00242 '''))
00243         self.assertEqual(pr['WhiteSpace'], ' foo   bar\nbaz\n  blip  ')
00244         self.assertEqual(pr['Last'], 'foo\n')
00245 
00246         # empty lines in values must have a leading space in coding
00247         invalid_spacing = BytesIO(b'''WhiteSpace:
00248  first
00249 
00250  second
00251 ''')
00252         pr = problem_report.ProblemReport()
00253         self.assertRaises(ValueError, pr.load, invalid_spacing)
00254 
00255         # test that load() cleans up properly
00256         pr.load(BytesIO(b'ProblemType: Crash'))
00257         self.assertEqual(list(pr.keys()), ['ProblemType'])

reading, modifying fields, and writing back.

Definition at line 526 of file test_problem_report.py.

00526 
00527     def test_modify(self):
00528         '''reading, modifying fields, and writing back.'''
00529 
00530         report = b'''ProblemType: Crash
00531 Date: now!
00532 Long:
00533  xxx
00534  .
00535  yyy
00536 Short: Bar
00537 File: base64
00538  H4sICAAAAAAC/0ZpbGUA
00539  c3RyxIAMcBAFAK/2p9MfAAAA
00540 '''
00541 
00542         pr = problem_report.ProblemReport()
00543         pr.load(BytesIO(report))
00544 
00545         self.assertEqual(pr['Long'], 'xxx\n.\nyyy')
00546 
00547         # write back unmodified
00548         io = BytesIO()
00549         pr.write(io)
00550         self.assertEqual(io.getvalue(), report)
00551 
00552         pr['Short'] = 'aaa\nbbb'
00553         pr['Long'] = '123'
00554         io = BytesIO()
00555         pr.write(io)
00556         self.assertEqual(io.getvalue(),
00557                          b'''ProblemType: Crash
00558 Date: now!
00559 Long: 123
00560 Short:
00561  aaa
00562  bbb
00563 File: base64
00564  H4sICAAAAAAC/0ZpbGUA
00565  c3RyxIAMcBAFAK/2p9MfAAAA
00566 ''')

reading a report with binary data.

Definition at line 376 of file test_problem_report.py.

00376 
00377     def test_read_file(self):
00378         '''reading a report with binary data.'''
00379 
00380         bin_report = b'''ProblemType: Crash
00381 Date: now!
00382 File: base64
00383  H4sICAAAAAAC/0ZpbGUA
00384  c3RyhEIGBoYoRiYAM5XUCxAAAAA=
00385 Foo: Bar
00386 '''
00387 
00388         # test with reading everything
00389         pr = problem_report.ProblemReport()
00390         pr.load(BytesIO(bin_report))
00391         self.assertEqual(pr['File'], bin_data)
00392         self.assertEqual(pr.has_removed_fields(), False)
00393 
00394         # test with skipping binary data
00395         pr.load(BytesIO(bin_report), binary=False)
00396         self.assertEqual(pr['File'], '')
00397         self.assertEqual(pr.has_removed_fields(), True)
00398 
00399         # test with keeping compressed binary data
00400         pr.load(BytesIO(bin_report), binary='compressed')
00401         self.assertEqual(pr['Foo'], 'Bar')
00402         self.assertEqual(pr.has_removed_fields(), False)
00403         self.assertTrue(isinstance(pr['File'], problem_report.CompressedValue))
00404         self.assertEqual(len(pr['File']), len(bin_data))
00405         self.assertEqual(pr['File'].get_value(), bin_data)

reading a report with binary data in legacy format without gzip
header.

Definition at line 406 of file test_problem_report.py.

00406 
00407     def test_read_file_legacy(self):
00408         '''reading a report with binary data in legacy format without gzip
00409         header.'''
00410 
00411         bin_report = b'''ProblemType: Crash
00412 Date: now!
00413 File: base64
00414  eJw=
00415  c3RyxIAMcBAFAG55BXk=
00416 Foo: Bar
00417 '''
00418 
00419         # test with reading everything
00420         pr = problem_report.ProblemReport()
00421         pr.load(BytesIO(bin_report))
00422         self.assertEqual(pr['File'], b'AB' * 10 + b'\0' * 10 + b'Z')
00423         self.assertEqual(pr.has_removed_fields(), False)
00424 
00425         # test with skipping binary data
00426         pr.load(BytesIO(bin_report), binary=False)
00427         self.assertEqual(pr['File'], '')
00428         self.assertEqual(pr.has_removed_fields(), True)
00429 
00430         # test with keeping CompressedValues
00431         pr.load(BytesIO(bin_report), binary='compressed')
00432         self.assertEqual(pr.has_removed_fields(), False)
00433         self.assertEqual(len(pr['File']), 31)
00434         self.assertEqual(pr['File'].get_value(), b'AB' * 10 + b'\0' * 10 + b'Z')
00435         io = BytesIO()
00436         pr['File'].write(io)
00437         io.seek(0)
00438         self.assertEqual(io.read(), b'AB' * 10 + b'\0' * 10 + b'Z')

various error conditions.

Definition at line 40 of file test_problem_report.py.

00040 
00041     def test_sanity_checks(self):
00042         '''various error conditions.'''
00043 
00044         pr = problem_report.ProblemReport()
00045         self.assertRaises(AssertionError, pr.__setitem__, 'a b', '1')
00046         self.assertRaises(AssertionError, pr.__setitem__, 'a', 1)
00047         self.assertRaises(AssertionError, pr.__setitem__, 'a', 1)
00048         self.assertRaises(AssertionError, pr.__setitem__, 'a', (1,))
00049         self.assertRaises(AssertionError, pr.__setitem__, 'a', ('/tmp/nonexistant', ''))
00050         self.assertRaises(KeyError, pr.__getitem__, 'Nonexistant')

writing and a big random file with a size limit key.

Definition at line 477 of file test_problem_report.py.

00477 
00478     def test_size_limit(self):
00479         '''writing and a big random file with a size limit key.'''
00480 
00481         # create 1 MB random file
00482         temp = tempfile.NamedTemporaryFile()
00483         data = os.urandom(1048576)
00484         temp.write(data)
00485         temp.flush()
00486 
00487         # write it into problem report
00488         pr = problem_report.ProblemReport()
00489         pr['FileSmallLimit'] = (temp.name, True, 100)
00490         pr['FileLimitMinus1'] = (temp.name, True, 1048575)
00491         pr['FileExactLimit'] = (temp.name, True, 1048576)
00492         pr['FileLimitPlus1'] = (temp.name, True, 1048577)
00493         pr['FileLimitNone'] = (temp.name, True, None)
00494         pr['Before'] = 'xtestx'
00495         pr['ZAfter'] = 'ytesty'
00496         io = BytesIO()
00497         pr.write(io)
00498         temp.close()
00499 
00500         # read it again
00501         io.seek(0)
00502         pr = problem_report.ProblemReport()
00503         pr.load(io)
00504 
00505         self.assertFalse('FileSmallLimit' in pr)
00506         self.assertFalse('FileLimitMinus1' in pr)
00507         self.assertTrue(pr['FileExactLimit'] == data)
00508         self.assertTrue(pr['FileLimitPlus1'] == data)
00509         self.assertTrue(pr['FileLimitNone'] == data)
00510         self.assertEqual(pr['Before'], 'xtestx')
00511         self.assertEqual(pr['ZAfter'], 'ytesty')

new_keys() and write() with only_new=True.

Definition at line 943 of file test_problem_report.py.

00943 
00944     def test_updating(self):
00945         '''new_keys() and write() with only_new=True.'''
00946 
00947         pr = problem_report.ProblemReport()
00948         self.assertEqual(pr.new_keys(), set(['ProblemType', 'Date']))
00949         pr.load(BytesIO(b'''ProblemType: Crash
00950 Date: now!
00951 Foo: bar
00952 Baz: blob
00953 '''))
00954 
00955         self.assertEqual(pr.new_keys(), set())
00956 
00957         pr['Foo'] = 'changed'
00958         pr['NewKey'] = 'new new'
00959         self.assertEqual(pr.new_keys(), set(['NewKey']))
00960 
00961         out = BytesIO()
00962         pr.write(out, only_new=True)
00963         self.assertEqual(out.getvalue(), b'NewKey: new new\n')

write() and proper formatting.

Definition at line 92 of file test_problem_report.py.

00092 
00093     def test_write(self):
00094         '''write() and proper formatting.'''
00095 
00096         pr = problem_report.ProblemReport(date='now!')
00097         pr['Simple'] = 'bar'
00098         if sys.version.startswith('2'):
00099             pr['SimpleUTF8'] = '1äö2Φ3'
00100             pr['SimpleUnicode'] = '1äö2Φ3'.decode('UTF-8')
00101             pr['TwoLineUnicode'] = 'pi-π\nnu-η'.decode('UTF-8')
00102             pr['TwoLineUTF8'] = 'pi-π\nnu-η'
00103         else:
00104             pr['SimpleUTF8'] = '1äö2Φ3'.encode('UTF-8')
00105             pr['SimpleUnicode'] = '1äö2Φ3'
00106             pr['TwoLineUnicode'] = 'pi-π\nnu-η'
00107             pr['TwoLineUTF8'] = 'pi-π\nnu-η'.encode('UTF-8')
00108         pr['WhiteSpace'] = ' foo   bar\nbaz\n  blip  \n\nafteremptyline'
00109         io = BytesIO()
00110         pr.write(io)
00111         expected = '''ProblemType: Crash
00112 Date: now!
00113 Simple: bar
00114 SimpleUTF8: 1äö2Φ3
00115 SimpleUnicode: 1äö2Φ3
00116 TwoLineUTF8:
00117  pi-π
00118  nu-η
00119 TwoLineUnicode:
00120  pi-π
00121  nu-η
00122 WhiteSpace:
00123   foo   bar
00124  baz
00125    blip  
00126  
00127  afteremptyline
00128 '''
00129         if sys.version > '3':
00130             expected = expected.encode('UTF-8')
00131         self.assertEqual(io.getvalue(), expected)

write() with appending to an existing file.

Definition at line 132 of file test_problem_report.py.

00132 
00133     def test_write_append(self):
00134         '''write() with appending to an existing file.'''
00135 
00136         pr = problem_report.ProblemReport(date='now!')
00137         pr['Simple'] = 'bar'
00138         pr['WhiteSpace'] = ' foo   bar\nbaz\n  blip  '
00139         io = BytesIO()
00140         pr.write(io)
00141 
00142         pr.clear()
00143         pr['Extra'] = 'appended'
00144         pr.write(io)
00145 
00146         self.assertEqual(io.getvalue(),
00147                          b'''ProblemType: Crash
00148 Date: now!
00149 Simple: bar
00150 WhiteSpace:
00151   foo   bar
00152  baz
00153    blip  
00154 Extra: appended
00155 ''')
00156 
00157         temp = tempfile.NamedTemporaryFile()
00158         temp.write(bin_data)
00159         temp.flush()
00160 
00161         pr = problem_report.ProblemReport(date='now!')
00162         pr['File'] = (temp.name,)
00163         io = BytesIO()
00164         pr.write(io)
00165         temp.close()
00166 
00167         pr.clear()
00168         pr['Extra'] = 'appended'
00169         pr.write(io)
00170 
00171         io.seek(0)
00172         pr = problem_report.ProblemReport()
00173         pr.load(io)
00174 
00175         self.assertEqual(pr['Date'], 'now!')
00176         self.assertEqual(pr['File'], bin_data)
00177         self.assertEqual(pr['Extra'], 'appended')

writing a report with file pointers and delayed data.

Definition at line 345 of file test_problem_report.py.

00345 
00346     def test_write_delayed_fileobj(self):
00347         '''writing a report with file pointers and delayed data.'''
00348 
00349         (fout, fin) = os.pipe()
00350 
00351         if os.fork() == 0:
00352             os.close(fout)
00353             time.sleep(0.3)
00354             os.write(fin, b'ab' * 512 * 1024)
00355             time.sleep(0.3)
00356             os.write(fin, b'hello')
00357             time.sleep(0.3)
00358             os.write(fin, b' world')
00359             os.close(fin)
00360             os._exit(0)
00361 
00362         os.close(fin)
00363 
00364         pr = problem_report.ProblemReport(date='now!')
00365         io = BytesIO()
00366         with os.fdopen(fout, 'rb') as f:
00367             pr['BinFile'] = (f,)
00368             pr.write(io)
00369         assert os.wait()[1] == 0
00370 
00371         io.seek(0)
00372 
00373         pr2 = problem_report.ProblemReport()
00374         pr2.load(io)
00375         self.assertEqual(pr2['BinFile'], 'ab' * 512 * 1024 + 'hello world')

writing a report with a pointer to a file-like object with enforcing non-emptyness.

Definition at line 329 of file test_problem_report.py.

00329 
00330     def test_write_empty_fileobj(self):
00331         '''writing a report with a pointer to a file-like object with enforcing non-emptyness.'''
00332 
00333         tempbin = BytesIO(b'')
00334         tempasc = BytesIO(b'')
00335 
00336         pr = problem_report.ProblemReport(date='now!')
00337         pr['BinFile'] = (tempbin, True, None, True)
00338         io = BytesIO()
00339         self.assertRaises(IOError, pr.write, io)
00340 
00341         pr = problem_report.ProblemReport(date='now!')
00342         pr['AscFile'] = (tempasc, False, None, True)
00343         io = BytesIO()
00344         self.assertRaises(IOError, pr.write, io)

writing a report with binary file data.

Definition at line 258 of file test_problem_report.py.

00258 
00259     def test_write_file(self):
00260         '''writing a report with binary file data.'''
00261 
00262         temp = tempfile.NamedTemporaryFile()
00263         temp.write(bin_data)
00264         temp.flush()
00265 
00266         pr = problem_report.ProblemReport(date='now!')
00267         pr['File'] = (temp.name,)
00268         pr['Afile'] = (temp.name,)
00269         io = BytesIO()
00270         pr.write(io)
00271         temp.close()
00272 
00273         self.assertEqual(io.getvalue(),
00274                          b'''ProblemType: Crash
00275 Date: now!
00276 Afile: base64
00277  H4sICAAAAAAC/0FmaWxlAA==
00278  c3RyhEIGBoYoRiYAM5XUCxAAAAA=
00279 File: base64
00280  H4sICAAAAAAC/0ZpbGUA
00281  c3RyhEIGBoYoRiYAM5XUCxAAAAA=
00282 ''')
00283 
00284         # force compression/encoding bool
00285         temp = tempfile.NamedTemporaryFile()
00286         temp.write(b'foo\0bar')
00287         temp.flush()
00288         pr = problem_report.ProblemReport(date='now!')
00289         pr['File'] = (temp.name, False)
00290         io = BytesIO()
00291         pr.write(io)
00292 
00293         self.assertEqual(io.getvalue(),
00294                          b'''ProblemType: Crash
00295 Date: now!
00296 File: foo\0bar
00297 ''')
00298 
00299         pr['File'] = (temp.name, True)
00300         io = BytesIO()
00301         pr.write(io)
00302 
00303         self.assertEqual(io.getvalue(),
00304                          b'''ProblemType: Crash
00305 Date: now!
00306 File: base64
00307  H4sICAAAAAAC/0ZpbGUA
00308  S8vPZ0hKLAIACq50HgcAAAA=
00309 ''')
00310         temp.close()

writing a report with a pointer to a file-like object.

Definition at line 311 of file test_problem_report.py.

00311 
00312     def test_write_fileobj(self):
00313         '''writing a report with a pointer to a file-like object.'''
00314 
00315         tempbin = BytesIO(bin_data)
00316         tempasc = BytesIO(b'Hello World')
00317 
00318         pr = problem_report.ProblemReport(date='now!')
00319         pr['BinFile'] = (tempbin,)
00320         pr['AscFile'] = (tempasc, False)
00321         io = BytesIO()
00322         pr.write(io)
00323         io.seek(0)
00324 
00325         pr = problem_report.ProblemReport()
00326         pr.load(io)
00327         self.assertEqual(pr['BinFile'], tempbin.getvalue())
00328         self.assertEqual(pr['AscFile'], tempasc.getvalue().decode())

write_mime() for binary values and file references.

Definition at line 751 of file test_problem_report.py.

00751 
00752     def test_write_mime_binary(self):
00753         '''write_mime() for binary values and file references.'''
00754 
00755         temp = tempfile.NamedTemporaryFile()
00756         temp.write(bin_data)
00757         temp.flush()
00758 
00759         tempgz = tempfile.NamedTemporaryFile()
00760         gz = gzip.GzipFile('File1', 'w', fileobj=tempgz)
00761         gz.write(bin_data)
00762         gz.close()
00763         tempgz.flush()
00764 
00765         pr = problem_report.ProblemReport(date='now!')
00766         pr['Context'] = 'Test suite'
00767         pr['File1'] = (temp.name,)
00768         pr['File1.gz'] = (tempgz.name,)
00769         pr['Value1'] = bin_data
00770         with open(tempgz.name, 'rb') as f:
00771             pr['Value1.gz'] = f.read()
00772         pr['ZValue'] = problem_report.CompressedValue(bin_data)
00773         io = BytesIO()
00774         pr.write_mime(io)
00775         io.seek(0)
00776 
00777         msg = email.message_from_binary_file(io)
00778         parts = [p for p in msg.walk()]
00779         self.assertEqual(len(parts), 7)
00780 
00781         # first part is the multipart container
00782         self.assertTrue(parts[0].is_multipart())
00783 
00784         # second part should be an inline text/plain attachments with all short
00785         # fields
00786         self.assertTrue(not parts[1].is_multipart())
00787         self.assertEqual(parts[1].get_content_type(), 'text/plain')
00788         self.assertEqual(parts[1].get_content_charset(), 'utf-8')
00789         self.assertEqual(parts[1].get_filename(), None)
00790         self.assertEqual(parts[1].get_payload(decode=True),
00791                          b'ProblemType: Crash\nContext: Test suite\nDate: now!\n')
00792 
00793         # third part should be the File1: file contents as gzip'ed attachment
00794         self.assertTrue(not parts[2].is_multipart())
00795         self.assertEqual(parts[2].get_content_type(), 'application/x-gzip')
00796         self.assertEqual(parts[2].get_filename(), 'File1.gz')
00797         f = tempfile.TemporaryFile()
00798         f.write(parts[2].get_payload(decode=True))
00799         f.seek(0)
00800         self.assertEqual(gzip.GzipFile(mode='rb', fileobj=f).read(), bin_data)
00801         f.close()
00802 
00803         # fourth part should be the File1.gz: file contents as gzip'ed
00804         # attachment; write_mime() should not compress it again
00805         self.assertTrue(not parts[3].is_multipart())
00806         self.assertEqual(parts[3].get_content_type(), 'application/x-gzip')
00807         self.assertEqual(parts[3].get_filename(), 'File1.gz')
00808         f = tempfile.TemporaryFile()
00809         f.write(parts[3].get_payload(decode=True))
00810         f.seek(0)
00811         self.assertEqual(gzip.GzipFile(mode='rb', fileobj=f).read(), bin_data)
00812         f.close()
00813 
00814         # fifth part should be the Value1: value as gzip'ed attachment
00815         self.assertTrue(not parts[4].is_multipart())
00816         self.assertEqual(parts[4].get_content_type(), 'application/x-gzip')
00817         self.assertEqual(parts[4].get_filename(), 'Value1.gz')
00818         f = tempfile.TemporaryFile()
00819         f.write(parts[4].get_payload(decode=True))
00820         f.seek(0)
00821         self.assertEqual(gzip.GzipFile(mode='rb', fileobj=f).read(), bin_data)
00822         f.close()
00823 
00824         # sixth part should be the Value1: value as gzip'ed attachment;
00825         # write_mime should not compress it again
00826         self.assertTrue(not parts[5].is_multipart())
00827         self.assertEqual(parts[5].get_content_type(), 'application/x-gzip')
00828         self.assertEqual(parts[5].get_filename(), 'Value1.gz')
00829         f = tempfile.TemporaryFile()
00830         f.write(parts[5].get_payload(decode=True))
00831         f.seek(0)
00832         self.assertEqual(gzip.GzipFile(mode='rb', fileobj=f).read(), bin_data)
00833         f.close()
00834 
00835         # seventh part should be the ZValue: value as gzip'ed attachment;
00836         # write_mime should not compress it again
00837         self.assertTrue(not parts[6].is_multipart())
00838         self.assertEqual(parts[6].get_content_type(), 'application/x-gzip')
00839         self.assertEqual(parts[6].get_filename(), 'ZValue.gz')
00840         f = tempfile.TemporaryFile()
00841         f.write(parts[6].get_payload(decode=True))
00842         f.seek(0)
00843         self.assertEqual(gzip.GzipFile(mode='rb', fileobj=f).read(), bin_data)
00844         f.close()

write_mime() with extra headers.

Definition at line 845 of file test_problem_report.py.

00845 
00846     def test_write_mime_extra_headers(self):
00847         '''write_mime() with extra headers.'''
00848 
00849         pr = problem_report.ProblemReport(date='now!')
00850         pr['Simple'] = 'bar'
00851         pr['TwoLine'] = 'first\nsecond\n'
00852         io = BytesIO()
00853         pr.write_mime(io, extra_headers={'Greeting': 'hello world',
00854                                          'Foo': 'Bar'})
00855         io.seek(0)
00856 
00857         msg = email.message_from_binary_file(io)
00858         self.assertEqual(msg['Greeting'], 'hello world')
00859         self.assertEqual(msg['Foo'], 'Bar')
00860         parts = [p for p in msg.walk()]
00861         self.assertEqual(len(parts), 2)
00862 
00863         # first part is the multipart container
00864         self.assertTrue(parts[0].is_multipart())
00865 
00866         # second part should be an inline text/plain attachments with all short
00867         # fields
00868         self.assertTrue(not parts[1].is_multipart())
00869         self.assertEqual(parts[1].get_content_type(), 'text/plain')
00870         self.assertTrue(b'Simple: bar' in parts[1].get_payload(decode=True))

write_mime() with key filters.

Definition at line 871 of file test_problem_report.py.

00871 
00872     def test_write_mime_filter(self):
00873         '''write_mime() with key filters.'''
00874 
00875         pr = problem_report.ProblemReport(date='now!')
00876         pr['GoodText'] = 'Hi'
00877         pr['BadText'] = 'YouDontSeeMe'
00878         pr['GoodBin'] = bin_data
00879         pr['BadBin'] = 'Y' + '\x05' * 10 + '-'
00880         io = BytesIO()
00881         pr.write_mime(io, skip_keys=['BadText', 'BadBin'])
00882         io.seek(0)
00883 
00884         msg = email.message_from_binary_file(io)
00885         parts = [p for p in msg.walk()]
00886         self.assertEqual(len(parts), 3)
00887 
00888         # first part is the multipart container
00889         self.assertTrue(parts[0].is_multipart())
00890 
00891         # second part should be an inline text/plain attachments with all short
00892         # fields
00893         self.assertTrue(not parts[1].is_multipart())
00894         self.assertEqual(parts[1].get_content_type(), 'text/plain')
00895         self.assertEqual(parts[1].get_content_charset(), 'utf-8')
00896         self.assertEqual(parts[1].get_filename(), None)
00897         self.assertEqual(parts[1].get_payload(decode=True), b'''ProblemType: Crash
00898 Date: now!
00899 GoodText: Hi
00900 ''')
00901 
00902         # third part should be the GoodBin: field as attachment
00903         self.assertTrue(not parts[2].is_multipart())
00904         f = tempfile.TemporaryFile()
00905         f.write(parts[2].get_payload(decode=True))
00906         f.seek(0)
00907         self.assertEqual(gzip.GzipFile(mode='rb', fileobj=f).read(), bin_data)
00908         f.close()

write_mime() with keys ordered.

Definition at line 909 of file test_problem_report.py.

00909 
00910     def test_write_mime_order(self):
00911         '''write_mime() with keys ordered.'''
00912 
00913         pr = problem_report.ProblemReport(date='now!')
00914         pr['SecondText'] = 'What'
00915         pr['FirstText'] = 'Who'
00916         pr['FourthText'] = 'Today'
00917         pr['ThirdText'] = "I Don't Know"
00918         io = BytesIO()
00919         pr.write_mime(io, priority_fields=['FirstText', 'SecondText',
00920                                            'ThirdText', 'Unknown', 'FourthText'])
00921         io.seek(0)
00922 
00923         msg = email.message_from_binary_file(io)
00924         parts = [p for p in msg.walk()]
00925         self.assertEqual(len(parts), 2)
00926 
00927         # first part is the multipart container
00928         self.assertTrue(parts[0].is_multipart())
00929 
00930         # second part should be an inline text/plain attachments with all short
00931         # fields
00932         self.assertTrue(not parts[1].is_multipart())
00933         self.assertEqual(parts[1].get_content_type(), 'text/plain')
00934         self.assertEqual(parts[1].get_content_charset(), 'utf-8')
00935         self.assertEqual(parts[1].get_filename(), None)
00936         self.assertEqual(parts[1].get_payload(decode=True), b'''FirstText: Who
00937 SecondText: What
00938 ThirdText: I Don't Know
00939 FourthText: Today
00940 ProblemType: Crash
00941 Date: now!
00942 ''')

write_mime() for text values.

Definition at line 647 of file test_problem_report.py.

00647 
00648     def test_write_mime_text(self):
00649         '''write_mime() for text values.'''
00650 
00651         pr = problem_report.ProblemReport(date='now!')
00652         pr['Simple'] = 'bar'
00653         if sys.version.startswith('2'):
00654             pr['SimpleUTF8'] = '1äö2Φ3'
00655             pr['SimpleUnicode'] = '1äö2Φ3'.decode('UTF-8')
00656             pr['TwoLineUnicode'] = 'pi-π\nnu-η\n'.decode('UTF-8')
00657             pr['TwoLineUTF8'] = 'pi-π\nnu-η\n'
00658         else:
00659             pr['SimpleUTF8'] = '1äö2Φ3'.encode('UTF-8')
00660             pr['SimpleUnicode'] = '1äö2Φ3'
00661             pr['TwoLineUnicode'] = 'pi-π\nnu-η\n'
00662             pr['TwoLineUTF8'] = 'pi-π\nnu-η\n'.encode('UTF-8')
00663         pr['SimpleLineEnd'] = 'bar\n'
00664         pr['TwoLine'] = 'first\nsecond\n'
00665         pr['InlineMargin'] = 'first\nsecond\nthird\nfourth\nfifth\n'
00666         pr['Multiline'] = ' foo   bar\nbaz\n  blip  \nline4\nline♥5!!\nłıµ€ ⅝\n'
00667 
00668         # still small enough for inline text
00669         pr['Largeline'] = 'A' * 999
00670         pr['LargeMultiline'] = 'A' * 120 + '\n' + 'B' * 90
00671 
00672         # too big for inline text, these become attachments
00673         pr['Hugeline'] = 'A' * 10000
00674         pr['HugeMultiline'] = 'A' * 900 + '\n' + 'B' * 900 + '\n' + 'C' * 900
00675         io = BytesIO()
00676         pr.write_mime(io)
00677         io.seek(0)
00678 
00679         msg = email.message_from_binary_file(io)
00680         parts = [p for p in msg.walk()]
00681         self.assertEqual(len(parts), 5)
00682 
00683         # first part is the multipart container
00684         self.assertTrue(parts[0].is_multipart())
00685 
00686         # second part should be an inline text/plain attachments with all short
00687         # fields
00688         self.assertTrue(not parts[1].is_multipart())
00689         self.assertEqual(parts[1].get_content_type(), 'text/plain')
00690         self.assertEqual(parts[1].get_content_charset(), 'utf-8')
00691         self.assertEqual(parts[1].get_filename(), None)
00692         expected = '''ProblemType: Crash
00693 Date: now!
00694 InlineMargin:
00695  first
00696  second
00697  third
00698  fourth
00699  fifth
00700 LargeMultiline:
00701  AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
00702  BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
00703 Largeline: %s
00704 Simple: bar
00705 SimpleLineEnd: bar
00706 SimpleUTF8: 1äö2Φ3
00707 SimpleUnicode: 1äö2Φ3
00708 TwoLine:
00709  first
00710  second
00711 TwoLineUTF8:
00712  pi-π
00713  nu-η
00714 TwoLineUnicode:
00715  pi-π
00716  nu-η
00717 ''' % pr['Largeline']
00718         if sys.version >= '3':
00719             expected = expected.encode('UTF-8')
00720         self.assertEqual(parts[1].get_payload(decode=True), expected)
00721 
00722         # third part should be the HugeMultiline: field as attachment
00723         self.assertTrue(not parts[2].is_multipart())
00724         self.assertEqual(parts[2].get_content_type(), 'text/plain')
00725         self.assertEqual(parts[2].get_content_charset(), 'utf-8')
00726         self.assertEqual(parts[2].get_filename(), 'HugeMultiline.txt')
00727         self.assertEqual(parts[2].get_payload(decode=True), pr['HugeMultiline'].encode())
00728 
00729         # fourth part should be the Hugeline: field as attachment
00730         self.assertTrue(not parts[3].is_multipart())
00731         self.assertEqual(parts[3].get_content_type(), 'text/plain')
00732         self.assertEqual(parts[3].get_content_charset(), 'utf-8')
00733         self.assertEqual(parts[3].get_filename(), 'Hugeline.txt')
00734         self.assertEqual(parts[3].get_payload(decode=True), pr['Hugeline'].encode())
00735 
00736         # fifth part should be the Multiline: field as attachment
00737         self.assertTrue(not parts[4].is_multipart())
00738         self.assertEqual(parts[4].get_content_type(), 'text/plain')
00739         self.assertEqual(parts[4].get_content_charset(), 'utf-8')
00740         self.assertEqual(parts[4].get_filename(), 'Multiline.txt')
00741         expected = ''' foo   bar
00742 baz
00743   blip  
00744 line4
00745 line♥5!!
00746 łıµ€ ⅝
00747 '''
00748         if sys.version >= '3':
00749             expected = expected.encode('UTF-8')
00750         self.assertEqual(parts[4].get_payload(decode=True), expected)


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