~wwoods/apport/fedora

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/python
'''Test the various package hooks.'''

import unittest, subprocess, tempfile, os, shutil, os.path, sys, optparse
import apport, apport.fileutils

# parse command line options
optparser = optparse.OptionParser('%prog [options]')
optparser.add_option('-l', '--local',
    help='Test scripts in ./bin/ instead of /usr/share/apport/',
    action='store_true', dest='local', default=False)
options, args = optparser.parse_args()

if options.local:
    basedir = 'bin'
else:
    basedir = '/usr/share/apport'

class _HooksTest(unittest.TestCase):
    def setUp(self):
        self.orig_report_dir = apport.fileutils.report_dir
        apport.fileutils.report_dir = tempfile.mkdtemp()
        os.environ['APPORT_REPORT_DIR'] = apport.fileutils.report_dir

        self.workdir = tempfile.mkdtemp()

    def tearDown(self):
        shutil.rmtree(apport.fileutils.report_dir)
        apport.fileutils.report_dir = self.orig_report_dir 

        shutil.rmtree(self.workdir)

    def test_package_hook_nologs(self):
        '''Test package_hook without any log files.'''

        ph = subprocess.Popen(['%s/package_hook' % basedir, '-p', 'bash'],
            stdin=subprocess.PIPE)
        ph.communicate('something is wrong')
        self.assertEqual(ph.returncode, 0, 'package_hook finished successfully')

        reps = apport.fileutils.get_new_reports()
        self.assertEqual(len(reps), 1, 'package_hook created a report')

        r = apport.Report()
        r.load(open(reps[0]))

        self.assertEqual(r['ProblemType'], 'Package')
        self.assertEqual(r['Package'], 'bash')
        self.assertEqual(r['SourcePackage'], 'bash')
        self.assertEqual(r['ErrorMessage'], 'something is wrong')

    def test_package_hook_uninstalled(self):
        '''Test package_hook on an uninstalled package (might fail to install).'''

        #FIXME: This is a really Debian/Ubuntu specific dodgy test case
        ph = subprocess.Popen(['%s/package_hook' % basedir, '-p', 'libdb4.3-tcl'],
            stdin=subprocess.PIPE)
        ph.communicate('something is wrong')
        self.assertEqual(ph.returncode, 0, 'package_hook finished successfully')

        reps = apport.fileutils.get_new_reports()
        self.assertEqual(len(reps), 1, 'package_hook created a report')

        r = apport.Report()
        r.load(open(reps[0]))

        self.assertEqual(r['ProblemType'], 'Package')
        self.assertEqual(r['Package'], 'libdb4.3-tcl')
        self.assertEqual(r['SourcePackage'], 'db4.3')
        self.assertEqual(r['ErrorMessage'], 'something is wrong')

    def test_package_hook_logs(self):
        '''Test package_hook with a log dir and a log file.'''

        open(os.path.join(self.workdir, 'log_1.log'), 'w').write('Log 1\nbla')
        open(os.path.join(self.workdir, 'log2'), 'w').write('Yet\nanother\nlog')

        ph = subprocess.Popen(['%s/package_hook' % basedir, '-p', 'bash', '-l',
            os.path.realpath(sys.argv[0]), '-l', self.workdir],
            stdin=subprocess.PIPE)
        ph.communicate('something is wrong')
        self.assertEqual(ph.returncode, 0, 'package_hook finished successfully')

        reps = apport.fileutils.get_new_reports()
        self.assertEqual(len(reps), 1, 'package_hook created a report')

        r = apport.Report()
        r.load(open(reps[0]))

        filekey = None
        log1key = None
        log2key = None
        for k in r.keys():
            if k.endswith('Testhooks'):
                filekey = k
            elif k.endswith('Log1log'):
                log1key = k
            elif k.endswith('Log2'):
                log2key = k

        self.assert_(filekey)
        self.assert_(log1key)
        self.assert_(log2key)
        self.assert_('0234lkjas' in r[filekey])
        self.assertEqual(len(r[filekey]), os.path.getsize(sys.argv[0]))
        self.assertEqual(r[log1key], 'Log 1\nbla')
        self.assertEqual(r[log2key], 'Yet\nanother\nlog')

    def test_kernel_hook(self):
        '''Test kernel_hook.'''

        self.assertEqual(subprocess.call('%s/kernel_hook' % basedir), 0,
            'kernel_hook finished successfully')

        reps = apport.fileutils.get_new_reports()
        self.assertEqual(len(reps), 1, 'kernel_hook created a report')

        r = apport.Report()
        r.load(open(reps[0]))

        self.assertEqual(set(r.keys()), set(['Date', 'Dmesg', 'ProcModules',
            'LsPciVV', 'LsPciVVN', 'Package', 'ProblemType', 'ProcCpuInfo',
            'ProcVersion', 'ProcVersionSignature', 'ProcCmdline', 'SourcePackage']))

        self.assertEqual(r['ProblemType'], 'Kernel')

        self.assert_(os.uname()[2].split('-')[0] in r['SourcePackage'])
        self.assertEqual(r['ProcVersion'],
            open('/proc/version').read().strip())

# call tests

tl = unittest.TestLoader()
tests_all = unittest.TestSuite((
    tl.loadTestsFromName('__main__')
))
unittest.TextTestRunner(verbosity=2).run(tests_all)