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

Revision 25356, 5.1 KB checked in by jdreed, 13 years ago (diff)
Update test to add additional call to get_cups_uri
  • 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        common.is_cups_server('GET-PRINT.MIT.EDU').AndReturn(True)
105        common._hesiod_lookup('ajax', 'pcap').AndReturn(['ajax:rp=ajax:rm=GET-PRINT.MIT.EDU:ka#0:mc#0:'])
106        common.get_cups_uri('ajax').AndReturn(None)
107        common.is_cups_server('GET-PRINT.MIT.EDU').AndReturn(True)
108
109        # Result:
110        os.execvp('cups-lpr', ['lpr', '-Ujdreed', '-Pajax', '-m'])
111
112        self.mox.ReplayAll()
113
114        lpr._main(['lpr', '-P', 'ajax'])
115
116class TestLPRngQueue(TestLpr):
117    environ = {'ATHENA_USER': 'jdreed'}
118    backends = ['get-print.mit.edu']
119
120    def test(self):
121        """Test printing to an LPRng queue
122
123        Ensure we pass zephyr arguments correctly.
124        (because 'lpr -Pfoo' will count as 'CUPS argument style')"""
125        # We call common.find_queue twice
126        common._hesiod_lookup('w20thesis', 'pcap').AndReturn(['w20thesis:rp=w20thesis:rm=IO.MIT.EDU:ka#0:mc#0:auth=kerberos5:xn:'])
127        common.get_default_printer().AndReturn(None)
128        common.get_cups_uri('w20thesis').AndReturn(None)
129        common.is_cups_server('IO.MIT.EDU').AndReturn(False)
130        common._hesiod_lookup('w20thesis', 'pcap').AndReturn(['w20thesis:rp=w20thesis:rm=IO.MIT.EDU:ka#0:mc#0:auth=kerberos5:xn:'])
131        common.get_cups_uri('w20thesis').AndReturn(None)
132        common.is_cups_server('IO.MIT.EDU').AndReturn(False)
133
134        # Result:
135        os.execvp('mit-lpr', ['lpr', '-Ujdreed', '-Pw20thesis', '-mzephyr%jdreed'])
136
137        self.mox.ReplayAll()
138
139        lpr._main(['lpr', '-P', 'w20thesis'])
140
141if __name__ == '__main__':
142    unittest.main()
Note: See TracBrowser for help on using the repository browser.