Skip to content

Commit

Permalink
- test.util.test_cpio: make jenkins/different environments happy...
Browse files Browse the repository at this point in the history
It is not a good idea to hardcode uid and gid in a fixture
file if the testcases are executed in a different env and the
result depends on the euid, egid of the executing user...
The following hack rewrites the fixture files and inserts the euid,
egid of the executing user (this is really ugly but sufficient for
our needs)
  • Loading branch information
marcus-h committed Jun 24, 2012
1 parent a9d7925 commit c7cc60c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 13 deletions.
72 changes: 59 additions & 13 deletions test/util/test_cpio.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,54 @@ def suite():
return unittest.makeSuite(TestCpio)


# it is no good idea to hardcode uid and gid in a fixture
# file if the testcases are executed in a different env...
# the methods below are needed for copyin testcases
def uid(exp_uid):
"""Returns the used uid.
exp_uid is the expected uid.
"""
uid = os.geteuid()
if uid != exp_uid or uid != 0:
return uid
return exp_uid


def gid(exp_gid):
"""Returns the used gid.
exp_gid is the expected gid.
"""
gid = os.getegid()
if gid != exp_gid or gid != 0:
return gid
return exp_gid


class TestCpio(OscTest):
def __init__(self, *args, **kwargs):
kwargs['fixtures_dir'] = os.path.join('util', 'test_cpio_fixtures')
super(TestCpio, self).__init__(*args, **kwargs)

def replace_uid_gid(self, filename):
"""Sets uid and gid in cpio archive"""
# only needed in order to make jenkins happy
# additionally it is no good idea to hardcode uid and gid in a
# fixture file if the testcases are executed in a different env...
fname = self.fixture_file(filename)
fname_replaced = self.fixture_file(filename + '.replaced')
uid = '%08X' % os.geteuid()
gid = '%08X' % os.getegid()
with open(fname, 'r') as source:
with open(fname_replaced, 'w') as f:
data = source.read().replace('@UID@', uid)
data = data.replace('@GID@', gid)
f.write(data)
return filename + '.replaced'

def test1(self):
"""test FileWrapper with filename (no mmap)"""
fname = self.fixture_file('filewrapper1.txt')
Expand Down Expand Up @@ -458,8 +501,8 @@ def test15(self):
st = os.stat(fname)
self.assertEqual(st.st_mode, 33188)
self.assertEqual(st.st_mtime, 1340493596)
self.assertEqual(st.st_uid, 1000)
self.assertEqual(st.st_gid, 100)
self.assertEqual(st.st_uid, uid(1000))
self.assertEqual(st.st_gid, gid(100))
# copyin file foobar
archive_file = archive_reader.next_file()
archive_file.copyin(dest)
Expand All @@ -470,15 +513,16 @@ def test15(self):
st = os.stat(fname)
self.assertEqual(st.st_mode, 33188)
self.assertEqual(st.st_mtime, 1340493602)
self.assertEqual(st.st_uid, 1000)
self.assertEqual(st.st_gid, 100)
self.assertEqual(st.st_uid, uid(1000))
self.assertEqual(st.st_gid, gid(100))

def test16(self):
"""test CpioFile's copyin method unseekable fobj (dest is directory)"""
# identical to test14 except that the input is not seekable
dest = self.fixture_file('copyin')
os.mkdir(dest)
fname = self.fixture_file('new_ascii_reader3.cpio')
st = os.stat(fname)
sio = StringIO(open(fname, 'r').read())
f = FileWrapper(fobj=sio)
archive_reader = NewAsciiReader(f)
Expand All @@ -492,8 +536,8 @@ def test16(self):
st = os.stat(fname)
self.assertEqual(st.st_mode, 33188)
self.assertEqual(st.st_mtime, 1340493596)
self.assertEqual(st.st_uid, 1000)
self.assertEqual(st.st_gid, 100)
self.assertEqual(st.st_uid, uid(1000))
self.assertEqual(st.st_gid, gid(100))
# copyin file foobar
archive_file = archive_reader.next_file()
archive_file.copyin(dest)
Expand All @@ -504,8 +548,8 @@ def test16(self):
st = os.stat(fname)
self.assertEqual(st.st_mode, 33188)
self.assertEqual(st.st_mtime, 1340493602)
self.assertEqual(st.st_uid, 1000)
self.assertEqual(st.st_gid, 100)
self.assertEqual(st.st_uid, uid(1000))
self.assertEqual(st.st_gid, gid(100))

def test17(self):
"""test CpioFile's copyin method (dest is a file-like object)"""
Expand Down Expand Up @@ -543,8 +587,9 @@ def test18(self):
self.assertEqual(hdr.magic, '070701')
# self.assertEqual(hdr.ino, 1788176)
self.assertEqual(hdr.mode, 33188)
self.assertEqual(hdr.uid, 1000)
self.assertEqual(hdr.gid, 100)
st = os.stat(fname)
self.assertEqual(hdr.uid, st.st_uid)
self.assertEqual(hdr.gid, st.st_gid)
self.assertEqual(hdr.nlink, 1)
self.assertEqual(hdr.mtime, 1340493596)
self.assertEqual(hdr.filesize, 9)
Expand All @@ -564,8 +609,8 @@ def test19(self):
archive_writer = NewAsciiWriter(sio)
archive_writer.append('foo', fobj=sio_fobj)
self.assertEqual(archive_writer._bytes_written, 128)
self.assertEqualFile(sio.getvalue(),
'new_ascii_writer_foo_header_default')
fname = self.replace_uid_gid('new_ascii_writer_foo_header_default')
self.assertEqualFile(sio.getvalue(), fname)

def test20(self):
"""test NewAsciiWriter's _append_trailer method"""
Expand Down Expand Up @@ -601,7 +646,8 @@ def test22(self):
archive_writer.append('last_file', fobj=sio)
archive_writer.copyout()
self.assertEqual(archive_writer._bytes_written, 1024)
self.assertEqualFile(f.getvalue(), 'new_ascii_writer_sio.cpio')
fname = self.replace_uid_gid('new_ascii_writer_sio.cpio')
self.assertEqualFile(f.getvalue(), fname)

def test23(self):
"""test CpioArchive class"""
Expand Down
Binary file not shown.
Binary file modified test/util/test_cpio_fixtures/new_ascii_writer_sio.cpio
Binary file not shown.

0 comments on commit c7cc60c

Please sign in to comment.