File: hook_test.py

package info (click to toggle)
python-certbot 0.10.2-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 1,548 kB
  • sloc: python: 12,428; makefile: 175; sh: 44
file content (110 lines) | stat: -rw-r--r-- 4,619 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""Tests for hooks.py"""
# pylint: disable=protected-access

import os
import unittest

import mock
from six.moves import reload_module  # pylint: disable=import-error

from certbot import errors
from certbot import hooks

class HookTest(unittest.TestCase):
    def setUp(self):
        reload_module(hooks)

    @mock.patch('certbot.hooks._prog')
    def test_validate_hooks(self, mock_prog):
        config = mock.MagicMock(pre_hook="", post_hook="ls -lR", renew_hook="uptime")
        hooks.validate_hooks(config)
        self.assertEqual(mock_prog.call_count, 2)
        self.assertEqual(mock_prog.call_args_list[1][0][0], 'uptime')
        self.assertEqual(mock_prog.call_args_list[0][0][0], 'ls')
        mock_prog.return_value = None
        config = mock.MagicMock(pre_hook="explodinator", post_hook="", renew_hook="")
        self.assertRaises(errors.HookCommandNotFound, hooks.validate_hooks, config)

    @mock.patch('certbot.hooks.util.exe_exists')
    @mock.patch('certbot.hooks.plug_util.path_surgery')
    def test_prog(self, mock_ps, mock_exe_exists):
        mock_exe_exists.return_value = True
        self.assertEqual(hooks._prog("funky"), "funky")
        self.assertEqual(mock_ps.call_count, 0)
        mock_exe_exists.return_value = False
        self.assertEqual(hooks._prog("funky"), None)
        self.assertEqual(mock_ps.call_count, 1)

    def _test_a_hook(self, config, hook_function, calls_expected, **kwargs):
        with mock.patch('certbot.hooks.logger') as mock_logger:
            mock_logger.warning = mock.MagicMock()
            with mock.patch('certbot.hooks._run_hook') as mock_run_hook:
                hook_function(config, **kwargs)
                hook_function(config, **kwargs)
                self.assertEqual(mock_run_hook.call_count, calls_expected)
            return mock_logger.warning

    def test_pre_hook(self):
        config = mock.MagicMock(pre_hook="true")
        self._test_a_hook(config, hooks.pre_hook, 1)
        self._test_a_hook(config, hooks.pre_hook, 0)
        config = mock.MagicMock(pre_hook="more_true")
        self._test_a_hook(config, hooks.pre_hook, 1)
        self._test_a_hook(config, hooks.pre_hook, 0)
        config = mock.MagicMock(pre_hook="")
        self._test_a_hook(config, hooks.pre_hook, 0)

    def _test_renew_post_hooks(self, expected_count):
        with mock.patch('certbot.hooks.logger.info') as mock_info:
            with mock.patch('certbot.hooks._run_hook') as mock_run:
                hooks.run_saved_post_hooks()
                self.assertEqual(mock_run.call_count, expected_count)
                self.assertEqual(mock_info.call_count, expected_count)

    def test_post_hooks(self):
        config = mock.MagicMock(post_hook="true", verb="splonk")
        self._test_a_hook(config, hooks.post_hook, 2)
        self._test_renew_post_hooks(0)

        config = mock.MagicMock(post_hook="true", verb="renew")
        self._test_a_hook(config, hooks.post_hook, 0)
        self._test_renew_post_hooks(1)
        self._test_a_hook(config, hooks.post_hook, 0)
        self._test_renew_post_hooks(1)

        config = mock.MagicMock(post_hook="more_true", verb="renew")
        self._test_a_hook(config, hooks.post_hook, 0)
        self._test_renew_post_hooks(2)

    def test_renew_hook(self):
        with mock.patch.dict('os.environ', {}):
            domains = ["a", "b"]
            lineage = "thing"
            rhook = lambda x: hooks.renew_hook(x, domains, lineage)

            config = mock.MagicMock(renew_hook="true", dry_run=False)
            self._test_a_hook(config, rhook, 2)
            self.assertEqual(os.environ["RENEWED_DOMAINS"], "a b")
            self.assertEqual(os.environ["RENEWED_LINEAGE"], "thing")

            config = mock.MagicMock(renew_hook="true", dry_run=True)
            mock_warn = self._test_a_hook(config, rhook, 0)
            self.assertEqual(mock_warn.call_count, 2)

    @mock.patch('certbot.hooks.Popen')
    def test_run_hook(self, mock_popen):
        with mock.patch('certbot.hooks.logger.error') as mock_error:
            mock_cmd = mock.MagicMock()
            mock_cmd.returncode = 1
            mock_cmd.communicate.return_value = ("", "")
            mock_popen.return_value = mock_cmd
            hooks._run_hook("ls")
            self.assertEqual(mock_error.call_count, 1)
        with mock.patch('certbot.hooks.logger.error') as mock_error:
            mock_cmd.communicate.return_value = ("", "thing")
            hooks._run_hook("ls")
            self.assertEqual(mock_error.call_count, 2)


if __name__ == '__main__':
    unittest.main()  # pragma: no cover