1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
|
# frozen_string_literal: true
RSpec.describe QA::Git::Repository do
shared_context 'unresolvable git directory' do
let(:logger) { instance_double(Logger, info: nil, debug: nil) }
let(:default_user) { QA::Resource::User.new }
let(:repo_uri) { 'http://foo/bar.git' }
let(:repo_uri_with_credentials) { "http://#{default_user.username}@foo/bar.git" }
let(:env_vars) { [%q(HOME="temp")] }
let(:extra_env_vars) { [] }
let(:run_params) { { raise_on_failure: true, env: env_vars + extra_env_vars, sleep_internal: 0, log_prefix: "Git: " } }
let(:repository) do
described_class.new(command_retry_sleep_interval: 0).tap do |r|
r.uri = repo_uri
r.env_vars = env_vars
end
end
let(:tmp_git_dir) { Dir.mktmpdir }
let(:tmp_netrc_dir) { Dir.mktmpdir }
before do
allow(repository).to receive(:tmp_home_dir).and_return(tmp_netrc_dir)
allow(QA::Runtime::Logger).to receive(:logger).and_return(logger)
allow(QA::Runtime::UserStore).to receive(:test_user).and_return(default_user)
end
around do |example|
FileUtils.cd(tmp_git_dir) do
example.run
end
end
after do
FileUtils.remove_entry_secure(tmp_git_dir, true)
FileUtils.remove_entry_secure(tmp_netrc_dir, true)
end
end
shared_examples 'command with retries' do
let(:result_output) { +'Command successful' }
let(:result) { described_class::Result.new(any_args, 0, result_output) }
let(:command_return) { result_output }
context 'when command is successful' do
it 'returns the #run command Result output' do
expect(repository).to receive(:run).with(command, run_params.merge(max_attempts: 3)).and_return(result)
expect(call_method).to eq(command_return)
end
end
context 'when command is not successful the first time' do
context 'and retried command is successful' do
it 'retries the command twice and returns the successful #run command Result output' do
expect(Open3).to receive(:capture2e).and_return([+'', double(exitstatus: 1)]).twice
expect(Open3).to receive(:capture2e).and_return([result_output, double(exitstatus: 0)])
expect(call_method).to eq(command_return)
end
end
context 'and retried command is not successful after 3 attempts' do
it 'raises a CommandError exception' do
expect(Open3).to receive(:capture2e).and_return([+'FAILURE', double(exitstatus: 42)]).exactly(3).times
expect do
call_method
end.to raise_error(QA::Support::Run::CommandError,
/The command .* failed \(42\) with the following output:\nFAILURE/)
end
end
end
end
shared_examples 'command with no retries' do
let(:result_output) { +'Command successful' }
let(:result) { described_class::Result.new(any_args, 0, result_output) }
let(:command_return) { result_output }
context 'when command is successful' do
it 'returns the #run command Result output' do
expect(repository).to receive(:run).with(command, run_params.merge(max_attempts: 1)).and_return(result)
expect(call_method).to eq(command_return)
end
end
context 'when command is not successful' do
it 'raises a CommandError exception' do
expect(Open3).to receive(:capture2e).and_return([+'FAILURE', double(exitstatus: 42)]).once
expect do
call_method
end.to raise_error(QA::Support::Run::CommandError,
/The command .* failed \(42\) with the following output:\nFAILURE/)
end
end
end
context 'with default credentials' do
include_context 'unresolvable git directory' do
before do
repository.use_default_credentials
end
end
describe '#clone' do
let(:opts) { '' }
let(:call_method) { repository.clone }
let(:command) { "git clone #{opts} #{repo_uri_with_credentials} ./" }
context 'when no opts is given' do
it_behaves_like 'command with retries'
end
context 'when opts is given' do
let(:opts) { '--depth 1' }
it_behaves_like 'command with retries' do
let(:call_method) { repository.clone(opts) }
end
end
end
describe '#shallow_clone' do
it_behaves_like 'command with retries' do
let(:call_method) { repository.shallow_clone }
let(:command) { "git clone --depth 1 #{repo_uri_with_credentials} ./" }
end
end
describe '#delete_tag' do
it_behaves_like 'command with retries' do
let(:tag_name) { 'v1.0' }
let(:call_method) { repository.delete_tag(tag_name) }
let(:command) { "git push origin --delete #{tag_name}" }
end
end
describe '#push_changes' do
let(:branch) { QA::Runtime::Env.default_branch }
let(:call_method) { repository.push_changes }
let(:command) { "git push #{repo_uri_with_credentials} #{branch}" }
context 'when no branch is given' do
it_behaves_like 'command with retries'
end
context 'when branch is given' do
let(:branch) { 'my-branch' }
it_behaves_like 'command with retries' do
let(:call_method) { repository.push_changes(branch) }
end
end
context 'when max_attempts is exactly 1' do
it_behaves_like 'command with no retries' do
let(:call_method) { repository.push_changes(max_attempts: 1) }
end
end
context 'with push options' do
let(:command) { "git push #{push_options} #{repo_uri_with_credentials} #{branch}" }
context 'when set to create a merge request' do
it_behaves_like 'command with retries' do
let(:push_options) { '-o merge_request.create' }
let(:call_method) { repository.push_changes(push_options: { create: true }) }
end
end
context 'when set to merge when pipeline succeeds' do
it_behaves_like 'command with retries' do
let(:push_options) { '-o merge_request.merge_when_pipeline_succeeds' }
let(:call_method) { repository.push_changes(push_options: { merge_when_pipeline_succeeds: true }) }
end
end
context 'when set to remove source branch' do
it_behaves_like 'command with retries' do
let(:push_options) { '-o merge_request.remove_source_branch' }
let(:call_method) { repository.push_changes(push_options: { remove_source_branch: true }) }
end
end
context 'when title is given' do
it_behaves_like 'command with retries' do
let(:push_options) { '-o merge_request.title="Is A Title"' }
let(:call_method) { repository.push_changes(push_options: { title: 'Is A Title' }) }
end
end
context 'when description is given' do
it_behaves_like 'command with retries' do
let(:push_options) { '-o merge_request.description="Is A Description"' }
let(:call_method) { repository.push_changes(push_options: { description: 'Is A Description' }) }
end
end
context 'when target branch is given' do
it_behaves_like 'command with retries' do
let(:push_options) { '-o merge_request.target="is-a-target-branch"' }
let(:call_method) { repository.push_changes(push_options: { target: 'is-a-target-branch' }) }
end
end
context 'when a label is given' do
it_behaves_like 'command with retries' do
let(:push_options) { '-o merge_request.label="is-a-label"' }
let(:call_method) { repository.push_changes(push_options: { label: ['is-a-label'] }) }
end
end
context 'when two labels are given' do
it_behaves_like 'command with retries' do
let(:push_options) { '-o merge_request.label="is-a-label" -o merge_request.label="is-another-label"' }
let(:call_method) { repository.push_changes(push_options: { label: %w[is-a-label is-another-label] }) }
end
end
end
end
describe '#git_protocol=' do
[0, 1, 2].each do |version|
it "configures git to use protocol version #{version}" do
expect(repository).to receive(:run).with("git config protocol.version #{version}",
run_params.merge(max_attempts: 1))
repository.git_protocol = version
end
end
it 'raises an error if the version is unsupported' do
expect do
repository.git_protocol = 'foo'
end.to raise_error(ArgumentError,
"Please specify the protocol you would like to use: 0, 1, or 2")
end
end
describe '#fetch_supported_git_protocol' do
let(:call_method) { repository.fetch_supported_git_protocol }
it_behaves_like 'command with retries' do
let(:command) { "git ls-remote #{repo_uri_with_credentials}" }
let(:result_output) { +'packet: ls-remote< version 2' }
let(:command_return) { '2' }
let(:extra_env_vars) { ["GIT_TRACE_PACKET=1"] }
end
it "reports the detected version" do
expect(repository).to receive(:run).and_return(described_class::Result.new(any_args, 0,
"packet: ls-remote< version 2"))
expect(call_method).to eq('2')
end
it 'reports unknown if version is unknown' do
expect(repository).to receive(:run).and_return(described_class::Result.new(any_args, 0,
"packet: ls-remote< version -1"))
expect(call_method).to eq('unknown')
end
it 'reports unknown if content does not identify a version' do
expect(repository).to receive(:run).and_return(described_class::Result.new(any_args, 0, "foo"))
expect(call_method).to eq('unknown')
end
end
describe '#use_default_credentials' do
it 'adds credentials to .netrc' do
expect(File.read(File.join(tmp_netrc_dir, '.netrc')))
.to eq("machine foo login #{default_user.username} password #{default_user.password}\n")
end
end
end
context 'with specific credentials' do
include_context 'unresolvable git directory'
context 'before setting credentials' do
it 'does not add credentials to .netrc' do
expect(repository).not_to receive(:save_netrc_content)
end
end
describe '#password=' do
it 'raises an error if no username was given' do
expect { repository.password = 'foo' }
.to raise_error(QA::Git::Repository::InvalidCredentialsError,
"Please provide a username when setting a password")
end
it 'adds credentials to .netrc' do
repository.username = 'user'
repository.password = 'foo'
expect(File.read(File.join(tmp_netrc_dir, '.netrc')))
.to eq("machine foo login user password foo\n")
end
it 'adds credentials with special characters' do
password = %q[!"#$%&')(*+,-./:;<=>?]
repository.username = 'user'
repository.password = password
expect(File.read(File.join(tmp_netrc_dir, '.netrc')))
.to eq("machine foo login user password #{password}\n")
end
end
end
end
|