Welcome to mirror list, hosted at ThFree Co, Russian Federation.

rollout_status_spec.rb « kubernetes « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8ed9fdd799ca161c991d35d38333f73cb642373a (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Kubernetes::RolloutStatus do
  include KubernetesHelpers

  let(:track) { nil }
  let(:specs) { specs_all_finished }

  let(:pods) do
    create_pods(name: "one", count: 3, track: 'stable') + create_pods(name: "two", count: 3, track: "canary")
  end

  let(:ingresses) { [] }

  let(:specs_all_finished) do
    [
      kube_deployment(name: 'one'),
      kube_deployment(name: 'two', track: track)
    ]
  end

  let(:specs_half_finished) do
    [
      kube_deployment(name: 'one'),
      kube_deployment(name: 'two', track: track)
    ]
  end

  subject(:rollout_status) { described_class.from_deployments(*specs, pods_attrs: pods, ingresses: ingresses) }

  describe '#deployments' do
    it 'stores the deployments' do
      expect(rollout_status.deployments).to be_kind_of(Array)
      expect(rollout_status.deployments.size).to eq(2)
      expect(rollout_status.deployments.first).to be_kind_of(::Gitlab::Kubernetes::Deployment)
    end
  end

  describe '#instances' do
    context 'for stable track' do
      let(:track) { "any" }

      let(:pods) do
        create_pods(name: "one", count: 3, track: 'stable') + create_pods(name: "two", count: 3, track: "any")
      end

      it 'stores the union of deployment instances' do
        expected = [
          { status: 'running', pod_name: "two", tooltip: 'two (Running)', track: 'any', stable: false },
          { status: 'running', pod_name: "two", tooltip: 'two (Running)', track: 'any', stable: false },
          { status: 'running', pod_name: "two", tooltip: 'two (Running)', track: 'any', stable: false },
          { status: 'running', pod_name: "one", tooltip: 'one (Running)', track: 'stable', stable: true },
          { status: 'running', pod_name: "one", tooltip: 'one (Running)', track: 'stable', stable: true },
          { status: 'running', pod_name: "one", tooltip: 'one (Running)', track: 'stable', stable: true }
        ]

        expect(rollout_status.instances).to eq(expected)
      end
    end

    context 'for stable track' do
      let(:track) { 'canary' }

      let(:pods) do
        create_pods(name: "one", count: 3, track: 'stable') + create_pods(name: "two", count: 3, track: track)
      end

      it 'sorts stable instances last' do
        expected = [
          { status: 'running', pod_name: "two", tooltip: 'two (Running)', track: 'canary', stable: false },
          { status: 'running', pod_name: "two", tooltip: 'two (Running)', track: 'canary', stable: false },
          { status: 'running', pod_name: "two", tooltip: 'two (Running)', track: 'canary', stable: false },
          { status: 'running', pod_name: "one", tooltip: 'one (Running)', track: 'stable', stable: true },
          { status: 'running', pod_name: "one", tooltip: 'one (Running)', track: 'stable', stable: true },
          { status: 'running', pod_name: "one", tooltip: 'one (Running)', track: 'stable', stable: true }
        ]

        expect(rollout_status.instances).to eq(expected)
      end
    end
  end

  describe '#completion' do
    subject { rollout_status.completion }

    context 'when all instances are finished' do
      let(:track) { 'canary' }

      it { is_expected.to eq(100) }
    end

    context 'when half of the instances are finished' do
      let(:track) { "canary" }

      let(:pods) do
        create_pods(name: "one", count: 3, track: 'stable') + create_pods(name: "two", count: 3, track: track, status: "Pending")
      end

      let(:specs) { specs_half_finished }

      it { is_expected.to eq(50) }
    end

    context 'with one deployment' do
      it 'sets the completion percentage when a deployment has more running pods than desired' do
        deployments = [kube_deployment(name: 'one', track: 'one', replicas: 2)]
        pods = create_pods(name: 'one', track: 'one', count: 3)
        rollout_status = described_class.from_deployments(*deployments, pods_attrs: pods)

        expect(rollout_status.completion).to eq(100)
      end
    end

    context 'with two deployments on different tracks' do
      it 'sets the completion percentage when all pods are complete' do
        deployments = [
          kube_deployment(name: 'one', track: 'one', replicas: 2),
          kube_deployment(name: 'two', track: 'two', replicas: 2)
        ]
        pods = create_pods(name: 'one', track: 'one', count: 2) + create_pods(name: 'two', track: 'two', count: 2)
        rollout_status = described_class.from_deployments(*deployments, pods_attrs: pods)

        expect(rollout_status.completion).to eq(100)
      end
    end

    context 'with two deployments that both have track set to "stable"' do
      it 'sets the completion percentage when all pods are complete' do
        deployments = [
          kube_deployment(name: 'one', track: 'stable', replicas: 2),
          kube_deployment(name: 'two', track: 'stable', replicas: 2)
        ]
        pods = create_pods(name: 'one', track: 'stable', count: 2) + create_pods(name: 'two', track: 'stable', count: 2)
        rollout_status = described_class.from_deployments(*deployments, pods_attrs: pods)

        expect(rollout_status.completion).to eq(100)
      end

      it 'sets the completion percentage when no pods are complete' do
        deployments = [
          kube_deployment(name: 'one', track: 'stable', replicas: 3),
          kube_deployment(name: 'two', track: 'stable', replicas: 7)
        ]
        rollout_status = described_class.from_deployments(*deployments, pods_attrs: [])

        expect(rollout_status.completion).to eq(0)
      end

      it 'sets the completion percentage when a quarter of the pods are complete' do
        deployments = [
          kube_deployment(name: 'one', track: 'stable', replicas: 6),
          kube_deployment(name: 'two', track: 'stable', replicas: 2)
        ]
        pods = create_pods(name: 'one', track: 'stable', count: 2)
        rollout_status = described_class.from_deployments(*deployments, pods_attrs: pods)

        expect(rollout_status.completion).to eq(25)
      end
    end

    context 'with two deployments, one with track set to "stable" and one with no track label' do
      it 'sets the completion percentage when all pods are complete' do
        deployments = [
          kube_deployment(name: 'one', track: 'stable', replicas: 3),
          kube_deployment(name: 'two', track: nil, replicas: 3)
        ]
        pods = create_pods(name: 'one', track: 'stable', count: 3) + create_pods(name: 'two', track: nil, count: 3)
        rollout_status = described_class.from_deployments(*deployments, pods_attrs: pods)

        expect(rollout_status.completion).to eq(100)
      end

      it 'sets the completion percentage when no pods are complete' do
        deployments = [
          kube_deployment(name: 'one', track: 'stable', replicas: 1),
          kube_deployment(name: 'two', track: nil, replicas: 1)
        ]
        rollout_status = described_class.from_deployments(*deployments, pods_attrs: [])

        expect(rollout_status.completion).to eq(0)
      end

      it 'sets the completion percentage when a third of the pods are complete' do
        deployments = [
          kube_deployment(name: 'one', track: 'stable', replicas: 2),
          kube_deployment(name: 'two', track: nil, replicas: 7)
        ]
        pods = create_pods(name: 'one', track: 'stable', count: 2) + create_pods(name: 'two', track: nil, count: 1)
        rollout_status = described_class.from_deployments(*deployments, pods_attrs: pods)

        expect(rollout_status.completion).to eq(33)
      end
    end
  end

  describe '#complete?' do
    subject { rollout_status.complete? }

    context 'when all instances are finished' do
      let(:track) { 'canary' }

      it { is_expected.to be_truthy }
    end

    context 'when half of the instances are finished' do
      let(:track) { "canary" }

      let(:pods) do
        create_pods(name: "one", count: 3, track: 'stable') + create_pods(name: "two", count: 3, track: track, status: "Pending")
      end

      let(:specs) { specs_half_finished }

      it { is_expected.to be_falsy}
    end
  end

  describe '#found?' do
    context 'when the specs are passed' do
      it { is_expected.to be_found }
    end

    context 'when list of specs is empty' do
      let(:specs) { [] }

      it { is_expected.not_to be_found }
    end
  end

  describe '.loading' do
    subject { described_class.loading }

    it { is_expected.to be_loading }
  end

  describe '#not_found?' do
    context 'when the specs are passed' do
      it { is_expected.not_to be_not_found }
    end

    context 'when list of specs is empty' do
      let(:specs) { [] }

      it { is_expected.to be_not_found }
    end
  end

  describe '#canary_ingress_exists?' do
    context 'when canary ingress exists' do
      let(:ingresses) { [kube_ingress(track: :canary)] }

      it 'returns true' do
        expect(rollout_status.canary_ingress_exists?).to eq(true)
      end
    end

    context 'when canary ingress does not exist' do
      let(:ingresses) { [kube_ingress(track: :stable)] }

      it 'returns false' do
        expect(rollout_status.canary_ingress_exists?).to eq(false)
      end
    end
  end

  def create_pods(name:, count:, track: nil, status: 'Running' )
    Array.new(count, kube_pod(name: name, status: status, track: track))
  end
end