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