Revision 23e3c9b7 test/ganeti.bdev_unittest.py

b/test/ganeti.bdev_unittest.py
373 373
                      output_extra_matches, volume_name)
374 374

  
375 375

  
376
class TestCheckFileStoragePath(unittest.TestCase):
376
class TestComputeWrongFileStoragePathsInternal(unittest.TestCase):
377
  def testPaths(self):
378
    paths = bdev._GetForbiddenFileStoragePaths()
379

  
380
    for path in ["/bin", "/usr/local/sbin", "/lib64", "/etc", "/sys"]:
381
      self.assertTrue(path in paths)
382

  
383
    self.assertEqual(set(map(os.path.normpath, paths)), paths)
384

  
385
  def test(self):
386
    vfsp = bdev._ComputeWrongFileStoragePaths
387
    self.assertEqual(vfsp([]), [])
388
    self.assertEqual(vfsp(["/tmp"]), [])
389
    self.assertEqual(vfsp(["/bin/ls"]), ["/bin/ls"])
390
    self.assertEqual(vfsp(["/bin"]), ["/bin"])
391
    self.assertEqual(vfsp(["/usr/sbin/vim", "/srv/file-storage"]),
392
                     ["/usr/sbin/vim"])
393

  
394

  
395
class TestComputeWrongFileStoragePaths(testutils.GanetiTestCase):
396
  def test(self):
397
    tmpfile = self._CreateTempFile()
398

  
399
    utils.WriteFile(tmpfile, data="""
400
      /tmp
401
      x/y///z/relative
402
      # This is a test file
403
      /srv/storage
404
      /bin
405
      /usr/local/lib32/
406
      relative/path
407
      """)
408

  
409
    self.assertEqual(bdev.ComputeWrongFileStoragePaths(_filename=tmpfile), [
410
      "/bin",
411
      "/usr/local/lib32",
412
      "relative/path",
413
      "x/y/z/relative",
414
      ])
415

  
416

  
417
class TestCheckFileStoragePathInternal(unittest.TestCase):
377 418
  def testNonAbsolute(self):
378 419
    for i in ["", "tmp", "foo/bar/baz"]:
379 420
      self.assertRaises(errors.FileStoragePathError,
......
395 436
    bdev._CheckFileStoragePath("/tmp/foo/a/x", ["/tmp/foo"])
396 437

  
397 438

  
439
class TestCheckFileStoragePath(testutils.GanetiTestCase):
440
  def testNonExistantFile(self):
441
    filename = "/tmp/this/file/does/not/exist"
442
    assert not os.path.exists(filename)
443
    self.assertRaises(errors.FileStoragePathError,
444
                      bdev.CheckFileStoragePath, "/bin/", _filename=filename)
445
    self.assertRaises(errors.FileStoragePathError,
446
                      bdev.CheckFileStoragePath, "/srv/file-storage",
447
                      _filename=filename)
448

  
449
  def testAllowedPath(self):
450
    tmpfile = self._CreateTempFile()
451

  
452
    utils.WriteFile(tmpfile, data="""
453
      /srv/storage
454
      """)
455

  
456
    bdev.CheckFileStoragePath("/srv/storage/inst1", _filename=tmpfile)
457

  
458
    # No additional path component
459
    self.assertRaises(errors.FileStoragePathError,
460
                      bdev.CheckFileStoragePath, "/srv/storage",
461
                      _filename=tmpfile)
462

  
463
    # Forbidden path
464
    self.assertRaises(errors.FileStoragePathError,
465
                      bdev.CheckFileStoragePath, "/usr/lib64/xyz",
466
                      _filename=tmpfile)
467

  
468

  
398 469
class TestLoadAllowedFileStoragePaths(testutils.GanetiTestCase):
399 470
  def testDevNull(self):
400
    self.assertEqual(bdev.LoadAllowedFileStoragePaths("/dev/null"), [])
471
    self.assertEqual(bdev._LoadAllowedFileStoragePaths("/dev/null"), [])
401 472

  
402 473
  def testNonExistantFile(self):
403 474
    filename = "/tmp/this/file/does/not/exist"
404 475
    assert not os.path.exists(filename)
405
    self.assertEqual(bdev.LoadAllowedFileStoragePaths(filename), [])
476
    self.assertEqual(bdev._LoadAllowedFileStoragePaths(filename), [])
406 477

  
407 478
  def test(self):
408 479
    tmpfile = self._CreateTempFile()
......
414 485
      relative/path
415 486
      """)
416 487

  
417
    self.assertEqual(bdev.LoadAllowedFileStoragePaths(tmpfile), [
488
    self.assertEqual(bdev._LoadAllowedFileStoragePaths(tmpfile), [
418 489
      "/tmp",
419 490
      "/srv/storage",
420 491
      "relative/path",

Also available in: Unified diff