Skip to content
This repository has been archived by the owner on Mar 6, 2020. It is now read-only.

Commit

Permalink
Merge pull request #53 from sensu/feature/client-deregistration
Browse files Browse the repository at this point in the history
Validate client deregistration and registration status
  • Loading branch information
portertech authored Nov 22, 2016
2 parents 1b15735 + e06cd08 commit 71e9f03
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
32 changes: 32 additions & 0 deletions lib/sensu/settings/validators/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,37 @@ def validate_client_registration(client)
invalid(client, "client registration must be a hash")
if is_a_hash?(client[:registration])
validate_client_registration_handlers(client)
must_be_an_integer_if_set(client[:registration][:status]) ||
invalid(client, "client registration status must be an integer")
end
end

# Validate client deregistration handlers.
# Validates: deregistration (handler, handlers)
#
# @param client [Hash] sensu client definition.
def validate_client_deregistration_handlers(client)
must_be_a_string_if_set(client[:deregistration][:handler]) ||
invalid(client, "client deregistration handler must be a string")
must_be_an_array_if_set(client[:deregistration][:handlers]) ||
invalid(client, "client deregistration handlers must be an array")
if is_an_array?(client[:deregistration][:handlers])
items_must_be_strings(client[:deregistration][:handlers]) ||
invalid(client, "client deregistration handlers must each be a string")
end
end

# Validate client deregistration.
# Validates: deregistration
#
# @param client [Hash] sensu client definition.
def validate_client_deregistration(client)
must_be_a_hash_if_set(client[:deregistration]) ||
invalid(client, "client deregistration must be a hash")
if is_a_hash?(client[:deregistration])
validate_client_deregistration_handlers(client)
must_be_an_integer_if_set(client[:deregistration][:status]) ||
invalid(client, "client deregistration status must be an integer")
end
end

Expand All @@ -163,6 +194,7 @@ def validate_client(client)
validate_client_redact(client)
validate_client_signature(client)
validate_client_registration(client)
validate_client_deregistration(client)
end
end
end
Expand Down
49 changes: 49 additions & 0 deletions spec/validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,55 @@
client[:registration][:handlers] = ["foo"]
@validator.validate_client(client)
expect(@validator.reset).to eq(0)
client[:registration][:status] = true
@validator.validate_client(client)
expect(@validator.reset).to eq(1)
client[:registration][:status] = "3"
@validator.validate_client(client)
expect(@validator.reset).to eq(1)
client[:registration][:status] = 3
@validator.validate_client(client)
expect(@validator.reset).to eq(0)
end

it "can validate client deregistration" do
client = {
:name => "foo",
:address => "127.0.0.1",
:subscriptions => ["bar"]
}
@validator.validate_client(client)
expect(@validator.reset).to eq(0)
client[:deregistration] = true
@validator.validate_client(client)
expect(@validator.reset).to eq(1)
client[:deregistration] = {}
@validator.validate_client(client)
expect(@validator.reset).to eq(0)
client[:deregistration][:handler] = 1
@validator.validate_client(client)
expect(@validator.reset).to eq(1)
client[:deregistration][:handler] = "foo"
@validator.validate_client(client)
expect(@validator.reset).to eq(0)
client[:deregistration][:handlers] = 1
@validator.validate_client(client)
expect(@validator.reset).to eq(1)
client[:deregistration][:handlers] = [1]
@validator.validate_client(client)
expect(@validator.reset).to eq(1)
client[:deregistration][:handlers] = ["foo"]
@validator.validate_client(client)
expect(@validator.reset).to eq(0)
client[:deregistration][:status] = true
@validator.validate_client(client)
expect(@validator.reset).to eq(1)
client[:deregistration][:status] = "3"
@validator.validate_client(client)
expect(@validator.reset).to eq(1)
client[:deregistration][:status] = 3
@validator.validate_client(client)
expect(@validator.reset).to eq(0)
end

it "can run, validating client" do
Expand Down

0 comments on commit 71e9f03

Please sign in to comment.