~bac/charms/precise/mongodb/logrotate

« back to all changes in this revision

Viewing changes to tests/test_write_log_rotate_config.py

  • Committer: Brad Crittenden
  • Date: 2013-12-11 13:17:03 UTC
  • Revision ID: bac@canonical.com-20131211131703-xrsiddif8cdpq5lw
Add logrotate config with test.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import hooks
 
2
import mock
 
3
import os
 
4
import unittest
 
5
import tempfile
 
6
 
 
7
 
 
8
class TestWriteLogrotateConfigFile(unittest.TestCase):
 
9
 
 
10
    def test_success(self):
 
11
        logpath = '/tmp/foo/foo.log'
 
12
        config_data = {
 
13
            'logpath': logpath,
 
14
            'logrotate-frequency': 'daily',
 
15
            'logrotate-maxsize': '5G',
 
16
            'logrotate-rotate': 5,
 
17
        }
 
18
        fd, temp_fn = tempfile.mkstemp()
 
19
        os.close(fd)
 
20
        with mock.patch('hooks.juju_log') as mock_juju_log:
 
21
            with mock.patch('hooks.open', create=True) as mock_open:
 
22
                mock_open.return_value = mock.MagicMock(spec=file)
 
23
                hooks.write_logrotate_config(config_data, temp_fn)
 
24
                os.unlink(temp_fn)
 
25
        self.assertFalse(mock_juju_log.called)
 
26
        mock_open.assert_called_once_with(temp_fn, 'w')
 
27
        mock_file = mock_open().__enter__()
 
28
        call_args = mock_file.write.call_args[0][0]
 
29
        self.assertTrue(mock_file.write.called)
 
30
        self.assertIn(logpath, call_args)
 
31
        self.assertIn('daily', call_args)
 
32
        self.assertIn('maxsize 5G', call_args)
 
33
        self.assertIn('rotate 5', call_args)