Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Added Storages::HealthService spec

Andreas Pfohlc5d114398330
diff --git a/modules/storages/app/services/storages/health_service.rb b/modules/storages/app/services/storages/health_service.rb
index 0e7b61df578..3e48a77a8cc 100644
--- a/modules/storages/app/services/storages/health_service.rb
+++ b/modules/storages/app/services/storages/health_service.rb
@@ -33,12 +33,12 @@ module Storages
end
def healthy
- if @storage.health_status == 'healthy'
+ if @storage.health_status == "healthy"
@storage.touch(:health_checked_at)
else
reason = @storage.health_reason
- @storage.update(health_status: 'healthy',
+ @storage.update(health_status: "healthy",
health_changed_at: Time.now.utc,
health_checked_at: Time.now.utc,
health_reason: nil)
@@ -50,18 +50,22 @@ module Storages
end
def unhealthy(reason:)
- if @storage.health_status == 'unhealthy'
- unless reason_is_same(reason)
- @storage.update(health_changed_at: Time.now.utc,
- health_checked_at: Time.now.utc,
+ time = Time.now.utc
+
+ if @storage.health_status == "unhealthy"
+ if reason_is_same(reason)
+ @storage.update(health_checked_at: time)
+ else
+ @storage.update(health_changed_at: time,
+ health_checked_at: time,
health_reason: reason)
notify_unhealthy_admin_users
end
else
- @storage.update(health_status: 'unhealthy',
- health_changed_at: Time.now.utc,
- health_checked_at: Time.now.utc,
+ @storage.update(health_status: "unhealthy",
+ health_changed_at: time,
+ health_checked_at: time,
health_reason: reason)
notify_unhealthy_admin_users
@@ -78,7 +82,7 @@ module Storages
def admin_users
User.where(admin: true)
- .where.not(mail: [nil, ''])
+ .where.not(mail: [nil, ""])
end
def schedule_mail_job(storage)
@@ -86,11 +90,11 @@ module Storages
end
def mail_job_exists?
- Delayed::Job.where('handler LIKE ?', "%job_class: Storages::HealthStatusMailerJob%").any?
+ Delayed::Job.where("handler LIKE ?", "%job_class: Storages::HealthStatusMailerJob%").any?
end
def reason_is_same(new_health_reason)
- @storage.health_reason_identifier == Storages::Storage.extract_part_from_piped_string(new_health_reason, 0)
+ @storage.health_reason_identifier == ::Storages::Storage.extract_part_from_piped_string(new_health_reason, 0)
end
end
end
diff --git a/modules/storages/spec/models/storages/nextcloud_storage_spec.rb b/modules/storages/spec/models/storages/nextcloud_storage_spec.rb
index 15c9c801b98..f66e59a7460 100644
--- a/modules/storages/spec/models/storages/nextcloud_storage_spec.rb
+++ b/modules/storages/spec/models/storages/nextcloud_storage_spec.rb
@@ -51,71 +51,6 @@ RSpec.describe Storages::NextcloudStorage do
it { expect(storage).not_to be_a_provider_type_one_drive }
end
- describe "health attributes" do
- it "can be marked as healthy or unhealthy" do
- healthy_time = Time.parse "2021-03-14T15:17:00Z"
- unhealthy_time = Time.parse "2023-03-14T15:17:00Z"
-
- Timecop.freeze(healthy_time) do
- expect do
- storage.mark_as_healthy
- end.to(change(storage, :health_changed_at).to(healthy_time)
- .and(change(storage, :health_status).from("pending").to("healthy")))
- expect(storage.health_healthy?).to be(true)
- expect(storage.health_unhealthy?).to be(false)
- end
-
- Timecop.freeze(unhealthy_time) do
- expect do
- storage.mark_as_unhealthy(reason: "thou_shall_not_pass_error")
- end.to(change(storage, :health_changed_at).from(healthy_time).to(unhealthy_time)
- .and(change(storage, :health_status).from("healthy").to("unhealthy"))
- .and(change(storage, :health_reason).from(nil).to("thou_shall_not_pass_error")))
- end
- expect(storage.health_healthy?).to be(false)
- expect(storage.health_unhealthy?).to be(true)
- end
-
- it "has the correct changed_at and checked_at attributes" do
- healthy_time = Time.parse "2021-03-14T15:17:00Z"
- unhealthy_time_a = Time.parse "2023-03-14T00:00:00Z"
- unhealthy_time_b = Time.parse "2023-03-14T22:22:00Z"
- unhealthy_time_c = Time.parse "2023-03-14T11:11:00Z"
- reason_a = "thou_shall_not_pass_error"
- reason_b = "inception_error"
-
- Timecop.freeze(healthy_time) do
- expect do
- storage.mark_as_healthy
- end.to(change(storage, :health_changed_at).to(healthy_time)
- .and(change(storage, :health_checked_at).to(healthy_time)))
- end
-
- Timecop.freeze(unhealthy_time_a) do
- expect do
- storage.mark_as_unhealthy(reason: reason_a)
- end.to(change(storage, :health_changed_at).from(healthy_time).to(unhealthy_time_a)
- .and(change(storage, :health_reason).from(nil).to(reason_a))
- .and(change(storage, :health_checked_at).from(healthy_time).to(unhealthy_time_a)))
- end
-
- Timecop.freeze(unhealthy_time_b) do
- expect do
- storage.mark_as_unhealthy(reason: reason_a)
- end.to(change(storage, :health_checked_at).from(unhealthy_time_a).to(unhealthy_time_b))
- expect(storage.health_changed_at).to eq(unhealthy_time_a)
- end
-
- Timecop.freeze(unhealthy_time_c) do
- expect do
- storage.mark_as_unhealthy(reason: reason_b)
- end.to(change(storage, :health_checked_at).from(unhealthy_time_b).to(unhealthy_time_c)
- .and(change(storage, :health_changed_at).from(unhealthy_time_a).to(unhealthy_time_c))
- .and(change(storage, :health_reason).from(reason_a).to(reason_b)))
- end
- end
- end
-
describe "#configured?" do
context "with a complete configuration" do
let(:storage) do
diff --git a/modules/storages/spec/services/storages/health_service_spec.rb b/modules/storages/spec/services/storages/health_service_spec.rb
new file mode 100644
index 00000000000..4d539d808a3
--- /dev/null
+++ b/modules/storages/spec/services/storages/health_service_spec.rb
@@ -0,0 +1,111 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require "spec_helper"
+require_module_spec_helper
+
+RSpec.describe Storages::HealthService do
+ let(:storage) { create(:nextcloud_storage) }
+ let(:health_service) { described_class.new(storage:) }
+
+ describe "health attributes" do
+ it "can be marked as healthy or unhealthy" do
+ healthy_time = Time.parse "2021-03-14T15:17:00Z"
+ unhealthy_time = Time.parse "2023-03-14T15:17:00Z"
+
+ Timecop.freeze(healthy_time) do
+ expect do
+ health_service.healthy
+ end.to(change(storage, :health_changed_at).to(healthy_time)
+ .and(change(storage, :health_status).from("pending").to("healthy")))
+ expect(storage.health_healthy?).to be(true)
+ expect(storage.health_unhealthy?).to be(false)
+ end
+
+ Timecop.freeze(unhealthy_time) do
+ expect do
+ health_service.unhealthy(reason: "thou_shall_not_pass_error")
+ end.to(change(storage, :health_changed_at).from(healthy_time).to(unhealthy_time)
+ .and(change(storage, :health_status).from("healthy").to("unhealthy"))
+ .and(change(storage, :health_reason).from(nil).to("thou_shall_not_pass_error")))
+ end
+ expect(storage.health_healthy?).to be(false)
+ expect(storage.health_unhealthy?).to be(true)
+ end
+
+ # rubocop:disable RSpec/ExampleLength
+ it "has the correct changed_at and checked_at attributes" do
+ healthy_time = Time.parse "2021-03-14T15:17:00Z"
+ unhealthy_time_a = Time.parse "2023-03-14T00:00:00Z"
+ unhealthy_time_b = Time.parse "2023-03-14T22:22:00Z"
+ unhealthy_time_c = Time.parse "2023-03-14T11:11:00Z"
+ reason_a = "thou_shall_not_pass_error"
+ reason_b = "inception_error"
+
+ Timecop.freeze(healthy_time) do
+ expect do
+ health_service.healthy
+ end.to(change(storage, :health_changed_at).to(healthy_time)
+ .and(change(storage, :health_checked_at).to(healthy_time)))
+ end
+
+ Timecop.freeze(unhealthy_time_a) do
+ expect do
+ health_service.unhealthy(reason: reason_a)
+ end.to(change(storage, :health_changed_at)
+ .from(healthy_time).to(unhealthy_time_a)
+ .and(change(storage, :health_reason)
+ .from(nil).to(reason_a))
+ .and(change(storage, :health_checked_at)
+ .from(healthy_time).to(unhealthy_time_a)))
+ end
+
+ Timecop.freeze(unhealthy_time_b) do
+ expect do
+ health_service.unhealthy(reason: reason_a)
+ end.to(change(storage, :health_checked_at)
+ .from(unhealthy_time_a).to(unhealthy_time_b))
+ expect(storage.health_changed_at).to eq(unhealthy_time_a)
+ end
+
+ Timecop.freeze(unhealthy_time_c) do
+ expect do
+ health_service.unhealthy(reason: reason_b)
+ end.to(change(storage, :health_checked_at)
+ .from(unhealthy_time_b).to(unhealthy_time_c)
+ .and(change(storage, :health_changed_at)
+ .from(unhealthy_time_a).to(unhealthy_time_c))
+ .and(change(storage, :health_reason)
+ .from(reason_a).to(reason_b)))
+ end
+ end
+ # rubocop:enable RSpec/ExampleLength
+ end
+end