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' ...@@ -4,20 +4,9 @@ require 'uri'
module QA module QA
module Git module Git
class Repository class Repository
include Scenario::Actable autoload :Location, 'qa/git/repository/location'
# See: config/initializers/1_settings.rb include Scenario::Actable
# 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
def self.perform(*args) def self.perform(*args)
Dir.mktmpdir do |dir| 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 ...@@ -33,6 +33,10 @@ module QA
find('#project_clone').value find('#project_clone').value
end end
def repository_location_uri
Git::Repository::Location.parse(repository_location)
end
def project_name def project_name
find('.qa-project-name').text find('.qa-project-name').text
end end
......
...@@ -3,71 +3,86 @@ require 'digest/sha1' ...@@ -3,71 +3,86 @@ require 'digest/sha1'
module QA module QA
feature 'cloning code using a deploy key', :core, :docker do feature 'cloning code using a deploy key', :core, :docker do
let(:runner_name) { "qa-runner-#{Time.now.to_i}" } let(:runner_name) { "qa-runner-#{Time.now.to_i}" }
let(:key) { Runtime::RSAKey.new }
after do given(:project) do
Service::Runner.new(runner_name).remove! Factory::Resource::Project.fabricate! do |resource|
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|
resource.name = 'deploy-key-clone-project' resource.name = 'deploy-key-clone-project'
end end
end
Factory::Resource::Runner.fabricate! do |runner| def fabricate_runner
runner.project = project Factory::Resource::Runner.fabricate! do |resource|
runner.name = runner_name resource.project = project
runner.tags = %w[qa docker] resource.name = runner_name
runner.image = 'gitlab/gitlab-runner:ubuntu' resource.tags = %w[qa docker]
resource.image = 'gitlab/gitlab-runner:ubuntu'
end end
end
key = Runtime::RSAKey.new def fabricate_deploy_key
Factory::Resource::DeployKey.fabricate! do |resource| Factory::Resource::DeployKey.fabricate! do |resource|
resource.project = project resource.project = project
resource.title = 'deploy key title' resource.title = 'deploy key title'
resource.key = key.public_key resource.key = key.public_key
end end
end
def fabricate_secret_variable
Factory::Resource::SecretVariable.fabricate! do |resource| Factory::Resource::SecretVariable.fabricate! do |resource|
resource.project = project resource.project = project
resource.key = 'DEPLOY_KEY' resource.key = 'DEPLOY_KEY'
resource.value = key.to_pem resource.value = key.to_pem
end end
end
project.visit! def fabricate_gitlab_ci
repository_uri = Page::Project::Show.act do
repository_url = Page::Project::Show.act do
choose_repository_clone_ssh choose_repository_clone_ssh
repository_location repository_location_uri
end end
repository_uri = Git::Repository.parse_uri(repository_url) <<~YAML
gitlab_ci = <<~YAML
cat-config: cat-config:
script: script:
- mkdir -p ~/.ssh - 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) - eval $(ssh-agent -s)
- echo "$DEPLOY_KEY" | ssh-add - - echo "$DEPLOY_KEY" | ssh-add -
- git clone #{repository_url} - git clone #{repository_uri.git_uri}
- sha1sum #{project.name}/.gitlab-ci.yml - sha1sum #{project.name}/.gitlab-ci.yml
tags: tags:
- qa - qa
- docker - docker
YAML YAML
end
sha1sum = Digest::SHA1.hexdigest(gitlab_ci) def fabricate_push(gitlab_ci)
Factory::Repository::Push.fabricate! do |resource|
Factory::Repository::Push.fabricate! do |push| resource.project = project
push.project = project resource.file_name = '.gitlab-ci.yml'
push.file_name = '.gitlab-ci.yml' resource.commit_message = 'Add .gitlab-ci.yml'
push.commit_message = 'Add .gitlab-ci.yml' resource.file_content = gitlab_ci
push.file_content = gitlab_ci
end end
end
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::Project::Show.act { wait_for_push }
Page::Menu::Side.act { click_ci_cd_pipelines } Page::Menu::Side.act { click_ci_cd_pipelines }
......
describe QA::Git::Repository do describe QA::Git::Repository::Location do
describe '.parse_uri' do describe '.parse' do
context 'when URI starts with ssh://' do context 'when URI starts with ssh://' do
context 'when URI has port' do context 'when URI has port' do
it 'parses correctly' do it 'parses correctly' do
uri = described_class 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.user).to eq('git')
expect(uri.host).to eq('qa.test') expect(uri.host).to eq('qa.test')
...@@ -16,10 +16,11 @@ describe QA::Git::Repository do ...@@ -16,10 +16,11 @@ describe QA::Git::Repository do
context 'when URI does not have port' do context 'when URI does not have port' do
it 'parses correctly' do it 'parses correctly' do
uri = described_class 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.user).to eq('git')
expect(uri.host).to eq('qa.test') expect(uri.host).to eq('qa.test')
expect(uri.port).to eq(22)
expect(uri.path).to eq('/sandbox/qa/repo.git') expect(uri.path).to eq('/sandbox/qa/repo.git')
end end
end end
...@@ -29,10 +30,11 @@ describe QA::Git::Repository do ...@@ -29,10 +30,11 @@ describe QA::Git::Repository do
context 'when host does not have colons' do context 'when host does not have colons' do
it 'parses correctly' do it 'parses correctly' do
uri = described_class 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.user).to eq('git')
expect(uri.host).to eq('qa.test') expect(uri.host).to eq('qa.test')
expect(uri.port).to eq(22)
expect(uri.path).to eq('/sandbox/qa/repo.git') expect(uri.path).to eq('/sandbox/qa/repo.git')
end end
end end
...@@ -40,10 +42,11 @@ describe QA::Git::Repository do ...@@ -40,10 +42,11 @@ describe QA::Git::Repository do
context 'when host has a colon' do context 'when host has a colon' do
it 'parses correctly' do it 'parses correctly' do
uri = described_class 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.user).to eq('git')
expect(uri.host).to eq('qa%3Atest') expect(uri.host).to eq('qa%3Atest')
expect(uri.port).to eq(22)
expect(uri.path).to eq('/sandbox/qa/repo.git') expect(uri.path).to eq('/sandbox/qa/repo.git')
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