Fix cfn_helper test for random hash seed

test_cfn_helper.py has a part of code which enforces the order of packages
or services processes. But the order is non-deterministic.

Change-Id: I37c4abe697fb3391793ce74fc730b127e920710a
Closes-Bug: #1360212
This commit is contained in:
Yukinori Sagara 2014-08-23 06:36:30 +09:00 committed by Clint 'SpamapS' Byrum
parent 3d1236d0ed
commit 2e544679e1

View File

@ -46,6 +46,10 @@ class MockPopenTestCase(testtools.TestCase):
return subprocess.Popen( return subprocess.Popen(
command, cwd=cwd, env=env, stderr=-1, stdout=-1) command, cwd=cwd, env=env, stderr=-1, stdout=-1)
def mock_unorder_cmd_run(self, command, cwd=None, env=None):
return subprocess.Popen(
command, cwd=cwd, env=env, stderr=-1, stdout=-1).InAnyOrder()
def setUp(self): def setUp(self):
super(MockPopenTestCase, self).setUp() super(MockPopenTestCase, self).setUp()
self.m = mox.Mox() self.m = mox.Mox()
@ -79,19 +83,20 @@ class TestPackages(MockPopenTestCase):
def test_yum_install(self): def test_yum_install(self):
install_list = [] install_list = []
for pack in ('httpd', 'wordpress', 'mysql-server'): for pack in ('httpd', 'wordpress', 'mysql-server'):
self.mock_cmd_run(['su', 'root', '-c', self.mock_unorder_cmd_run(
'rpm -q %s' % pack]).AndReturn( ['su', 'root', '-c', 'rpm -q %s' % pack]) \
FakePOpen(returncode=1)) .AndReturn(FakePOpen(returncode=1))
self.mock_cmd_run( self.mock_unorder_cmd_run(
['su', 'root', '-c', ['su', 'root', '-c',
'yum -y --showduplicates list available %s' % pack]) \ 'yum -y --showduplicates list available %s' % pack]) \
.AndReturn(FakePOpen(returncode=0)) .AndReturn(FakePOpen(returncode=0))
install_list.append(pack) install_list.append(pack)
self.mock_cmd_run( # This mock call corresponding to 'su root -c yum -y install .*'
['su', 'root', '-c', # But there is no way to ignore the order of the parameters, so only
'yum -y install %s' % ' '.join(install_list)]) \ # check the return value.
.AndReturn(FakePOpen(returncode=0)) self.mock_cmd_run(mox.IgnoreArg()).AndReturn(FakePOpen(
returncode=0))
self.m.ReplayAll() self.m.ReplayAll()
packages = { packages = {
@ -108,19 +113,20 @@ class TestPackages(MockPopenTestCase):
def test_zypper_install(self): def test_zypper_install(self):
install_list = [] install_list = []
for pack in ('httpd', 'wordpress', 'mysql-server'): for pack in ('httpd', 'wordpress', 'mysql-server'):
self.mock_cmd_run(['su', 'root', '-c', self.mock_unorder_cmd_run(
'rpm -q %s' % pack]).AndReturn( ['su', 'root', '-c', 'rpm -q %s' % pack]) \
FakePOpen(returncode=1)) .AndReturn(FakePOpen(returncode=1))
self.mock_cmd_run( self.mock_unorder_cmd_run(
['su', 'root', '-c', ['su', 'root', '-c',
'zypper -n --no-refresh search %s' % pack]) \ 'zypper -n --no-refresh search %s' % pack]) \
.AndReturn(FakePOpen(returncode=0)) .AndReturn(FakePOpen(returncode=0))
install_list.append(pack) install_list.append(pack)
self.mock_cmd_run( # This mock call corresponding to 'su root -c zypper -n install .*'
['su', 'root', '-c', # But there is no way to ignore the order of the parameters, so only
'zypper -n install %s' % ' '.join(install_list)]) \ # check the return value.
.AndReturn(FakePOpen(returncode=0)) self.mock_cmd_run(mox.IgnoreArg()).AndReturn(FakePOpen(
returncode=0))
self.m.ReplayAll() self.m.ReplayAll()
packages = { packages = {
@ -135,12 +141,12 @@ class TestPackages(MockPopenTestCase):
self.m.VerifyAll() self.m.VerifyAll()
def test_apt_install(self): def test_apt_install(self):
install_list = 'httpd wordpress mysql-server' # This mock call corresponding to
cmd = 'DEBIAN_FRONTEND=noninteractive apt-get -y install' # 'DEBIAN_FRONTEND=noninteractive su root -c apt-get -y install .*'
# But there is no way to ignore the order of the parameters, so only
self.mock_cmd_run(['su', 'root', '-c', # check the return value.
'%s %s' % (cmd, install_list)]).AndReturn( self.mock_cmd_run(mox.IgnoreArg()).AndReturn(FakePOpen(
FakePOpen(returncode=0)) returncode=0))
self.m.ReplayAll() self.m.ReplayAll()
packages = { packages = {
@ -161,51 +167,51 @@ class TestServicesHandler(MockPopenTestCase):
self.m.StubOutWithMock(os.path, 'exists') self.m.StubOutWithMock(os.path, 'exists')
os.path.exists('/bin/systemctl').MultipleTimes().AndReturn(True) os.path.exists('/bin/systemctl').MultipleTimes().AndReturn(True)
# apply_services # apply_services
self.mock_cmd_run( self.mock_unorder_cmd_run(
['su', 'root', '-c', '/bin/systemctl enable httpd.service'] ['su', 'root', '-c', '/bin/systemctl enable httpd.service']
).AndReturn(FakePOpen()) ).AndReturn(FakePOpen())
self.mock_cmd_run( self.mock_unorder_cmd_run(
['su', 'root', '-c', '/bin/systemctl status httpd.service'] ['su', 'root', '-c', '/bin/systemctl status httpd.service']
).AndReturn(FakePOpen(returncode=-1)) ).AndReturn(FakePOpen(returncode=-1))
self.mock_cmd_run( self.mock_unorder_cmd_run(
['su', 'root', '-c', '/bin/systemctl start httpd.service'] ['su', 'root', '-c', '/bin/systemctl start httpd.service']
).AndReturn(FakePOpen()) ).AndReturn(FakePOpen())
self.mock_cmd_run( self.mock_unorder_cmd_run(
['su', 'root', '-c', '/bin/systemctl enable mysqld.service'] ['su', 'root', '-c', '/bin/systemctl enable mysqld.service']
).AndReturn(FakePOpen()) ).AndReturn(FakePOpen())
self.mock_cmd_run( self.mock_unorder_cmd_run(
['su', 'root', '-c', '/bin/systemctl status mysqld.service'] ['su', 'root', '-c', '/bin/systemctl status mysqld.service']
).AndReturn(FakePOpen(returncode=-1)) ).AndReturn(FakePOpen(returncode=-1))
self.mock_cmd_run( self.mock_unorder_cmd_run(
['su', 'root', '-c', '/bin/systemctl start mysqld.service'] ['su', 'root', '-c', '/bin/systemctl start mysqld.service']
).AndReturn(FakePOpen()) ).AndReturn(FakePOpen())
# monitor_services not running # monitor_services not running
self.mock_cmd_run( self.mock_unorder_cmd_run(
['su', 'root', '-c', '/bin/systemctl status httpd.service'] ['su', 'root', '-c', '/bin/systemctl status httpd.service']
).AndReturn(FakePOpen(returncode=-1)) ).AndReturn(FakePOpen(returncode=-1))
self.mock_cmd_run( self.mock_unorder_cmd_run(
['su', 'root', '-c', '/bin/systemctl start httpd.service'] ['su', 'root', '-c', '/bin/systemctl start httpd.service']
).AndReturn(FakePOpen()) ).AndReturn(FakePOpen())
self.mock_cmd_run( self.mock_unorder_cmd_run(
['su', 'root', '-c', '/bin/services_restarted'] ['su', 'root', '-c', '/bin/services_restarted']
).AndReturn(FakePOpen()) ).AndReturn(FakePOpen())
self.mock_cmd_run( self.mock_unorder_cmd_run(
['su', 'root', '-c', '/bin/systemctl status mysqld.service'] ['su', 'root', '-c', '/bin/systemctl status mysqld.service']
).AndReturn(FakePOpen(returncode=-1)) ).AndReturn(FakePOpen(returncode=-1))
self.mock_cmd_run( self.mock_unorder_cmd_run(
['su', 'root', '-c', '/bin/systemctl start mysqld.service'] ['su', 'root', '-c', '/bin/systemctl start mysqld.service']
).AndReturn(FakePOpen()) ).AndReturn(FakePOpen())
self.mock_cmd_run( self.mock_unorder_cmd_run(
['su', 'root', '-c', '/bin/services_restarted'] ['su', 'root', '-c', '/bin/services_restarted']
).AndReturn(FakePOpen()) ).AndReturn(FakePOpen())
# monitor_services running # monitor_services running
self.mock_cmd_run( self.mock_unorder_cmd_run(
['su', 'root', '-c', '/bin/systemctl status httpd.service'] ['su', 'root', '-c', '/bin/systemctl status httpd.service']
).AndReturn(FakePOpen()) ).AndReturn(FakePOpen())
self.mock_cmd_run( self.mock_unorder_cmd_run(
['su', 'root', '-c', '/bin/systemctl status mysqld.service'] ['su', 'root', '-c', '/bin/systemctl status mysqld.service']
).AndReturn(FakePOpen()) ).AndReturn(FakePOpen())
@ -239,22 +245,22 @@ class TestServicesHandler(MockPopenTestCase):
self.m.StubOutWithMock(os.path, 'exists') self.m.StubOutWithMock(os.path, 'exists')
os.path.exists('/bin/systemctl').MultipleTimes().AndReturn(True) os.path.exists('/bin/systemctl').MultipleTimes().AndReturn(True)
# apply_services # apply_services
self.mock_cmd_run( self.mock_unorder_cmd_run(
['su', 'root', '-c', '/bin/systemctl disable httpd.service'] ['su', 'root', '-c', '/bin/systemctl disable httpd.service']
).AndReturn(FakePOpen()) ).AndReturn(FakePOpen())
self.mock_cmd_run( self.mock_unorder_cmd_run(
['su', 'root', '-c', '/bin/systemctl status httpd.service'] ['su', 'root', '-c', '/bin/systemctl status httpd.service']
).AndReturn(FakePOpen()) ).AndReturn(FakePOpen())
self.mock_cmd_run( self.mock_unorder_cmd_run(
['su', 'root', '-c', '/bin/systemctl stop httpd.service'] ['su', 'root', '-c', '/bin/systemctl stop httpd.service']
).AndReturn(FakePOpen()) ).AndReturn(FakePOpen())
self.mock_cmd_run( self.mock_unorder_cmd_run(
['su', 'root', '-c', '/bin/systemctl disable mysqld.service'] ['su', 'root', '-c', '/bin/systemctl disable mysqld.service']
).AndReturn(FakePOpen()) ).AndReturn(FakePOpen())
self.mock_cmd_run( self.mock_unorder_cmd_run(
['su', 'root', '-c', '/bin/systemctl status mysqld.service'] ['su', 'root', '-c', '/bin/systemctl status mysqld.service']
).AndReturn(FakePOpen()) ).AndReturn(FakePOpen())
self.mock_cmd_run( self.mock_unorder_cmd_run(
['su', 'root', '-c', '/bin/systemctl stop mysqld.service'] ['su', 'root', '-c', '/bin/systemctl stop mysqld.service']
).AndReturn(FakePOpen()) ).AndReturn(FakePOpen())