diff --git a/qa/qa/git/repository.rb b/qa/qa/git/repository.rb index 606fc66b4d5..903d292b69c 100644 --- a/qa/qa/git/repository.rb +++ b/qa/qa/git/repository.rb @@ -4,20 +4,9 @@ require 'uri' module QA module Git class Repository - include Scenario::Actable + autoload :Location, 'qa/git/repository/location' - # See: config/initializers/1_settings.rb - # Settings#build_gitlab_shell_ssh_path_prefix - def self.parse_uri(git_uri) - if git_uri.start_with?('ssh://') - URI.parse(git_uri) - else - *rest, path = git_uri.split(':') - # Host cannot have : so we'll need to escape it - user_host = rest.join('%3A').sub(/\A\[(.+)\]\z/, '\1') - URI.parse("ssh://#{user_host}/#{path}") - end - end + include Scenario::Actable def self.perform(*args) Dir.mktmpdir do |dir| diff --git a/qa/qa/git/repository/location.rb b/qa/qa/git/repository/location.rb new file mode 100644 index 00000000000..dce8327ce82 --- /dev/null +++ b/qa/qa/git/repository/location.rb @@ -0,0 +1,41 @@ +require 'uri' +require 'forwardable' + +module QA + module Git + class Repository + class Location + extend Forwardable + + attr_reader :git_uri, :uri + def_delegators :@uri, :user, :host, :path + + # See: config/initializers/1_settings.rb + # Settings#build_gitlab_shell_ssh_path_prefix + def self.parse(git_uri) + if git_uri.start_with?('ssh://') + new(git_uri, URI.parse(git_uri)) + else + *rest, path = git_uri.split(':') + # Host cannot have : so we'll need to escape it + user_host = rest.join('%3A').sub(/\A\[(.+)\]\z/, '\1') + new(git_uri, URI.parse("ssh://#{user_host}/#{path}")) + end + end + + def initialize(git_uri, uri) + @git_uri = git_uri + @uri = uri + end + + def scheme + uri.scheme || 'ssh' + end + + def port + uri.port || 22 + end + end + end + end +end diff --git a/qa/qa/page/project/show.rb b/qa/qa/page/project/show.rb index 93fcfe6d5e5..834a36c4bd0 100644 --- a/qa/qa/page/project/show.rb +++ b/qa/qa/page/project/show.rb @@ -33,6 +33,10 @@ module QA find('#project_clone').value end + def repository_location_uri + Git::Repository::Location.parse(repository_location) + end + def project_name find('.qa-project-name').text end diff --git a/qa/qa/specs/features/project/deploy_key_clone_spec.rb b/qa/qa/specs/features/project/deploy_key_clone_spec.rb index 297e677175f..9b4897a1f96 100644 --- a/qa/qa/specs/features/project/deploy_key_clone_spec.rb +++ b/qa/qa/specs/features/project/deploy_key_clone_spec.rb @@ -3,6 +3,68 @@ require 'digest/sha1' module QA feature 'cloning code using a deploy key', :core, :docker do let(:runner_name) { "qa-runner-#{Time.now.to_i}" } + let(:key) { Runtime::RSAKey.new } + + given(:project) do + Factory::Resource::Project.fabricate! do |resource| + resource.name = 'deploy-key-clone-project' + end + end + + def fabricate_runner + Factory::Resource::Runner.fabricate! do |resource| + resource.project = project + resource.name = runner_name + resource.tags = %w[qa docker] + resource.image = 'gitlab/gitlab-runner:ubuntu' + end + end + + def fabricate_deploy_key + Factory::Resource::DeployKey.fabricate! do |resource| + resource.project = project + resource.title = 'deploy key title' + resource.key = key.public_key + end + end + + def fabricate_secret_variable + Factory::Resource::SecretVariable.fabricate! do |resource| + resource.project = project + resource.key = 'DEPLOY_KEY' + resource.value = key.to_pem + end + end + + def fabricate_gitlab_ci + repository_uri = Page::Project::Show.act do + choose_repository_clone_ssh + repository_location_uri + end + + <<~YAML + cat-config: + script: + - mkdir -p ~/.ssh + - ssh-keyscan -p #{repository_uri.port} #{repository_uri.host} >> ~/.ssh/known_hosts + - eval $(ssh-agent -s) + - echo "$DEPLOY_KEY" | ssh-add - + - git clone #{repository_uri.git_uri} + - sha1sum #{project.name}/.gitlab-ci.yml + tags: + - qa + - docker + YAML + end + + def fabricate_push(gitlab_ci) + Factory::Repository::Push.fabricate! do |resource| + resource.project = project + resource.file_name = '.gitlab-ci.yml' + resource.commit_message = 'Add .gitlab-ci.yml' + resource.file_content = gitlab_ci + end + end after do Service::Runner.new(runner_name).remove! @@ -12,63 +74,16 @@ module QA Runtime::Browser.visit(:gitlab, Page::Main::Login) Page::Main::Login.act { sign_in_using_credentials } - project = Factory::Resource::Project.fabricate! do |resource| - resource.name = 'deploy-key-clone-project' - end - - Factory::Resource::Runner.fabricate! do |runner| - runner.project = project - runner.name = runner_name - runner.tags = %w[qa docker] - runner.image = 'gitlab/gitlab-runner:ubuntu' - end - - key = Runtime::RSAKey.new - - Factory::Resource::DeployKey.fabricate! do |resource| - resource.project = project - resource.title = 'deploy key title' - resource.key = key.public_key - end - - Factory::Resource::SecretVariable.fabricate! do |resource| - resource.project = project - resource.key = 'DEPLOY_KEY' - resource.value = key.to_pem - end + fabricate_runner + fabricate_deploy_key + fabricate_secret_variable project.visit! - repository_url = Page::Project::Show.act do - choose_repository_clone_ssh - repository_location - end - - repository_uri = Git::Repository.parse_uri(repository_url) - - gitlab_ci = <<~YAML - cat-config: - script: - - mkdir -p ~/.ssh - - ssh-keyscan -p #{repository_uri.port || 22} #{repository_uri.host} >> ~/.ssh/known_hosts - - eval $(ssh-agent -s) - - echo "$DEPLOY_KEY" | ssh-add - - - git clone #{repository_url} - - sha1sum #{project.name}/.gitlab-ci.yml - tags: - - qa - - docker - YAML - + gitlab_ci = fabricate_gitlab_ci + fabricate_push(gitlab_ci) sha1sum = Digest::SHA1.hexdigest(gitlab_ci) - Factory::Repository::Push.fabricate! do |push| - push.project = project - push.file_name = '.gitlab-ci.yml' - push.commit_message = 'Add .gitlab-ci.yml' - push.file_content = gitlab_ci - end - Page::Project::Show.act { wait_for_push } Page::Menu::Side.act { click_ci_cd_pipelines } Page::Project::Pipeline::Index.act { go_to_latest_pipeline } diff --git a/qa/spec/git/repository_spec.rb b/qa/spec/git/repository/location_spec.rb similarity index 75% rename from qa/spec/git/repository_spec.rb rename to qa/spec/git/repository/location_spec.rb index ae58355d199..c1fe01becd7 100644 --- a/qa/spec/git/repository_spec.rb +++ b/qa/spec/git/repository/location_spec.rb @@ -1,10 +1,10 @@ -describe QA::Git::Repository do - describe '.parse_uri' do +describe QA::Git::Repository::Location do + describe '.parse' do context 'when URI starts with ssh://' do context 'when URI has port' do it 'parses correctly' do uri = described_class - .parse_uri('ssh://git@qa.test:2222/sandbox/qa/repo.git') + .parse('ssh://git@qa.test:2222/sandbox/qa/repo.git') expect(uri.user).to eq('git') expect(uri.host).to eq('qa.test') @@ -16,10 +16,11 @@ describe QA::Git::Repository do context 'when URI does not have port' do it 'parses correctly' do uri = described_class - .parse_uri('ssh://git@qa.test/sandbox/qa/repo.git') + .parse('ssh://git@qa.test/sandbox/qa/repo.git') expect(uri.user).to eq('git') expect(uri.host).to eq('qa.test') + expect(uri.port).to eq(22) expect(uri.path).to eq('/sandbox/qa/repo.git') end end @@ -29,10 +30,11 @@ describe QA::Git::Repository do context 'when host does not have colons' do it 'parses correctly' do uri = described_class - .parse_uri('git@qa.test:sandbox/qa/repo.git') + .parse('git@qa.test:sandbox/qa/repo.git') expect(uri.user).to eq('git') expect(uri.host).to eq('qa.test') + expect(uri.port).to eq(22) expect(uri.path).to eq('/sandbox/qa/repo.git') end end @@ -40,10 +42,11 @@ describe QA::Git::Repository do context 'when host has a colon' do it 'parses correctly' do uri = described_class - .parse_uri('[git@qa:test]:sandbox/qa/repo.git') + .parse('[git@qa:test]:sandbox/qa/repo.git') expect(uri.user).to eq('git') expect(uri.host).to eq('qa%3Atest') + expect(uri.port).to eq(22) expect(uri.path).to eq('/sandbox/qa/repo.git') end end