upgrade to CAM v2.0
[stack/cam.git] / cam / tests / test_openssl_wrap.py
diff --git a/cam/tests/test_openssl_wrap.py b/cam/tests/test_openssl_wrap.py
new file mode 100644 (file)
index 0000000..6392e1b
--- /dev/null
@@ -0,0 +1,56 @@
+import os
+import unittest
+import subprocess
+import mox
+from cam import openssl_wrap
+
+
+class PopenStub(object):
+
+    def __init__(self, stdout='', returncode=0):
+        self.stdout = stdout
+        self.returncode = returncode
+
+    def communicate(self):
+        return self.stdout, 'unused'
+
+
+class OpensslWrapTest(unittest.TestCase):
+
+    def setUp(self):
+        self.mox = mox.Mox()
+
+    def tearDown(self):
+        self.mox.VerifyAll()
+        self.mox.UnsetStubs()
+
+    def test_run(self):
+        self.mox.StubOutWithMock(subprocess, 'Popen', use_mock_anything=True)
+        pipe_stub = PopenStub(stdout='output')
+        subprocess.Popen(['openssl', 'test'], stdout=subprocess.PIPE
+                         ).AndReturn(pipe_stub)
+        self.mox.ReplayAll()
+        result = openssl_wrap.run('test')
+        self.assertEquals('output', result)
+
+    def test_run_fails(self):
+        self.mox.StubOutWithMock(subprocess, 'Popen', use_mock_anything=True)
+        pipe_stub = PopenStub(returncode=1)
+        subprocess.Popen(['openssl', 'test'], stdout=subprocess.PIPE
+                         ).AndReturn(pipe_stub)
+        self.mox.ReplayAll()
+        def r():
+            result = openssl_wrap.run('test')
+        self.assertRaises(openssl_wrap.CommandError, r)
+
+    def test_run_with_config(self):
+        self.mox.StubOutWithMock(subprocess, 'Popen', use_mock_anything=True)
+        pipe_stub = PopenStub(stdout='output')
+        subprocess.Popen(['openssl', 'test', '-config', 'conf', '-batch', 'arg'],
+                         stdout=subprocess.PIPE).AndReturn(pipe_stub)
+        self.mox.ReplayAll()
+        result = openssl_wrap.run_with_config('conf', 'test', 'arg')
+
+
+if __name__ == '__main__':
+    unittest.main()