Module: check_mk
Branch: master
Commit: aeaeefb9742c87d6b23dc4bb7565de47e8167582
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=aeaeefb9742c87…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Thu Feb 7 09:00:05 2019 +0100
7082 FIX Fixed "omd create" when fstab has 0 bytes
The "omd create" command failed with a "IndexError: string index out of
range"
in case a /etc/fstab file existed but was empty. While regular systems most likely
have entries in there, this can often happen for containers.
CMK-1636
Change-Id: I8acc7921276801a3f2f4e132f96fd66e54300aee
---
.werks/7082 | 13 +++++++++
omd/packages/omd/omdlib/main.py | 14 +++++++---
tests/unit/omdlib/test_omdlib_main.py | 50 +++++++++++++++++++++++++++++++++++
3 files changed, 73 insertions(+), 4 deletions(-)
diff --git a/.werks/7082 b/.werks/7082
new file mode 100644
index 0000000..b42816b
--- /dev/null
+++ b/.werks/7082
@@ -0,0 +1,13 @@
+Title: Fixed "omd create" when fstab has 0 bytes
+Level: 1
+Component: omd
+Class: fix
+Compatible: compat
+Edition: cre
+State: unknown
+Version: 1.6.0i1
+Date: 1549523521
+
+The "omd create" command failed with a "IndexError: string index out of
range"
+in case a /etc/fstab file existed but was empty. While regular systems most likely
+have entries in there, this can often happen for containers.
diff --git a/omd/packages/omd/omdlib/main.py b/omd/packages/omd/omdlib/main.py
index d152e63..6a2a97e 100644
--- a/omd/packages/omd/omdlib/main.py
+++ b/omd/packages/omd/omdlib/main.py
@@ -1681,13 +1681,18 @@ def unmount_tmpfs(site, output=True, kill=False):
return False
+# Extracted to separate function to be able to monkeypatch the path for tests
+def fstab_path():
+ return "/etc/fstab"
+
+
def add_to_fstab(site, tmpfs_size=None):
- if not os.path.exists("/etc/fstab"):
+ if not os.path.exists(fstab_path()):
return # Don't do anything in case there is no fstab
# tmpfs /opt/omd/sites/b01/tmp tmpfs user,uid=b01,gid=b01 0 0
mountpoint = "/opt" + site.tmp_dir
- sys.stdout.write("Adding %s to /etc/fstab.\n" % mountpoint)
+ sys.stdout.write("Adding %s to %s.\n" % (mountpoint, fstab_path()))
# No size option: using up to 50% of the RAM
sizespec = ''
@@ -1695,9 +1700,10 @@ def add_to_fstab(site, tmpfs_size=None):
sizespec = ',size=%s' % tmpfs_size
# Ensure the fstab has a newline char at it's end before appending
- complete_last_line = file("/etc/fstab").read()[-1] != "\n"
+ previous_fstab = file(fstab_path()).read()
+ complete_last_line = previous_fstab and not previous_fstab.endswith("\n")
- with file("/etc/fstab", "a+") as fstab:
+ with file(fstab_path(), "a+") as fstab:
if complete_last_line:
fstab.write("\n")
diff --git a/tests/unit/omdlib/test_omdlib_main.py
b/tests/unit/omdlib/test_omdlib_main.py
index db517a8..f779b9b 100644
--- a/tests/unit/omdlib/test_omdlib_main.py
+++ b/tests/unit/omdlib/test_omdlib_main.py
@@ -1,3 +1,4 @@
+import pytest
from pathlib2 import Path
import omdlib.main
@@ -24,3 +25,52 @@ def test_initialize_site_ca(monkeypatch, tmpdir):
omdlib.main.initialize_site_ca(omdlib.main.SiteContext(site_id))
assert (ca_path / "ca.pem").exists()
assert (ca_path / "sites" / ("%s.pem" % site_id)).exists()
+
+
+(a)pytest.fixture()
+def site_context():
+ return omdlib.main.SiteContext("unit")
+
+
+(a)pytest.fixture()
+def tmp_fstab(tmp_path, monkeypatch):
+ fstab_path = tmp_path / "fstab"
+ monkeypatch.setattr(omdlib.main, "fstab_path", lambda: str(fstab_path))
+ return fstab_path
+
+
+def test_add_to_fstab_not_existing(tmp_fstab, site_context):
+ assert not tmp_fstab.exists()
+ omdlib.main.add_to_fstab(site_context)
+ assert not tmp_fstab.exists()
+
+
+def test_add_to_fstab(tmp_fstab, site_context):
+ tmp_fstab.open("w", encoding="utf-8").write(u"# system fstab
bla\n")
+ omdlib.main.add_to_fstab(site_context)
+ assert tmp_fstab.open().read() == (
+ "# system fstab bla\n"
+ "tmpfs /opt/omd/sites/unit/tmp tmpfs noauto,user,mode=755,uid=unit,gid=unit
0 0\n")
+
+
+def test_add_to_fstab_with_size(tmp_fstab, site_context):
+ tmp_fstab.open("w", encoding="utf-8").write(u"# system fstab
bla\n")
+ omdlib.main.add_to_fstab(site_context, tmpfs_size="1G")
+ assert tmp_fstab.open().read() == (
+ "# system fstab bla\n"
+ "tmpfs /opt/omd/sites/unit/tmp tmpfs
noauto,user,mode=755,uid=unit,gid=unit,size=1G 0 0\n")
+
+
+def test_add_to_fstab_no_newline_at_end(tmp_fstab, site_context):
+ tmp_fstab.open("w", encoding="utf-8").write(u"# system fstab
bla")
+ omdlib.main.add_to_fstab(site_context)
+ assert tmp_fstab.open().read() == (
+ "# system fstab bla\n"
+ "tmpfs /opt/omd/sites/unit/tmp tmpfs noauto,user,mode=755,uid=unit,gid=unit
0 0\n")
+
+
+def test_add_to_fstab_empty(tmp_fstab, site_context):
+ tmp_fstab.open("w", encoding="utf-8").write(u"")
+ omdlib.main.add_to_fstab(site_context)
+ assert tmp_fstab.open().read() == (
+ "tmpfs /opt/omd/sites/unit/tmp tmpfs noauto,user,mode=755,uid=unit,gid=unit
0 0\n")