~gary/charms/oneiric/buildbot-slave/run-as-buildbot

« back to all changes in this revision

Viewing changes to hooks/tests.py

  • Committer: Graham Binns
  • Date: 2012-02-07 13:50:37 UTC
  • mfrom: (3.4.9 bbs)
  • Revision ID: graham@canonical.com-20120207135037-6jrkd32hse6e1dg8
Merged Brad's branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2012 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
from subprocess import CalledProcessError
 
5
import unittest
 
6
 
 
7
from helpers import command
 
8
 
 
9
 
 
10
class TestCommand(unittest.TestCase):
 
11
 
 
12
    def test_simple_command(self):
 
13
        # Creating a simple command (ls) works and running the command
 
14
        # produces a string.
 
15
        ls = command('/bin/ls')
 
16
        self.assertIsInstance(ls(), str)
 
17
 
 
18
    def test_arguments(self):
 
19
        # Arguments can be passed to commands.
 
20
        ls = command('/bin/ls')
 
21
        self.assertIn('Usage:', ls('--help'))
 
22
 
 
23
    def test_missing_executable(self):
 
24
        # If the command does not exist, an OSError (No such file or
 
25
        # directory) is raised.
 
26
        bad = command('this command does not exist')
 
27
        with self.assertRaises(OSError) as info:
 
28
            bad()
 
29
        self.assertEqual(2, info.exception.errno)
 
30
 
 
31
    def test_error(self):
 
32
        # If the command returns a non-zero exit code, an exception is raised.
 
33
        ls = command('/bin/ls')
 
34
        with self.assertRaises(CalledProcessError):
 
35
            ls('--not a valid switch')
 
36
 
 
37
    def test_baked_in_arguments(self):
 
38
        # Arguments can be passed when creating the command as well as when
 
39
        # executing it.
 
40
        ll = command('/bin/ls', '-l')
 
41
        self.assertIn('rw', ll()) # Assumes a file is r/w in the pwd.
 
42
        self.assertIn('Usage:', ll('--help'))
 
43
 
 
44
    def test_quoting(self):
 
45
        # There is no need to quote special shell characters in commands.
 
46
        ls = command('/bin/ls')
 
47
        ls('--help', '>')
 
48
 
 
49
 
 
50
if __name__ == '__main__':
 
51
    unittest.main()