Commit 9be006b0 authored by Thomas Larrieu's avatar Thomas Larrieu

changing 2 tests, making assertion on method call and not on side effects

parent d4b9c175
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
# #
############################################################################## ##############################################################################
from __future__ import absolute_import
import httplib import httplib
import logging import logging
import os import os
...@@ -40,6 +41,7 @@ import unittest ...@@ -40,6 +41,7 @@ import unittest
import urlparse import urlparse
import xml_marshaller import xml_marshaller
from mock import patch
import slapos.slap.slap import slapos.slap.slap
import slapos.grid.utils import slapos.grid.utils
...@@ -49,6 +51,7 @@ from slapos.grid.utils import md5digest ...@@ -49,6 +51,7 @@ from slapos.grid.utils import md5digest
from slapos.grid.watchdog import Watchdog, getWatchdogID from slapos.grid.watchdog import Watchdog, getWatchdogID
from slapos.grid import SlapObject from slapos.grid import SlapObject
dummylogger = logging.getLogger() dummylogger = logging.getLogger()
...@@ -238,13 +241,13 @@ class MasterMixin(BasicMixin): ...@@ -238,13 +241,13 @@ class MasterMixin(BasicMixin):
def _patchHttplib(self): def _patchHttplib(self):
"""Overrides httplib""" """Overrides httplib"""
import mock.httplib import slapos.tests.mock.httplib
self.saved_httplib = {} self.saved_httplib = {}
for fake in vars(mock.httplib): for fake in vars(slapos.tests.mock.httplib):
self.saved_httplib[fake] = getattr(httplib, fake, None) self.saved_httplib[fake] = getattr(httplib, fake, None)
setattr(httplib, fake, getattr(mock.httplib, fake)) setattr(httplib, fake, getattr(slapos.tests.mock.httplib, fake))
def _unpatchHttplib(self): def _unpatchHttplib(self):
"""Restores httplib overriding""" """Restores httplib overriding"""
...@@ -1172,43 +1175,44 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase): ...@@ -1172,43 +1175,44 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase):
""" """
Checks that a partition is not processed when Checks that a partition is not processed when
its periodicity is negative its periodicity is negative
1. We setup one instance and set periodicity at -1
2. We mock the install method from slapos.grid.slapgrid.Partition
3. We launch slapgrid once so that .timestamp file is created and check that install method is
indeed called (through mocked_method.called
4. We launch slapgrid anew and check that install as not been called again
""" """
def get_runtime(instance):
self.assertTrue(os.path.exists(os.path.join(instance.partition_path, '.timestamp')))
return os.path.getmtime(os.path.join(instance.partition_path, '.timestamp'))
periodicity = -1
timestamp = str(int(time.time())) timestamp = str(int(time.time()))
computer = ComputerForTest(self.software_root, self.instance_root, 1, 1) computer = ComputerForTest(self.software_root, self.instance_root, 1, 1)
instance = computer.instance_list[0] instance = computer.instance_list[0]
instance.software.setPeriodicity(periodicity) instance.software.setPeriodicity(-1)
instance.timestamp = timestamp instance.timestamp = timestamp
with patch.object(slapos.grid.slapgrid.Partition, 'install', return_value=None) as mock_method:
self.launchSlapgrid() self.launchSlapgrid()
first_runtime = get_runtime(instance) self.assertTrue(mock_method.called)
self.launchSlapgrid() self.launchSlapgrid()
second_runtime = get_runtime(instance) self.assertEqual(mock_method.call_count, 1)
self.assertEqual(first_runtime, second_runtime)
def test_one_partition_is_always_processed_when_periodicity_is_zero(self): def test_one_partition_is_always_processed_when_periodicity_is_zero(self):
""" """
Checks that a partition is always processed when Checks that a partition is always processed when
its periodicity is 0 its periodicity is 0
1. We setup one instance and set periodicity at 0
2. We mock the install method from slapos.grid.slapgrid.Partition
3. We launch slapgrid once so that .timestamp file is created
4. We launch slapgrid anew and check that install has been called twice (one time because of the
new setup and one time because of periodicity = 0)
""" """
def get_runtime(instance):
self.assertTrue(os.path.exists(os.path.join(instance.partition_path, '.timestamp')))
return os.path.getmtime(os.path.join(instance.partition_path, '.timestamp'))
periodicity = 0
timestamp = str(int(time.time())) timestamp = str(int(time.time()))
computer = ComputerForTest(self.software_root, self.instance_root, 1, 1) computer = ComputerForTest(self.software_root, self.instance_root, 1, 1)
instance = computer.instance_list[0] instance = computer.instance_list[0]
instance.software.setPeriodicity(periodicity) instance.software.setPeriodicity(0)
instance.timestamp = timestamp instance.timestamp = timestamp
with patch.object(slapos.grid.slapgrid.Partition, 'install', return_value=None) as mock_method:
self.launchSlapgrid() self.launchSlapgrid()
first_runtime = get_runtime(instance)
self.launchSlapgrid() self.launchSlapgrid()
second_runtime = get_runtime(instance) self.assertEqual(mock_method.call_count, 2)
self.assertNotEqual(first_runtime, second_runtime)
def test_one_partition_buildout_fail_does_not_disturb_others(self): def test_one_partition_buildout_fail_does_not_disturb_others(self):
""" """
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment