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

Add support for tcp check type #207

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 7 additions & 3 deletions lib/puppet/parser/functions/consul_validate_checks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@ def validate_checks(obj)
end
elsif obj.key?("http")
if (! obj.key?("interval"))
raise Puppet::ParseError.new('http must be defined for interval checks')
raise Puppet::ParseError.new('interval must be defined for http checks')
end
elsif obj.key?("tcp")
if (! obj.key?("interval"))
raise Puppet::ParseError.new('interval must be defined for tcp checks')
end
elsif obj.key?("script")
if (! obj.key?("interval"))
raise Puppet::ParseError.new('script must be defined for interval checks')
raise Puppet::ParseError.new('interval must be defined for script checks')
end
else
raise Puppet::ParseError.new('One of ttl, script, or http must be defined.')
raise Puppet::ParseError.new('One of http, script, tcp, or ttl must be defined.')
end
else
raise Puppet::ParseError.new("Unable to handle object of type <%s>" % obj.class.to_s)
Expand Down
6 changes: 6 additions & 0 deletions manifests/check.pp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
# Full path to the location of the healthcheck script. Must be nagios
# compliant with regards to the return codes.
#
# [*tcp*]
# The IP/hostname and port for the service healthcheck. Should be in
# 'hostname:port' format.
#
# [*interval*]
# Value in seconds for the interval between runs of the check
#
Expand All @@ -44,6 +48,7 @@
$ttl = undef,
$http = undef,
$script = undef,
$tcp = undef,
$interval = undef,
$service_id = undef,
$timeout = undef,
Expand All @@ -58,6 +63,7 @@
'ttl' => $ttl,
'http' => $http,
'script' => $script,
'tcp' => $tcp,
'interval' => $interval,
'timeout ' => $timeout,
'service_id' => $service_id,
Expand Down
58 changes: 56 additions & 2 deletions spec/defines/consul_check_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,52 @@
.that_notifies("Class[consul::reload_service]") \
}
end
describe 'with tcp' do
let(:params) {{
'tcp' => 'localhost:80',
'interval' => '30s',
}}
it {
should contain_file("/etc/consul/check_my_check.json") \
.with_content(/"id" *: *"my_check"/) \
.with_content(/"name" *: *"my_check"/) \
.with_content(/"check" *: *\{/) \
.with_content(/"tcp" *: *"localhost:80"/) \
.with_content(/"interval" *: *"30s"/)
}
end
describe 'with script and service_id' do
let(:params) {{
'tcp' => 'localhost:80',
'interval' => '30s',
'service_id' => 'my_service'
}}
it {
should contain_file("/etc/consul/check_my_check.json") \
.with_content(/"id" *: *"my_check"/) \
.with_content(/"name" *: *"my_check"/) \
.with_content(/"check" *: *\{/) \
.with_content(/"tcp" *: *"localhost:80"/) \
.with_content(/"interval" *: *"30s"/) \
.with_content(/"service_id" *: *"my_service"/)
}
end
describe 'reload service with script and token' do
let(:params) {{
'tcp' => 'localhost:80',
'interval' => '30s',
'token' => 'too-cool-for-this-script'
}}
it {
should contain_file("/etc/consul/check_my_check.json") \
.with_content(/"id" *: *"my_check"/) \
.with_content(/"name" *: *"my_check"/) \
.with_content(/"tcp" *: *"localhost:80"/) \
.with_content(/"interval" *: *"30s"/) \
.with_content(/"token" *: *"too-cool-for-this-script"/) \
.that_notifies("Class[consul::reload_service]") \
}
end
describe 'with both ttl and interval' do
let(:params) {{
'ttl' => '30s',
Expand Down Expand Up @@ -197,15 +243,23 @@
'script' => 'true',
}}
it {
should raise_error(Puppet::Error, /script must be defined for interval checks/)
should raise_error(Puppet::Error, /interval must be defined for script checks/)
}
end
describe 'with http but no interval' do
let(:params) {{
'http' => 'http://localhost/health',
}}
it {
should raise_error(Puppet::Error, /http must be defined for interval checks/)
should raise_error(Puppet::Error, /interval must be defined for http checks/)
}
end
describe 'with tcp but no interval' do
let(:params) {{
'tcp' => 'localhost:80',
}}
it {
should raise_error(Puppet::Error, /interval must be defined for tcp checks/)
}
end
describe 'with a / in the id' do
Expand Down
18 changes: 18 additions & 0 deletions spec/functions/consul_validate_checks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,22 @@
}
]).and_raise_error(Exception) }
end

describe 'validate tcp check' do
it {should run.with_params([
{
'tcp' => 'localhost:80',
'interval' => '30s',
}
])}
end

describe 'validate tcp missing interval' do
it {should run.with_params([
{
'tcp' => 'localhost:80'
}
]).and_raise_error(Exception) }
end

end