Revision 3039e2dc test/py/ganeti.bootstrap_unittest.py

b/test/py/ganeti.bootstrap_unittest.py
28 28
from ganeti import bootstrap
29 29
from ganeti import constants
30 30
from ganeti import errors
31
from ganeti import pathutils
31 32

  
32 33
import testutils
34
import mock
33 35

  
34 36

  
35 37
class TestPrepareFileStorage(unittest.TestCase):
36 38
  def setUp(self):
39
    unittest.TestCase.setUp(self)
37 40
    self.tmpdir = tempfile.mkdtemp()
38 41

  
39 42
  def tearDown(self):
40 43
    shutil.rmtree(self.tmpdir)
41 44

  
42
  def testFileStorageEnabled(self):
43
    enabled_disk_templates = [constants.DT_FILE]
45
  def enableFileStorage(self, enable):
46
    self.enabled_disk_templates = []
47
    if enable:
48
      self.enabled_disk_templates.append(constants.DT_FILE)
49
    else:
50
      # anything != DT_FILE would do here
51
      self.enabled_disk_templates.append(constants.DT_DISKLESS)
52

  
53
  def testFallBackToDefaultPathAcceptedFileStorageEnabled(self):
54
    expected_file_storage_dir = pathutils.DEFAULT_FILE_STORAGE_DIR
55
    acceptance_fn = mock.Mock()
56
    init_fn = mock.Mock(return_value=expected_file_storage_dir)
57
    self.enableFileStorage(True)
44 58
    file_storage_dir = bootstrap._PrepareFileStorage(
45
        enabled_disk_templates, self.tmpdir)
59
        self.enabled_disk_templates, None, acceptance_fn=acceptance_fn,
60
        init_fn=init_fn)
61
    self.assertEqual(expected_file_storage_dir, file_storage_dir)
62
    acceptance_fn.assert_called_with(expected_file_storage_dir)
63
    init_fn.assert_called_with(expected_file_storage_dir)
64

  
65
  def testPathAcceptedFileStorageEnabled(self):
66
    acceptance_fn = mock.Mock()
67
    init_fn = mock.Mock(return_value=self.tmpdir)
68
    self.enableFileStorage(True)
69
    file_storage_dir = bootstrap._PrepareFileStorage(
70
        self.enabled_disk_templates, self.tmpdir, acceptance_fn=acceptance_fn,
71
        init_fn=init_fn)
46 72
    self.assertEqual(self.tmpdir, file_storage_dir)
73
    acceptance_fn.assert_called_with(self.tmpdir)
74
    init_fn.assert_called_with(self.tmpdir)
47 75

  
48
  def testFileStorageDisabled(self):
49
    # anything != DT_FILE would do here
50
    enabled_disk_templates = [constants.DT_PLAIN]
76
  def testPathAcceptedFileStorageDisabled(self):
77
    acceptance_fn = mock.Mock()
78
    init_fn = mock.Mock()
79
    self.enableFileStorage(False)
51 80
    file_storage_dir = bootstrap._PrepareFileStorage(
52
        enabled_disk_templates, self.tmpdir)
53
    self.assertEqual('', file_storage_dir)
81
        self.enabled_disk_templates, self.tmpdir, acceptance_fn=acceptance_fn,
82
        init_fn=init_fn)
83
    self.assertEqual(self.tmpdir, file_storage_dir)
84
    self.assertFalse(init_fn.called)
85
    self.assertFalse(acceptance_fn.called)
86

  
87
  def testPathNotAccepted(self):
88
    acceptance_fn = mock.Mock()
89
    acceptance_fn.side_effect = errors.FileStoragePathError
90
    init_fn = mock.Mock()
91
    self.enableFileStorage(True)
92
    self.assertRaises(errors.OpPrereqError, bootstrap._PrepareFileStorage,
93
        self.enabled_disk_templates, self.tmpdir, acceptance_fn=acceptance_fn,
94
        init_fn=init_fn)
95
    acceptance_fn.assert_called_with(self.tmpdir)
54 96

  
55 97

  
56 98
class TestInitCheckEnabledDiskTemplates(unittest.TestCase):

Also available in: Unified diff