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 ...@@ -39,7 +39,6 @@ module QA
autoload :MailHog, 'qa/runtime/mail_hog' autoload :MailHog, 'qa/runtime/mail_hog'
autoload :IPAddress, 'qa/runtime/ip_address' autoload :IPAddress, 'qa/runtime/ip_address'
autoload :Search, 'qa/runtime/search' autoload :Search, 'qa/runtime/search'
autoload :Project, 'qa/runtime/project'
autoload :ApplicationSettings, 'qa/runtime/application_settings' autoload :ApplicationSettings, 'qa/runtime/application_settings'
module API module API
...@@ -88,6 +87,7 @@ module QA ...@@ -88,6 +87,7 @@ module QA
autoload :Tag, 'qa/resource/tag' autoload :Tag, 'qa/resource/tag'
autoload :ProjectMember, 'qa/resource/project_member' autoload :ProjectMember, 'qa/resource/project_member'
autoload :UserGPG, 'qa/resource/user_gpg' autoload :UserGPG, 'qa/resource/user_gpg'
autoload :Visibility, 'qa/resource/visibility'
module Events module Events
autoload :Base, 'qa/resource/events/base' autoload :Base, 'qa/resource/events/base'
......
...@@ -7,11 +7,11 @@ module QA ...@@ -7,11 +7,11 @@ module QA
class Project < Base class Project < Base
include Events::Project include Events::Project
include Members include Members
include Visibility
attr_accessor :repository_storage # requires admin access attr_accessor :repository_storage # requires admin access
attr_writer :initialize_with_readme attr_writer :initialize_with_readme
attr_writer :auto_devops_enabled attr_writer :auto_devops_enabled
attr_writer :visibility
attribute :id attribute :id
attribute :name attribute :name
...@@ -19,6 +19,7 @@ module QA ...@@ -19,6 +19,7 @@ module QA
attribute :description attribute :description
attribute :standalone attribute :standalone
attribute :runners_token attribute :runners_token
attribute :visibility
attribute :group do attribute :group do
Group.fabricate! Group.fabricate!
...@@ -50,7 +51,7 @@ module QA ...@@ -50,7 +51,7 @@ module QA
@description = 'My awesome project' @description = 'My awesome project'
@initialize_with_readme = false @initialize_with_readme = false
@auto_devops_enabled = false @auto_devops_enabled = false
@visibility = 'public' @visibility = :public
end end
def name=(raw_name) def name=(raw_name)
...@@ -83,6 +84,10 @@ module QA ...@@ -83,6 +84,10 @@ module QA
"/projects/#{CGI.escape(path_with_namespace)}" "/projects/#{CGI.escape(path_with_namespace)}"
end end
def api_visibility_path
"/projects/#{id}"
end
def api_get_archive_path(type = 'tar.gz') def api_get_archive_path(type = 'tar.gz')
"#{api_get_path}/repository/archive.#{type}" "#{api_get_path}/repository/archive.#{type}"
end 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 ...@@ -6,12 +6,20 @@ module QA
context 'Enablement:Search' do context 'Enablement:Search' do
include Support::Api include Support::Api
describe 'Elasticsearch advanced global search with advanced syntax', :orchestrated, :elasticsearch, :requires_admin, quarantine: { type: :new } do describe 'Elasticsearch advanced global search with advanced syntax', :orchestrated, :elasticsearch, :requires_admin, quarantine: { type: :new } do
before(:all) do let(:project_name_suffix) { SecureRandom.hex(8) }
@api_client = Runtime::API::Client.new(:gitlab) let(:api_client) { Runtime::API::Client.new(:gitlab) }
@project_name_suffix = SecureRandom.hex(8)
@elasticsearch_original_state_on = Runtime::Search.elasticsearch_on?(@api_client)
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! QA::EE::Resource::Settings::Elasticsearch.fabricate_via_api!
sleep(60) sleep(60)
# wait for the change to propagate before inserting records or else # wait for the change to propagate before inserting records or else
...@@ -21,25 +29,27 @@ module QA ...@@ -21,25 +29,27 @@ module QA
# as per this issue https://gitlab.com/gitlab-org/quality/team-tasks/issues/395 # as per this issue https://gitlab.com/gitlab-org/quality/team-tasks/issues/395
end end
@project = Runtime::Project.create_project("es-adv-global-search-#{@project_name_suffix}", Resource::Repository::Commit.fabricate_via_api! do |commit|
@api_client, commit.project = project
"This is a unique project description #{@project_name_suffix}") commit.add_files([
Runtime::Project.push_file_to_project(@project, 'elasticsearch.rb', "elasticsearch: #{SecureRandom.hex(8)}") { file_path: 'elasticsearch.rb', content: "elasticsearch: #{SecureRandom.hex(8)}" }
])
end
end end
after(:all) do after do
if !@elasticsearch_original_state_on && !@api_client.nil? if !elasticsearch_original_state_on? && !api_client.nil?
Runtime::Search.disable_elasticsearch(@api_client) Runtime::Search.disable_elasticsearch(api_client)
end end
end end
context 'when searching for projects using advanced syntax' do context 'when searching for projects using advanced syntax' do
it 'searches in the project name' 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 end
it 'searches in the project description' do 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
end end
...@@ -47,12 +57,12 @@ module QA ...@@ -47,12 +57,12 @@ module QA
def expect_search_to_find_project(search_term) def expect_search_to_find_project(search_term)
QA::Support::Retrier.retry_on_exception(max_attempts: 10, sleep_interval: 3) do 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) expect_status(QA::Support::Api::HTTP_STATUS_OK)
raise 'Empty search result returned' if json_body.empty? 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 end
end end
......
...@@ -6,14 +6,21 @@ module QA ...@@ -6,14 +6,21 @@ module QA
context 'Enablement:Search' do context 'Enablement:Search' do
include Support::Api include Support::Api
describe 'When using elasticsearch API to search for a known blob', :orchestrated, :elasticsearch, :requires_admin, quarantine: { type: :new } do describe 'When using elasticsearch API to search for a known blob', :orchestrated, :elasticsearch, :requires_admin, quarantine: { type: :new } do
before(:all) do let(:project_file_content) { "elasticsearch: #{SecureRandom.hex(8)}" }
@api_client = Runtime::API::Client.new(:gitlab) let(:non_member_user) { Resource::User.fabricate_or_use('non_member_user', 'non_member_user_password') }
@project_file_content = "elasticsearch: #{SecureRandom.hex(8)}" let(:api_client) { Runtime::API::Client.new(:gitlab) }
non_member_user = Resource::User.fabricate_or_use('non_member_user', 'non_member_user_password') let(:non_member_api_client) { Runtime::API::Client.new(user: non_member_user) }
@non_member_api_client = Runtime::API::Client.new(user: non_member_user)
@elasticsearch_original_state_on = Runtime::Search.elasticsearch_on?(@api_client)
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! QA::EE::Resource::Settings::Elasticsearch.fabricate_via_api!
sleep(60) sleep(60)
# wait for the change to propagate before inserting records or else # wait for the change to propagate before inserting records or else
...@@ -23,32 +30,36 @@ module QA ...@@ -23,32 +30,36 @@ module QA
# as per this issue https://gitlab.com/gitlab-org/quality/team-tasks/issues/395 # as per this issue https://gitlab.com/gitlab-org/quality/team-tasks/issues/395
end end
@project = Runtime::Project.create_project("api-es-#{SecureRandom.hex(8)}", @api_client) Resource::Repository::Commit.fabricate_via_api! do |commit|
Runtime::Project.push_file_to_project(@project, 'README.md', @project_file_content) commit.project = project
commit.add_files([
{ file_path: 'README.md', content: project_file_content }
])
end
end end
after(:all) do after do
if !@elasticsearch_original_state_on && !@api_client.nil? if !elasticsearch_original_state_on? && !api_client.nil?
Runtime::Search.disable_elasticsearch(@api_client) Runtime::Search.disable_elasticsearch(api_client)
end end
end end
it 'searches public project and finds a blob as an non-member user' do 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 end
describe 'When searching a private repository' do describe 'When searching a private repository' do
before(:all) do before do
Runtime::Project.set_project_visibility(@api_client, @project.id, 'private') project.set_visibility(:private)
end end
it 'finds a blob as an authorized user' do it 'finds a blob as an authorized user' do
successful_search(@api_client) successful_search(api_client)
end end
it 'does not find a blob as an non-member user' do 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 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_status(QA::Support::Api::HTTP_STATUS_OK)
expect(json_body).to be_empty expect(json_body).to be_empty
end end
...@@ -59,13 +70,13 @@ module QA ...@@ -59,13 +70,13 @@ module QA
def successful_search(api_client) def successful_search(api_client)
QA::Support::Retrier.retry_on_exception(max_attempts: 10, sleep_interval: 3) do 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) expect_status(QA::Support::Api::HTTP_STATUS_OK)
raise 'Empty search result returned' if json_body.empty? raise 'Empty search result returned' if json_body.empty?
expect(json_body[0][:data]).to match(@project_file_content) expect(json_body[0][:data]).to match(project_file_content)
expect(json_body[0][:project_id]).to equal(@project.id) expect(json_body[0][:project_id]).to equal(project.id)
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