Commit dc3c560e authored by Erick Banks's avatar Erick Banks Committed by Dan Davison

Use best practices in elasticsearch test files

Use let instead of instance variables
Move helper methods to Flow::Project module
Change from before(:all) to just before
Do same with after(:all)
parent d2958460
......@@ -39,7 +39,6 @@ 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
......@@ -88,6 +87,7 @@ module QA
autoload :Tag, 'qa/resource/tag'
autoload :ProjectMember, 'qa/resource/project_member'
autoload :UserGPG, 'qa/resource/user_gpg'
autoload :Visibility, 'qa/resource/visibility'
module Events
autoload :Base, 'qa/resource/events/base'
......
......@@ -7,11 +7,11 @@ module QA
class Project < Base
include Events::Project
include Members
include Visibility
attr_accessor :repository_storage # requires admin access
attr_writer :initialize_with_readme
attr_writer :auto_devops_enabled
attr_writer :visibility
attribute :id
attribute :name
......@@ -19,6 +19,7 @@ module QA
attribute :description
attribute :standalone
attribute :runners_token
attribute :visibility
attribute :group do
Group.fabricate!
......@@ -50,7 +51,7 @@ module QA
@description = 'My awesome project'
@initialize_with_readme = false
@auto_devops_enabled = false
@visibility = 'public'
@visibility = :public
end
def name=(raw_name)
......@@ -83,6 +84,10 @@ module QA
"/projects/#{CGI.escape(path_with_namespace)}"
end
def api_visibility_path
"/projects/#{id}"
end
def api_get_archive_path(type = 'tar.gz')
"#{api_get_path}/repository/archive.#{type}"
end
......
# frozen_string_literal: true
module QA
module Resource
module Visibility
def set_visibility(visibility)
put Runtime::API::Request.new(api_client, api_visibility_path).url, { visibility: visibility }
end
class VisibilityLevel
%i(public internal private).each do |level|
const_set(level.upcase, level)
end
end
end
end
end
# 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
......@@ -6,12 +6,20 @@ module QA
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 = Runtime::Search.elasticsearch_on?(@api_client)
let(:project_name_suffix) { SecureRandom.hex(8) }
let(:api_client) { Runtime::API::Client.new(:gitlab) }
unless @elasticsearch_original_state_on
let(:project) do
Resource::Project.fabricate_via_api! do |project|
project.name = "es-adv-global-search-#{project_name_suffix}"
project.description = "This is a unique project description #{project_name_suffix}"
end
end
let(:elasticsearch_original_state_on?) { Runtime::Search.elasticsearch_on?(api_client) }
before do
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
......@@ -21,25 +29,27 @@ module QA
# as per this issue https://gitlab.com/gitlab-org/quality/team-tasks/issues/395
end
@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)}")
Resource::Repository::Commit.fabricate_via_api! do |commit|
commit.project = project
commit.add_files([
{ file_path: 'elasticsearch.rb', content: "elasticsearch: #{SecureRandom.hex(8)}" }
])
end
end
after(:all) do
if !@elasticsearch_original_state_on && !@api_client.nil?
Runtime::Search.disable_elasticsearch(@api_client)
after do
if !elasticsearch_original_state_on? && !api_client.nil?
Runtime::Search.disable_elasticsearch(api_client)
end
end
context 'when searching for projects using advanced syntax' do
it 'searches in the project name' do
expect_search_to_find_project("es-adv-*#{@project_name_suffix}")
expect_search_to_find_project("es-adv-*#{project_name_suffix}")
end
it 'searches in the project description' do
expect_search_to_find_project("unique +#{@project_name_suffix}")
expect_search_to_find_project("unique +#{project_name_suffix}")
end
end
......@@ -47,12 +57,12 @@ module QA
def expect_search_to_find_project(search_term)
QA::Support::Retrier.retry_on_exception(max_attempts: 10, sleep_interval: 3) do
get Runtime::Search.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)
raise 'Empty search result returned' if json_body.empty?
expect(json_body[0][:name]).to eq(@project.name)
expect(json_body[0][:name]).to eq(project.name)
end
end
end
......
......@@ -6,14 +6,21 @@ module QA
context 'Enablement:Search' do
include Support::Api
describe 'When using elasticsearch API to search for a known blob', :orchestrated, :elasticsearch, :requires_admin, quarantine: { type: :new } do
before(:all) do
@api_client = Runtime::API::Client.new(:gitlab)
@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 = Runtime::Search.elasticsearch_on?(@api_client)
let(:project_file_content) { "elasticsearch: #{SecureRandom.hex(8)}" }
let(:non_member_user) { Resource::User.fabricate_or_use('non_member_user', 'non_member_user_password') }
let(:api_client) { Runtime::API::Client.new(:gitlab) }
let(:non_member_api_client) { Runtime::API::Client.new(user: non_member_user) }
unless @elasticsearch_original_state_on
let(:project) do
Resource::Project.fabricate_via_api! do |project|
project.name = "api-es-#{SecureRandom.hex(8)}"
end
end
let(:elasticsearch_original_state_on?) { Runtime::Search.elasticsearch_on?(api_client) }
before do
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
......@@ -23,32 +30,36 @@ module QA
# as per this issue https://gitlab.com/gitlab-org/quality/team-tasks/issues/395
end
@project = Runtime::Project.create_project("api-es-#{SecureRandom.hex(8)}", @api_client)
Runtime::Project.push_file_to_project(@project, 'README.md', @project_file_content)
Resource::Repository::Commit.fabricate_via_api! do |commit|
commit.project = project
commit.add_files([
{ file_path: 'README.md', content: project_file_content }
])
end
end
after(:all) do
if !@elasticsearch_original_state_on && !@api_client.nil?
Runtime::Search.disable_elasticsearch(@api_client)
after do
if !elasticsearch_original_state_on? && !api_client.nil?
Runtime::Search.disable_elasticsearch(api_client)
end
end
it 'searches public project and finds a blob as an non-member user' do
successful_search(@non_member_api_client)
successful_search(non_member_api_client)
end
describe 'When searching a private repository' do
before(:all) do
Runtime::Project.set_project_visibility(@api_client, @project.id, 'private')
before do
project.set_visibility(:private)
end
it 'finds a blob as an authorized user' do
successful_search(@api_client)
successful_search(api_client)
end
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 Runtime::Search.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
......@@ -59,13 +70,13 @@ module QA
def successful_search(api_client)
QA::Support::Retrier.retry_on_exception(max_attempts: 10, sleep_interval: 3) do
get Runtime::Search.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)
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)
expect(json_body[0][:data]).to match(project_file_content)
expect(json_body[0][:project_id]).to equal(project.id)
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