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

Revision 24857, 6.0 KB checked in by jdreed, 14 years ago (diff)
In printing-config: * Test system and argstyle, when adding zephyr args so as not to break * Modify tests to account for the fact that we're now calling common.find_queue twice
  • 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 TestNonexistantPrinter(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
83        # Result:
84        os.execvp('cups-lpr', ['lpr', '-Uquentin', '-Pstark', '-m', 'puppies biting nose.jpg'])
85
86        self.mox.ReplayAll()
87
88        lpr._main(['lpr', '-Pstark', 'puppies biting nose.jpg'])
89
90
91class TestLpropt(TestLpr):
92    environ = {'ATHENA_USER': 'jdreed', 'LPROPT': '-Zduplex'}
93    backends = ['get-print.mit.edu']
94
95    def test(self):
96        """Test printing with LPROPT set.
97
98        Taken from Trac #509, reported on Mar 12, 2010."""
99        # We now call common.find_queue twice
100        common._hesiod_lookup('ajax', 'pcap').AndReturn(['ajax:rp=ajax:rm=GET-PRINT.MIT.EDU:ka#0:mc#0:'])
101        common.get_cups_uri('ajax').AndReturn(None)
102        common.is_cups_server('GET-PRINT.MIT.EDU').AndReturn(True)
103        common.get_default_printer().AndReturn(None)
104        common._hesiod_lookup('ajax', 'pcap').AndReturn(['ajax:rp=ajax:rm=GET-PRINT.MIT.EDU:ka#0:mc#0:'])
105        common.get_cups_uri('ajax').AndReturn(None)
106        common.is_cups_server('GET-PRINT.MIT.EDU').AndReturn(True)
107
108        # Result:
109        os.execvp('cups-lpr', ['lpr', '-Ujdreed', '-Pajax', '-osides=two-sided-long-edge', '-m'])
110
111        self.mox.ReplayAll()
112
113        lpr._main(['lpr', '-P', 'ajax'])
114
115
116class TestNoLpropt(TestLpr):
117    environ = {'ATHENA_USER': 'jdreed'}
118    backends = ['get-print.mit.edu']
119
120    def test(self):
121        """Test printing with LPROPT unset.
122
123        Taken from Trac #509, reported on Mar 12, 2010."""
124        # We now call common.find_queue twice
125        common._hesiod_lookup('ajax', 'pcap').AndReturn(['ajax:rp=ajax:rm=GET-PRINT.MIT.EDU:ka#0:mc#0:'])
126        common.get_default_printer().AndReturn(None)
127        common.get_cups_uri('ajax').AndReturn(None)
128        common.is_cups_server('GET-PRINT.MIT.EDU').AndReturn(True)
129        common._hesiod_lookup('ajax', 'pcap').AndReturn(['ajax:rp=ajax:rm=GET-PRINT.MIT.EDU:ka#0:mc#0:'])
130        common.get_cups_uri('ajax').AndReturn(None)
131        common.is_cups_server('GET-PRINT.MIT.EDU').AndReturn(True)
132
133        # Result:
134        os.execvp('cups-lpr', ['lpr', '-Ujdreed', '-Pajax', '-m'])
135
136        self.mox.ReplayAll()
137
138        lpr._main(['lpr', '-P', 'ajax'])
139
140class TestLPRngQueue(TestLpr):
141    environ = {'ATHENA_USER': 'jdreed'}
142    backends = ['get-print.mit.edu']
143
144    def test(self):
145        """Test printing to an LPRng queue
146
147        Ensure we pass zephyr arguments correctly.
148        (because 'lpr -Pfoo' will count as 'CUPS argument style')"""
149        # We call common.find_queue twice
150        common._hesiod_lookup('w20thesis', 'pcap').AndReturn(['w20thesis:rp=w20thesis:rm=IO.MIT.EDU:ka#0:mc#0:auth=kerberos5:xn:'])
151        common.get_default_printer().AndReturn(None)
152        common.get_cups_uri('w20thesis').AndReturn(None)
153        common.is_cups_server('IO.MIT.EDU').AndReturn(False)
154        common._hesiod_lookup('w20thesis', 'pcap').AndReturn(['w20thesis:rp=w20thesis:rm=IO.MIT.EDU:ka#0:mc#0:auth=kerberos5:xn:'])
155        common.get_cups_uri('w20thesis').AndReturn(None)
156        common.is_cups_server('IO.MIT.EDU').AndReturn(False)
157
158        # Result:
159        os.execvp('mit-lpr', ['lpr', '-Ujdreed', '-Pw20thesis', '-mzephyr%jdreed'])
160
161        self.mox.ReplayAll()
162
163        lpr._main(['lpr', '-P', 'w20thesis'])
164
165if __name__ == '__main__':
166    unittest.main()
Note: See TracBrowser for help on using the repository browser.