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
clean_frontend_fixtures('static/')
end
fixtures_path = File.expand_path(JavaScriptFixturesHelpers::FIXTURE_PATH, Rails.root)
haml_fixtures = Dir.glob(File.expand_path('**/*.haml', fixtures_path)).map do |file_path|
file_path.sub(/\A#{fixtures_path}#{File::SEPARATOR}/, '')
end
JavaScriptFixturesHelpers::FIXTURE_PATHS.each do |fixture_path|
fixtures_path = File.expand_path(fixture_path, Rails.root)
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|
fixture_file_name = example.description
rendered = render_template(template_file_name)
rendered = render_template(fixture_path, template_file_name)
store_frontend_fixture(rendered, fixture_file_name)
end
end
end
private
def render_template(template_file_name)
fixture_path = JavaScriptFixturesHelpers::FIXTURE_PATH
def render_template(fixture_path, template_file_name)
controller = ApplicationController.new
controller.prepend_view_path(fixture_path)
controller.render_to_string(template: template_file_name, layout: false)
......
module SchemaPath
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
......
# 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 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
module DbCleaner
def deletion_except_tables
[]
end
config.append_after do
DatabaseCleaner.clean
def setup_database_cleaner
DatabaseCleaner[:active_record, { connection: ActiveRecord::Base }]
end
end
......@@ -2,11 +2,12 @@ require 'action_dispatch/testing/test_request'
require 'fileutils'
module JavaScriptFixturesHelpers
extend ActiveSupport::Concern
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|
# pick an arbitrary date from the past, so tests are not time dependent
Timecop.freeze(Time.utc(2015, 7, 3, 10)) { example.run }
......@@ -15,27 +16,31 @@ module JavaScriptFixturesHelpers
# 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)
directory_name = File.expand_path(directory_name, FIXTURE_PATH)
FIXTURE_PATHS.each do |fixture_path|
directory_name = File.expand_path(directory_name, fixture_path)
Dir[File.expand_path('*.html.raw', directory_name)].each do |file_name|
FileUtils.rm(file_name)
end
end
end
# Public: Store a response object as fixture file
#
# 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)
fixture_file_name = File.expand_path(fixture_file_name, FIXTURE_PATH)
FIXTURE_PATHS.each do |fixture_path|
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))
File.write(fixture_file_name, fixture)
end
end
def remove_repository(project)
Gitlab::Shell.new.remove_repository(project.repository_storage, project.disk_path)
......
......@@ -9,6 +9,10 @@ module KubernetesHelpers
kube_response(kube_pods_body)
end
def kube_logs_response
kube_response(kube_logs_body)
end
def kube_deployments_response
kube_response(kube_deployments_body)
end
......@@ -34,6 +38,13 @@ module KubernetesHelpers
WebMock.stub_request(:get, pods_url).to_return(response || kube_pods_response)
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)
stub_kubeclient_discover(service.api_url)
deployments_url = service.api_url + "/apis/extensions/v1beta1/namespaces/#{service.actual_namespace}/deployments"
......@@ -212,6 +223,10 @@ module KubernetesHelpers
}
end
def kube_logs_body
"Log 1\nLog 2\nLog 3"
end
def kube_deployments_body
{
"kind" => "DeploymentList",
......
......@@ -7,28 +7,27 @@ module AccessMatchers
extend RSpec::Matchers::DSL
include Warden::Test::Helpers
def emulate_user(user, membership = nil)
case user
when :user
login_as(create(:user))
def emulate_user(user_type_or_trait, membership = nil)
case user_type_or_trait
when :user, :admin
login_as(create(user_type_or_trait))
when :external, :auditor
login_as(create(:user, user_type_or_trait))
when :visitor
logout
when :admin
login_as(create(:admin))
when :external
login_as(create(:user, external: true))
when User
login_as(user)
login_as(user_type_or_trait)
when *Gitlab::Access.sym_options_with_owner.keys
raise ArgumentError, "cannot emulate #{user} without membership parent" unless membership
role = user
raise ArgumentError, "cannot emulate #{user_type_or_trait} without membership parent" unless membership
role = user_type_or_trait
user =
if role == :owner && membership.owner
user = membership.owner
membership.owner
else
user = create(:user)
membership.public_send(:"add_#{role}", user)
create(:user).tap do |new_user|
membership.public_send(:"add_#{role}", new_user)
end
end
login_as(user)
......
......@@ -26,6 +26,14 @@ Service.available_services_names.each do |service|
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)
service_item = project.find_or_initialize_service(service)
service_item.properties = service_attrs
......
require 'webmock'
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