Statistics
| Branch: | Tag: | Revision:

root / test / ganeti.cmdlib_unittest.py @ 7eac4a4d

History | View | Annotate | Download (3.6 kB)

1
#!/usr/bin/python
2
#
3

    
4
# Copyright (C) 2008 Google Inc.
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 0.0510-1301, USA.
20

    
21

    
22
"""Script for unittesting the cmdlib module"""
23

    
24

    
25
import os
26
import unittest
27
import time
28
import tempfile
29
import shutil
30

    
31
from ganeti import cmdlib
32
from ganeti import errors
33

    
34
import testutils
35

    
36

    
37
class TestCertVerification(testutils.GanetiTestCase):
38
  def setUp(self):
39
    testutils.GanetiTestCase.setUp(self)
40

    
41
    self.tmpdir = tempfile.mkdtemp()
42

    
43
  def tearDown(self):
44
    shutil.rmtree(self.tmpdir)
45

    
46
  def testVerifyCertificate(self):
47
    cmdlib._VerifyCertificate(self._TestDataFilename("cert1.pem"))
48

    
49
    nonexist_filename = os.path.join(self.tmpdir, "does-not-exist")
50

    
51
    (errcode, msg) = cmdlib._VerifyCertificate(nonexist_filename)
52
    self.assertEqual(errcode, cmdlib.LUVerifyCluster.ETYPE_ERROR)
53

    
54
    # Try to load non-certificate file
55
    invalid_cert = self._TestDataFilename("bdev-net1.txt")
56
    (errcode, msg) = cmdlib._VerifyCertificate(invalid_cert)
57
    self.assertEqual(errcode, cmdlib.LUVerifyCluster.ETYPE_ERROR)
58

    
59

    
60
class TestVerifyCertificateInner(unittest.TestCase):
61
  FAKEFILE = "/tmp/fake/cert/file.pem"
62

    
63
  def test(self):
64
    vci = cmdlib._VerifyCertificateInner
65

    
66
    # Valid
67
    self.assertEqual(vci(self.FAKEFILE, False, 1263916313, 1298476313,
68
                         1266940313, warn_days=30, error_days=7),
69
                     (None, None))
70

    
71
    # Not yet valid
72
    (errcode, msg) = vci(self.FAKEFILE, False, 1266507600, 1267544400,
73
                         1266075600, warn_days=30, error_days=7)
74
    self.assertEqual(errcode, cmdlib.LUVerifyCluster.ETYPE_WARNING)
75

    
76
    # Expiring soon
77
    (errcode, msg) = vci(self.FAKEFILE, False, 1266507600, 1267544400,
78
                         1266939600, warn_days=30, error_days=7)
79
    self.assertEqual(errcode, cmdlib.LUVerifyCluster.ETYPE_ERROR)
80

    
81
    (errcode, msg) = vci(self.FAKEFILE, False, 1266507600, 1267544400,
82
                         1266939600, warn_days=30, error_days=1)
83
    self.assertEqual(errcode, cmdlib.LUVerifyCluster.ETYPE_WARNING)
84

    
85
    (errcode, msg) = vci(self.FAKEFILE, False, 1266507600, None,
86
                         1266939600, warn_days=30, error_days=7)
87
    self.assertEqual(errcode, None)
88

    
89
    # Expired
90
    (errcode, msg) = vci(self.FAKEFILE, True, 1266507600, 1267544400,
91
                         1266939600, warn_days=30, error_days=7)
92
    self.assertEqual(errcode, cmdlib.LUVerifyCluster.ETYPE_ERROR)
93

    
94
    (errcode, msg) = vci(self.FAKEFILE, True, None, 1267544400,
95
                         1266939600, warn_days=30, error_days=7)
96
    self.assertEqual(errcode, cmdlib.LUVerifyCluster.ETYPE_ERROR)
97

    
98
    (errcode, msg) = vci(self.FAKEFILE, True, 1266507600, None,
99
                         1266939600, warn_days=30, error_days=7)
100
    self.assertEqual(errcode, cmdlib.LUVerifyCluster.ETYPE_ERROR)
101

    
102
    (errcode, msg) = vci(self.FAKEFILE, True, None, None,
103
                         1266939600, warn_days=30, error_days=7)
104
    self.assertEqual(errcode, cmdlib.LUVerifyCluster.ETYPE_ERROR)
105

    
106

    
107
if __name__ == "__main__":
108
  testutils.GanetiTestProgram()