summaryrefslogtreecommitdiff
path: root/spec/workers/cluster_wait_for_ingress_ip_address_worker_spec.rb
blob: aea924c6dd66fb821ce1d8d241b529a863012086 (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
88
89
90
91
92
93
94
95
require 'spec_helper'

describe ClusterWaitForIngressIpAddressWorker do
  describe '#perform' do
    let(:service) { instance_double(Clusters::Applications::CheckIngressIpAddressService, execute: true) }
    let(:application) { instance_double(Clusters::Applications::Ingress) }
    let(:worker) { described_class.new }

    before do
      allow(worker)
        .to receive(:find_application)
        .with('ingress', 117)
        .and_yield(application)

      allow(Clusters::Applications::CheckIngressIpAddressService)
        .to receive(:new)
        .with(application)
        .and_return(service)

      allow(described_class)
        .to receive(:perform_in)
    end

    it 'finds the application and calls CheckIngressIpAddressService#execute' do
      worker.perform('ingress', 117, 2)

      expect(service).to have_received(:execute)
    end

    context 'when the service succeeds' do
      it 'does not schedule another worker' do
        worker.perform('ingress', 117, 2)

        expect(described_class)
          .not_to have_received(:perform_in)
      end
    end

    context 'when the service fails' do
      before do
        allow(service)
          .to receive(:execute)
          .and_return(false)
      end

      context 'when there are retries remaining' do
        it 'schedules another worker with 1 less retry' do
          worker.perform('ingress', 117, 2)

          expect(described_class)
            .to have_received(:perform_in)
            .with(ClusterWaitForIngressIpAddressWorker::INTERVAL, 'ingress', 117, 1)
        end
      end

      context 'when there are no retries_remaining' do
        it 'does not schedule another worker' do
          worker.perform('ingress', 117, 0)

          expect(described_class)
            .not_to have_received(:perform_in)
        end
      end
    end

    context 'when the update raises exception' do
      before do
        allow(service)
          .to receive(:execute)
          .and_raise(Clusters::Applications::CheckIngressIpAddressService::Error, "something went wrong")
      end

      context 'when there are retries remaining' do
        it 'schedules another worker with 1 less retry and re-raises the error' do
          expect { worker.perform('ingress', 117, 2) }
            .to raise_error(Clusters::Applications::CheckIngressIpAddressService::Error, "something went wrong")

          expect(described_class)
            .to have_received(:perform_in)
            .with(ClusterWaitForIngressIpAddressWorker::INTERVAL, 'ingress', 117, 1)
        end
      end

      context 'when there are no retries_remaining' do
        it 'does not schedule another worker but re-raises the error' do
          expect { worker.perform('ingress', 117, 0) }
            .to raise_error(Clusters::Applications::CheckIngressIpAddressService::Error, "something went wrong")

          expect(described_class)
            .not_to have_received(:perform_in)
        end
      end
    end
  end
end