Merge branch 'stable-2.7' into stable-2.8
[ganeti-local] / test / py / ganeti.jstore_unittest.py
1 #!/usr/bin/python
2 #
3
4 # Copyright (C) 2012 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 # 02110-1301, USA.
20
21
22 """Script for testing ganeti.jstore"""
23
24 import re
25 import unittest
26 import random
27
28 from ganeti import constants
29 from ganeti import utils
30 from ganeti import compat
31 from ganeti import errors
32 from ganeti import jstore
33
34 import testutils
35
36
37 class TestFormatJobID(testutils.GanetiTestCase):
38   def test(self):
39     self.assertEqual(jstore.FormatJobID(0), 0)
40     self.assertEqual(jstore.FormatJobID(30498), 30498)
41     self.assertEqual(jstore.FormatJobID(319472592764518609),
42                      319472592764518609)
43
44   def testErrors(self):
45     for i in [-1, -2288, -9667, -0.205641, 0.0, 0.1, 13041.4472, "", "Hello",
46               [], [1], {}]:
47       self.assertRaises(errors.ProgrammerError, jstore.FormatJobID, i)
48
49
50 class TestGetArchiveDirectory(testutils.GanetiTestCase):
51   def test(self):
52     tests = [
53       ("0", [0, 1, 3343, 9712, 9999]),
54       ("1", [10000, 13188, 19999]),
55       ("29", [290000, 296041, 298796, 299999]),
56       ("30", [300000, 309384]),
57       ]
58
59     for (exp, job_ids) in tests:
60       for job_id in job_ids:
61         fmt_id = jstore.FormatJobID(job_id)
62         self.assertEqual(jstore.GetArchiveDirectory(fmt_id), exp)
63         self.assertEqual(jstore.ParseJobId(fmt_id), job_id)
64
65   def testErrors(self):
66     self.assertRaises(errors.ParameterError, jstore.GetArchiveDirectory, None)
67     self.assertRaises(errors.ParameterError, jstore.GetArchiveDirectory, "foo")
68
69
70 class TestParseJobId(testutils.GanetiTestCase):
71   def test(self):
72     self.assertEqual(jstore.ParseJobId(29981), 29981)
73     self.assertEqual(jstore.ParseJobId("12918"), 12918)
74
75   def testErrors(self):
76     self.assertRaises(errors.ParameterError, jstore.ParseJobId, "")
77     self.assertRaises(errors.ParameterError, jstore.ParseJobId, "MXXI")
78     self.assertRaises(errors.ParameterError, jstore.ParseJobId, [])
79
80
81 class TestReadNumericFile(testutils.GanetiTestCase):
82   def testNonExistingFile(self):
83     result = jstore._ReadNumericFile("/tmp/this/file/does/not/exist")
84     self.assertTrue(result is None)
85
86   def testValidFile(self):
87     tmpfile = self._CreateTempFile()
88
89     for (data, exp) in [("123", 123), ("0\n", 0)]:
90       utils.WriteFile(tmpfile, data=data)
91       result = jstore._ReadNumericFile(tmpfile)
92       self.assertEqual(result, exp)
93
94   def testInvalidContent(self):
95     tmpfile = self._CreateTempFile()
96     utils.WriteFile(tmpfile, data="{wrong content")
97     self.assertRaises(errors.JobQueueError, jstore._ReadNumericFile, tmpfile)
98
99
100 if __name__ == "__main__":
101   testutils.GanetiTestProgram()