Commit 16561244 authored by Dylan Griffith's avatar Dylan Griffith

Merge branch 'ak/specs' into 'master'

Add suggested specs on developper access to logs

See merge request gitlab-org/gitlab!31915
parents 7a56ef68 ed0395e8
...@@ -18,41 +18,53 @@ describe Clusters::EnvironmentEntity do ...@@ -18,41 +18,53 @@ describe Clusters::EnvironmentEntity do
subject { described_class.new(environment, request: request).as_json } subject { described_class.new(environment, request: request).as_json }
before do context 'with maintainer access' do
group.add_maintainer(user)
end
context 'deploy board available' do
before do before do
allow(group).to receive(:feature_available?).and_call_original group.add_maintainer(user)
allow(group).to receive(:feature_available?).with(:cluster_deployments).and_return(true)
end end
it 'matches expected schema' do context 'deploy board available' do
expect(subject.with_indifferent_access).to match_schema('clusters/environment', dir: 'ee') before do
end allow(group).to receive(:feature_available?).and_call_original
allow(group).to receive(:feature_available?).with(:cluster_deployments).and_return(true)
end
it 'exposes rollout_status' do it 'matches expected schema' do
expect(subject).to include(:rollout_status) expect(subject.with_indifferent_access).to match_schema('clusters/environment', dir: 'ee')
end end
end
context 'deploy board not available' do it 'exposes rollout_status' do
before do expect(subject).to include(:rollout_status)
allow(group).to receive(:feature_available?).with(:cluster_deployments).and_return(false) end
end end
it 'matches expected schema' do context 'deploy board not available' do
expect(subject.with_indifferent_access).to match_schema('clusters/environment', dir: 'ee') before do
allow(group).to receive(:feature_available?).with(:cluster_deployments).and_return(false)
end
it 'matches expected schema' do
expect(subject.with_indifferent_access).to match_schema('clusters/environment', dir: 'ee')
end
it 'does not expose rollout_status' do
expect(subject).not_to include(:rollout_status)
end
end end
it 'does not expose rollout_status' do it 'exposes logs_path' do
expect(subject).not_to include(:rollout_status) expect(subject).to include(:logs_path)
end end
end end
it 'exposes logs_path' do context 'with developer access' do
expect(subject).to include(:logs_path) before do
group.add_developer(user)
end
it 'does not expose logs_path' do
expect(subject).not_to include(:logs_path)
end
end end
end end
end end
...@@ -16,16 +16,23 @@ describe Projects::LogsController do ...@@ -16,16 +16,23 @@ describe Projects::LogsController do
let(:container) { 'container-1' } let(:container) { 'container-1' }
before do before do
project.add_maintainer(user)
sign_in(user) sign_in(user)
end end
describe 'GET #index' do describe 'GET #index' do
let(:empty_project) { create(:project) } let(:empty_project) { create(:project) }
it 'returns 404 with developer access' do
project.add_developer(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 it 'renders empty logs page if no environment exists' do
empty_project.add_maintainer(user) empty_project.add_maintainer(user)
get :index, params: { namespace_id: empty_project.namespace, project_id: empty_project } get :index, params: { namespace_id: empty_project.namespace, project_id: empty_project }
expect(response).to be_ok expect(response).to be_ok
...@@ -33,6 +40,8 @@ describe Projects::LogsController do ...@@ -33,6 +40,8 @@ describe Projects::LogsController do
end end
it 'renders index template' do it 'renders index template' do
project.add_maintainer(user)
get :index, params: environment_params get :index, params: environment_params
expect(response).to be_ok expect(response).to be_ok
...@@ -60,70 +69,84 @@ describe Projects::LogsController do ...@@ -60,70 +69,84 @@ describe Projects::LogsController do
end end
end end
it 'returns the service result' do it 'returns 404 with developer access' do
project.add_developer(user)
get endpoint, params: environment_params(pod_name: pod_name, format: :json) get endpoint, params: environment_params(pod_name: pod_name, format: :json)
expect(response).to have_gitlab_http_status(:success) expect(response).to have_gitlab_http_status(:not_found)
expect(json_response).to eq(service_result_json)
end end
it 'registers a usage of the endpoint' do context 'with maintainer access' do
expect(::Gitlab::UsageCounters::PodLogs).to receive(:increment).with(project.id) before do
project.add_maintainer(user)
end
get endpoint, params: environment_params(pod_name: pod_name, format: :json) 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(response).to have_gitlab_http_status(:success)
end expect(json_response).to eq(service_result_json)
end
it 'sets the polling header' do it 'registers a usage of the endpoint' do
get endpoint, params: environment_params(pod_name: pod_name, format: :json) expect(::Gitlab::UsageCounters::PodLogs).to receive(:increment).with(project.id)
expect(response).to have_gitlab_http_status(:success) get endpoint, params: environment_params(pod_name: pod_name, format: :json)
expect(response.headers['Poll-Interval']).to eq('3000')
end
context 'when service is processing' do expect(response).to have_gitlab_http_status(:success)
let(:service_result) { nil } end
it 'returns a 202' do it 'sets the polling header' do
get endpoint, params: environment_params(pod_name: pod_name, format: :json) get endpoint, params: environment_params(pod_name: pod_name, format: :json)
expect(response).to have_gitlab_http_status(:accepted) expect(response).to have_gitlab_http_status(:success)
expect(response.headers['Poll-Interval']).to eq('3000')
end end
end
shared_examples 'unsuccessful execution response' do |message| context 'when service is processing' do
let(:service_result) do let(:service_result) { nil }
{
status: :error,
message: message
}
end
it 'returns the error' do it 'returns a 202' do
get endpoint, params: environment_params(pod_name: pod_name, format: :json) get endpoint, params: environment_params(pod_name: pod_name, format: :json)
expect(response).to have_gitlab_http_status(:bad_request) expect(response).to have_gitlab_http_status(:accepted)
expect(json_response).to eq(service_result_json) end
end end
end
context 'when service is failing' do shared_examples 'unsuccessful execution response' do |message|
it_behaves_like 'unsuccessful execution response', 'some error' let(:service_result) do
end {
status: :error,
message: message
}
end
context 'when cluster is nil' do it 'returns the error' do
let!(:cluster) { nil } get endpoint, params: environment_params(pod_name: pod_name, format: :json)
it_behaves_like 'unsuccessful execution response', 'Environment does not have deployments' expect(response).to have_gitlab_http_status(:bad_request)
end expect(json_response).to eq(service_result_json)
end
end
context 'when namespace is empty' do context 'when service is failing' do
before do it_behaves_like 'unsuccessful execution response', 'some error'
allow(environment).to receive(:deployment_namespace).and_return('') end
context 'when cluster is nil' do
let!(:cluster) { nil }
it_behaves_like 'unsuccessful execution response', 'Environment does not have deployments'
end end
it_behaves_like 'unsuccessful execution response', 'Environment does not have deployments' 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
end end
......
...@@ -10,7 +10,13 @@ describe EnvironmentEntity do ...@@ -10,7 +10,13 @@ describe EnvironmentEntity do
described_class.new(environment, request: spy('request')) described_class.new(environment, request: spy('request'))
end end
let(:environment) { create(:environment) } let_it_be(:user) { create(:user) }
let_it_be(:project) { create(:project) }
let_it_be(:environment) { create(:environment, project: project) }
before do
allow(entity).to receive(:current_user).and_return(user)
end
subject { entity.as_json } subject { entity.as_json }
...@@ -67,28 +73,48 @@ describe EnvironmentEntity do ...@@ -67,28 +73,48 @@ describe EnvironmentEntity do
end end
context 'with auto_stop_in' do context 'with auto_stop_in' do
let(:environment) { create(:environment, :will_auto_stop) } let(:environment) { create(:environment, :will_auto_stop, project: project) }
it 'exposes auto stop related information' do it 'exposes auto stop related information' do
project.add_maintainer(user)
expect(subject).to include(:cancel_auto_stop_path, :auto_stop_at) expect(subject).to include(:cancel_auto_stop_path, :auto_stop_at)
end end
end end
context 'pod_logs' do context 'pod_logs' do
it 'exposes logs keys' do context 'with developer access' do
expect(subject).to include(:logs_path) before do
expect(subject).to include(:logs_api_path) project.add_developer(user)
expect(subject).to include(:enable_advanced_logs_querying) end
end
it 'uses k8s api when ES is not available' do it 'does not expose logs keys' do
expect(subject[:logs_api_path]).to eq(k8s_project_logs_path(environment.project, environment_name: environment.name, format: :json)) expect(subject).not_to include(:logs_path)
expect(subject).not_to include(:logs_api_path)
expect(subject).not_to include(:enable_advanced_logs_querying)
end
end end
it 'uses ES api when ES is available' do context 'with maintainer access' do
allow(environment).to receive(:elastic_stack_available?).and_return(true) before do
project.add_maintainer(user)
end
it 'exposes logs keys' do
expect(subject).to include(:logs_path)
expect(subject).to include(:logs_api_path)
expect(subject).to include(:enable_advanced_logs_querying)
end
expect(subject[:logs_api_path]).to eq(elasticsearch_project_logs_path(environment.project, environment_name: environment.name, format: :json)) it 'uses k8s api when ES is not available' do
expect(subject[:logs_api_path]).to eq(k8s_project_logs_path(project, environment_name: environment.name, format: :json))
end
it 'uses ES api when ES is available' do
allow(environment).to receive(:elastic_stack_available?).and_return(true)
expect(subject[:logs_api_path]).to eq(elasticsearch_project_logs_path(project, environment_name: environment.name, format: :json))
end
end end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment