Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Socket support #25

Merged
merged 8 commits into from
May 24, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/god.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
require 'god/conditions/disk_usage'
require 'god/conditions/complex'
require 'god/conditions/file_mtime'
require 'god/conditions/socket_responding'

require 'god/socket'
require 'god/driver'
Expand Down
132 changes: 132 additions & 0 deletions lib/god/conditions/socket_responding.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
require 'socket'
include Socket::Constants

module God
module Conditions
# Condition Symbol :socket_running
# Type: Poll
#
# Trigger when a TCP or UNIX socket is running or not
#
# Parameters
# Required
# +family+ is the family of socket: either 'tcp' or 'unix'
# --one of port or path--
# +port+ is the port (required if +family+ is 'tcp')
# +path+ is the path (required if +family+ is 'unix')
#
# Examples
#
# Trigger if the TCP socket on port 80 is not responding or the connection is refused
#
# on.condition(:socket_responding) do |c|
# c.family = 'tcp'
# c.port = '80'
# end
#
# Trigger if the socket is not responding or the connection is refused (use alternate compact +socket+ interface)
#
# on.condition(:socket_responding) do |c|
# c.socket = 'tcp:80'
# end
#
# Trigger if the socket is not responding or the connection is refused 5 times in a row
#
# on.condition(:socket_responding) do |c|
# c.socket = 'tcp:80'
# c.times = 5
# end
#
# Trigger if the Unix socket on path '/tmp/sock' is not responding or non-existent
#
# on.condition(:socket_responding) do |c|
# c.family = 'unix'
# c.port = '/tmp/sock'
# end
#


class SocketResponding < PollCondition
attr_accessor :family, :addr, :port, :path, :times

def initialize
super
# default to tcp on the localhost
self.family = 'tcp'
self.addr = '127.0.0.1'
# Set these to nil/0 values
self.port = 0
self.path = nil

self.times = [1, 1]
end

def prepare
if self.times.kind_of?(Integer)
self.times = [self.times, self.times]
end

@timeline = Timeline.new(self.times[1])
@history = Timeline.new(self.times[1])
end

def reset
@timeline.clear
@history.clear
end

def socket=(s)
components = s.split(':')
if components.size == 3
@family,@addr,@port = components
@port = @port.to_i
elsif components[0] =~ /^tcp$/
@family = components[0]
@port = components[1].to_i
elsif components[0] =~ /^unix$/
@family = components[0]
@path = components[1]
end
end

def valid?
valid = true
if self.family == 'tcp' and @port == 0
valid &= complain("Attribute 'port' must be specified for tcp sockets", self)
end
if self.family == 'unix' and self.path.nil?
valid &= complain("Attribute 'path' must be specified for unix sockets", self)
end
valid = false unless %w{tcp unix}.member?(self.family)
valid
end

def test
if self.family == 'tcp'
begin
s = TCPSocket.new(self.addr, self.port)
rescue SystemCallError
end
status = s.nil?
elsif self.family == 'unix'
begin
s = UNIXSocket.new(self.path)
rescue SystemCallError
end
status = s.nil?
else
status = false
end
@timeline.push(status)
history = "[" + @timeline.map {|s| s ? '*' : ''}.join(',') + "]"
if @timeline.select { |x| x }.size >= self.times.first
self.info = "socket out of bounds #{history}"
return true
else
self.info = "socket within bounds #{history}"
return false
end
end
end
end
end
122 changes: 122 additions & 0 deletions test/test_conditions_socket_responding.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
require File.dirname(__FILE__) + '/helper'

class TestConditionsSocketResponding < Test::Unit::TestCase
# valid?

def test_valid_should_return_false_if_no_options_set
c = Conditions::SocketResponding.new
c.watch = stub(:name => 'foo')
assert_equal false, c.valid?

end

def test_valid_should_return_true_if_required_options_set_for_default
c = Conditions::SocketResponding.new
c.port = 443
assert_equal true, c.valid?
end

def test_valid_should_return_true_if_required_options_set_for_tcp
c = Conditions::SocketResponding.new
c.family = 'tcp'
c.port = 443
assert_equal true, c.valid?
end

def test_valid_should_return_true_if_required_options_set_for_unix
c = Conditions::SocketResponding.new
c.path = 'some-path'
c.family = 'unix'
assert_equal true, c.valid?
end

def test_valid_should_return_true_if_family_is_tcp
c = Conditions::SocketResponding.new
c.port = 443
c.family = 'tcp'
assert_equal true, c.valid?
end

def test_valid_should_return_true_if_family_is_unix
c = Conditions::SocketResponding.new
c.path = 'some-path'
c.family = 'unix'
c.watch = stub(:name => 'foo')
assert_equal true, c.valid?
end

# socket method
def test_socket_should_return_127_0_0_1_for_default_addr
c = Conditions::SocketResponding.new
c.socket = 'tcp:443'
assert_equal c.addr, '127.0.0.1'
end

def test_socket_should_set_properties_for_tcp
c = Conditions::SocketResponding.new
c.socket = 'tcp:127.0.0.1:443'
assert_equal c.family, 'tcp'
assert_equal c.addr, '127.0.0.1'
assert_equal c.port, 443
# path should not be set for tcp sockets
assert_equal c.path, nil
end

def test_socket_should_set_properties_for_unix
c = Conditions::SocketResponding.new
c.socket = 'unix:/tmp/process.sock'
assert_equal c.family, 'unix'
assert_equal c.path, '/tmp/process.sock'
# path should not be set for unix domain sockets
assert_equal c.port, 0
end

# test

def test_test_tcp_should_return_false_if_socket_is_listening
c = Conditions::SocketResponding.new
c.prepare

TCPSocket.expects(:new).returns(0)
assert_equal false, c.test
end

def test_test_tcp_should_return_true_if_no_socket_is_listening
c = Conditions::SocketResponding.new
c.prepare

TCPSocket.expects(:new).returns(nil)
assert_equal true, c.test
end

def test_test_unix_should_return_false_if_socket_is_listening
c = Conditions::SocketResponding.new
c.socket = 'unix:/some/path'

c.prepare
UNIXSocket.expects(:new).returns(0)
assert_equal false, c.test
end

def test_test_unix_should_return_true_if_no_socket_is_listening

c = Conditions::SocketResponding.new
c.socket = 'unix:/some/path'
c.prepare

UNIXSocket.expects(:new).returns(nil)
assert_equal true, c.test
end

def test_test_unix_should_return_true_if_socket_is_listening_2_times

c = Conditions::SocketResponding.new
c.socket = 'unix:/some/path'
c.times = [2, 2]
c.prepare

UNIXSocket.expects(:new).returns(nil).times(2)
assert_equal false, c.test
assert_equal true, c.test
end
end