Commit 0b7d6ca4 authored by Erick Banks's avatar Erick Banks

Apply Nialia's suggestions

Moved helper methods to Runtime::Project and Runtime::Search.
Used a parse response instead of matching where applicable.
parent da2aac03
......@@ -39,6 +39,7 @@ module QA
autoload :MailHog, 'qa/runtime/mail_hog'
autoload :IPAddress, 'qa/runtime/ip_address'
autoload :Search, 'qa/runtime/search'
autoload :Project, 'qa/runtime/project'
autoload :ApplicationSettings, 'qa/runtime/application_settings'
module API
......
# frozen_string_literal: true
module QA
module Runtime
module Project
extend self
extend Support::Api
def create_project(project_name, api_client, project_description = 'default')
project = Resource::Project.fabricate_via_api! do |project|
project.add_name_uuid = false
project.name = project_name
project.description = project_description
project.api_client = api_client
project.visibility = 'public'
end
project
end
def push_file_to_project(target_project, file_name, file_content)
Resource::Repository::ProjectPush.fabricate! do |push|
push.project = target_project
push.file_name = file_name
push.file_content = file_content
end
end
def set_project_visibility(api_client, project_id, visibility)
request = Runtime::API::Request.new(api_client, "/projects/#{project_id}")
response = put request.url, visibility: visibility
response.code.equal?(QA::Support::Api::HTTP_STATUS_OK)
end
end
end
end
......@@ -42,6 +42,22 @@ module QA
end
end
def elasticsearch_on?(api_client)
elasticsearch_state_request = Runtime::API::Request.new(api_client, '/application/settings')
response = get elasticsearch_state_request.url
parse_body(response)[:elasticsearch_search] && parse_body(response)[:elasticsearch_indexing]
end
def disable_elasticsearch(api_client)
disable_elasticsearch_request = Runtime::API::Request.new(api_client, '/application/settings')
put disable_elasticsearch_request.url, elasticsearch_search: false, elasticsearch_indexing: false
end
def create_search_request(api_client, scope, search_term)
Runtime::API::Request.new(api_client, '/search', scope: scope, search: search_term)
end
def find_code(file_name, search_term)
find_target_in_scope('blobs', search_term) do |record|
record[:filename] == file_name && record[:data].include?(search_term)
......
......@@ -3,29 +3,33 @@
require 'securerandom'
module QA
context 'Create' do
context 'Enablement:Search' do
include Support::Api
describe 'Elasticsearch advanced global search with advanced syntax', :orchestrated, :elasticsearch, :requires_admin, quarantine: { type: :new } do
before(:all) do
@api_client = Runtime::API::Client.new(:gitlab)
@project_name_suffix = SecureRandom.hex(8)
@elasticsearch_original_state_on = elasticsearch_on?(@api_client)
@elasticsearch_original_state_on = Runtime::Search.elasticsearch_on?(@api_client)
unless @elasticsearch_original_state_on
QA::EE::Resource::Settings::Elasticsearch.fabricate_via_api!
sleep(60) # wait for the change to propagate before inserting records or else Gitlab::CurrentSettings.elasticsearch_indexing and Elastic::ApplicationVersionedSearch::searchable? will be false
# this sleep can be removed after we're able to query logs via the API as per this issue https://gitlab.com/gitlab-org/quality/team-tasks/issues/395
sleep(60)
# wait for the change to propagate before inserting records or else
# Gitlab::CurrentSettings.elasticsearch_indexing and
# Elastic::ApplicationVersionedSearch::searchable? will be false
# this sleep can be removed after we're able to query logs via the API
# as per this issue https://gitlab.com/gitlab-org/quality/team-tasks/issues/395
end
@project = create_project("es-adv-global-search-#{@project_name_suffix}",
@api_client,
"This is a unique project description #{@project_name_suffix}")
push_file_to_project(@project, 'elasticsearch.rb', "elasticsearch: #{SecureRandom.hex(8)}")
@project = Runtime::Project.create_project("es-adv-global-search-#{@project_name_suffix}",
@api_client,
"This is a unique project description #{@project_name_suffix}")
Runtime::Project.push_file_to_project(@project, 'elasticsearch.rb', "elasticsearch: #{SecureRandom.hex(8)}")
end
after(:all) do
if !@elasticsearch_original_state_on && !@api_client.nil?
disable_elasticsearch(@api_client)
Runtime::Search.disable_elasticsearch(@api_client)
end
end
......@@ -43,12 +47,10 @@ module QA
def expect_search_to_find_project(search_term)
QA::Support::Retrier.retry_on_exception(max_attempts: 10, sleep_interval: 3) do
get create_search_request(@api_client, 'projects', search_term).url
get Runtime::Search.create_search_request(@api_client, 'projects', search_term).url
expect_status(QA::Support::Api::HTTP_STATUS_OK)
if json_body.empty?
raise 'Empty search result returned'
end
raise 'Empty search result returned' if json_body.empty?
expect(json_body[0][:name]).to eq(@project.name)
end
......
......@@ -11,21 +11,25 @@ module QA
@project_file_content = "elasticsearch: #{SecureRandom.hex(8)}"
non_member_user = Resource::User.fabricate_or_use('non_member_user', 'non_member_user_password')
@non_member_api_client = Runtime::API::Client.new(user: non_member_user)
@elasticsearch_original_state_on = elasticsearch_on?(@api_client)
@elasticsearch_original_state_on = Runtime::Search.elasticsearch_on?(@api_client)
unless @elasticsearch_original_state_on
QA::EE::Resource::Settings::Elasticsearch.fabricate_via_api!
sleep(60) # wait for the change to propagate before inserting records or else Gitlab::CurrentSettings.elasticsearch_indexing and Elastic::ApplicationVersionedSearch::searchable? will be false
# this sleep can be removed after we're able to query logs via the API as per this issue https://gitlab.com/gitlab-org/quality/team-tasks/issues/395
sleep(60)
# wait for the change to propagate before inserting records or else
# Gitlab::CurrentSettings.elasticsearch_indexing and
# Elastic::ApplicationVersionedSearch::searchable? will be false
# this sleep can be removed after we're able to query logs via the API
# as per this issue https://gitlab.com/gitlab-org/quality/team-tasks/issues/395
end
@project = create_project("api-es-#{SecureRandom.hex(8)}", @api_client)
push_file_to_project(@project, 'README.md', @project_file_content)
@project = Runtime::Project.create_project("api-es-#{SecureRandom.hex(8)}", @api_client)
Runtime::Project.push_file_to_project(@project, 'README.md', @project_file_content)
end
after(:all) do
if !@elasticsearch_original_state_on && !@api_client.nil?
disable_elasticsearch(@api_client)
Runtime::Search.disable_elasticsearch(@api_client)
end
end
......@@ -35,7 +39,7 @@ module QA
describe 'When searching a private repository' do
before(:all) do
set_project_visibility(@api_client, @project.id, 'private')
Runtime::Project.set_project_visibility(@api_client, @project.id, 'private')
end
it 'finds a blob as an authorized user' do
......@@ -44,7 +48,7 @@ module QA
it 'does not find a blob as an non-member user' do
QA::Support::Retrier.retry_on_exception(max_attempts: 10, sleep_interval: 3) do
get create_search_request(@non_member_api_client, 'blobs', @project_file_content).url
get Runtime::Search.create_search_request(@non_member_api_client, 'blobs', @project_file_content).url
expect_status(QA::Support::Api::HTTP_STATUS_OK)
expect(json_body).to be_empty
end
......@@ -55,12 +59,10 @@ module QA
def successful_search(api_client)
QA::Support::Retrier.retry_on_exception(max_attempts: 10, sleep_interval: 3) do
get create_search_request(api_client, 'blobs', @project_file_content).url
get Runtime::Search.create_search_request(api_client, 'blobs', @project_file_content).url
expect_status(QA::Support::Api::HTTP_STATUS_OK)
if json_body.empty?
raise 'Empty search result returned'
end
raise 'Empty search result returned' if json_body.empty?
expect(json_body[0][:data]).to match(@project_file_content)
expect(json_body[0][:project_id]).to equal(@project.id)
......
......@@ -65,51 +65,6 @@ module QA
error.response
end
def create_project(project_name, api_client, project_description = 'default')
project = Resource::Project.fabricate_via_api! do |project|
project.add_name_uuid = false
project.name = project_name
project.description = project_description
project.api_client = api_client
project.visibility = 'public'
end
project
end
def push_file_to_project(target_project, file_name, file_content)
Resource::Repository::ProjectPush.fabricate! do |push|
push.project = target_project
push.file_name = file_name
push.file_content = file_content
end
end
def set_project_visibility(api_client, project_id, visibility)
request = Runtime::API::Request.new(api_client, "/projects/#{project_id}")
put request.url, visibility: visibility
expect_status(HTTP_STATUS_OK)
end
def create_search_request(api_client, scope, search_term)
Runtime::API::Request.new(api_client, '/search', scope: scope, search: search_term)
end
def elasticsearch_on?(api_client)
elasticsearch_state_request = Runtime::API::Request.new(api_client, '/application/settings')
response = get elasticsearch_state_request.url
if response.to_s.match(/"elasticsearch_search":true/) && response.to_s.match(/"elasticsearch_indexing":true/)
return true
else
return false
end
end
def disable_elasticsearch(api_client)
disable_elasticsearch_request = Runtime::API::Request.new(api_client, '/application/settings')
put disable_elasticsearch_request.url, elasticsearch_search: false, elasticsearch_indexing: false
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