def test_version_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.version(99999)
def test_version_parse_problem(self): ''' Test with invalid context data. The context value must be a dict, so this should raise a HubbleInvocationError. ''' with patch('subprocess.Popen') as popen_mock: popen_mock.return_value = Mock(communicate=lambda *args, **kwargs: ('invalid', None), pid=lambda: 12345, retcode=0) # Test without context dict passed self.assertIsNone(_systemd.version()) # 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.assertIsNone(_systemd.version(context)) self.assertEqual(context, {})
def test_version_return_from_context(self): ''' Test that the context data is returned when present. To ensure we're getting data from the context dict, we use a non-integer value to differentiate it from the integer return this function normally produces. ''' context = {'hubblestack.utils.systemd.version': 'foo'} self.assertEqual(_systemd.version(context), 'foo')
def test_version(self): ''' Test that hubblestack.utils.systemd.booted() returns True when minion is systemd-booted. ''' with patch('subprocess.Popen') as popen_mock: _version = 231 output = 'systemd {0}\n-SYSVINIT'.format(_version) popen_mock.return_value = Mock(communicate=lambda *args, **kwargs: (output, None), pid=lambda: 12345, retcode=0) # Test without context dict passed self.assertEqual(_systemd.version(), _version) # Test that context key is set when context dict is passed context = {} self.assertTrue(_systemd.version(context)) self.assertEqual(context, {'hubblestack.utils.systemd.version': _version})
def test_version_generated_from_git_describe(self): ''' Test with version string matching versions generated by git describe in systemd. This feature is used in systemd>=241. ''' with patch('subprocess.Popen') as popen_mock: _version = 241 output = 'systemd {0} ({0}.0-0-dist)\n-SYSVINIT'.format(_version) popen_mock.return_value = Mock(communicate=lambda *args, **kwargs: (output, None), pid=lambda: 12345, retcode=0) # Test without context dict passed self.assertEqual(_systemd.version(), _version) # Test that context key is set when context dict is passed context = {} self.assertTrue(_systemd.version(context)) self.assertEqual(context, {'hubblestack.utils.systemd.version': _version})