add .gitignore
[stack/cam.git] / cam / tests / test_main.py
1 import getpass
2 import os
3 import shutil
4 import subprocess
5 import sys
6 import tempfile
7 import unittest
8 from cam import main
9
10
11 class MainTest(unittest.TestCase):
12
13     def setUp(self):
14         self.tmpdir = tempfile.mkdtemp()
15         self.cfgfile = os.path.join(self.tmpdir, 'test.conf')
16         with open(self.cfgfile, 'w') as fd:
17             fd.write("""
18 [ca]
19 cn = Test Ca
20 org = Test
21 country = IE
22 email = ca@test.org
23 bits = 1024
24
25 [web]
26 cn = www.test.org
27 """)
28
29         def _fake_getpass(prompt):
30             return 'testpass'
31         getpass.getpass = _fake_getpass
32
33     def tearDown(self):
34         shutil.rmtree(self.tmpdir)
35
36     def _run(self, *args):
37         sys.argv = ['cam', '--config=%s' % self.cfgfile] + list(args)
38         try:
39             return main.main()
40         except SystemExit, e:
41             return e.code
42
43     def test_init_and_sanity_check(self):
44         self.assertEquals(None, self._run('init'))
45         self.assertEquals(None, self._run('gen', 'web'))
46
47         ca_file = os.path.join(self.tmpdir, 'public/ca.pem')
48         crt_file = os.path.join(self.tmpdir, 'public/certs/web.pem')
49         self.assertTrue(os.path.exists(ca_file))
50         self.assertTrue(os.path.exists(crt_file))
51
52         subprocess.call(['openssl', 'x509', '-in', ca_file,
53                          '-noout', '-text'])
54         subprocess.call(['openssl', 'x509', '-in', crt_file,
55                          '-noout', '-text'])
56
57         pipe = subprocess.Popen(
58             ['openssl', 'verify', '-CAfile', ca_file, '-verbose', crt_file],
59             stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
60         output = pipe.communicate()[0]
61         result = pipe.returncode
62         self.assertEquals(0, result)
63
64         print output
65         self.assertTrue('error ' not in output)
66