~apport-hackers/apport/trunk

« back to all changes in this revision

Viewing changes to apport/hookutils.py

  • Committer: Martin Pitt
  • Date: 2012-02-23 10:31:55 UTC
  • Revision ID: martin.pitt@canonical.com-20120223103155-m7p4is95x27vzm8n
Move all test suites out of the code modules into test/test_<module>.py. This avoids having to load it every time the program runs, and also allows running the tests against the installed version of Apport.

Show diffs side-by-side

added added

removed removed

Lines of Context:
806
806
        return None
807
807
 
808
808
    return session_start_time <= report_time
809
 
 
810
 
#
811
 
# Unit test
812
 
#
813
 
 
814
 
if __name__ == '__main__':
815
 
 
816
 
    import unittest
817
 
 
818
 
    class _T(unittest.TestCase):
819
 
        def test_module_license_evaluation(self):
820
 
            '''module licenses can be validated correctly.'''
821
 
 
822
 
            def _build_ko(license):
823
 
                asm = tempfile.NamedTemporaryFile(prefix='%s-' % (license),
824
 
                                                  suffix='.S')
825
 
                asm.write(('.section .modinfo\n.string "license=%s"\n' % (license)).encode())
826
 
                asm.flush()
827
 
                ko = tempfile.NamedTemporaryFile(prefix='%s-' % (license),
828
 
                                                 suffix='.ko')
829
 
                subprocess.call(['/usr/bin/as',asm.name,'-o',ko.name])
830
 
                return ko
831
 
            
832
 
            good_ko = _build_ko('GPL')
833
 
            bad_ko  = _build_ko('BAD')
834
 
 
835
 
            # test:
836
 
            #  - loaded real module
837
 
            #  - unfindable module
838
 
            #  - fake GPL module
839
 
            #  - fake BAD module
840
 
 
841
 
            # direct license check
842
 
            self.assertTrue('GPL' in _get_module_license('isofs'))
843
 
            self.assertEqual(_get_module_license('does-not-exist'), 'invalid')
844
 
            self.assertTrue('GPL' in _get_module_license(good_ko.name))
845
 
            self.assertTrue('BAD' in _get_module_license(bad_ko.name))
846
 
 
847
 
            # check via nonfree_kernel_modules logic
848
 
            f = tempfile.NamedTemporaryFile()
849
 
            f.write(('isofs\ndoes-not-exist\n%s\n%s\n' %
850
 
                    (good_ko.name,bad_ko.name)).encode())
851
 
            f.flush()
852
 
            nonfree = nonfree_kernel_modules(f.name)
853
 
            self.assertFalse('isofs' in nonfree)
854
 
            self.assertTrue('does-not-exist' in nonfree)
855
 
            self.assertFalse(good_ko.name in nonfree)
856
 
            self.assertTrue(bad_ko.name in nonfree)
857
 
 
858
 
        def test_attach_dmesg(self):
859
 
            '''attach_dmesg() does not overwrite already existing data'''
860
 
 
861
 
            report = {}
862
 
 
863
 
            attach_dmesg(report)
864
 
            self.assertTrue(report['BootDmesg'].startswith('['))
865
 
            self.assertTrue(len(report['BootDmesg']) > 500)
866
 
            self.assertTrue(report['CurrentDmesg'].startswith(b'['))
867
 
 
868
 
        def test_dmesg_overwrite(self):
869
 
            '''attach_dmesg() does not overwrite already existing data'''
870
 
 
871
 
            report = {'BootDmesg': 'existingboot'}
872
 
 
873
 
            attach_dmesg(report)
874
 
            self.assertEqual(report['BootDmesg'][:50], 'existingboot')
875
 
            self.assertTrue(report['CurrentDmesg'].startswith(b'['))
876
 
            
877
 
            report = {'BootDmesg': 'existingboot', 'CurrentDmesg': 'existingcurrent' }
878
 
 
879
 
            attach_dmesg(report)
880
 
            self.assertEqual(report['BootDmesg'], 'existingboot')
881
 
            self.assertEqual(report['CurrentDmesg'], 'existingcurrent')
882
 
 
883
 
        def test_attach_file(self):
884
 
            '''attach_file()'''
885
 
 
886
 
            with open('/etc/motd', 'rb') as f:
887
 
                motd_contents = f.read().strip()
888
 
            with open('/etc/issue', 'rb') as f:
889
 
                issue_contents = f.read().strip()
890
 
 
891
 
            # default key name
892
 
            report = {}
893
 
            attach_file(report, '/etc/motd')
894
 
            self.assertEqual(list(report), ['.etc.motd'])
895
 
            self.assertEqual(report['.etc.motd'], motd_contents)
896
 
 
897
 
            # custom key name
898
 
            report = {}
899
 
            attach_file(report, '/etc/motd', 'Motd')
900
 
            self.assertEqual(list(report), ['Motd'])
901
 
            self.assertEqual(report['Motd'], motd_contents)
902
 
 
903
 
            # nonexisting file
904
 
            report = {}
905
 
            attach_file(report, '/nonexisting')
906
 
            self.assertEqual(list(report), ['.nonexisting'])
907
 
            self.assertTrue(report['.nonexisting'].startswith('Error: '))
908
 
 
909
 
            # existing key
910
 
            report = {}
911
 
            attach_file(report, '/etc/motd')
912
 
            attach_file(report, '/etc/motd')
913
 
            self.assertEqual(list(report), ['.etc.motd'])
914
 
            self.assertEqual(report['.etc.motd'], motd_contents)
915
 
 
916
 
            attach_file(report, '/etc/issue', '.etc.motd', overwrite=False)
917
 
            self.assertEqual(sorted(report.keys()), ['.etc.motd', '.etc.motd_'])
918
 
            self.assertEqual(report['.etc.motd'], motd_contents)
919
 
            self.assertEqual(report['.etc.motd_'], issue_contents)
920
 
 
921
 
        def test_attach_file_if_exists(self):
922
 
            '''attach_file_if_exists()'''
923
 
 
924
 
            with open('/etc/motd', 'rb') as f:
925
 
                motd_contents = f.read().strip()
926
 
 
927
 
            # default key name
928
 
            report = {}
929
 
            attach_file_if_exists(report, '/etc/motd')
930
 
            self.assertEqual(list(report), ['.etc.motd'])
931
 
            self.assertEqual(report['.etc.motd'], motd_contents)
932
 
 
933
 
            # custom key name
934
 
            report = {}
935
 
            attach_file_if_exists(report, '/etc/motd', 'Motd')
936
 
            self.assertEqual(list(report), ['Motd'])
937
 
            self.assertEqual(report['Motd'], motd_contents)
938
 
 
939
 
            # nonexisting file
940
 
            report = {}
941
 
            attach_file_if_exists(report, '/nonexisting')
942
 
            self.assertEqual(list(report), [])
943
 
 
944
 
        def test_recent_logfile(self):
945
 
            self.assertEqual(recent_logfile('/nonexisting', re.compile('.')), '')
946
 
            self.assertEqual(recent_syslog(re.compile('ThisCantPossiblyHitAnything')), '')
947
 
            self.assertNotEqual(len(recent_syslog(re.compile('.'))), 0)
948
 
 
949
 
        @unittest.skipIf(in_session_of_problem(apport.Report()) is None, 'no ConsoleKit session')
950
 
        def test_in_session_of_problem(self):
951
 
            '''in_session_of_problem()'''
952
 
 
953
 
            old_ctime = locale.getlocale(locale.LC_TIME)
954
 
            locale.setlocale(locale.LC_TIME, 'C')
955
 
 
956
 
            report = {'Date': 'Sat Jan  1 12:00:00 2011'}
957
 
            self.assertFalse(in_session_of_problem(report))
958
 
 
959
 
            report = {'Date': 'Mon Oct 10 21:06:03 2009'}
960
 
            self.assertFalse(in_session_of_problem(report))
961
 
 
962
 
            report = {'Date': 'Tue Jan  1 12:00:00 2211'}
963
 
            self.assertTrue(in_session_of_problem(report))
964
 
 
965
 
            locale.setlocale(locale.LC_TIME, '')
966
 
 
967
 
            report = {'Date': 'Sat Jan  1 12:00:00 2011'}
968
 
            self.assertFalse(in_session_of_problem(report))
969
 
 
970
 
            report = {'Date': 'Mon Oct 10 21:06:03 2009'}
971
 
            self.assertFalse(in_session_of_problem(report))
972
 
 
973
 
            report = apport.Report()
974
 
            self.assertTrue(in_session_of_problem(report))
975
 
 
976
 
            self.assertEqual(in_session_of_problem({}), None)
977
 
 
978
 
            locale.setlocale(locale.LC_TIME, old_ctime)
979
 
 
980
 
        def test_no_crashes(self):
981
 
            '''functions do not crash (very shallow)'''
982
 
 
983
 
            report = {}
984
 
            attach_hardware(report)
985
 
            attach_alsa(report)
986
 
            attach_network(report)
987
 
            attach_wifi(report)
988
 
            attach_printing(report)
989
 
            attach_conffiles(report, 'bash')
990
 
            attach_conffiles(report, 'apport')
991
 
            attach_conffiles(report, 'nonexisting')
992
 
            attach_upstart_overrides(report, 'apport')
993
 
            attach_upstart_overrides(report, 'nonexisting')
994
 
 
995
 
    unittest.main()