Revision f3ebe73e test/py/ganeti.storage.bdev_unittest.py

b/test/py/ganeti.storage.bdev_unittest.py
104 104
    self.assertRaises(errors.BlockDeviceError, parse_function,
105 105
                      self.output_invalid, self.volume_name)
106 106

  
107
class TestComputeWrongFileStoragePathsInternal(unittest.TestCase):
108
  def testPaths(self):
109
    paths = bdev._GetForbiddenFileStoragePaths()
110

  
111
    for path in ["/bin", "/usr/local/sbin", "/lib64", "/etc", "/sys"]:
112
      self.assertTrue(path in paths)
113

  
114
    self.assertEqual(set(map(os.path.normpath, paths)), paths)
115

  
116
  def test(self):
117
    vfsp = bdev._ComputeWrongFileStoragePaths
118
    self.assertEqual(vfsp([]), [])
119
    self.assertEqual(vfsp(["/tmp"]), [])
120
    self.assertEqual(vfsp(["/bin/ls"]), ["/bin/ls"])
121
    self.assertEqual(vfsp(["/bin"]), ["/bin"])
122
    self.assertEqual(vfsp(["/usr/sbin/vim", "/srv/file-storage"]),
123
                     ["/usr/sbin/vim"])
124

  
125

  
126
class TestComputeWrongFileStoragePaths(testutils.GanetiTestCase):
127
  def test(self):
128
    tmpfile = self._CreateTempFile()
129

  
130
    utils.WriteFile(tmpfile, data="""
131
      /tmp
132
      x/y///z/relative
133
      # This is a test file
134
      /srv/storage
135
      /bin
136
      /usr/local/lib32/
137
      relative/path
138
      """)
139

  
140
    self.assertEqual(bdev.ComputeWrongFileStoragePaths(_filename=tmpfile), [
141
      "/bin",
142
      "/usr/local/lib32",
143
      "relative/path",
144
      "x/y/z/relative",
145
      ])
146

  
147

  
148
class TestCheckFileStoragePathInternal(unittest.TestCase):
149
  def testNonAbsolute(self):
150
    for i in ["", "tmp", "foo/bar/baz"]:
151
      self.assertRaises(errors.FileStoragePathError,
152
                        bdev._CheckFileStoragePath, i, ["/tmp"])
153

  
154
    self.assertRaises(errors.FileStoragePathError,
155
                      bdev._CheckFileStoragePath, "/tmp", ["tmp", "xyz"])
156

  
157
  def testNoAllowed(self):
158
    self.assertRaises(errors.FileStoragePathError,
159
                      bdev._CheckFileStoragePath, "/tmp", [])
160

  
161
  def testNoAdditionalPathComponent(self):
162
    self.assertRaises(errors.FileStoragePathError,
163
                      bdev._CheckFileStoragePath, "/tmp/foo", ["/tmp/foo"])
164

  
165
  def testAllowed(self):
166
    bdev._CheckFileStoragePath("/tmp/foo/a", ["/tmp/foo"])
167
    bdev._CheckFileStoragePath("/tmp/foo/a/x", ["/tmp/foo"])
168

  
169

  
170
class TestCheckFileStoragePath(testutils.GanetiTestCase):
171
  def testNonExistantFile(self):
172
    filename = "/tmp/this/file/does/not/exist"
173
    assert not os.path.exists(filename)
174
    self.assertRaises(errors.FileStoragePathError,
175
                      bdev.CheckFileStoragePath, "/bin/", _filename=filename)
176
    self.assertRaises(errors.FileStoragePathError,
177
                      bdev.CheckFileStoragePath, "/srv/file-storage",
178
                      _filename=filename)
179

  
180
  def testAllowedPath(self):
181
    tmpfile = self._CreateTempFile()
182

  
183
    utils.WriteFile(tmpfile, data="""
184
      /srv/storage
185
      """)
186

  
187
    bdev.CheckFileStoragePath("/srv/storage/inst1", _filename=tmpfile)
188

  
189
    # No additional path component
190
    self.assertRaises(errors.FileStoragePathError,
191
                      bdev.CheckFileStoragePath, "/srv/storage",
192
                      _filename=tmpfile)
193

  
194
    # Forbidden path
195
    self.assertRaises(errors.FileStoragePathError,
196
                      bdev.CheckFileStoragePath, "/usr/lib64/xyz",
197
                      _filename=tmpfile)
198

  
199

  
200
class TestLoadAllowedFileStoragePaths(testutils.GanetiTestCase):
201
  def testDevNull(self):
202
    self.assertEqual(bdev._LoadAllowedFileStoragePaths("/dev/null"), [])
203

  
204
  def testNonExistantFile(self):
205
    filename = "/tmp/this/file/does/not/exist"
206
    assert not os.path.exists(filename)
207
    self.assertEqual(bdev._LoadAllowedFileStoragePaths(filename), [])
208

  
209
  def test(self):
210
    tmpfile = self._CreateTempFile()
211

  
212
    utils.WriteFile(tmpfile, data="""
213
      # This is a test file
214
      /tmp
215
      /srv/storage
216
      relative/path
217
      """)
218

  
219
    self.assertEqual(bdev._LoadAllowedFileStoragePaths(tmpfile), [
220
      "/tmp",
221
      "/srv/storage",
222
      "relative/path",
223
      ])
224

  
225

  
226 107
class TestExclusiveStoragePvs(unittest.TestCase):
227 108
  """Test cases for functions dealing with LVM PV and exclusive storage"""
228 109
  # Allowance for rounding

Also available in: Unified diff