def test_ctrl_c(self, m_sys, m_docker_client): """ Test _attach_and_stream when a Keyboard interrupt is generated. :return: None """ # attach(..., stream=True) returns a generator. def container_output_gen(): yield ("Some output\n") yield ("from the container.") raise KeyboardInterrupt() yield ("This output is not printed.") m_docker_client.attach.return_value = container_output_gen() m_stdout = Mock(spec=sys.stdout) m_sys.stdout = m_stdout m_container = Mock() node._attach_and_stream(m_container) m_docker_client.attach.assert_called_once_with(m_container, stream=True) self.assertFalse(m_container.called) m_stdout.write.assert_has_calls([call("Some output\n"), call("from the container.")]) self.assertEqual(m_stdout.write.call_count, 2) m_docker_client.stop.assert_called_once_with(m_container)
def test_ctrl_c(self, m_sys, m_docker_client): """ Test _attach_and_stream when a Keyboard interrupt is generated. :return: None """ # attach(..., stream=True) returns a generator. def container_output_gen(): yield ("Some output\n") yield ("from the container.") raise KeyboardInterrupt() yield ("This output is not printed.") m_docker_client.attach.return_value = container_output_gen() m_stdout = Mock(spec=sys.stdout) m_sys.stdout = m_stdout m_container = Mock() node._attach_and_stream(m_container) m_docker_client.attach.assert_called_once_with(m_container, stream=True) self.assertFalse(m_container.called) m_stdout.write.assert_has_calls( [call("Some output\n"), call("from the container.")]) self.assertEqual(m_stdout.write.call_count, 2) m_docker_client.stop.assert_called_once_with(m_container)
def test_container_stops_normally(self, m_sys, m_docker_client): """ Test _attach_and_stream when the container stops normally. :return: None """ # attach(..., stream=True) returns a generator. def container_output_gen(): yield ("Some output\n") yield ("from the container.") m_docker_client.attach.return_value = container_output_gen() m_stdout = Mock(spec=sys.stdout) m_sys.stdout = m_stdout m_container = Mock() node._attach_and_stream(m_container) m_docker_client.attach.assert_called_once_with(m_container, stream=True) self.assertFalse(m_container.called) m_stdout.write.assert_has_calls( [call("Some output\n"), call("from the container.")]) m_docker_client.stop.assert_called_once_with(m_container)
def test_killed(self, m_sys, m_docker_client): """ Test _attach_and_stream when killed by another process. :return: None """ # attach(..., stream=True) returns a generator. def container_output_gen(): yield ("Some output\n") yield ("from the container.") # Commit suicide, simulating being killed from another terminal. os.kill(os.getpid(), signal.SIGTERM) yield ("\nThis output is printed, but only because we nerf'd " "sys.exit()") m_docker_client.attach.return_value = container_output_gen() m_stdout = Mock(spec=sys.stdout) m_sys.stdout = m_stdout m_container = Mock() node._attach_and_stream(m_container, False) m_docker_client.attach.assert_called_once_with(m_container, stream=True) self.assertFalse(m_container.called) m_stdout.write.assert_has_calls([ call("Some output\n"), call("from the container."), call("\nThis output is printed, but " "only because we nerf'd " "sys.exit()") ]) # Stop gets called twice, once for SIGTERM, and because sys.exit() gets # mocked, the function continues and we get another call when the # generator ends normally. m_docker_client.stop.assert_has_calls( [call(m_container), call(m_container)]) self.assertEqual(m_docker_client.stop.call_count, 2) # sys.exit gets called twice: once when handling the SIGTERM and once # when stopping the container for an unknown reason m_sys.exit.assert_has_calls([call(0), call(1)])
def test_killed(self, m_sys, m_docker_client): """ Test _attach_and_stream when killed by another process. :return: None """ # attach(..., stream=True) returns a generator. def container_output_gen(): yield ("Some output\n") yield ("from the container.") # Commit suicide, simulating being killed from another terminal. os.kill(os.getpid(), signal.SIGTERM) yield ("\nThis output is printed, but only because we nerf'd " "sys.exit()") m_docker_client.attach.return_value = container_output_gen() m_stdout = Mock(spec=sys.stdout) m_sys.stdout = m_stdout m_container = Mock() node._attach_and_stream(m_container, False) m_docker_client.attach.assert_called_once_with(m_container, stream=True) self.assertFalse(m_container.called) m_stdout.write.assert_has_calls([call("Some output\n"), call("from the container."), call("\nThis output is printed, but " "only because we nerf'd " "sys.exit()")]) # Stop gets called twice, once for SIGTERM, and because sys.exit() gets # mocked, the function continues and we get another call when the # generator ends normally. m_docker_client.stop.assert_has_calls([call(m_container), call(m_container)]) self.assertEqual(m_docker_client.stop.call_count, 2) # sys.exit gets called twice: once when handling the SIGTERM and once # when stopping the container for an unknown reason m_sys.exit.assert_has_calls([call(0), call(1)])
def test_container_stops_normally(self, m_sys, m_docker_client): """ Test _attach_and_stream when the container stops normally. :return: None """ # attach(..., stream=True) returns a generator. def container_output_gen(): yield ("Some output\n") yield ("from the container.") m_docker_client.attach.return_value = container_output_gen() m_stdout = Mock(spec=sys.stdout) m_sys.stdout = m_stdout m_container = Mock() node._attach_and_stream(m_container, False) m_docker_client.attach.assert_called_once_with(m_container, stream=True) self.assertFalse(m_container.called) m_stdout.write.assert_has_calls([call("Some output\n"), call("from the container.")]) m_docker_client.stop.assert_called_once_with(m_container)