def test_has_scope_systemd206(self): ''' Scopes are available in systemd>=205. Make sure that this function returns the expected boolean. We do three separate unit tests for versions 204 through 206 because mock doesn't like us altering the return_value in a loop. ''' with patch('subprocess.Popen') as popen_mock: _expected = True _version = 206 _output = 'systemd {0}\n-SYSVINIT'.format(_version) popen_mock.return_value = Mock(communicate=lambda *args, **kwargs: (_output, None), pid=lambda: 12345, retcode=0) # Ensure that os.stat returns True. os.stat doesn't return a bool # normally, but the code is doing a simple truth check on the # return data, so it is sufficient enough to mock it as True for # these tests. with patch('os.stat', side_effect=_booted_effect): # Test without context dict passed self.assertEqual(_systemd.has_scope(), _expected) # Test that context key is set when context dict is passed context = {} self.assertEqual(_systemd.has_scope(context), _expected) self.assertEqual( context, { 'hubblestack.utils.systemd.booted': True, 'hubblestack.utils.systemd.version': _version }, )
def test_has_scope_invalid_context(self): ''' Test with invalid context data. The context value must be a dict, so this should raise a HubbleInvocationError. ''' # Test with invalid context data with self.assertRaises(HubbleInvocationError): _systemd.has_scope(99999)
def test_has_scope_no_systemd(self): ''' Test the case where the system is not systemd-booted. We should not be performing a version check in these cases as there is no need. ''' with patch('os.stat', side_effect=_not_booted_effect): # Test without context dict passed self.assertFalse(_systemd.has_scope()) # Test that context key is set when context dict is passed. # Because we are not systemd-booted, there should be no key in the # context dict for the version check, as we shouldn't have # performed this check. context = {} self.assertFalse(_systemd.has_scope(context)) self.assertEqual(context, {'hubblestack.utils.systemd.booted': False})
def test_has_scope_version_parse_problem(self): ''' Test the case where the system is systemd-booted, but we failed to parse the "systemctl --version" output. ''' with patch('subprocess.Popen') as popen_mock: popen_mock.return_value = Mock(communicate=lambda *args, **kwargs: ('invalid', None), pid=lambda: 12345, retcode=0) with patch('os.stat', side_effect=_booted_effect): # Test without context dict passed self.assertFalse(_systemd.has_scope()) # Test that context key is set when context dict is passed. A # failure to parse the systemctl output should not set a context # key, so it should not be present in the context dict. context = {} self.assertFalse(_systemd.has_scope(context)) self.assertEqual(context, {'hubblestack.utils.systemd.booted': True})