Permalink
Browse files

conf: fix right permissions of generated file to 644

  • Loading branch information...
1 parent 5f18526 commit 4220c7c476358f295fb1774ad0e95d1d05fd5280 @vincentbernat vincentbernat committed with vincentbernat Feb 22, 2013
Showing with 25 additions and 1 deletion.
  1. +21 −0 tests/test_conf.py
  2. +4 −1 zkfarmer/conf.py
View
@@ -5,6 +5,7 @@
import yaml
import os
import sys
+import stat
from mock import patch
from zkfarmer import conf
@@ -98,6 +99,26 @@ def test_json_no_overwrite_on_failure(self):
self.assertRaises(TypeError, a.write, json)
self.assertEqual(a.read(), {"1": "2"})
+ def test_json_appropriate_rights(self):
+ """Check if a file is created with the appropriate rights"""
+ os.umask(022)
+ name = "%s/test.json" % self.tmpdir
+ a = conf.Conf(name)
+ a.write({"1": "2"})
+ del a
+ a = os.stat(name)
+ self.assertEqual(a.st_mode & 0777, 0644)
+
+ def test_json_appropriate_rights_umask(self):
+ """Check if a file is created with appropriate rights using non standard umask."""
+ os.umask(027)
+ name = "%s/test.json" % self.tmpdir
+ a = conf.Conf(name)
+ a.write({"1": "2"})
+ del a
+ a = os.stat(name)
+ self.assertEqual(a.st_mode & 0777, 0640)
+
class TestConfYAML(TempDirectoryTestCase):
def test_yaml_write_from_extension(self):
View
@@ -73,12 +73,15 @@ def open(self, write=False):
return
tmp, tmpname = tempfile.mkstemp(dir=os.path.dirname(os.path.abspath(self.file_path)))
try:
+ current_umask = os.umask(0)
+ os.umask(current_umask)
+ os.chmod(tmpname, 0666 & ~current_umask)
f = os.fdopen(tmp, "w")
yield f
f.close()
os.rename(tmpname, self.file_path)
except:
- os.unlink(tmp)
+ os.unlink(tmpname)
raise
class ConfJSON(ConfFile):

0 comments on commit 4220c7c

Please sign in to comment.