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

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