Forráskód Böngészése

Add signal in the kill CLI commando to send a specific signal to the service

Signed-off-by: Raúl Cumplido <[email protected]>
Raúl Cumplido 11 éve
szülő
commit
9abdd337b5
4 módosított fájl, 48 hozzáadás és 5 törlés
  1. 3 1
      docs/cli.md
  2. 8 2
      fig/cli/main.py
  3. 2 2
      fig/container.py
  4. 35 0
      tests/integration/cli_test.py

+ 3 - 1
docs/cli.md

@@ -42,7 +42,9 @@ Get help on a command.
 
 ### kill
 
-Force stop service containers.
+Force stop running containers by sending a `SIGKILL` signal. Optionally the signal can be passed, for example:
+
+    $ fig kill -s SIGINT
 
 ### logs
 

+ 8 - 2
fig/cli/main.py

@@ -133,9 +133,15 @@ class TopLevelCommand(Command):
         """
         Force stop service containers.
 
-        Usage: kill [SERVICE...]
+        Usage: kill [options] [SERVICE...]
+
+        Options:
+            -s SIGNAL         SIGNAL to send to the container.
+                              Default signal is SIGKILL.
         """
-        project.kill(service_names=options['SERVICE'])
+        signal = options.get('-s', 'SIGKILL')
+
+        project.kill(service_names=options['SERVICE'], signal=signal)
 
     def logs(self, project, options):
         """

+ 2 - 2
fig/container.py

@@ -124,8 +124,8 @@ class Container(object):
     def stop(self, **options):
         return self.client.stop(self.id, **options)
 
-    def kill(self):
-        return self.client.kill(self.id)
+    def kill(self, **options):
+        return self.client.kill(self.id, **options)
 
     def restart(self):
         return self.client.restart(self.id)

+ 35 - 0
tests/integration/cli_test.py

@@ -84,6 +84,7 @@ class CLITestCase(DockerClientTestCase):
         self.command.dispatch(['build', '--no-cache', 'simple'], None)
         output = mock_stdout.getvalue()
         self.assertNotIn(cache_indicator, output)
+
     def test_up(self):
         self.command.dispatch(['up', '-d'], None)
         service = self.project.get_service('simple')
@@ -244,6 +245,40 @@ class CLITestCase(DockerClientTestCase):
         self.command.dispatch(['rm', '--force'], None)
         self.assertEqual(len(service.containers(stopped=True)), 0)
 
+    def test_kill(self):
+        self.command.dispatch(['up', '-d'], None)
+        service = self.project.get_service('simple')
+        self.assertEqual(len(service.containers()), 1)
+        self.assertTrue(service.containers()[0].is_running)
+
+        self.command.dispatch(['kill'], None)
+
+        self.assertEqual(len(service.containers(stopped=True)), 1)
+        self.assertFalse(service.containers(stopped=True)[0].is_running)
+
+    def test_kill_signal_sigint(self):
+        self.command.dispatch(['up', '-d'], None)
+        service = self.project.get_service('simple')
+        self.assertEqual(len(service.containers()), 1)
+        self.assertTrue(service.containers()[0].is_running)
+
+        self.command.dispatch(['kill', '-s', 'SIGINT'], None)
+
+        self.assertEqual(len(service.containers()), 1)
+        # The container is still running. It has been only interrupted
+        self.assertTrue(service.containers()[0].is_running)
+
+    def test_kill_interrupted_service(self):
+        self.command.dispatch(['up', '-d'], None)
+        service = self.project.get_service('simple')
+        self.command.dispatch(['kill', '-s', 'SIGINT'], None)
+        self.assertTrue(service.containers()[0].is_running)
+
+        self.command.dispatch(['kill', '-s', 'SIGKILL'], None)
+
+        self.assertEqual(len(service.containers(stopped=True)), 1)
+        self.assertFalse(service.containers(stopped=True)[0].is_running)
+
     def test_restart(self):
         service = self.project.get_service('simple')
         container = service.create_container()