Revision f3ebe73e

b/lib/storage/filestorage.py
167 167

  
168 168
  """
169 169
  allowed = _LoadAllowedFileStoragePaths(_filename)
170
  if not allowed:
171
    raise errors.FileStoragePathError("No paths are valid or path file '%s'"
172
                                      " was not accessible." % _filename)
170 173

  
171 174
  if _ComputeWrongFileStoragePaths([path]):
172 175
    raise errors.FileStoragePathError("Path '%s' uses a forbidden prefix" %
......
185 188

  
186 189
  """
187 190
  if not os.path.isdir(path):
188
    raise errors.FileStoragePathError("Path '%s' is not exisiting or not a"
191
    raise errors.FileStoragePathError("Path '%s' is not existing or not a"
189 192
                                      " directory." % path)
190 193
  if not os.access(path, os.W_OK):
191 194
    raise errors.FileStoragePathError("Path '%s' is not writable" % path)
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
b/test/py/ganeti.storage.filestorage_unittest.py
28 28

  
29 29
from ganeti import errors
30 30
from ganeti.storage import filestorage
31
from ganeti import utils
31 32

  
32 33
import testutils
33 34

  
......
95 96
    self.assertTrue("not acceptable" in result)
96 97

  
97 98

  
99
class TestLoadAllowedFileStoragePaths(testutils.GanetiTestCase):
100
  def testDevNull(self):
101
    self.assertEqual(filestorage._LoadAllowedFileStoragePaths("/dev/null"), [])
102

  
103
  def testNonExistantFile(self):
104
    filename = "/tmp/this/file/does/not/exist"
105
    assert not os.path.exists(filename)
106
    self.assertEqual(filestorage._LoadAllowedFileStoragePaths(filename), [])
107

  
108
  def test(self):
109
    tmpfile = self._CreateTempFile()
110

  
111
    utils.WriteFile(tmpfile, data="""
112
      # This is a test file
113
      /tmp
114
      /srv/storage
115
      relative/path
116
      """)
117

  
118
    self.assertEqual(filestorage._LoadAllowedFileStoragePaths(tmpfile), [
119
      "/tmp",
120
      "/srv/storage",
121
      "relative/path",
122
      ])
123

  
124

  
125
class TestComputeWrongFileStoragePathsInternal(unittest.TestCase):
126
  def testPaths(self):
127
    paths = filestorage._GetForbiddenFileStoragePaths()
128

  
129
    for path in ["/bin", "/usr/local/sbin", "/lib64", "/etc", "/sys"]:
130
      self.assertTrue(path in paths)
131

  
132
    self.assertEqual(set(map(os.path.normpath, paths)), paths)
133

  
134
  def test(self):
135
    vfsp = filestorage._ComputeWrongFileStoragePaths
136
    self.assertEqual(vfsp([]), [])
137
    self.assertEqual(vfsp(["/tmp"]), [])
138
    self.assertEqual(vfsp(["/bin/ls"]), ["/bin/ls"])
139
    self.assertEqual(vfsp(["/bin"]), ["/bin"])
140
    self.assertEqual(vfsp(["/usr/sbin/vim", "/srv/file-storage"]),
141
                     ["/usr/sbin/vim"])
142

  
143

  
144
class TestComputeWrongFileStoragePaths(testutils.GanetiTestCase):
145
  def test(self):
146
    tmpfile = self._CreateTempFile()
147

  
148
    utils.WriteFile(tmpfile, data="""
149
      /tmp
150
      x/y///z/relative
151
      # This is a test file
152
      /srv/storage
153
      /bin
154
      /usr/local/lib32/
155
      relative/path
156
      """)
157

  
158
    self.assertEqual(
159
        filestorage.ComputeWrongFileStoragePaths(_filename=tmpfile),
160
        ["/bin",
161
         "/usr/local/lib32",
162
         "relative/path",
163
         "x/y/z/relative",
164
        ])
165

  
166

  
167
class TestCheckFileStoragePathInternal(unittest.TestCase):
168
  def testNonAbsolute(self):
169
    for i in ["", "tmp", "foo/bar/baz"]:
170
      self.assertRaises(errors.FileStoragePathError,
171
                        filestorage._CheckFileStoragePath, i, ["/tmp"])
172

  
173
    self.assertRaises(errors.FileStoragePathError,
174
                      filestorage._CheckFileStoragePath, "/tmp", ["tmp", "xyz"])
175

  
176
  def testNoAllowed(self):
177
    self.assertRaises(errors.FileStoragePathError,
178
                      filestorage._CheckFileStoragePath, "/tmp", [])
179

  
180
  def testNoAdditionalPathComponent(self):
181
    self.assertRaises(errors.FileStoragePathError,
182
                      filestorage._CheckFileStoragePath, "/tmp/foo",
183
                      ["/tmp/foo"])
184

  
185
  def testAllowed(self):
186
    filestorage._CheckFileStoragePath("/tmp/foo/a", ["/tmp/foo"])
187
    filestorage._CheckFileStoragePath("/tmp/foo/a/x", ["/tmp/foo"])
188

  
189

  
190
class TestCheckFileStoragePathExistance(testutils.GanetiTestCase):
191
  def testNonExistantFile(self):
192
    filename = "/tmp/this/file/does/not/exist"
193
    assert not os.path.exists(filename)
194
    self.assertRaises(errors.FileStoragePathError,
195
                      filestorage.CheckFileStoragePathAcceptance, "/bin/",
196
                      _filename=filename)
197
    self.assertRaises(errors.FileStoragePathError,
198
                      filestorage.CheckFileStoragePathAcceptance,
199
                      "/srv/file-storage", _filename=filename)
200

  
201
  def testAllowedPath(self):
202
    tmpfile = self._CreateTempFile()
203

  
204
    utils.WriteFile(tmpfile, data="""
205
      /srv/storage
206
      """)
207

  
208
    filestorage.CheckFileStoragePathAcceptance(
209
        "/srv/storage/inst1", _filename=tmpfile)
210

  
211
    # No additional path component
212
    self.assertRaises(errors.FileStoragePathError,
213
                      filestorage.CheckFileStoragePathAcceptance,
214
                      "/srv/storage", _filename=tmpfile)
215

  
216
    # Forbidden path
217
    self.assertRaises(errors.FileStoragePathError,
218
                      filestorage.CheckFileStoragePathAcceptance,
219
                      "/usr/lib64/xyz", _filename=tmpfile)
220

  
221

  
98 222
if __name__ == "__main__":
99 223
  testutils.GanetiTestProgram()

Also available in: Unified diff