[DRE-commits] [ruby-aruba] 05/74: Set signal used to stop/terminate a command

Hideki Yamane henrich at moszumanska.debian.org
Sat Nov 28 01:16:27 UTC 2015


This is an automated email from the git hooks/post-receive script.

henrich pushed a commit to branch debian/sid
in repository ruby-aruba.

commit 0988fec6e0333330b20c1bb790d2e611cfdb7f4d
Author: Dennis Günnewig <dg1 at ratiodata.de>
Date:   Wed Nov 18 16:07:26 2015 +0100

    Set signal used to stop/terminate a command
---
 History.md                           | 11 +++++-
 features/api/command/run.feature     | 70 ++++++++++++++++++++++++++++++++++++
 lib/aruba/announcer.rb               |  1 +
 lib/aruba/api/command.rb             | 13 +++++--
 lib/aruba/command.rb                 |  3 +-
 lib/aruba/config.rb                  |  1 +
 lib/aruba/cucumber/core.rb           |  4 +++
 lib/aruba/cucumber/hooks.rb          |  5 +++
 lib/aruba/processes/basic_process.rb |  8 ++++-
 lib/aruba/processes/in_process.rb    |  9 ++++-
 lib/aruba/processes/spawn_process.rb | 31 +++++++++++++---
 lib/aruba/rspec.rb                   |  2 ++
 12 files changed, 147 insertions(+), 11 deletions(-)

diff --git a/History.md b/History.md
index f1ff78b..5afe172 100644
--- a/History.md
+++ b/History.md
@@ -1,14 +1,23 @@
 # Latest Release
 
+## [v0.11.0](https://github.com/cucumber/aruba/compare/v0.10.2...v0.11.0)
+
+* Set stop signal which should be used to stop a process after a timeout or
+  used to terminate a process. This can be used to stop processes running
+  docker + "systemd". If you send a systemd-enable container SIGINT it will be
+  stopped.
+
+# Old releases
+
 ## [v0.10.2](https://github.com/cucumber/aruba/compare/v0.10.1...v0.10.2)
 
 * Fixed problem in regex after merge of step definitions
 
+
 ## [v0.10.1](https://github.com/cucumber/aruba/compare/v0.10.0...v0.10.1)
 
 * Merged remove steps for file and directory from 4 into 2 step definitions
 
-# Old releases
 
 ## [v0.10.0](https://github.com/cucumber/aruba/compare/v0.10.0.pre2...v0.10.0)
 
diff --git a/features/api/command/run.feature b/features/api/command/run.feature
index d03bc92..949d969 100644
--- a/features/api/command/run.feature
+++ b/features/api/command/run.feature
@@ -53,3 +53,73 @@ Feature: Run command
     """
     When I run `rspec`
     Then the specs should all pass
+
+  Scenario: Stop successful command with configured signal
+    Given an executable named "bin/cli" with:
+    """bash
+    #!/bin/bash
+    function usr1 {
+      echo "Exit..."
+      exit 0
+    }
+
+    function term {
+      echo "No! No exit here. Try USR1. I stop the command with exit 1."
+      exit 1
+    }
+
+    trap usr1 USR1
+    trap term TERM
+    while [ true ]; do sleep 1; done
+    """
+    And a file named "spec/run_spec.rb" with:
+    """ruby
+    require 'spec_helper'
+
+    Aruba.configure do |config|
+      config.stop_signal  = 'USR1'
+      config.exit_timeout = 1
+    end
+
+    RSpec.describe 'Run command', :type => :aruba do
+      before(:each) { run('cli') }
+      it { expect(last_command_started).to be_successfully_executed }
+    end
+    """
+    When I run `rspec`
+    Then the specs should all pass
+
+  Scenario: Stop unsuccessful command with configured signal
+    Given an executable named "bin/cli" with:
+    """bash
+    #!/bin/bash
+    function usr1 {
+      echo "Exit..."
+      exit 2
+    }
+
+    function term {
+      echo "No! No exit here. Try USR1. I stop the command with exit 1."
+      exit 1
+    }
+
+    trap usr1 USR1
+    trap term TERM
+    while [ true ]; do sleep 1; done
+    """
+    And a file named "spec/run_spec.rb" with:
+    """ruby
+    require 'spec_helper'
+
+    Aruba.configure do |config|
+      config.stop_signal  = 'USR1'
+      config.exit_timeout = 1
+    end
+
+    RSpec.describe 'Run command', :type => :aruba do
+      before(:each) { run('cli') }
+      it { expect(last_command_started).to have_exit_status 2 }
+    end
+    """
+    When I run `rspec`
+    Then the specs should all pass
diff --git a/lib/aruba/announcer.rb b/lib/aruba/announcer.rb
index c9c91f4..24d7e3a 100644
--- a/lib/aruba/announcer.rb
+++ b/lib/aruba/announcer.rb
@@ -84,6 +84,7 @@ module Aruba
       output_format :modified_environment, proc { |n, v| format('$ export %s=%s', n, Shellwords.escape(v)) }
       output_format :full_environment, proc { |h| Aruba.platform.simple_table(h) }
       output_format :timeout, '# %s-timeout: %s seconds'
+      output_format :stop_signal, proc { |p, s| format('Command will be stopped with `kill -%s %s`', s, p) }
       output_format :stderr, "<<-STDERR\n%s\nSTDERR"
       output_format :stdout, "<<-STDOUT\n%s\nSTDOUT"
 
diff --git a/lib/aruba/api/command.rb b/lib/aruba/api/command.rb
index 90e7961..223958c 100644
--- a/lib/aruba/api/command.rb
+++ b/lib/aruba/api/command.rb
@@ -122,13 +122,18 @@ module Aruba
       # @param [Integer] timeout
       #   If the timeout is reached the command will be killed
       #
+      # @param [String] stop_signal
+      #   Use signal to stop command (Private)
+      #
       # @yield [SpawnProcess]
       #   Run block with process
       #
       # rubocop:disable Metrics/MethodLength
-      def run(cmd, exit_timeout = nil, io_wait_timeout = nil)
+      # rubocop:disable Metrics/CyclomaticComplexity
+      def run(cmd, exit_timeout = nil, io_wait_timeout = nil, stop_signal = nil)
         exit_timeout ||= aruba.config.exit_timeout
         io_wait_timeout ||= aruba.config.io_wait_timeout
+        stop_signal ||= aruba.config.stop_signal
 
         @commands ||= []
         @commands << cmd
@@ -169,7 +174,8 @@ module Aruba
           :io_wait_timeout   => io_wait_timeout,
           :working_directory => working_directory,
           :environment       => environment,
-          :main_class        => main_class
+          :main_class        => main_class,
+          :stop_signal       => stop_signal
         )
 
         if aruba.config.before? :cmd
@@ -184,10 +190,13 @@ module Aruba
         process_monitor.register_process(cmd, command)
         command.start
 
+        announcer.announce(:stop_signal, command.pid, stop_signal) if stop_signal
+
         aruba.config.after(:command, self, command)
 
         block_given? ? yield(command) : command
       end
+      # rubocop:enable Metrics/CyclomaticComplexity
       # rubocop:enable Metrics/MethodLength
 
       # Run a command with aruba
diff --git a/lib/aruba/command.rb b/lib/aruba/command.rb
index fc696b8..b5891c0 100644
--- a/lib/aruba/command.rb
+++ b/lib/aruba/command.rb
@@ -18,7 +18,8 @@ module Aruba
         opts.fetch(:io_wait_timeout),
         opts.fetch(:working_directory),
         opts.fetch(:environment),
-        opts.fetch(:main_class)
+        opts.fetch(:main_class),
+        opts.fetch(:stop_signal)
       )
     rescue KeyError => e
       raise ArgumentError, e.message
diff --git a/lib/aruba/config.rb b/lib/aruba/config.rb
index a1341a4..f335f78 100644
--- a/lib/aruba/config.rb
+++ b/lib/aruba/config.rb
@@ -29,6 +29,7 @@ module Aruba
     end
 
     option_accessor :exit_timeout, :contract => { Num => Num }, :default => 15
+    option_accessor :stop_signal, :contract => { Maybe[String] => Maybe[String] }, :default => nil
     option_accessor :io_wait_timeout, :contract => { Num => Num }, :default => 0.1
     option_accessor :fixtures_directories, :contract => { Array => ArrayOf[String] }, :default => %w(features/fixtures spec/fixtures test/fixtures fixtures)
     option_accessor :command_runtime_environment, :contract => { Hash => Hash }, :default => ENV.to_hash.dup
diff --git a/lib/aruba/cucumber/core.rb b/lib/aruba/cucumber/core.rb
index c93a804..b9e323a 100644
--- a/lib/aruba/cucumber/core.rb
+++ b/lib/aruba/cucumber/core.rb
@@ -27,3 +27,7 @@ end
 Given(/the default aruba exit timeout is (\d+) seconds/) do |seconds|
   aruba.config.exit_timeout = seconds.to_i
 end
+
+Given(/the default aruba stop signal is "([^"]*)"/) do |signal|
+  aruba.config.stop_signal = signal
+end
diff --git a/lib/aruba/cucumber/hooks.rb b/lib/aruba/cucumber/hooks.rb
index 17627a7..8740b49 100644
--- a/lib/aruba/cucumber/hooks.rb
+++ b/lib/aruba/cucumber/hooks.rb
@@ -65,6 +65,10 @@ Before('@announce-directory') do
   announcer.activate :directory
 end
 
+Before('@announce-stop-signal') do
+  announcer.activate :stop_signal
+end
+
 Before('@announce-env') do
   Aruba.platform.deprecated 'The use of "@announce-env"-hook is deprecated. Please use "@announce-modified-environment"'
 
@@ -98,6 +102,7 @@ Before('@announce') do
   announcer.activate :full_environment
   announcer.activate :environment
   announcer.activate :timeout
+  announcer.activate :stop_signal
 end
 
 Before('@debug') do
diff --git a/lib/aruba/processes/basic_process.rb b/lib/aruba/processes/basic_process.rb
index 3389a43..23e69f1 100644
--- a/lib/aruba/processes/basic_process.rb
+++ b/lib/aruba/processes/basic_process.rb
@@ -6,12 +6,13 @@ module Aruba
     class BasicProcess
       attr_reader :exit_status, :environment
 
-      def initialize(cmd, exit_timeout, io_wait, working_directory, environment = ENV.to_hash.dup, main_class = nil)
+      def initialize(cmd, exit_timeout, io_wait, working_directory, environment = ENV.to_hash.dup, main_class = nil, stop_signal = nil)
         @cmd               = cmd
         @working_directory = working_directory
         @environment       = environment
         @main_class        = main_class
         @exit_status       = nil
+        @stop_signal       = stop_signal
 
         @exit_timeout = exit_timeout
         @io_wait      = io_wait
@@ -22,6 +23,11 @@ module Aruba
         @cmd
       end
 
+      # Output pid of process
+      def pid
+        'No implemented'
+      end
+
       # Output stderr and stdout
       def output(opts = {})
         stdout(opts) + stderr(opts)
diff --git a/lib/aruba/processes/in_process.rb b/lib/aruba/processes/in_process.rb
index 9759c40..3db943c 100644
--- a/lib/aruba/processes/in_process.rb
+++ b/lib/aruba/processes/in_process.rb
@@ -33,7 +33,7 @@ module Aruba
       # @private
       attr_reader :main_class
 
-      def initialize(cmd, exit_timeout, io_wait, working_directory, environment = ENV.to_hash.dup, main_class = nil)
+      def initialize(cmd, exit_timeout, io_wait, working_directory, environment = ENV.to_hash.dup, main_class = nil, stop_signal = nil)
         @cmd               = cmd
         @argv              = arguments
         @stdin             = StringIO.new
@@ -90,6 +90,13 @@ module Aruba
       def terminate
         stop
       end
+
+      # Output pid of process
+      #
+      # This is the PID of the ruby process! So be careful
+      def pid
+        $PROCESS_ID
+      end
     end
   end
 end
diff --git a/lib/aruba/processes/spawn_process.rb b/lib/aruba/processes/spawn_process.rb
index c92a500..a103731 100644
--- a/lib/aruba/processes/spawn_process.rb
+++ b/lib/aruba/processes/spawn_process.rb
@@ -27,12 +27,12 @@ module Aruba
       #
       # @params [String] working_directory
       #   The directory where the command will be executed
-      def initialize(cmd, exit_timeout, io_wait, working_directory, environment = ENV.to_hash.dup, main_class = nil)
+      def initialize(cmd, exit_timeout, io_wait, working_directory, environment = ENV.to_hash.dup, main_class = nil, stop_signal = nil)
         super
 
         @process      = nil
-        @stdout_cache       = nil
-        @stderr_cache       = nil
+        @stdout_cache = nil
+        @stderr_cache = nil
       end
 
       # Run the command
@@ -163,6 +163,7 @@ module Aruba
         end
       end
 
+      # rubocop:disable Metrics/MethodLength
       def stop(reader)
         @stopped = true
 
@@ -172,7 +173,14 @@ module Aruba
           @process.poll_for_exit(@exit_timeout) unless @process.exited?
         rescue ChildProcess::TimeoutError
           @timed_out = true
-          @process.stop
+
+          if @stop_signal
+            Process.kill @stop_signal, pid
+            # set the exit status
+            @process.wait
+          else
+            @process.stop
+          end
         end
 
         @exit_status = @process.exit_code
@@ -188,14 +196,27 @@ module Aruba
 
         @exit_status
       end
+      # rubocop:enable Metrics/MethodLength
 
       def terminate
         return unless @process
 
-        @process.stop
+        if @stop_signal
+          Process.kill @stop_signal, pid
+        else
+          @process.stop
+        end
+
         stop nil
       end
 
+      # Output pid of process
+      #
+      # This is the PID of the spawned process.
+      def pid
+        @process.pid
+      end
+
       private
 
       def wait_for_io(time_to_wait, &block)
diff --git a/lib/aruba/rspec.rb b/lib/aruba/rspec.rb
index d6c0cdc..2364c1e 100644
--- a/lib/aruba/rspec.rb
+++ b/lib/aruba/rspec.rb
@@ -53,6 +53,7 @@ RSpec.configure do |config|
     announcer.activate(:directory)            if example.metadata[:announce_directory]
     announcer.activate(:stdout)               if example.metadata[:announce_stdout]
     announcer.activate(:stderr)               if example.metadata[:announce_stderr]
+    announcer.activate(:stop_signal)          if example.metadata[:announce_stop_signal]
 
     if example.metadata[:announce_output]
       announcer.activate(:stderr)
@@ -67,6 +68,7 @@ RSpec.configure do |config|
       announcer.activate(:full_environment)
       announcer.activate(:command)
       announcer.activate(:directory)
+      announcer.activate(:stop_signal)
     end
   end
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-ruby-extras/ruby-aruba.git



More information about the Pkg-ruby-extras-commits mailing list