def test_run_commands_many_commands(self, system, load_config): load_config.return_value = { 'pre-restart': ['testdata/pre.sh', 'testdata/pre2.sh'], } run_commands('pre-restart') calls = [call("testdata/pre.sh"), call("testdata/pre2.sh")] system.assert_has_calls(calls)
def test_run_commands_without_config(self, set_uid, check_output, load_config): load_config.return_value = { 'hooks': {'pre-restart': []} } run_commands('pre-restart', watcher=self.watcher) self.assertFalse(check_output.called)
def test_run_commands_config_without_a_hook(self, set_uid, Stream, check_output, load_config): load_config.return_value = { 'hooks': { 'pre-restart': ['testdata/pre.sh'], } } run_commands('post-restart', watcher=self.watcher)
def test_log_only_when_commands_are_executed(self, set_uid, Stream, load_config): load_config.return_value = { 'hooks': { 'pre-restart': ['ps -ef'], } } stream = Stream.return_value run_commands('post-restart', watcher=self.watcher) self.assertFalse(stream.called)
def test_run_commands_with_config(self, set_uid, check_output, load_config): load_config.return_value = { 'hooks': { 'pre-restart': ['testdata/pre.sh'], } } set_uid.return_value = 10 run_commands('pre-restart', watcher=self.watcher) check_output.assert_called_with(self.cmd("testdata/pre.sh"), stderr=subprocess.STDOUT, cwd=self.watcher.working_dir, preexec_fn=10)
def test_run_commands_should_log(self, set_uid, Stream, check_output, load_config): load_config.return_value = { 'hooks': { 'pre-restart': ['testdata/pre.sh'], } } check_output.return_value = "ble" stream = Stream.return_value run_commands('pre-restart', watcher=self.watcher) calls = [call({"data": " ---> Running pre-restart"}), call({"data": "ble"})] stream.assert_has_calls(calls)
def test_run_commands_should_log(self, set_uid, Stream, check_output, load_config): load_config.return_value = { 'hooks': { 'pre-restart': ['testdata/pre.sh'], } } check_output.return_value = "ble" stream = Stream.return_value run_commands('pre-restart', watcher=self.watcher) calls = [ call({"data": " ---> Running pre-restart"}), call({"data": "ble"}) ] stream.assert_has_calls(calls)
def test_run_commands_that_returns_errors(self, set_uid, Stream, check_output, load_config): load_config.return_value = { 'hooks': { 'pre-restart': ['exit 1'], } } stream = Stream.return_value check_output.side_effect = subprocess.CalledProcessError(1, "cmd") run_commands('pre-restart', watcher=self.watcher) calls = [ call({'data': ' ---> Running pre-restart'}), call({'data': "Command 'cmd' returned non-zero exit status 1"}), call({'data': None}) ] stream.assert_has_calls(calls)
def test_should_return_true(self, set_uid, Stream, check_output, load_config): result = run_commands('pre-restart', watcher=self.watcher) self.assertTrue(result)
def test_run_commands_config_without_hooks(self, set_uid, Stream, check_output, load_config): load_config.return_value = {} run_commands('post-restart', watcher=self.watcher)
def test_run_commands_without_config(self, system, load_config): load_config.return_value = { 'pre-restart': [], } run_commands('pre-restart') self.assertFalse(system.called)
def test_run_commands_without_config(self, set_uid, check_output, load_config): load_config.return_value = {'hooks': {'pre-restart': []}} run_commands('pre-restart', watcher=self.watcher) self.assertFalse(check_output.called)
def test_run_commands_with_config(self, system, load_config): load_config.return_value = { 'pre-restart': ['testdata/pre.sh'], } run_commands('pre-restart') system.assert_called_with("testdata/pre.sh")