Commit 410b4d64 authored by Mark Lapierre's avatar Mark Lapierre Committed by Ramya Authappan

Add default branch name e2e test

Adds an e2e test of instance-wide default branch name setting
- Includes a test of creating a project from an existing repo

Update ApplicationSettings
- Makes it a class since it's not intended to be included (and this
avoids rubocop complaints about a module with instance variables)
- Allow settings to be restored
- Dedupe API client code
parent f9e2925d
...@@ -118,6 +118,10 @@ module QA ...@@ -118,6 +118,10 @@ module QA
run(%Q{git config user.signingkey #{@gpg_key_id} && git config gpg.program $(command -v gpg) && git commit -S -m "#{message}"}).to_s run(%Q{git config user.signingkey #{@gpg_key_id} && git config gpg.program $(command -v gpg) && git commit -S -m "#{message}"}).to_s
end end
def current_branch
run("git rev-parse --abbrev-ref HEAD").to_s
end
def push_changes(branch = 'master') def push_changes(branch = 'master')
run("git push #{uri} #{branch}", max_attempts: 3).to_s run("git push #{uri} #{branch}", max_attempts: 3).to_s
end end
......
...@@ -13,6 +13,7 @@ module QA ...@@ -13,6 +13,7 @@ module QA
attr_writer :initialize_with_readme attr_writer :initialize_with_readme
attr_writer :auto_devops_enabled attr_writer :auto_devops_enabled
attribute :default_branch
attribute :id attribute :id
attribute :name attribute :name
attribute :add_name_uuid attribute :add_name_uuid
......
...@@ -2,43 +2,39 @@ ...@@ -2,43 +2,39 @@
module QA module QA
module Runtime module Runtime
module ApplicationSettings class ApplicationSettings
extend self class << self
extend Support::Api include Support::Api
APPLICATION_SETTINGS_PATH = '/application/settings' APPLICATION_SETTINGS_PATH = '/application/settings'
# Set a GitLab application setting # Set a GitLab application setting
# Example: # Example:
# #set({ allow_local_requests_from_web_hooks_and_services: true }) # #set({ allow_local_requests_from_web_hooks_and_services: true })
# #set(allow_local_requests_from_web_hooks_and_services: true) # #set(allow_local_requests_from_web_hooks_and_services: true)
# https://docs.gitlab.com/ee/api/settings.html # https://docs.gitlab.com/ee/api/settings.html
def set_application_settings(**application_settings) def set_application_settings(**application_settings)
QA::Runtime::Logger.info("Setting application settings: #{application_settings}") @original_application_settings = get_application_settings
r = put(Runtime::API::Request.new(api_client, APPLICATION_SETTINGS_PATH).url, **application_settings)
raise "Couldn't set application settings #{application_settings.inspect}" unless r.code == QA::Support::Api::HTTP_STATUS_OK QA::Runtime::Logger.info("Setting application settings: #{application_settings}")
end r = put(Runtime::API::Request.new(api_client, APPLICATION_SETTINGS_PATH).url, **application_settings)
raise "Couldn't set application settings #{application_settings.inspect}" unless r.code == QA::Support::Api::HTTP_STATUS_OK
def get_application_settings end
parse_body(get(Runtime::API::Request.new(api_client, APPLICATION_SETTINGS_PATH).url))
end
private
def api_client def get_application_settings
@api_client ||= begin parse_body(get(Runtime::API::Request.new(api_client, APPLICATION_SETTINGS_PATH).url))
return Runtime::API::Client.new(:gitlab, personal_access_token: Runtime::Env.admin_personal_access_token) if Runtime::Env.admin_personal_access_token end
user = Resource::User.fabricate_via_api! do |user| def restore_application_settings(*application_settings_keys)
user.username = Runtime::User.admin_username set_application_settings(@original_application_settings.slice(*application_settings_keys))
user.password = Runtime::User.admin_password end
end
unless user.admin? private
raise "Administrator access is required to set application settings. User '#{user.username}' is not an administrator."
end
Runtime::API::Client.new(:gitlab, user: user) def api_client
@api_client ||= Runtime::API::Client.as_admin
rescue AuthorizationError => e
raise "Administrator access is required to set application settings. #{e.message}"
end end
end end
end end
......
# frozen_string_literal: true
require 'securerandom'
module QA
RSpec.describe 'Create' do
describe 'Default branch name instance setting', :requires_admin, :skip_live_env do
before(:context) do
Runtime::ApplicationSettings.set_application_settings(default_branch_name: 'main')
end
after(:context) do
Runtime::ApplicationSettings.restore_application_settings(:default_branch_name)
end
it 'sets the default branch name for a new project' do
project = Resource::Project.fabricate_via_api! do |project|
project.name = "default-branch-name"
project.initialize_with_readme = true
end
# It takes a moment to create the project. We wait until we
# know it exists before we try to clone it
Support::Waiter.wait_until { project.has_file?('README.md') }
Git::Repository.perform do |repository|
repository.uri = project.repository_http_location.uri
repository.use_default_credentials
repository.clone
expect(repository.current_branch).to eq('main')
end
end
it 'allows a project to be created via the CLI with a different default branch name' do
project_name = "default-branch-name-via-cli-#{SecureRandom.hex(8)}"
group = Resource::Group.fabricate_via_api!
Git::Repository.perform do |repository|
repository.init_repository
repository.uri = "#{Runtime::Scenario.gitlab_address}/#{group.full_path}/#{project_name}"
repository.use_default_credentials
repository.configure_identity('GitLab QA', 'root@gitlab.com')
repository.checkout('trunk', new_branch: true)
repository.commit_file('README.md', 'Created via the CLI', 'initial commit via CLI')
repository.push_changes('trunk')
end
project = Resource::Project.fabricate_via_api! do |project|
project.add_name_uuid = false
project.name = project_name
project.group = group
end
expect(project.default_branch).to eq('trunk')
expect(project).to have_file('README.md')
expect(project.commits.map { |commit| commit[:message].chomp })
.to include('initial commit via CLI')
end
end
end
end
...@@ -16,12 +16,14 @@ describe QA::Runtime::ApplicationSettings do ...@@ -16,12 +16,14 @@ describe QA::Runtime::ApplicationSettings do
.with(api_client, '/application/settings') .with(api_client, '/application/settings')
.and_return(request) .and_return(request)
expect(described_class).to receive(:get_application_settings)
expect(described_class) expect(described_class)
.to receive(:put) .to receive(:put)
.with(request.url, { allow_local_requests_from_web_hooks_and_services: true }) .with(request.url, { allow_local_requests_from_web_hooks_and_services: true })
.and_return(Struct.new(:code).new(200)) .and_return(Struct.new(:code).new(200))
subject.set_application_settings(allow_local_requests_from_web_hooks_and_services: true) described_class.set_application_settings(allow_local_requests_from_web_hooks_and_services: true)
end end
end end
...@@ -37,7 +39,7 @@ describe QA::Runtime::ApplicationSettings do ...@@ -37,7 +39,7 @@ describe QA::Runtime::ApplicationSettings do
.with(request.url) .with(request.url)
.and_return(get_response) .and_return(get_response)
subject.get_application_settings described_class.get_application_settings
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