add .gitignore
[stack/cam.git] / cam / tests / test_openssl_wrap.py
1 import os
2 import unittest
3 import subprocess
4 import mox
5 from cam import openssl_wrap
6
7
8 class PopenStub(object):
9
10     def __init__(self, stdout='', returncode=0):
11         self.stdout = stdout
12         self.returncode = returncode
13
14     def communicate(self):
15         return self.stdout, 'unused'
16
17
18 class OpensslWrapTest(unittest.TestCase):
19
20     def setUp(self):
21         self.mox = mox.Mox()
22
23     def tearDown(self):
24         self.mox.VerifyAll()
25         self.mox.UnsetStubs()
26
27     def test_run(self):
28         self.mox.StubOutWithMock(subprocess, 'Popen', use_mock_anything=True)
29         pipe_stub = PopenStub(stdout='output')
30         subprocess.Popen(['openssl', 'test'], stdout=subprocess.PIPE, env=mox.IsA(dict)
31                          ).AndReturn(pipe_stub)
32         self.mox.ReplayAll()
33         result = openssl_wrap.run('test')
34         self.assertEquals('output', result)
35
36     def test_run_fails(self):
37         self.mox.StubOutWithMock(subprocess, 'Popen', use_mock_anything=True)
38         pipe_stub = PopenStub(returncode=1)
39         subprocess.Popen(['openssl', 'test'], stdout=subprocess.PIPE, env=mox.IsA(dict)
40                          ).AndReturn(pipe_stub)
41         self.mox.ReplayAll()
42         def r():
43             result = openssl_wrap.run('test')
44         self.assertRaises(openssl_wrap.CommandError, r)
45
46     def test_run_with_config(self):
47         self.mox.StubOutWithMock(subprocess, 'Popen', use_mock_anything=True)
48         pipe_stub = PopenStub(stdout='output')
49         subprocess.Popen(['openssl', 'test', '-config', 'conf', '-batch', 'arg'],
50                          stdout=subprocess.PIPE, env=mox.IsA(dict)).AndReturn(pipe_stub)
51         self.mox.ReplayAll()
52         result = openssl_wrap.run_with_config('.', 'conf', 'test', 'arg')
53
54
55 if __name__ == '__main__':
56     unittest.main()