Commit 565fdd63 authored by Lin Jen-Shin's avatar Lin Jen-Shin

Rearrange the test structure and introduce

a new repository location class.
parent 2f34ef34
......@@ -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|
......
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
......@@ -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
......
......@@ -3,72 +3,87 @@ 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 }
after do
Service::Runner.new(runner_name).remove!
end
scenario 'user sets up a deploy key to clone code using pipelines' do
Runtime::Browser.visit(:gitlab, Page::Main::Login)
Page::Main::Login.act { sign_in_using_credentials }
project = Factory::Resource::Project.fabricate! do |resource|
given(:project) do
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
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
project.visit!
repository_url = Page::Project::Show.act do
def fabricate_gitlab_ci
repository_uri = Page::Project::Show.act do
choose_repository_clone_ssh
repository_location
repository_location_uri
end
repository_uri = Git::Repository.parse_uri(repository_url)
gitlab_ci = <<~YAML
<<~YAML
cat-config:
script:
- mkdir -p ~/.ssh
- ssh-keyscan -p #{repository_uri.port || 22} #{repository_uri.host} >> ~/.ssh/known_hosts
- ssh-keyscan -p #{repository_uri.port} #{repository_uri.host} >> ~/.ssh/known_hosts
- eval $(ssh-agent -s)
- echo "$DEPLOY_KEY" | ssh-add -
- git clone #{repository_url}
- git clone #{repository_uri.git_uri}
- sha1sum #{project.name}/.gitlab-ci.yml
tags:
- qa
- docker
YAML
end
sha1sum = Digest::SHA1.hexdigest(gitlab_ci)
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
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
after do
Service::Runner.new(runner_name).remove!
end
scenario 'user sets up a deploy key to clone code using pipelines' do
Runtime::Browser.visit(:gitlab, Page::Main::Login)
Page::Main::Login.act { sign_in_using_credentials }
fabricate_runner
fabricate_deploy_key
fabricate_secret_variable
project.visit!
gitlab_ci = fabricate_gitlab_ci
fabricate_push(gitlab_ci)
sha1sum = Digest::SHA1.hexdigest(gitlab_ci)
Page::Project::Show.act { wait_for_push }
Page::Menu::Side.act { click_ci_cd_pipelines }
Page::Project::Pipeline::Index.act { go_to_latest_pipeline }
......
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
......
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