diff --git a/CHANGELOG.md b/CHANGELOG.md index bedb73be..26b39acd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# Unreleased + +* Add new Redis healthcheck and relevant tests (https://github.com/alphagov/govuk_app_config/pull/183) + # 2.8.2 * Allow apps to configure the host and protocol for Statsd ([#180](https://github.com/alphagov/govuk_app_config/pull/180)) diff --git a/lib/govuk_app_config/govuk_healthcheck.rb b/lib/govuk_app_config/govuk_healthcheck.rb index 0244e6be..71e29edd 100644 --- a/lib/govuk_app_config/govuk_healthcheck.rb +++ b/lib/govuk_app_config/govuk_healthcheck.rb @@ -2,6 +2,7 @@ require "govuk_app_config/govuk_healthcheck/active_record" require "govuk_app_config/govuk_healthcheck/mongoid" require "govuk_app_config/govuk_healthcheck/rails_cache" +require "govuk_app_config/govuk_healthcheck/redis" require "govuk_app_config/govuk_healthcheck/sidekiq_redis" require "govuk_app_config/govuk_healthcheck/threshold_check" require "govuk_app_config/govuk_healthcheck/sidekiq_queue_check" diff --git a/lib/govuk_app_config/govuk_healthcheck/redis.rb b/lib/govuk_app_config/govuk_healthcheck/redis.rb new file mode 100644 index 00000000..7042679c --- /dev/null +++ b/lib/govuk_app_config/govuk_healthcheck/redis.rb @@ -0,0 +1,21 @@ +module GovukHealthcheck + class Redis + def name + :redis + end + + def status + client = ::Redis.new + + client.set("healthcheck", "val") + client.get("healthcheck") + client.del("healthcheck") + + client.close + + GovukHealthcheck::OK + rescue StandardError + GovukHealthcheck::CRITICAL + end + end +end diff --git a/spec/lib/govuk_healthcheck/redis_spec.rb b/spec/lib/govuk_healthcheck/redis_spec.rb new file mode 100644 index 00000000..ccc9a453 --- /dev/null +++ b/spec/lib/govuk_healthcheck/redis_spec.rb @@ -0,0 +1,30 @@ +require "spec_helper" +require "govuk_app_config/govuk_healthcheck" +require_relative "shared_interface" + +RSpec.describe GovukHealthcheck::Redis do + let(:redis) { double(:redis) } + let(:redis_client) { double(:redis_client, set: "OK", get: "val", del: 1, close: nil) } + + before do + stub_const("Redis", redis) + allow(redis).to receive(:new).and_return(redis_client) + end + + context "when the database is connected" do + before { allow(redis_client).to receive(:set) } + it_behaves_like "a healthcheck" + it "returns OK" do + expect(redis_client).to receive(:set).with(anything, anything) + expect(subject.status).to eq(GovukHealthcheck::OK) + end + end + + context "when the database is not connected" do + before { allow(redis_client).to receive(:set).with(anything, anything).and_raise("error") } + it_behaves_like "a healthcheck" + it "returns CRITICAL" do + expect(subject.status).to eq(GovukHealthcheck::CRITICAL) + end + end +end