source: trunk/debathena/config/printing-config/debathena/printing/test_common.py @ 25835

Revision 25835, 9.3 KB checked in by jdreed, 12 years ago (diff)
In printing-config: * Kill off LPRng (Trac: #1021). Anyone wishing to print to or otherwise use an lpr printer should use the rlpr(1) suite of commands.
  • Property svn:executable set to *
RevLine 
[24579]1#!/usr/bin/python
[24665]2"""Test suite for debathena.printing.common"""
[24579]3
4
[24648]5import os
[24583]6import unittest
7
[24579]8import cups
[24589]9import hesiod
[24579]10import mox
11
[24665]12from debathena.printing import common
[24579]13
14
[24589]15class TestHesiodLookup(mox.MoxTestBase):
16    def setUp(self):
17        super(TestHesiodLookup, self).setUp()
18
[24666]19        self.mox.StubOutWithMock(hesiod, 'Lookup', use_mock_anything=True)
[24589]20
21    def test_valid(self):
22        """Test _hesiod_lookup on a record that exists"""
23        class FakeResults(object): pass
24        h = FakeResults()
25        h.results = ['ajax:rp=ajax:rm=GET-PRINT.MIT.EDU:ka#0:mc#0:']
26
27        hesiod.Lookup('ajax', 'pcap').AndReturn(h)
28
29        self.mox.ReplayAll()
30
[24665]31        self.assertEqual(common._hesiod_lookup('ajax', 'pcap'),
[24589]32                         h.results)
33
34    def test_enoent(self):
35        """Test _hesiod_lookup on nonexistent record"""
36        hesiod.Lookup('doesnt_exist', 'pcap').AndRaise(
37            IOError(2, 'No such file or directory'))
38
39        self.mox.ReplayAll()
40
[24665]41        self.assertEqual(common._hesiod_lookup('doesnt_exist', 'pcap'),
[24589]42                         [])
43
44
[24603]45class TestParseArgs(mox.MoxTestBase):
46    def setUp(self):
47        super(TestParseArgs, self).setUp()
48
[25835]49        # Multiple argument styles can be added here if we ever have any
[24665]50        self.optinfo = ((common.SYSTEM_CUPS, 'P:'),
[25835]51                       )
[24603]52
53    def test_valid_primary_args(self):
54        """Test parsing arguments with the first set of options"""
[24665]55        self.assertEqual(common.parse_args(['-Pmeadow', 'my_job'], self.optinfo),
56                         (common.SYSTEM_CUPS, [('-P', 'meadow')], ['my_job']))
[24603]57
[25835]58    # We no longer have multiple argument parsing styles
59    # def test_valid_secondary_args(self):
60    #     """Test parsing arguments with the second set of options"""
61    #     self.assertEqual(common.parse_args(['-Xmeadow', 'my_job'], self.optinfo),
62    #                      (common.SYSTEM_LPRNG, [('-X', 'meadow')], ['my_job']))
[24603]63
64    def test_empty_args(self):
65        """Test parsing an empty argument list"""
[24665]66        self.assertEqual(common.parse_args([], self.optinfo),
67                         (common.SYSTEM_CUPS, [], []))
[24603]68
[25228]69    # def test_invalid_args(self):
70    #     """Test parsing an argument list that fails to parse"""
71    #     self.assertEqual(common.parse_args(['-wtf'], self.optinfo),
72    #                      None)
[24603]73
74
[24589]75class TestCanonicalizeQueue(mox.MoxTestBase):
76    def setUp(self):
77        super(TestCanonicalizeQueue, self).setUp()
78
79        def _setup_side_effects():
[24665]80            common.CUPS_FRONTENDS = ['printers.mit.edu', 'cluster-printers.mit.edu']
81            common.CUPS_BACKENDS = ['get-print.mit.edu']
82        self.mox.StubOutWithMock(common, '_setup')
83        common._setup().WithSideEffects(_setup_side_effects)
[24589]84
[24665]85        self.mox.StubOutWithMock(common, 'get_cups_uri')
[24589]86
87    def test_non_local_queue(self):
88        """Test canonicalize_queue with a non-local queue name"""
[24665]89        common.get_cups_uri('python').AndReturn(None)
[24589]90        self.mox.ReplayAll()
[24665]91        self.assertEqual(common.canonicalize_queue('python'),
[24589]92                         'python')
93
94    def test_local_only_name(self):
95        """Test canonicalize_queue on a local-only queue"""
[24665]96        common.get_cups_uri('patience').AndReturn('mdns://patience._printer._tcp.local.')
[24589]97        self.mox.ReplayAll()
[24665]98        self.assertEqual(common.canonicalize_queue('patience'),
[24589]99                         None)
100
101    def test_invalid_queue_uri(self):
102        """Test canonicalize_queue with a URL we don't understand"""
[24665]103        common.get_cups_uri('screwedup').AndReturn('ipp://PRINTERS.MIT.EDU/stuff/screwedup')
[24589]104        self.mox.ReplayAll()
[24665]105        self.assertEqual(common.canonicalize_queue('screwedup'),
[24589]106                         None)
107
108    def test_valid_printer(self):
109        """Test a locally configured bounce to an Athena printer"""
[24665]110        common.get_cups_uri('ajax').AndReturn('ipp://cluster-printers.mit.edu:631/printers/ajax')
[24589]111        self.mox.ReplayAll()
[24665]112        self.assertEqual(common.canonicalize_queue('ajax'),
[24589]113                         'ajax')
114
115    def test_misnamed_valid_printer(self):
116        """Test a local bounce queue with a different name from the Athena queue"""
[24665]117        common.get_cups_uri('w20').AndReturn('ipp://cluster-printers.mit.edu:631/printers/ajax')
[24589]118        self.mox.ReplayAll()
[24665]119        self.assertEqual(common.canonicalize_queue('w20'),
[24589]120                         'ajax')
121
122    def test_valid_class(self):
123        """Test a locally configured bounce queue to an Athena class"""
[24665]124        common.get_cups_uri('ajax2').AndReturn('ipp://cluster-printers.mit.edu:631/classes/ajax2')
[24589]125        self.mox.ReplayAll()
[24665]126        self.assertEqual(common.canonicalize_queue('ajax2'),
[24589]127                         'ajax2')
128
129
[24648]130class TestGetHesiodPrintServer(mox.MoxTestBase):
131    def setUp(self):
132        super(TestGetHesiodPrintServer, self).setUp()
133
134    def test_parse_pcap(self):
135        """Test get_hesiod_print_server's ability to parse pcap records"""
[24665]136        self.mox.StubOutWithMock(common, '_hesiod_lookup')
[24648]137
[24665]138        common._hesiod_lookup('ajax', 'pcap').AndReturn(
[24648]139            ['ajax:rp=ajax:rm=GET-PRINT.MIT.EDU:ka#0:mc#0:'])
140
141        self.mox.ReplayAll()
142
[24665]143        self.assertEqual(common.get_hesiod_print_server('ajax'),
[24648]144                         'GET-PRINT.MIT.EDU')
145
146
[24579]147class TestFindQueue(mox.MoxTestBase):
148    def setUp(self):
149        super(TestFindQueue, self).setUp()
150
[24665]151        self.mox.StubOutWithMock(common, 'canonicalize_queue')
152        self.mox.StubOutWithMock(common, 'get_hesiod_print_server')
153        self.mox.StubOutWithMock(common, 'is_cups_server')
[24579]154
[24581]155    def test_local_mdns_queue(self):
[24580]156        """Verify that find_queue doesn't interfere with truly local printers."""
[24665]157        common.canonicalize_queue('foo').AndReturn(None)
[24579]158
159        self.mox.ReplayAll()
160
[24665]161        self.assertEqual(common.find_queue('foo'),
162                         (common.SYSTEM_CUPS, None, 'foo'))
[24579]163
[24587]164    def test_athena_cups_queue(self):
165        """Verify that find_queue can find non-local Athena queues on CUPS"""
[24665]166        common.canonicalize_queue('ajax').AndReturn('ajax')
167        common.get_hesiod_print_server('ajax').AndReturn('GET-PRINT.MIT.EDU')
[25835]168        # We no longer call "is_cups_server"
169        # common.is_cups_server('GET-PRINT.MIT.EDU').AndReturn(True)
[24579]170
[24587]171        self.mox.ReplayAll()
172
[24665]173        self.assertEqual(common.find_queue('ajax'),
174                         (common.SYSTEM_CUPS, 'GET-PRINT.MIT.EDU', 'ajax'))
[24587]175
[25835]176    # def test_athena_lprng_queue(self):
177    #     """Verify that find_queue can find non-local Athena queues on LPRng"""
178    #     common.canonicalize_queue('ashdown').AndReturn('ashdown')
179    #     common.get_hesiod_print_server('ashdown').AndReturn('MULCH.MIT.EDU')
180    #     common.is_cups_server('MULCH.MIT.EDU').AndReturn(False)
[24587]181
[25835]182    #     self.mox.ReplayAll()
[24587]183
[25835]184    #     self.assertEqual(common.find_queue('ashdown'),
185    #                      (common.SYSTEM_LPRNG, 'MULCH.MIT.EDU', 'ashdown'))
[24587]186
[24588]187    def test_misnamed_local_queue(self):
188        """Verify that find_queue will use canonicalized queue names"""
[24665]189        common.canonicalize_queue('w20').AndReturn('ajax')
190        common.get_hesiod_print_server('ajax').AndReturn('GET-PRINT.MIT.EDU')
[25835]191        # We no longer call "is_cups_server"
192        # common.is_cups_server('GET-PRINT.MIT.EDU').AndReturn(True)
[24587]193
[24588]194        self.mox.ReplayAll()
195
[24665]196        self.assertEqual(common.find_queue('w20'),
197                         (common.SYSTEM_CUPS, 'GET-PRINT.MIT.EDU', 'ajax'))
[24588]198
199    def test_queue_with_instance(self):
200        """Verify that find_queue will strip instances"""
[24665]201        common.canonicalize_queue('ajax/2sided').AndReturn('ajax/2sided')
202        common.get_hesiod_print_server('ajax').AndReturn('GET-PRINT.MIT.EDU')
[25835]203        # We no longer call "is_cups_server"
204        # common.is_cups_server('GET-PRINT.MIT.EDU').AndReturn(True)
[24588]205
206        self.mox.ReplayAll()
207
[24665]208        self.assertEqual(common.find_queue('ajax/2sided'),
209                         (common.SYSTEM_CUPS, 'GET-PRINT.MIT.EDU', 'ajax'))
[24588]210
[24648]211    def test_canonicalize_queue_confusion(self):
212        """Test that find_queue will bail in case of confusion"""
[24665]213        common.canonicalize_queue('ajax').AndReturn('ajax')
214        common.get_hesiod_print_server('ajax').AndReturn(None)
[24588]215
[24648]216        self.mox.ReplayAll()
217
[24665]218        self.assertEqual(common.find_queue('ajax'),
219                         (common.SYSTEM_CUPS, None, 'ajax'))
[24648]220
221
222class TestDispatchCommand(mox.MoxTestBase):
223    def setUp(self):
224        super(TestDispatchCommand, self).setUp()
225
226        self.mox.StubOutWithMock(os, 'execvp')
227
228    def test_dispatch_cups(self):
229        """Test dispatch_command dispatching to CUPS"""
230        os.execvp('cups-lp', ['lp', '-dajax'])
231
232        self.mox.ReplayAll()
233
[24665]234        common.dispatch_command(common.SYSTEM_CUPS, 'lp', ['-dajax'])
[24648]235
[25835]236    # def test_dispatch_lprng(self):
237    #     """Test dispatch_command dispatching to LPRng"""
238    #     os.execvp('rlprm', ['lprm', '-Pbw', '123'])
[24648]239
[25835]240    #     self.mox.ReplayAll()
[24648]241
[25835]242    #     common.dispatch_command(common.SYSTEM_LPRNG, 'lprm', ['-Pbw', '123'])
[24648]243
244    def test_dispatch_error(self):
245        """Test that dispatch_command errors out when it doesn't know what to do"""
[24665]246        self.mox.StubOutWithMock(common, 'error')
247        common.error(1, mox.IgnoreArg()).AndRaise(Exception())
[24648]248
249        self.mox.ReplayAll()
250
251        self.assertRaises(Exception,
[24665]252                          common.dispatch_command,
[24648]253                          42,
254                          'life',
255                          ['the_universe', 'everything'])
256
257
[24579]258if __name__ == '__main__':
259    unittest.main()
Note: See TracBrowser for help on using the repository browser.