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):
69
exists.side_effect = [init_exists, init_d_exists]
70
mock_call.return_value = 0
71
action = 'some-action'
72
service_name = 'foo-service'
74
result = host.service(action, service_name)
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)),
82
@patch('subprocess.call')
83
@patch('os.path.exists')
84
def test_runs_service_action_on_init_d(self, exists, mock_call):
87
exists.side_effect = [init_exists, init_d_exists]
88
mock_call.return_value = 0
89
action = 'some-action'
90
service_name = 'foo-service'
92
result = host.service(action, service_name)
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)),
101
@patch('subprocess.call')
102
@patch('os.path.exists')
103
def test_doesnt_run_service_if_it_doesn_exist(self, exists, mock_call):
105
init_d_exists = False
106
exists.side_effect = [init_exists, init_d_exists]
107
action = 'some-action'
108
service_name = 'foo-service'
110
result = host.service(action, service_name)
112
self.assertFalse(result)
113
self.assertFalse(mock_call.called)
115
@patch('subprocess.call')
116
@patch('os.path.exists')
117
def test_returns_false_when_service_fails(self, exists, mock_call):
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'
70
result = host.service(action, service_name)
72
self.assertTrue(result)
73
mock_call.assert_called_with(['service', service_name, action])
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)
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])
131
86
@patch.object(host, 'service')
132
87
def test_starts_a_service(self, service):
143
98
service.assert_called_with('stop', service_name)
100
@patch.object(host, 'service')
101
def test_restarts_a_service(self, service):
102
service_name = 'foo-service'
103
host.service_restart(service_name)
105
service.assert_called_with('restart', service_name)
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)
113
service.assert_called_with('reload', service_name)
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)
121
service.assert_has_calls([
122
call('reload', service_name),
123
call('restart', service_name)
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)
132
service.assert_called_with('reload', service_name)
145
134
@patch('pwd.getpwnam')
146
135
@patch('subprocess.check_call')
147
136
@patch.object(host, 'log')