ingress_spec.rb 6.74 KB
Newer Older
1
2
# frozen_string_literal: true

3
require 'spec_helper'
Kamil Trzcinski's avatar
Kamil Trzcinski committed
4
5

describe Clusters::Applications::Ingress do
6
7
  let(:ingress) { create(:clusters_applications_ingress) }

Shinya Maeda's avatar
Shinya Maeda committed
8
9
  it_behaves_like 'having unique enum values'

10
  include_examples 'cluster application core specs', :clusters_applications_ingress
11
  include_examples 'cluster application status specs', :clusters_applications_ingress
12
  include_examples 'cluster application version specs', :clusters_applications_ingress
13
  include_examples 'cluster application helm specs', :clusters_applications_ingress
14
  include_examples 'cluster application initial status specs'
Kamil Trzcinski's avatar
Kamil Trzcinski committed
15

16
17
  before do
    allow(ClusterWaitForIngressIpAddressWorker).to receive(:perform_in)
18
    allow(ClusterWaitForIngressIpAddressWorker).to receive(:perform_async)
19
  end
20

21
22
23
  describe '#can_uninstall?' do
    subject { ingress.can_uninstall? }

24
    it 'returns true if external ip is set and no application exists' do
25
26
27
28
29
30
31
32
33
34
35
      ingress.external_ip = 'IP'

      is_expected.to be_truthy
    end

    it 'returns false if application_jupyter_nil_or_installable? is false' do
      create(:clusters_applications_jupyter, :installed, cluster: ingress.cluster)

      is_expected.to be_falsey
    end

36
37
38
39
40
41
    it 'returns false if application_elastic_stack_nil_or_installable? is false' do
      create(:clusters_applications_elastic_stack, :installed, cluster: ingress.cluster)

      is_expected.to be_falsey
    end

42
43
44
    it 'returns false if external_ip_or_hostname? is false' do
      is_expected.to be_falsey
    end
45
46
  end

47
  describe '#make_installed!' do
48
    before do
49
      application.make_installed!
50
51
    end

52
53
    let(:application) { create(:clusters_applications_ingress, :installing) }

54
55
    it 'schedules a ClusterWaitForIngressIpAddressWorker' do
      expect(ClusterWaitForIngressIpAddressWorker).to have_received(:perform_in)
56
        .with(Clusters::Applications::Ingress::FETCH_IP_ADDRESS_DELAY, 'ingress', application.id)
57
58
    end
  end
59

60
  describe '#schedule_status_update' do
61
62
63
    let(:application) { create(:clusters_applications_ingress, :installed) }

    before do
64
      application.schedule_status_update
65
66
67
    end

    it 'schedules a ClusterWaitForIngressIpAddressWorker' do
68
      expect(ClusterWaitForIngressIpAddressWorker).to have_received(:perform_async)
69
        .with('ingress', application.id)
70
71
72
73
74
75
    end

    context 'when the application is not installed' do
      let(:application) { create(:clusters_applications_ingress, :installing) }

      it 'does not schedule a ClusterWaitForIngressIpAddressWorker' do
76
        expect(ClusterWaitForIngressIpAddressWorker).not_to have_received(:perform_async)
77
78
79
80
81
82
83
84
85
86
      end
    end

    context 'when there is already an external_ip' do
      let(:application) { create(:clusters_applications_ingress, :installed, external_ip: '111.222.222.111') }

      it 'does not schedule a ClusterWaitForIngressIpAddressWorker' do
        expect(ClusterWaitForIngressIpAddressWorker).not_to have_received(:perform_in)
      end
    end
87
88
89
90
91
92
93
94

    context 'when there is already an external_hostname' do
      let(:application) { create(:clusters_applications_ingress, :installed, external_hostname: 'localhost.localdomain') }

      it 'does not schedule a ClusterWaitForIngressIpAddressWorker' do
        expect(ClusterWaitForIngressIpAddressWorker).not_to have_received(:perform_in)
      end
    end
95
  end
96
97
98
99
100
101

  describe '#install_command' do
    subject { ingress.install_command }

    it { is_expected.to be_an_instance_of(Gitlab::Kubernetes::Helm::InstallCommand) }

102
    it 'is initialized with ingress arguments' do
103
104
      expect(subject.name).to eq('ingress')
      expect(subject.chart).to eq('stable/nginx-ingress')
105
      expect(subject.version).to eq('1.22.1')
106
      expect(subject).to be_rbac
107
      expect(subject.files).to eq(ingress.files)
108
    end
109

110
    context 'on a non rbac enabled cluster' do
111
      before do
112
        ingress.cluster.platform_kubernetes.abac!
113
114
      end

115
      it { is_expected.not_to be_rbac }
116
117
    end

118
119
120
    context 'application failed to install previously' do
      let(:ingress) { create(:clusters_applications_ingress, :errored, version: 'nginx') }

121
      it 'is initialized with the locked version' do
122
        expect(subject.version).to eq('1.22.1')
123
124
      end
    end
125
126
  end

127
128
129
  describe '#files' do
    let(:application) { ingress }
    let(:values) { subject[:'values.yaml'] }
130

131
132
    subject { application.files }

133
    it 'includes ingress valid keys in values' do
134
135
136
137
138
      expect(values).to include('image')
      expect(values).to include('repository')
      expect(values).to include('stats')
      expect(values).to include('podAnnotations')
    end
139
  end
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158

  describe '#values' do
    let(:project) { build(:project) }
    let(:cluster) { build(:cluster, projects: [project]) }

    context 'when ingress_modsecurity is enabled' do
      before do
        stub_feature_flags(ingress_modsecurity: true)

        allow(subject).to receive(:cluster).and_return(cluster)
      end

      it 'includes modsecurity module enablement' do
        expect(subject.values).to include("enable-modsecurity: 'true'")
      end

      it 'includes modsecurity core ruleset enablement' do
        expect(subject.values).to include("enable-owasp-modsecurity-crs: 'true'")
      end
159
160
161
162
163
164
165
166
167

      it 'includes modsecurity.conf content' do
        expect(subject.values).to include('modsecurity.conf')
        # Includes file content from Ingress#modsecurity_config_content
        expect(subject.values).to include('SecAuditLog')

        expect(subject.values).to include('extraVolumes')
        expect(subject.values).to include('extraVolumeMounts')
      end
168
169
170
171
172
173

      it 'includes modsecurity sidecar container' do
        expect(subject.values).to include('modsecurity-log-volume')

        expect(subject.values).to include('extraContainers')
      end
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
    end

    context 'when ingress_modsecurity is disabled' do
      before do
        stub_feature_flags(ingress_modsecurity: false)

        allow(subject).to receive(:cluster).and_return(cluster)
      end

      it 'excludes modsecurity module enablement' do
        expect(subject.values).not_to include('enable-modsecurity')
      end

      it 'excludes modsecurity core ruleset enablement' do
        expect(subject.values).not_to include('enable-owasp-modsecurity-crs')
      end
190
191
192
193
194
195
196
197
198

      it 'excludes modsecurity.conf content' do
        expect(subject.values).not_to include('modsecurity.conf')
        # Excludes file content from Ingress#modsecurity_config_content
        expect(subject.values).not_to include('SecAuditLog')

        expect(subject.values).not_to include('extraVolumes')
        expect(subject.values).not_to include('extraVolumeMounts')
      end
199
200
201
202
203
204

      it 'excludes modsecurity sidecar container' do
        expect(subject.values).not_to include('modsecurity-log-volume')

        expect(subject.values).not_to include('extraContainers')
      end
205
206
    end
  end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
207
end