Skip to content

Commit

Permalink
handle patching os.uname correctly on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
djmitche committed Nov 27, 2011
1 parent 7897ec5 commit 4e485aa
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 6 deletions.
6 changes: 3 additions & 3 deletions master/buildbot/test/unit/test_master.py
Expand Up @@ -23,7 +23,7 @@
from buildbot import master, monkeypatches, config
from buildbot.util import subscription
from buildbot.db import connector
from buildbot.test.util import dirs, compat
from buildbot.test.util import dirs, compat, misc
from buildbot.test.fake import fakedb
from buildbot.util import epoch2datetime
from buildbot.changes import changes
Expand Down Expand Up @@ -403,7 +403,7 @@ def check(_):
return d


class Polling(dirs.DirsMixin, unittest.TestCase):
class Polling(dirs.DirsMixin, misc.PatcherMixin, unittest.TestCase):

def setUp(self):
self.gotten_changes = []
Expand All @@ -415,7 +415,7 @@ def setUp(self):
basedir = os.path.abspath('basedir')

# patch out os.uname so that we get a consistent hostname
self.patch(os, 'uname', lambda : [ 0, 'testhost.localdomain' ])
self.patch_os_uname(lambda : [ 0, 'testhost.localdomain' ])
self.master_name = "testhost.localdomain:%s" % (basedir,)

d = self.setUpDirs(basedir)
Expand Down
34 changes: 34 additions & 0 deletions master/buildbot/test/util/misc.py
@@ -0,0 +1,34 @@
# This file is part of Buildbot. Buildbot is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright Buildbot Team Members

import os

class PatcherMixin(object):
"""
Mix this in to get a few special-cased patching methods
"""

def patch_os_uname(self, replacement):
# twisted's 'patch' doesn't handle the case where an attribute
# doesn't exist..
if hasattr(os, 'uname'):
self.patch(os, 'uname', replacement)
else:
def cleanup():
del os.uname
self.addCleanup(cleanup)
os.uname = replacement


7 changes: 4 additions & 3 deletions slave/buildslave/test/unit/test_bot_BuildSlave.py
Expand Up @@ -24,6 +24,7 @@
from zope.interface import implements

from buildslave import bot
from buildslave.test.util import misc

from mock import Mock

Expand Down Expand Up @@ -61,7 +62,7 @@ def returnAvatar(_):
def shutdown(self):
return self.mind.broker.transport.loseConnection()

class TestBuildSlave(unittest.TestCase):
class TestBuildSlave(misc.PatcherMixin, unittest.TestCase):

def setUp(self):
self.realm = None
Expand Down Expand Up @@ -130,7 +131,7 @@ def call_print(mind):
return d

def test_recordHostname_uname(self):
self.patch(os, "uname", lambda : [ 0, 'test-hostname.domain.com' ])
self.patch_os_uname(lambda : [ 0, 'test-hostname.domain.com' ])

self.buildslave = bot.BuildSlave("127.0.0.1", 9999,
"testy", "westy", self.basedir,
Expand All @@ -142,7 +143,7 @@ def test_recordHostname_uname(self):
def test_recordHostname_getfqdn(self):
def missing():
raise AttributeError
self.patch(os, "uname", missing)
self.patch_os_uname(missing)
self.patch(socket, "getfqdn", lambda : 'test-hostname.domain.com')

self.buildslave = bot.BuildSlave("127.0.0.1", 9999,
Expand Down
17 changes: 17 additions & 0 deletions slave/buildslave/test/util/misc.py
Expand Up @@ -36,3 +36,20 @@ def setUpBasedir(self):
def tearDownBasedir(self):
if os.path.exists(self.basedir):
shutil.rmtree(self.basedir)

class PatcherMixin(object):
"""
Mix this in to get a few special-cased patching methods
"""

def patch_os_uname(self, replacement):
# twisted's 'patch' doesn't handle the case where an attribute
# doesn't exist..
if hasattr(os, 'uname'):
self.patch(os, 'uname', replacement)
else:
def cleanup():
del os.uname
self.addCleanup(cleanup)
os.uname = replacement

0 comments on commit 4e485aa

Please sign in to comment.