source: trunk/debathena/config/printing-config/debathena/printing/test_lpr.py @ 25837

Revision 25837, 5.2 KB checked in by jdreed, 12 years ago (diff)
Revert accidental line alteration while commenting it out
  • Property svn:executable set to *
Line 
1#!/usr/bin/python
2"""Test suite for debathena.printing.lpr
3
4These tests are intended to be end-to-end verification of the command
5line wrapper scripts. In order to be a faithful end-to-end check, all
6test cases should subclass the TestLpr class, and should not stub out
7any functions or objects which have not been stubbed out by TestLpr.
8"""
9
10
11import os
12import unittest
13
14import cups
15import mox
16
17from debathena.printing import common
18from debathena.printing import lpr
19
20
21class TestLpr(mox.MoxTestBase):
22    """Tests for the lpr command line wrapper script.
23
24    These tests are intended to be almost perfect end-to-end tests,
25    except that they should run consistently in the future in spite of
26    a changing environment, Hesiod data, etc.
27
28    In order to maintain the validity of these tests, strict limits
29    are placed on what functions and objects can be stubbed out. Those
30    stubs are created in this class, to be used in
31    subclasses. Subclasses are not allowed to stub out any additional
32    functions or objects.
33
34    The functions/objects that have been replaced with mocks are:
35
36      * debathena.printing.common._hesiod_lookup
37      * debathena.printing.common.get_cups_uri
38      * debathena.printing.common.is_cups_server
39      * debathena.printing.common.cupsd
40      * os.execvp
41
42    Additionally, while d.p.common.get_direct_printer is not strictly
43    at the boundry of Debathena code and the environment, it has been
44    stubbed out to avoid pointless boilerplate.
45
46    Finally, os.environ and d.p.common.CUPS_BACKENDS are populated by
47    the environ and backends (respectively) attributes of the test
48    class.
49    """
50    environ = {}
51    backends = []
52
53    def setUp(self):
54        super(TestLpr, self).setUp()
55
56        self.mox.stubs.Set(os, 'environ', self.environ)
57        self.mox.stubs.Set(common, 'CUPS_BACKENDS', self.backends)
58        self.mox.stubs.Set(common, 'cupsd', self.mox.CreateMock(cups.Connection))
59        self.mox.stubs.Set(common, '_loaded', True)
60
61        self.mox.StubOutWithMock(common, '_hesiod_lookup')
62        self.mox.StubOutWithMock(common, 'get_cups_uri')
63        self.mox.StubOutWithMock(common, 'is_cups_server')
64        self.mox.StubOutWithMock(common, 'get_default_printer')
65        self.mox.StubOutWithMock(os, 'execvp')
66
67
68class TestNonexistentPrinter(TestLpr):
69    # LPROPT, PRINTER are unset
70    environ = {'ATHENA_USER': 'quentin'}
71
72    def test(self):
73        """Test printing to a printer that is not in Hesiod.
74
75        Taken from -c debathena, reported by quentin on May 14, 2010."""
76        # We now call common.find_queue twice
77        common._hesiod_lookup('stark', 'pcap').AndReturn([])
78        common.get_cups_uri('stark').AndReturn(None)
79        common.get_default_printer().AndReturn(None)
80        common._hesiod_lookup('stark', 'pcap').AndReturn([])
81        common.get_cups_uri('stark').AndReturn(None)
82        common.get_cups_uri('stark').AndReturn(None)
83
84        # Result:
85        os.execvp('cups-lpr', ['lpr', '-Uquentin', '-Pstark', '-m', 'puppies biting nose.jpg'])
86
87        self.mox.ReplayAll()
88
89        lpr._main(['lpr', '-Pstark', 'puppies biting nose.jpg'])
90
91
92class TestNoLpropt(TestLpr):
93    environ = {'ATHENA_USER': 'jdreed'}
94    backends = ['get-print.mit.edu']
95
96    def test(self):
97        """Test printing with LPROPT unset.
98
99        Taken from Trac #509, reported on Mar 12, 2010."""
100        # We now call common.find_queue twice
101        common._hesiod_lookup('ajax', 'pcap').AndReturn(['ajax:rp=ajax:rm=GET-PRINT.MIT.EDU:ka#0:mc#0:'])
102        common.get_default_printer().AndReturn(None)
103        common.get_cups_uri('ajax').AndReturn(None)
104        # We no longer call "is_cups_server"
105        # common.is_cups_server('GET-PRINT.MIT.EDU').AndReturn(True)
106        common._hesiod_lookup('ajax', 'pcap').AndReturn(['ajax:rp=ajax:rm=GET-PRINT.MIT.EDU:ka#0:mc#0:'])
107        common.get_cups_uri('ajax').AndReturn(None)
108        # We no longer call "is_cups_server"
109        # common.is_cups_server('GET-PRINT.MIT.EDU').AndReturn(True)
110
111        # Result:
112        os.execvp('cups-lpr', ['lpr', '-Ujdreed', '-Pajax', '-m'])
113
114        self.mox.ReplayAll()
115
116        lpr._main(['lpr', '-P', 'ajax'])
117
118# class TestLPRngQueue(TestLpr):
119#     environ = {'ATHENA_USER': 'jdreed'}
120#     backends = ['get-print.mit.edu']
121
122#     def test(self):
123#         """Test printing to an LPRng queue
124
125#         Ensure we pass zephyr arguments correctly.
126#         (because 'lpr -Pfoo' will count as 'CUPS argument style')"""
127#         # We call common.find_queue twice
128#         common._hesiod_lookup('w20thesis', 'pcap').AndReturn(['w20thesis:rp=w20thesis:rm=IO.MIT.EDU:ka#0:mc#0:auth=kerberos5:xn:'])
129#         common.get_default_printer().AndReturn(None)
130#         common.get_cups_uri('w20thesis').AndReturn(None)
131#         common.is_cups_server('IO.MIT.EDU').AndReturn(False)
132#         common._hesiod_lookup('w20thesis', 'pcap').AndReturn(['w20thesis:rp=w20thesis:rm=IO.MIT.EDU:ka#0:mc#0:auth=kerberos5:xn:'])
133#         common.get_cups_uri('w20thesis').AndReturn(None)
134#         common.is_cups_server('IO.MIT.EDU').AndReturn(False)
135
136#         # Result:
137#         os.execvp('mit-lpr', ['lpr', '-Ujdreed', '-Pw20thesis', '-mzephyr%jdreed'])
138
139#         self.mox.ReplayAll()
140
141#         lpr._main(['lpr', '-P', 'w20thesis'])
142
143if __name__ == '__main__':
144    unittest.main()
Note: See TracBrowser for help on using the repository browser.