Files
ubicloud/spec/serializers/postgres_spec.rb
Burak Yucesoy 25381f50bc Remove hard dependency to MinIO connection in Postgres serializer
We need to connect to MinIO cluster to calculate earliest restore time, however
this is not always possible and can error out due to various reasons. In such
cases, instead of returning 500, it is better to just hide restore UI. This
commit does that by surrounding the code that tries to connect MinIO with a
rescue block.
2024-12-05 14:49:56 +01:00

55 lines
3.2 KiB
Ruby

# frozen_string_literal: true
require_relative "../spec_helper"
RSpec.describe Serializers::Postgres do
let(:pg) { PostgresResource.new(name: "pg-name").tap { _1.id = "69c0f4cd-99c1-8ed0-acfe-7b013ce2fa0b" } }
it "can serialize when no earliest/latest restore times" do
expect(pg).to receive(:strand).and_return(instance_double(Strand, label: "start")).at_least(:once)
expect(pg).to receive(:timeline).and_return(instance_double(PostgresTimeline, earliest_restore_time: nil, latest_restore_time: nil)).exactly(3)
expect(pg).to receive(:representative_server).and_return(instance_double(PostgresServer, primary?: true, vm: nil, strand: nil)).at_least(:once)
data = described_class.serialize(pg, {detailed: true})
expect(data[:earliest_restore_time]).to be_nil
expect(data[:latest_restore_time]).to be_nil
end
it "can serialize when earliest_restore_time calculation raises an exception" do
expect(pg).to receive(:strand).and_return(instance_double(Strand, label: "start")).at_least(:once)
expect(pg).to receive(:timeline).and_return(instance_double(PostgresTimeline, latest_restore_time: nil)).exactly(4)
expect(pg).to receive(:representative_server).and_return(instance_double(PostgresServer, primary?: true, vm: nil, strand: nil)).at_least(:once)
expect(pg.timeline).to receive(:earliest_restore_time).and_raise("error")
data = described_class.serialize(pg, {detailed: true})
expect(data[:earliest_restore_time]).to be_nil
expect(data[:latest_restore_time]).to be_nil
end
it "can serialize when have earliest/latest restore times" do
time = Time.now
expect(pg).to receive(:strand).and_return(instance_double(Strand, label: "start")).at_least(:once)
expect(pg).to receive(:timeline).and_return(instance_double(PostgresTimeline, earliest_restore_time: time, latest_restore_time: time)).exactly(3)
expect(pg).to receive(:representative_server).and_return(instance_double(PostgresServer, primary?: true, vm: nil, strand: nil)).at_least(:once)
data = described_class.serialize(pg, {detailed: true})
expect(data[:earliest_restore_time]).to eq(time.iso8601)
expect(data[:latest_restore_time]).to eq(time.iso8601)
end
it "can serialize when not primary" do
expect(pg).to receive(:strand).and_return(instance_double(Strand, label: "start")).at_least(:once)
expect(pg).to receive(:representative_server).and_return(instance_double(PostgresServer, primary?: false, vm: nil, strand: nil)).at_least(:once)
data = described_class.serialize(pg, {detailed: true})
expect(data[:earliest_restore_time]).to be_nil
expect(data[:latest_restore_time]).to be_nil
end
it "can serialize when there is no server" do
expect(pg).to receive(:strand).and_return(instance_double(Strand, label: "start")).at_least(:once)
expect(pg).to receive(:timeline).and_return(instance_double(PostgresTimeline, earliest_restore_time: nil, latest_restore_time: nil))
expect(pg).to receive(:representative_server).and_return(nil).at_least(:once)
data = described_class.serialize(pg, {detailed: true})
expect(data[:primary?]).to be_nil
expect(data[:earliest_restore_time]).to be_nil
expect(data[:latest_restore_time]).to be_nil
end
end