1 | #!/usr/bin/python |
---|
2 | """Test suite for debathena.printing.common""" |
---|
3 | |
---|
4 | |
---|
5 | import os |
---|
6 | import unittest |
---|
7 | |
---|
8 | import cups |
---|
9 | import hesiod |
---|
10 | import mox |
---|
11 | |
---|
12 | from debathena.printing import common |
---|
13 | |
---|
14 | |
---|
15 | class 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 | |
---|
45 | class TestParseArgs(mox.MoxTestBase): |
---|
46 | def setUp(self): |
---|
47 | super(TestParseArgs, self).setUp() |
---|
48 | |
---|
49 | # Multiple argument styles can be added here if we ever have any |
---|
50 | self.optinfo = ((common.SYSTEM_CUPS, 'P:'), |
---|
51 | ) |
---|
52 | |
---|
53 | def test_valid_primary_args(self): |
---|
54 | """Test parsing arguments with the first set of options""" |
---|
55 | self.assertEqual(common.parse_args(['-Pmeadow', 'my_job'], self.optinfo), |
---|
56 | (common.SYSTEM_CUPS, [('-P', 'meadow')], ['my_job'])) |
---|
57 | |
---|
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'])) |
---|
63 | |
---|
64 | def test_empty_args(self): |
---|
65 | """Test parsing an empty argument list""" |
---|
66 | self.assertEqual(common.parse_args([], self.optinfo), |
---|
67 | (common.SYSTEM_CUPS, [], [])) |
---|
68 | |
---|
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) |
---|
73 | |
---|
74 | |
---|
75 | class TestCanonicalizeQueue(mox.MoxTestBase): |
---|
76 | def setUp(self): |
---|
77 | super(TestCanonicalizeQueue, self).setUp() |
---|
78 | |
---|
79 | def _setup_side_effects(): |
---|
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) |
---|
84 | |
---|
85 | self.mox.StubOutWithMock(common, 'get_cups_uri') |
---|
86 | |
---|
87 | def test_non_local_queue(self): |
---|
88 | """Test canonicalize_queue with a non-local queue name""" |
---|
89 | common.get_cups_uri('python').AndReturn(None) |
---|
90 | self.mox.ReplayAll() |
---|
91 | self.assertEqual(common.canonicalize_queue('python'), |
---|
92 | 'python') |
---|
93 | |
---|
94 | def test_local_only_name(self): |
---|
95 | """Test canonicalize_queue on a local-only queue""" |
---|
96 | common.get_cups_uri('patience').AndReturn('mdns://patience._printer._tcp.local.') |
---|
97 | self.mox.ReplayAll() |
---|
98 | self.assertEqual(common.canonicalize_queue('patience'), |
---|
99 | None) |
---|
100 | |
---|
101 | def test_invalid_queue_uri(self): |
---|
102 | """Test canonicalize_queue with a URL we don't understand""" |
---|
103 | common.get_cups_uri('screwedup').AndReturn('ipp://PRINTERS.MIT.EDU/stuff/screwedup') |
---|
104 | self.mox.ReplayAll() |
---|
105 | self.assertEqual(common.canonicalize_queue('screwedup'), |
---|
106 | None) |
---|
107 | |
---|
108 | def test_valid_printer(self): |
---|
109 | """Test a locally configured bounce to an Athena printer""" |
---|
110 | common.get_cups_uri('ajax').AndReturn('ipp://cluster-printers.mit.edu:631/printers/ajax') |
---|
111 | self.mox.ReplayAll() |
---|
112 | self.assertEqual(common.canonicalize_queue('ajax'), |
---|
113 | 'ajax') |
---|
114 | |
---|
115 | def test_misnamed_valid_printer(self): |
---|
116 | """Test a local bounce queue with a different name from the Athena queue""" |
---|
117 | common.get_cups_uri('w20').AndReturn('ipp://cluster-printers.mit.edu:631/printers/ajax') |
---|
118 | self.mox.ReplayAll() |
---|
119 | self.assertEqual(common.canonicalize_queue('w20'), |
---|
120 | 'ajax') |
---|
121 | |
---|
122 | def test_valid_class(self): |
---|
123 | """Test a locally configured bounce queue to an Athena class""" |
---|
124 | common.get_cups_uri('ajax2').AndReturn('ipp://cluster-printers.mit.edu:631/classes/ajax2') |
---|
125 | self.mox.ReplayAll() |
---|
126 | self.assertEqual(common.canonicalize_queue('ajax2'), |
---|
127 | 'ajax2') |
---|
128 | |
---|
129 | |
---|
130 | class 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""" |
---|
136 | self.mox.StubOutWithMock(common, '_hesiod_lookup') |
---|
137 | |
---|
138 | common._hesiod_lookup('ajax', 'pcap').AndReturn( |
---|
139 | ['ajax:rp=ajax:rm=GET-PRINT.MIT.EDU:ka#0:mc#0:']) |
---|
140 | |
---|
141 | self.mox.ReplayAll() |
---|
142 | |
---|
143 | self.assertEqual(common.get_hesiod_print_server('ajax'), |
---|
144 | 'GET-PRINT.MIT.EDU') |
---|
145 | |
---|
146 | |
---|
147 | class TestFindQueue(mox.MoxTestBase): |
---|
148 | def setUp(self): |
---|
149 | super(TestFindQueue, self).setUp() |
---|
150 | |
---|
151 | self.mox.StubOutWithMock(common, 'canonicalize_queue') |
---|
152 | self.mox.StubOutWithMock(common, 'get_hesiod_print_server') |
---|
153 | self.mox.StubOutWithMock(common, 'is_cups_server') |
---|
154 | |
---|
155 | def test_local_mdns_queue(self): |
---|
156 | """Verify that find_queue doesn't interfere with truly local printers.""" |
---|
157 | common.canonicalize_queue('foo').AndReturn(None) |
---|
158 | |
---|
159 | self.mox.ReplayAll() |
---|
160 | |
---|
161 | self.assertEqual(common.find_queue('foo'), |
---|
162 | (common.SYSTEM_CUPS, None, 'foo')) |
---|
163 | |
---|
164 | def test_athena_cups_queue(self): |
---|
165 | """Verify that find_queue can find non-local Athena queues on CUPS""" |
---|
166 | common.canonicalize_queue('ajax').AndReturn('ajax') |
---|
167 | common.get_hesiod_print_server('ajax').AndReturn('GET-PRINT.MIT.EDU') |
---|
168 | # We no longer call "is_cups_server" |
---|
169 | # common.is_cups_server('GET-PRINT.MIT.EDU').AndReturn(True) |
---|
170 | |
---|
171 | self.mox.ReplayAll() |
---|
172 | |
---|
173 | self.assertEqual(common.find_queue('ajax'), |
---|
174 | (common.SYSTEM_CUPS, 'GET-PRINT.MIT.EDU', 'ajax')) |
---|
175 | |
---|
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) |
---|
181 | |
---|
182 | # self.mox.ReplayAll() |
---|
183 | |
---|
184 | # self.assertEqual(common.find_queue('ashdown'), |
---|
185 | # (common.SYSTEM_LPRNG, 'MULCH.MIT.EDU', 'ashdown')) |
---|
186 | |
---|
187 | def test_misnamed_local_queue(self): |
---|
188 | """Verify that find_queue will use canonicalized queue names""" |
---|
189 | common.canonicalize_queue('w20').AndReturn('ajax') |
---|
190 | common.get_hesiod_print_server('ajax').AndReturn('GET-PRINT.MIT.EDU') |
---|
191 | # We no longer call "is_cups_server" |
---|
192 | # common.is_cups_server('GET-PRINT.MIT.EDU').AndReturn(True) |
---|
193 | |
---|
194 | self.mox.ReplayAll() |
---|
195 | |
---|
196 | self.assertEqual(common.find_queue('w20'), |
---|
197 | (common.SYSTEM_CUPS, 'GET-PRINT.MIT.EDU', 'ajax')) |
---|
198 | |
---|
199 | def test_queue_with_instance(self): |
---|
200 | """Verify that find_queue will strip instances""" |
---|
201 | common.canonicalize_queue('ajax/2sided').AndReturn('ajax/2sided') |
---|
202 | common.get_hesiod_print_server('ajax').AndReturn('GET-PRINT.MIT.EDU') |
---|
203 | # We no longer call "is_cups_server" |
---|
204 | # common.is_cups_server('GET-PRINT.MIT.EDU').AndReturn(True) |
---|
205 | |
---|
206 | self.mox.ReplayAll() |
---|
207 | |
---|
208 | self.assertEqual(common.find_queue('ajax/2sided'), |
---|
209 | (common.SYSTEM_CUPS, 'GET-PRINT.MIT.EDU', 'ajax')) |
---|
210 | |
---|
211 | def test_canonicalize_queue_confusion(self): |
---|
212 | """Test that find_queue will bail in case of confusion""" |
---|
213 | common.canonicalize_queue('ajax').AndReturn('ajax') |
---|
214 | common.get_hesiod_print_server('ajax').AndReturn(None) |
---|
215 | |
---|
216 | self.mox.ReplayAll() |
---|
217 | |
---|
218 | self.assertEqual(common.find_queue('ajax'), |
---|
219 | (common.SYSTEM_CUPS, None, 'ajax')) |
---|
220 | |
---|
221 | |
---|
222 | class 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 | |
---|
234 | common.dispatch_command(common.SYSTEM_CUPS, 'lp', ['-dajax']) |
---|
235 | |
---|
236 | # def test_dispatch_lprng(self): |
---|
237 | # """Test dispatch_command dispatching to LPRng""" |
---|
238 | # os.execvp('rlprm', ['lprm', '-Pbw', '123']) |
---|
239 | |
---|
240 | # self.mox.ReplayAll() |
---|
241 | |
---|
242 | # common.dispatch_command(common.SYSTEM_LPRNG, 'lprm', ['-Pbw', '123']) |
---|
243 | |
---|
244 | def test_dispatch_error(self): |
---|
245 | """Test that dispatch_command errors out when it doesn't know what to do""" |
---|
246 | self.mox.StubOutWithMock(common, 'error') |
---|
247 | common.error(1, mox.IgnoreArg()).AndRaise(Exception()) |
---|
248 | |
---|
249 | self.mox.ReplayAll() |
---|
250 | |
---|
251 | self.assertRaises(Exception, |
---|
252 | common.dispatch_command, |
---|
253 | 42, |
---|
254 | 'life', |
---|
255 | ['the_universe', 'everything']) |
---|
256 | |
---|
257 | |
---|
258 | if __name__ == '__main__': |
---|
259 | unittest.main() |
---|