Fixed reading topology file with newline at the end

Closes-bug: #1271776

Change-Id: I95ea0d785b294a179daad124e0073a8c3cc6a8ee
This commit is contained in:
Andrew Lazarev 2014-01-22 16:10:59 -08:00
parent 2c93c8ec07
commit 21641ee353
2 changed files with 59 additions and 0 deletions

View File

@ -13,7 +13,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import tempfile
import mock
from oslo.config import cfg
import unittest2
from savanna.conductor import objects as o
@ -137,3 +140,53 @@ class TopologyTestCase(unittest2.TestCase):
"0.0.1.3": "/r2/o2",
"s1": "/r1"
})
def _read_swift_topology(self, content):
temp_file = tempfile.NamedTemporaryFile()
try:
temp_file.write(content)
temp_file.flush()
cfg.CONF.set_override("swift_topology_file", temp_file.name)
return th._read_swift_topology()
finally:
cfg.CONF.clear_override("swift_topology_file")
temp_file.close()
def test_read_swift_topology(self):
topology = self._read_swift_topology("")
self.assertEqual(topology, {})
topology = self._read_swift_topology(
"192.168.1.1 /rack1\n192.168.1.2 /rack2")
self.assertEqual(
topology, {"192.168.1.1": "/rack1", "192.168.1.2": "/rack2"})
topology = self._read_swift_topology(
"192.168.1.1 /rack1\n192.168.1.2 /rack2\n\n")
self.assertEqual(
topology, {"192.168.1.1": "/rack1", "192.168.1.2": "/rack2"})
def _read_compute_topology(self, content):
temp_file = tempfile.NamedTemporaryFile()
try:
temp_file.write(content)
temp_file.flush()
cfg.CONF.set_override("compute_topology_file", temp_file.name)
return th._read_compute_topology()
finally:
cfg.CONF.clear_override("compute_topology_file")
temp_file.close()
def test_read_compute_topology(self):
topology = self._read_swift_topology("")
self.assertEqual(topology, {})
topology = self._read_swift_topology(
"192.168.1.1 /rack1\n192.168.1.2 /rack2")
self.assertEqual(len(topology), 2)
topology = self._read_swift_topology(
"192.168.1.1 /rack1\n192.168.1.2 /rack2\n\n")
self.assertEqual(len(topology), 2)

View File

@ -72,6 +72,9 @@ def _read_swift_topology():
try:
with open(CONF.swift_topology_file) as f:
for line in f:
line = line.strip()
if not line:
continue
(host, path) = line.split()
topology[host] = path
except IOError:
@ -88,6 +91,9 @@ def _read_compute_topology():
try:
with open(CONF.compute_topology_file) as f:
for line in f:
line = line.strip()
if not line:
continue
(host, path) = line.split()
#calulating host id based on tenant id and host
#using the same algorithm as in nova