summaryrefslogtreecommitdiff
path: root/spec/controllers/projects/logs_controller_spec.rb
blob: 1c81ae93b42cb0f37cca479d1205ba8b3b15acfd (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Projects::LogsController do
  include KubernetesHelpers

  let_it_be(:user) { create(:user) }
  let_it_be(:project) { create(:project) }

  let_it_be(:environment) do
    create(:environment, name: 'production', project: project)
  end

  let(:pod_name) { "foo" }
  let(:container) { 'container-1' }

  before do
    sign_in(user)
  end

  describe 'GET #index' do
    let(:empty_project) { create(:project) }

    it 'returns 404 with reporter access' do
      project.add_reporter(user)

      get :index, params: environment_params

      expect(response).to have_gitlab_http_status(:not_found)
    end

    it 'renders empty logs page if no environment exists' do
      empty_project.add_developer(user)

      get :index, params: { namespace_id: empty_project.namespace, project_id: empty_project }

      expect(response).to be_ok
      expect(response).to render_template 'empty_logs'
    end

    it 'renders index template' do
      project.add_developer(user)

      get :index, params: environment_params

      expect(response).to be_ok
      expect(response).to render_template 'index'
    end

    context 'with feature flag disabled' do
      before do
        stub_feature_flags(monitor_logging: false)
      end

      it 'returns 404 with reporter access' do
        project.add_developer(user)

        get :index, params: environment_params

        expect(response).to have_gitlab_http_status(:not_found)
      end
    end
  end

  shared_examples 'pod logs service' do |endpoint, service|
    let(:service_result) do
      {
        status: :success,
        logs: ['Log 1', 'Log 2', 'Log 3'],
        pods: [pod_name],
        pod_name: pod_name,
        container_name: container
      }
    end

    let(:service_result_json) { Gitlab::Json.parse(service_result.to_json) }

    let_it_be(:cluster) { create(:cluster, :provided_by_gcp, environment_scope: '*', projects: [project]) }

    before do
      allow_next_instance_of(service) do |instance|
        allow(instance).to receive(:execute).and_return(service_result)
      end
    end

    it 'returns 404 with reporter access' do
      project.add_reporter(user)

      get endpoint, params: environment_params(pod_name: pod_name, format: :json)

      expect(response).to have_gitlab_http_status(:not_found)
    end

    context 'with developer access' do
      before do
        project.add_developer(user)
      end

      it 'returns the service result' do
        get endpoint, params: environment_params(pod_name: pod_name, format: :json)

        expect(response).to have_gitlab_http_status(:success)
        expect(json_response).to eq(service_result_json)
      end
    end

    context 'with maintainer access' do
      before do
        project.add_maintainer(user)
      end

      it 'returns the service result' do
        get endpoint, params: environment_params(pod_name: pod_name, format: :json)

        expect(response).to have_gitlab_http_status(:success)
        expect(json_response).to eq(service_result_json)
      end

      it 'sets the polling header' do
        get endpoint, params: environment_params(pod_name: pod_name, format: :json)

        expect(response).to have_gitlab_http_status(:success)
        expect(response.headers['Poll-Interval']).to eq('3000')
      end

      context 'with gitlab managed apps logs' do
        it 'uses cluster finder services to select cluster', :aggregate_failures do
          cluster_list = [cluster]
          service_params = { params: ActionController::Parameters.new(pod_name: pod_name).permit! }
          request_params = {
            namespace_id: project.namespace,
            project_id: project,
            cluster_id: cluster.id,
            pod_name: pod_name,
            format: :json
          }

          expect_next_instance_of(ClusterAncestorsFinder, project, user) do |finder|
            expect(finder).to receive(:execute).and_return(cluster_list)
            expect(cluster_list).to receive(:find).and_call_original
          end

          expect_next_instance_of(service, cluster, Gitlab::Kubernetes::Helm::NAMESPACE, service_params) do |instance|
            expect(instance).to receive(:execute).and_return(service_result)
          end

          get endpoint, params: request_params

          expect(response).to have_gitlab_http_status(:success)
          expect(json_response).to eq(service_result_json)
        end
      end

      context 'when service is processing' do
        let(:service_result) { nil }

        it 'returns a 202' do
          get endpoint, params: environment_params(pod_name: pod_name, format: :json)

          expect(response).to have_gitlab_http_status(:accepted)
        end
      end

      shared_examples 'unsuccessful execution response' do |message|
        let(:service_result) do
          {
            status: :error,
            message: message
          }
        end

        it 'returns the error' do
          get endpoint, params: environment_params(pod_name: pod_name, format: :json)

          expect(response).to have_gitlab_http_status(:bad_request)
          expect(json_response).to eq(service_result_json)
        end
      end

      context 'when service is failing' do
        it_behaves_like 'unsuccessful execution response', 'some error'
      end

      context 'when cluster is nil' do
        let!(:cluster) { nil }

        it_behaves_like 'unsuccessful execution response', 'Environment does not have deployments'
      end

      context 'when namespace is empty' do
        before do
          allow(environment).to receive(:deployment_namespace).and_return('')
        end

        it_behaves_like 'unsuccessful execution response', 'Environment does not have deployments'
      end
    end
  end

  describe 'GET #k8s' do
    it_behaves_like 'pod logs service', :k8s, PodLogs::KubernetesService
  end

  describe 'GET #elasticsearch' do
    it_behaves_like 'pod logs service', :elasticsearch, PodLogs::ElasticsearchService
  end

  def environment_params(opts = {})
    opts.reverse_merge(namespace_id: project.namespace,
                       project_id: project,
                       environment_name: environment.name)
  end
end