~gnuoy/charm-helpers/bzr-fetch

« back to all changes in this revision

Viewing changes to tests/core/test_host.py

  • Committer: Matthew Wedgwood
  • Date: 2013-06-21 14:22:28 UTC
  • mfrom: (26.1.2 service_control)
  • Revision ID: matthew.wedgwood@canonical.com-20130621142228-5wr0bh9nr9ryw3jq
[james-page] Refactoring of service control code in host helper

1) Use 'service' command for all service control

Detecting upstart and init.d configuration files is overkill; this is
exactly what the 'service' command is design todo and it also deals with
saucy onwards where init.d and upstart configuration with the same
name might be installed.

'service' will always do the right thing

2) Added restart and reload helpers

reload detects an error (say the service is not running) and will fallback
to restart if so.

This is inline with the openstack charm helpers code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
62
62
 
63
63
class HelpersTest(TestCase):
64
64
    @patch('subprocess.call')
65
 
    @patch('os.path.exists')
66
 
    def test_runs_service_action(self, exists, mock_call):
67
 
        init_exists = True
68
 
        init_d_exists = False
69
 
        exists.side_effect = [init_exists, init_d_exists]
70
 
        mock_call.return_value = 0
71
 
        action = 'some-action'
72
 
        service_name = 'foo-service'
73
 
 
74
 
        result = host.service(action, service_name)
75
 
 
76
 
        self.assertTrue(result)
77
 
        mock_call.assert_called_with(['initctl', action, service_name])
78
 
        exists.assert_has_calls([
79
 
            call(os.path.join('/etc/init', '%s.conf' % service_name)),
80
 
        ])
81
 
 
82
 
    @patch('subprocess.call')
83
 
    @patch('os.path.exists')
84
 
    def test_runs_service_action_on_init_d(self, exists, mock_call):
85
 
        init_exists = False
86
 
        init_d_exists = True
87
 
        exists.side_effect = [init_exists, init_d_exists]
88
 
        mock_call.return_value = 0
89
 
        action = 'some-action'
90
 
        service_name = 'foo-service'
91
 
 
92
 
        result = host.service(action, service_name)
93
 
 
94
 
        self.assertTrue(result)
95
 
        mock_call.assert_called_with([os.path.join('/etc/init.d',
96
 
                                                   service_name), action])
97
 
        exists.assert_has_calls([
98
 
            call(os.path.join('/etc/init.d', service_name)),
99
 
        ])
100
 
 
101
 
    @patch('subprocess.call')
102
 
    @patch('os.path.exists')
103
 
    def test_doesnt_run_service_if_it_doesn_exist(self, exists, mock_call):
104
 
        init_exists = False
105
 
        init_d_exists = False
106
 
        exists.side_effect = [init_exists, init_d_exists]
107
 
        action = 'some-action'
108
 
        service_name = 'foo-service'
109
 
 
110
 
        result = host.service(action, service_name)
111
 
 
112
 
        self.assertFalse(result)
113
 
        self.assertFalse(mock_call.called)
114
 
 
115
 
    @patch('subprocess.call')
116
 
    @patch('os.path.exists')
117
 
    def test_returns_false_when_service_fails(self, exists, mock_call):
118
 
        init_exists = False
119
 
        init_d_exists = True
120
 
        exists.side_effect = [init_exists, init_d_exists]
 
65
    def test_runs_service_action(self, mock_call):
 
66
        mock_call.return_value = 0
 
67
        action = 'some-action'
 
68
        service_name = 'foo-service'
 
69
 
 
70
        result = host.service(action, service_name)
 
71
 
 
72
        self.assertTrue(result)
 
73
        mock_call.assert_called_with(['service', service_name, action])
 
74
 
 
75
    @patch('subprocess.call')
 
76
    def test_returns_false_when_service_fails(self, mock_call):
121
77
        mock_call.return_value = 1
122
78
        action = 'some-action'
123
79
        service_name = 'foo-service'
125
81
        result = host.service(action, service_name)
126
82
 
127
83
        self.assertFalse(result)
128
 
        mock_call.assert_called_with([os.path.join('/etc/init.d',
129
 
                                                   service_name), action])
 
84
        mock_call.assert_called_with(['service', service_name, action])
130
85
 
131
86
    @patch.object(host, 'service')
132
87
    def test_starts_a_service(self, service):
142
97
 
143
98
        service.assert_called_with('stop', service_name)
144
99
 
 
100
    @patch.object(host, 'service')
 
101
    def test_restarts_a_service(self, service):
 
102
        service_name = 'foo-service'
 
103
        host.service_restart(service_name)
 
104
 
 
105
        service.assert_called_with('restart', service_name)
 
106
 
 
107
    @patch.object(host, 'service')
 
108
    def test_reloads_a_service(self, service):
 
109
        service_name = 'foo-service'
 
110
        service.side_effect = [True]
 
111
        host.service_reload(service_name)
 
112
 
 
113
        service.assert_called_with('reload', service_name)
 
114
 
 
115
    @patch.object(host, 'service')
 
116
    def test_failed_reload_restarts_a_service(self, service):
 
117
        service_name = 'foo-service'
 
118
        service.side_effect = [False, True]
 
119
        host.service_reload(service_name, restart_on_failure=True)
 
120
 
 
121
        service.assert_has_calls([
 
122
                            call('reload', service_name),
 
123
                            call('restart', service_name)
 
124
                            ])
 
125
 
 
126
    @patch.object(host, 'service')
 
127
    def test_failed_reload_without_restart(self, service):
 
128
        service_name = 'foo-service'
 
129
        service.side_effect = [False]
 
130
        host.service_reload(service_name)
 
131
 
 
132
        service.assert_called_with('reload', service_name)
 
133
 
145
134
    @patch('pwd.getpwnam')
146
135
    @patch('subprocess.check_call')
147
136
    @patch.object(host, 'log')