<s> """<STR_LIT>""" <EOL> import os <EOL> import tempfile <EOL> import shutil <EOL> import errno <EOL> import grp <EOL> import pwd <EOL> import stat <EOL> from ganeti import constants <EOL> from ganeti import utils <EOL> from ganeti import compat <EOL> from ganeti import errors <EOL> import testutils <EOL> class TestWriteFile ( testutils . GanetiTestCase ) : <EOL> def setUp ( self ) : <EOL> testutils . GanetiTestCase . setUp ( self ) <EOL> self . tmpdir = None <EOL> self . tfile = tempfile . NamedTemporaryFile ( ) <EOL> self . did_pre = False <EOL> self . did_post = False <EOL> self . did_write = False <EOL> def tearDown ( self ) : <EOL> testutils . GanetiTestCase . tearDown ( self ) <EOL> if self . tmpdir : <EOL> shutil . rmtree ( self . tmpdir ) <EOL> def testFileUid ( self ) : <EOL> self . tmpdir = tempfile . mkdtemp ( ) <EOL> target = utils . PathJoin ( self . tmpdir , "<STR_LIT:target>" ) <EOL> tuid = os . geteuid ( ) + <NUM_LIT:1> <EOL> utils . WriteFile ( target , data = "<STR_LIT:data>" , uid = tuid + <NUM_LIT:1> ) <EOL> self . assertFileUid ( target , tuid + <NUM_LIT:1> ) <EOL> utils . WriteFile ( target , data = "<STR_LIT:data>" , uid = tuid ) <EOL> self . assertFileUid ( target , tuid ) <EOL> utils . WriteFile ( target , data = "<STR_LIT:data>" , uid = tuid + <NUM_LIT:1> , <EOL> keep_perms = utils . KP_IF_EXISTS ) <EOL> self . assertFileUid ( target , tuid ) <EOL> utils . WriteFile ( target , data = "<STR_LIT:data>" , keep_perms = utils . KP_ALWAYS ) <EOL> self . assertFileUid ( target , tuid ) <EOL> def testNewFileUid ( self ) : <EOL> self . tmpdir = tempfile . mkdtemp ( ) <EOL> target = utils . PathJoin ( self . tmpdir , "<STR_LIT:target>" ) <EOL> tuid = os . geteuid ( ) + <NUM_LIT:1> <EOL> utils . WriteFile ( target , data = "<STR_LIT:data>" , uid = tuid , <EOL> keep_perms = utils . KP_IF_EXISTS ) <EOL> self . assertFileUid ( target , tuid ) <EOL> def testFileGid ( self ) : <EOL> self . tmpdir = tempfile . mkdtemp ( ) <EOL> target = utils . PathJoin ( self . tmpdir , "<STR_LIT:target>" ) <EOL> tgid = os . getegid ( ) + <NUM_LIT:1> <EOL> utils . WriteFile ( target , data = "<STR_LIT:data>" , gid = tgid + <NUM_LIT:1> ) <EOL> self . assertFileGid ( target , tgid + <NUM_LIT:1> ) <EOL> utils . WriteFile ( target , data = "<STR_LIT:data>" , gid = tgid ) <EOL> self . assertFileGid ( target , tgid ) <EOL> utils . WriteFile ( target , data = "<STR_LIT:data>" , gid = tgid + <NUM_LIT:1> , <EOL> keep_perms = utils . KP_IF_EXISTS ) <EOL> self . assertFileGid ( target , tgid ) <EOL> utils . WriteFile ( target , data = "<STR_LIT:data>" , keep_perms = utils . KP_ALWAYS ) <EOL> self . assertFileGid ( target , tgid ) <EOL> def testNewFileGid ( self ) : <EOL> self . tmpdir = tempfile . mkdtemp ( ) <EOL> target = utils . PathJoin ( self . tmpdir , "<STR_LIT:target>" ) <EOL> tgid = os . getegid ( ) + <NUM_LIT:1> <EOL> utils . WriteFile ( target , data = "<STR_LIT:data>" , gid = tgid , <EOL> keep_perms = utils . KP_IF_EXISTS ) <EOL> self . assertFileGid ( target , tgid ) <EOL> class TestCanRead ( testutils . GanetiTestCase ) : <EOL> def setUp ( self ) : <EOL> testutils . GanetiTestCase . setUp ( self ) <EOL> self . tmpdir = tempfile . mkdtemp ( ) <EOL> self . confdUid = pwd . getpwnam ( constants . CONFD_USER ) . pw_uid <EOL> self . masterdUid = pwd . getpwnam ( constants . MASTERD_USER ) . pw_uid <EOL> self . masterdGid = grp . getgrnam ( constants . MASTERD_GROUP ) . gr_gid <EOL> def tearDown ( self ) : <EOL> testutils . GanetiTestCase . tearDown ( self ) <EOL> if self . tmpdir : <EOL> shutil . rmtree ( self . tmpdir ) <EOL> def testUserCanRead ( self ) : <EOL> target = utils . PathJoin ( self . tmpdir , "<STR_LIT>" ) <EOL> f = open ( target , "<STR_LIT:w>" ) <EOL> f . close ( ) <EOL> utils . EnforcePermission ( target , <NUM_LIT:0> <NUM_LIT> , uid = self . confdUid , <EOL> gid = self . masterdGid ) <EOL> self . assertTrue ( utils . CanRead ( constants . CONFD_USER , target ) ) <EOL> if constants . CONFD_USER != constants . MASTERD_USER : <EOL> self . assertFalse ( utils . CanRead ( constants . MASTERD_USER , target ) ) <EOL> def testGroupCanRead ( self ) : <EOL> target = utils . PathJoin ( self . tmpdir , "<STR_LIT>" ) <EOL> f = open ( target , "<STR_LIT:w>" ) <EOL> f . close ( ) <EOL> utils . EnforcePermission ( target , <NUM_LIT> <NUM_LIT> , uid = self . confdUid , <EOL> gid = self . masterdGid ) <EOL> self . assertFalse ( utils . CanRead ( constants . CONFD_USER , target ) ) <EOL> if constants . CONFD_USER != constants . MASTERD_USER : <EOL> self . assertTrue ( utils . CanRead ( constants . MASTERD_USER , target ) ) <EOL> utils . EnforcePermission ( target , <NUM_LIT> <NUM_LIT> , uid = self . masterdUid + <NUM_LIT:1> , <EOL> gid = self . masterdGid ) <EOL> self . assertTrue ( utils . CanRead ( constants . MASTERD_USER , target ) ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> testutils . GanetiTestProgram ( ) </s>
