summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/git/storage/health_spec.rb
blob: 2d3af3879718f6603d4c75ec145a29d6e14b99ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
require 'spec_helper'

describe Gitlab::Git::Storage::Health, clean_gitlab_redis_shared_state: true, broken_storage: true do
  let(:host1_key) { 'storage_accessible:broken:web01' }
  let(:host2_key) { 'storage_accessible:default:kiq01' }

  def set_in_redis(cache_key, value)
    Gitlab::Git::Storage.redis.with do |redis|
      redis.hmset(cache_key, :failure_count, value)
    end.first
  end

  describe '.for_failing_storages' do
    it 'only includes health status for failures' do
      set_in_redis(host1_key, 10)
      set_in_redis(host2_key, 0)

      expect(described_class.for_failing_storages.map(&:storage_name))
        .to contain_exactly('broken')
    end
  end

  describe '.load_for_keys' do
    let(:subject) do
      results = Gitlab::Git::Storage.redis.with do |redis|
        fake_future = double
        allow(fake_future).to receive(:value).and_return([host1_key])
        described_class.load_for_keys({ 'broken' => fake_future }, redis)
      end

      # Make sure the `Redis#future is loaded
      results.inject({}) do |result, (name, info)|
        info.each { |i| i[:failure_count] = i[:failure_count].value.to_i }

        result[name] = info

        result
      end
    end

    it 'loads when there is no info in redis' do
      expect(subject).to eq('broken' => [{ name: host1_key, failure_count: 0 }])
    end

    it 'reads the correct values for a storage from redis' do
      set_in_redis(host1_key, 5)
      set_in_redis(host2_key, 7)

      expect(subject).to eq('broken' => [{ name: host1_key, failure_count: 5 }])
    end
  end

  describe '.for_all_storages' do
    it 'loads health status for all configured storages' do
      healths = described_class.for_all_storages

      expect(healths.map(&:storage_name)).to contain_exactly('default', 'broken')
    end
  end

  describe '#failing_info' do
    it 'only contains storages that have failures' do
      health = described_class.new('broken', [{ name: host1_key, failure_count: 0 },
                                              { name: host2_key, failure_count: 3 }])

      expect(health.failing_info).to contain_exactly({ name: host2_key, failure_count: 3 })
    end
  end

  describe '#total_failures' do
    it 'sums up all the failures' do
      health = described_class.new('broken', [{ name: host1_key, failure_count: 2 },
                                              { name: host2_key, failure_count: 3 }])

      expect(health.total_failures).to eq(5)
    end
  end

  describe '#failing_on_hosts' do
    it 'collects only the failing hostnames' do
      health = described_class.new('broken', [{ name: host1_key, failure_count: 2 },
                                              { name: host2_key, failure_count: 0 }])

      expect(health.failing_on_hosts).to contain_exactly('web01')
    end
  end
end