Commit 7aa3c9b3 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge branch 'ce-extract-ee-specific-code-for-spec/support' into 'master'

[CE] Extract EE specific code for spec/support

See merge request gitlab-org/gitlab-ce!25569
parents 482b86ae f5a4adf4
...@@ -7,23 +7,23 @@ describe ApplicationController, '(Static JavaScript fixtures)', type: :controlle ...@@ -7,23 +7,23 @@ describe ApplicationController, '(Static JavaScript fixtures)', type: :controlle
clean_frontend_fixtures('static/') clean_frontend_fixtures('static/')
end end
fixtures_path = File.expand_path(JavaScriptFixturesHelpers::FIXTURE_PATH, Rails.root) JavaScriptFixturesHelpers::FIXTURE_PATHS.each do |fixture_path|
haml_fixtures = Dir.glob(File.expand_path('**/*.haml', fixtures_path)).map do |file_path| fixtures_path = File.expand_path(fixture_path, Rails.root)
file_path.sub(/\A#{fixtures_path}#{File::SEPARATOR}/, '')
end Dir.glob(File.expand_path('**/*.haml', fixtures_path)).map do |file_path|
template_file_name = file_path.sub(/\A#{fixtures_path}#{File::SEPARATOR}/, '')
haml_fixtures.each do |template_file_name| it "static/#{template_file_name.sub(/\.haml\z/, '.raw')}" do |example|
it "static/#{template_file_name.sub(/\.haml\z/, '.raw')}" do |example| fixture_file_name = example.description
fixture_file_name = example.description rendered = render_template(fixture_path, template_file_name)
rendered = render_template(template_file_name) store_frontend_fixture(rendered, fixture_file_name)
store_frontend_fixture(rendered, fixture_file_name) end
end end
end end
private private
def render_template(template_file_name) def render_template(fixture_path, template_file_name)
fixture_path = JavaScriptFixturesHelpers::FIXTURE_PATH
controller = ApplicationController.new controller = ApplicationController.new
controller.prepend_view_path(fixture_path) controller.prepend_view_path(fixture_path)
controller.render_to_string(template: template_file_name, layout: false) controller.render_to_string(template: template_file_name, layout: false)
......
module SchemaPath module SchemaPath
def self.expand(schema, dir = '') def self.expand(schema, dir = '')
Rails.root.join('spec', dir, "fixtures/api/schemas/#{schema}.json").to_s Rails.root.join(dir, 'spec', "fixtures/api/schemas/#{schema}.json").to_s
end end
end end
......
# frozen_string_literal: true
require 'database_cleaner/active_record/deletion'
require_relative 'db_cleaner'
module FakeInformationSchema
# Work around a bug in DatabaseCleaner when using the deletion strategy:
# https://github.com/DatabaseCleaner/database_cleaner/issues/347
#
# On MySQL, if the information schema is said to exist, we use an inaccurate
# row count leading to some tables not being cleaned when they should
def information_schema_exists?(_connection)
false
end
end
DatabaseCleaner::ActiveRecord::Deletion.prepend(FakeInformationSchema)
RSpec.configure do |config|
include DbCleaner
# Ensure all sequences are reset at the start of the suite run
config.before(:suite) do
setup_database_cleaner
DatabaseCleaner.clean_with(:truncation)
end
config.append_after(:context) do
DatabaseCleaner.clean_with(:deletion, cache_tables: false)
end
config.before do
setup_database_cleaner
DatabaseCleaner.strategy = :transaction
end
config.before(:each, :js) do
DatabaseCleaner.strategy = :deletion, { except: deletion_except_tables, cache_tables: false }
end
config.before(:each, :delete) do
DatabaseCleaner.strategy = :deletion, { except: deletion_except_tables, cache_tables: false }
end
config.before(:each, :migration) do
DatabaseCleaner.strategy = :deletion, { cache_tables: false }
end
config.before do
DatabaseCleaner.start
end
config.append_after do
DatabaseCleaner.clean
end
end
require 'database_cleaner/active_record/deletion' module DbCleaner
def deletion_except_tables
module FakeInformationSchema []
# Work around a bug in DatabaseCleaner when using the deletion strategy:
# https://github.com/DatabaseCleaner/database_cleaner/issues/347
#
# On MySQL, if the information schema is said to exist, we use an inaccurate
# row count leading to some tables not being cleaned when they should
def information_schema_exists?(_connection)
false
end
end
DatabaseCleaner::ActiveRecord::Deletion.prepend(FakeInformationSchema)
RSpec.configure do |config|
# Ensure all sequences are reset at the start of the suite run
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.append_after(:context) do
DatabaseCleaner.clean_with(:deletion, cache_tables: false)
end
config.before do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, :js) do
DatabaseCleaner.strategy = :deletion, { cache_tables: false }
end
config.before(:each, :delete) do
DatabaseCleaner.strategy = :deletion, { cache_tables: false }
end
config.before(:each, :migration) do
DatabaseCleaner.strategy = :deletion, { cache_tables: false }
end
config.before do
DatabaseCleaner.start
end end
config.append_after do def setup_database_cleaner
DatabaseCleaner.clean DatabaseCleaner[:active_record, { connection: ActiveRecord::Base }]
end end
end end
...@@ -2,11 +2,12 @@ require 'action_dispatch/testing/test_request' ...@@ -2,11 +2,12 @@ require 'action_dispatch/testing/test_request'
require 'fileutils' require 'fileutils'
module JavaScriptFixturesHelpers module JavaScriptFixturesHelpers
extend ActiveSupport::Concern
include Gitlab::Popen include Gitlab::Popen
FIXTURE_PATH = 'spec/javascripts/fixtures'.freeze FIXTURE_PATHS = %w[spec/javascripts/fixtures ee/spec/javascripts/fixtures].freeze
def self.included(base) included do |base|
base.around do |example| base.around do |example|
# pick an arbitrary date from the past, so tests are not time dependent # pick an arbitrary date from the past, so tests are not time dependent
Timecop.freeze(Time.utc(2015, 7, 3, 10)) { example.run } Timecop.freeze(Time.utc(2015, 7, 3, 10)) { example.run }
...@@ -15,26 +16,30 @@ module JavaScriptFixturesHelpers ...@@ -15,26 +16,30 @@ module JavaScriptFixturesHelpers
# Public: Removes all fixture files from given directory # Public: Removes all fixture files from given directory
# #
# directory_name - directory of the fixtures (relative to FIXTURE_PATH) # directory_name - directory of the fixtures (relative to FIXTURE_PATHS)
# #
def clean_frontend_fixtures(directory_name) def clean_frontend_fixtures(directory_name)
directory_name = File.expand_path(directory_name, FIXTURE_PATH) FIXTURE_PATHS.each do |fixture_path|
Dir[File.expand_path('*.html.raw', directory_name)].each do |file_name| directory_name = File.expand_path(directory_name, fixture_path)
FileUtils.rm(file_name) Dir[File.expand_path('*.html.raw', directory_name)].each do |file_name|
FileUtils.rm(file_name)
end
end end
end end
# Public: Store a response object as fixture file # Public: Store a response object as fixture file
# #
# response - string or response object to store # response - string or response object to store
# fixture_file_name - file name to store the fixture in (relative to FIXTURE_PATH) # fixture_file_name - file name to store the fixture in (relative to FIXTURE_PATHS)
# #
def store_frontend_fixture(response, fixture_file_name) def store_frontend_fixture(response, fixture_file_name)
fixture_file_name = File.expand_path(fixture_file_name, FIXTURE_PATH) FIXTURE_PATHS.each do |fixture_path|
fixture = response.respond_to?(:body) ? parse_response(response) : response fixture_file_name = File.expand_path(fixture_file_name, fixture_path)
fixture = response.respond_to?(:body) ? parse_response(response) : response
FileUtils.mkdir_p(File.dirname(fixture_file_name)) FileUtils.mkdir_p(File.dirname(fixture_file_name))
File.write(fixture_file_name, fixture) File.write(fixture_file_name, fixture)
end
end end
def remove_repository(project) def remove_repository(project)
......
...@@ -9,6 +9,10 @@ module KubernetesHelpers ...@@ -9,6 +9,10 @@ module KubernetesHelpers
kube_response(kube_pods_body) kube_response(kube_pods_body)
end end
def kube_logs_response
kube_response(kube_logs_body)
end
def kube_deployments_response def kube_deployments_response
kube_response(kube_deployments_body) kube_response(kube_deployments_body)
end end
...@@ -34,6 +38,13 @@ module KubernetesHelpers ...@@ -34,6 +38,13 @@ module KubernetesHelpers
WebMock.stub_request(:get, pods_url).to_return(response || kube_pods_response) WebMock.stub_request(:get, pods_url).to_return(response || kube_pods_response)
end end
def stub_kubeclient_logs(pod_name, response = nil)
stub_kubeclient_discover(service.api_url)
logs_url = service.api_url + "/api/v1/namespaces/#{service.actual_namespace}/pods/#{pod_name}/log?tailLines=#{Clusters::Platforms::Kubernetes::LOGS_LIMIT}"
WebMock.stub_request(:get, logs_url).to_return(response || kube_logs_response)
end
def stub_kubeclient_deployments(response = nil) def stub_kubeclient_deployments(response = nil)
stub_kubeclient_discover(service.api_url) stub_kubeclient_discover(service.api_url)
deployments_url = service.api_url + "/apis/extensions/v1beta1/namespaces/#{service.actual_namespace}/deployments" deployments_url = service.api_url + "/apis/extensions/v1beta1/namespaces/#{service.actual_namespace}/deployments"
...@@ -212,6 +223,10 @@ module KubernetesHelpers ...@@ -212,6 +223,10 @@ module KubernetesHelpers
} }
end end
def kube_logs_body
"Log 1\nLog 2\nLog 3"
end
def kube_deployments_body def kube_deployments_body
{ {
"kind" => "DeploymentList", "kind" => "DeploymentList",
......
...@@ -7,29 +7,28 @@ module AccessMatchers ...@@ -7,29 +7,28 @@ module AccessMatchers
extend RSpec::Matchers::DSL extend RSpec::Matchers::DSL
include Warden::Test::Helpers include Warden::Test::Helpers
def emulate_user(user, membership = nil) def emulate_user(user_type_or_trait, membership = nil)
case user case user_type_or_trait
when :user when :user, :admin
login_as(create(:user)) login_as(create(user_type_or_trait))
when :external, :auditor
login_as(create(:user, user_type_or_trait))
when :visitor when :visitor
logout logout
when :admin
login_as(create(:admin))
when :external
login_as(create(:user, external: true))
when User when User
login_as(user) login_as(user_type_or_trait)
when *Gitlab::Access.sym_options_with_owner.keys when *Gitlab::Access.sym_options_with_owner.keys
raise ArgumentError, "cannot emulate #{user} without membership parent" unless membership raise ArgumentError, "cannot emulate #{user_type_or_trait} without membership parent" unless membership
role = user
if role == :owner && membership.owner role = user_type_or_trait
user = membership.owner user =
else if role == :owner && membership.owner
user = create(:user) membership.owner
membership.public_send(:"add_#{role}", user) else
end create(:user).tap do |new_user|
membership.public_send(:"add_#{role}", new_user)
end
end
login_as(user) login_as(user)
else else
......
...@@ -26,6 +26,14 @@ Service.available_services_names.each do |service| ...@@ -26,6 +26,14 @@ Service.available_services_names.each do |service|
end end
end end
before do
if service == 'github' && respond_to?(:stub_licensed_features)
stub_licensed_features(github_project_service_integration: true)
project.clear_memoization(:disabled_services)
project.clear_memoization(:licensed_feature_available)
end
end
def initialize_service(service) def initialize_service(service)
service_item = project.find_or_initialize_service(service) service_item = project.find_or_initialize_service(service)
service_item.properties = service_attrs service_item.properties = service_attrs
......
require 'webmock' require 'webmock'
require 'webmock/rspec' require 'webmock/rspec'
WebMock.disable_net_connect!(allow_localhost: true) def webmock_allowed_hosts
%w[elasticsearch registry.gitlab.com-gitlab-org-test-elastic-image].tap do |hosts|
if ENV.key?('ELASTIC_URL')
hosts << URI.parse(ENV['ELASTIC_URL']).host
end
end.uniq
end
WebMock.disable_net_connect!(allow_localhost: true, allow: webmock_allowed_hosts)
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