bosh-workspace 0.9.2 → 0.9.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bosh-workspace.gemspec +5 -4
- data/lib/bosh/cli/commands/deployment_patch.rb +0 -1
- data/lib/bosh/cli/commands/prepare.rb +12 -11
- data/lib/bosh/workspace.rb +4 -3
- data/lib/bosh/workspace/credentials.rb +12 -5
- data/lib/bosh/workspace/git_credentials_provider.rb +80 -0
- data/lib/bosh/workspace/{git_remote_url.rb → helpers/git_protocol_helper.rb} +4 -8
- data/lib/bosh/workspace/helpers/project_deployment_helper.rb +10 -2
- data/lib/bosh/workspace/helpers/release_helper.rb +33 -7
- data/lib/bosh/workspace/manifest_builder.rb +6 -8
- data/lib/bosh/workspace/merge_tool.rb +73 -0
- data/lib/bosh/workspace/project_deployment.rb +20 -5
- data/lib/bosh/workspace/release.rb +103 -67
- data/lib/bosh/workspace/schemas/credentials.rb +22 -0
- data/lib/bosh/workspace/schemas/project_deployment.rb +14 -1
- data/lib/bosh/workspace/version.rb +1 -1
- data/spec/assets/bin/spruce +0 -0
- data/spec/assets/manifests-repo/deployments/foo.yml +1 -0
- data/spec/assets/manifests-repo/stubs/foo.yml +4 -0
- data/spec/commands/prepare_spec.rb +39 -12
- data/spec/credentials_spec.rb +8 -0
- data/spec/git_credentials_provider_spec.rb +82 -0
- data/spec/{git_remote_url_spec.rb → helpers/git_protocol_helper_spec.rb} +10 -11
- data/spec/helpers/project_deployment_helper_spec.rb +12 -1
- data/spec/helpers/release_helper_spec.rb +157 -73
- data/spec/manifest_builder_spec.rb +6 -5
- data/spec/merge_tool_spec.rb +98 -0
- data/spec/project_deployment_spec.rb +43 -1
- data/spec/release_spec.rb +369 -354
- data/spec/rugged_spec.rb +64 -0
- data/spec/schemas/credentials_spec.rb +22 -5
- data/spec/spec_helper.rb +1 -0
- metadata +35 -15
- data/lib/bosh/workspace/helpers/git_credentials_helper.rb +0 -111
- data/lib/bosh/workspace/helpers/spiff_helper.rb +0 -34
- data/spec/helpers/git_credentials_helper_spec.rb +0 -190
- data/spec/helpers/spiff_helper_spec.rb +0 -68
data/spec/rugged_spec.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# Excluded by default can be executed by running:
|
2
|
+
# rspec spec/rugged_spec.rb --tag rugged
|
3
|
+
|
4
|
+
describe "Rugged::Credentials allowed_types", rugged: true do
|
5
|
+
let(:repo) { Rugged::Repository.new(project_root)}
|
6
|
+
let(:remote) { repo.remotes.create_anonymous(url) }
|
7
|
+
let(:auth_callback) { double }
|
8
|
+
|
9
|
+
subject { remote.ls(credentials: auth_callback).first }
|
10
|
+
|
11
|
+
context 'git protocol' do
|
12
|
+
let(:url) { "git://github.com/example/foo.git" }
|
13
|
+
it "does not support authentication" do
|
14
|
+
allow(auth_callback).to receive(:call)
|
15
|
+
expect{ subject }.to raise_error /repository not found/i
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context "with allowed_types" do
|
20
|
+
let(:user) { nil }
|
21
|
+
before do
|
22
|
+
expect(auth_callback).to receive(:call).with(url, user, allowed_types)
|
23
|
+
.and_return(Rugged::Credentials::Default.new)
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'https protocol' do
|
27
|
+
let(:url) { "https://github.com/example/foo.git" }
|
28
|
+
let(:allowed_types) { [:plaintext] }
|
29
|
+
|
30
|
+
it "allows plaintext" do
|
31
|
+
expect{ subject }.to raise_error /invalid credential type/i
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'http protocol' do
|
36
|
+
let(:url) { "http://github.com/example/foo.git" }
|
37
|
+
let(:allowed_types) { [:plaintext] }
|
38
|
+
|
39
|
+
it "allows plaintext" do
|
40
|
+
expect{ subject }.to raise_error /invalid credential type/i
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'ssh protocol style 1' do
|
45
|
+
let(:url) { "git@github.com:example/foo.git" }
|
46
|
+
let(:user) { "git" }
|
47
|
+
let(:allowed_types) { [:ssh_key] }
|
48
|
+
|
49
|
+
it "allows ssh_key" do
|
50
|
+
expect{ subject }.to raise_error /invalid credential type/i
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context 'ssh protocol style 2' do
|
55
|
+
let(:url) { "ssh://git@github.com/example/foo.git" }
|
56
|
+
let(:user) { "git" }
|
57
|
+
let(:allowed_types) { [:ssh_key] }
|
58
|
+
|
59
|
+
it "allows ssh_key" do
|
60
|
+
expect{ subject }.to raise_error /invalid credential type/i
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -1,28 +1,45 @@
|
|
1
1
|
module Bosh::Workspace::Schemas
|
2
2
|
describe Credentials do
|
3
|
-
let(:
|
4
|
-
|
3
|
+
let(:user_pass_url) { "http://foo" }
|
4
|
+
let(:user_pass) do
|
5
|
+
{ "url" => user_pass_url, "username" => "bar", "password" => "baz" }
|
5
6
|
end
|
7
|
+
let(:ssh_url) { "ssh://bar" }
|
6
8
|
let(:ssh_key) do
|
7
|
-
{ "url" =>
|
9
|
+
{ "url" => ssh_url, "private_key" => "foobarkey" }
|
8
10
|
end
|
11
|
+
let(:credentials) { [user_pass, ssh_key] }
|
9
12
|
|
10
13
|
subject { Credentials.new.validate(credentials) }
|
11
14
|
|
12
15
|
context "valid credentials" do
|
13
|
-
let(:credentials) { [username_password, ssh_key] }
|
14
16
|
it { expect { subject }.to_not raise_error }
|
15
17
|
end
|
16
18
|
|
17
19
|
%w(url username password private_key).each do |field_name|
|
18
20
|
context "missing #{field_name}" do
|
19
21
|
let(:credentials) do
|
20
|
-
[
|
22
|
+
[user_pass, ssh_key].map do |c|
|
21
23
|
c.delete_if { |k| k == field_name }
|
22
24
|
end
|
23
25
|
end
|
24
26
|
it { expect { subject }.to raise_error(/doesn't validate/i) }
|
25
27
|
end
|
26
28
|
end
|
29
|
+
|
30
|
+
context "unsupported protocol" do
|
31
|
+
let(:ssh_url) { 'git://foo' }
|
32
|
+
it { expect { subject }.to raise_error /not supported/ }
|
33
|
+
end
|
34
|
+
|
35
|
+
context "http protocol credentials mismatch" do
|
36
|
+
let(:ssh_url) { 'http://foo' }
|
37
|
+
it { expect { subject }.to raise_error /username\/password/ }
|
38
|
+
end
|
39
|
+
|
40
|
+
context "ssh protocol credentials mismatch" do
|
41
|
+
let(:user_pass_url) { 'ssh://foo' }
|
42
|
+
it { expect { subject }.to raise_error /private_key/ }
|
43
|
+
end
|
27
44
|
end
|
28
45
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -23,6 +23,7 @@ require "bosh/workspace/tasks"
|
|
23
23
|
|
24
24
|
RSpec.configure do |config|
|
25
25
|
config.filter_run focus: true
|
26
|
+
config.filter_run_excluding rugged: true
|
26
27
|
config.run_all_when_everything_filtered = true
|
27
28
|
config.raise_errors_for_deprecations!
|
28
29
|
config.expect_with :rspec do |c|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bosh-workspace
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ruben Koster
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-10-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bosh_cli
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: 1.2905.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bosh-template
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 1.2905.0
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.2905.0
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: semi_semantic
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -114,28 +128,28 @@ dependencies:
|
|
114
128
|
requirements:
|
115
129
|
- - "~>"
|
116
130
|
- !ruby/object:Gem::Version
|
117
|
-
version: 3.
|
131
|
+
version: 3.3.0
|
118
132
|
type: :development
|
119
133
|
prerelease: false
|
120
134
|
version_requirements: !ruby/object:Gem::Requirement
|
121
135
|
requirements:
|
122
136
|
- - "~>"
|
123
137
|
- !ruby/object:Gem::Version
|
124
|
-
version: 3.
|
138
|
+
version: 3.3.0
|
125
139
|
- !ruby/object:Gem::Dependency
|
126
140
|
name: rspec-its
|
127
141
|
requirement: !ruby/object:Gem::Requirement
|
128
142
|
requirements:
|
129
143
|
- - "~>"
|
130
144
|
- !ruby/object:Gem::Version
|
131
|
-
version: 1.
|
145
|
+
version: 1.2.0
|
132
146
|
type: :development
|
133
147
|
prerelease: false
|
134
148
|
version_requirements: !ruby/object:Gem::Requirement
|
135
149
|
requirements:
|
136
150
|
- - "~>"
|
137
151
|
- !ruby/object:Gem::Version
|
138
|
-
version: 1.
|
152
|
+
version: 1.2.0
|
139
153
|
- !ruby/object:Gem::Dependency
|
140
154
|
name: rake
|
141
155
|
requirement: !ruby/object:Gem::Requirement
|
@@ -188,14 +202,14 @@ files:
|
|
188
202
|
- lib/bosh/workspace.rb
|
189
203
|
- lib/bosh/workspace/credentials.rb
|
190
204
|
- lib/bosh/workspace/deployment_patch.rb
|
191
|
-
- lib/bosh/workspace/
|
205
|
+
- lib/bosh/workspace/git_credentials_provider.rb
|
192
206
|
- lib/bosh/workspace/helpers/dns_helper.rb
|
193
|
-
- lib/bosh/workspace/helpers/
|
207
|
+
- lib/bosh/workspace/helpers/git_protocol_helper.rb
|
194
208
|
- lib/bosh/workspace/helpers/project_deployment_helper.rb
|
195
209
|
- lib/bosh/workspace/helpers/release_helper.rb
|
196
|
-
- lib/bosh/workspace/helpers/spiff_helper.rb
|
197
210
|
- lib/bosh/workspace/helpers/stemcell_helper.rb
|
198
211
|
- lib/bosh/workspace/manifest_builder.rb
|
212
|
+
- lib/bosh/workspace/merge_tool.rb
|
199
213
|
- lib/bosh/workspace/project_deployment.rb
|
200
214
|
- lib/bosh/workspace/release.rb
|
201
215
|
- lib/bosh/workspace/schemas/credentials.rb
|
@@ -212,6 +226,7 @@ files:
|
|
212
226
|
- lib/bosh/workspace/tasks/workspace.rake
|
213
227
|
- lib/bosh/workspace/version.rb
|
214
228
|
- spec/assets/bin/spiff
|
229
|
+
- spec/assets/bin/spruce
|
215
230
|
- spec/assets/dns/job-properties.yml
|
216
231
|
- spec/assets/dns/jobs.yml
|
217
232
|
- spec/assets/dns/networks-manual.yml
|
@@ -227,6 +242,7 @@ files:
|
|
227
242
|
- spec/assets/manifests-repo/.manifests/foo.yml
|
228
243
|
- spec/assets/manifests-repo/deployments/bar.yml
|
229
244
|
- spec/assets/manifests-repo/deployments/foo.yml
|
245
|
+
- spec/assets/manifests-repo/stubs/foo.yml
|
230
246
|
- spec/assets/manifests-repo/templates/foo/bar.yml
|
231
247
|
- spec/assets/submodule-boshrelease-repo.zip
|
232
248
|
- spec/assets/supermodule-boshrelease-repo.zip
|
@@ -236,16 +252,17 @@ files:
|
|
236
252
|
- spec/commands/project_deployment_spec.rb
|
237
253
|
- spec/credentials_spec.rb
|
238
254
|
- spec/deployment_patch_spec.rb
|
239
|
-
- spec/
|
255
|
+
- spec/git_credentials_provider_spec.rb
|
240
256
|
- spec/helpers/dns_helper_spec.rb
|
241
|
-
- spec/helpers/
|
257
|
+
- spec/helpers/git_protocol_helper_spec.rb
|
242
258
|
- spec/helpers/project_deployment_helper_spec.rb
|
243
259
|
- spec/helpers/release_helper_spec.rb
|
244
|
-
- spec/helpers/spiff_helper_spec.rb
|
245
260
|
- spec/helpers/stemcell_helper_spec.rb
|
246
261
|
- spec/manifest_builder_spec.rb
|
262
|
+
- spec/merge_tool_spec.rb
|
247
263
|
- spec/project_deployment_spec.rb
|
248
264
|
- spec/release_spec.rb
|
265
|
+
- spec/rugged_spec.rb
|
249
266
|
- spec/schemas/credentials_spec.rb
|
250
267
|
- spec/schemas/deployment_patch_spec.rb
|
251
268
|
- spec/schemas/project_deployment_spec.rb
|
@@ -285,6 +302,7 @@ specification_version: 4
|
|
285
302
|
summary: Manage your bosh workspace
|
286
303
|
test_files:
|
287
304
|
- spec/assets/bin/spiff
|
305
|
+
- spec/assets/bin/spruce
|
288
306
|
- spec/assets/dns/job-properties.yml
|
289
307
|
- spec/assets/dns/jobs.yml
|
290
308
|
- spec/assets/dns/networks-manual.yml
|
@@ -300,6 +318,7 @@ test_files:
|
|
300
318
|
- spec/assets/manifests-repo/.manifests/foo.yml
|
301
319
|
- spec/assets/manifests-repo/deployments/bar.yml
|
302
320
|
- spec/assets/manifests-repo/deployments/foo.yml
|
321
|
+
- spec/assets/manifests-repo/stubs/foo.yml
|
303
322
|
- spec/assets/manifests-repo/templates/foo/bar.yml
|
304
323
|
- spec/assets/submodule-boshrelease-repo.zip
|
305
324
|
- spec/assets/supermodule-boshrelease-repo.zip
|
@@ -309,16 +328,17 @@ test_files:
|
|
309
328
|
- spec/commands/project_deployment_spec.rb
|
310
329
|
- spec/credentials_spec.rb
|
311
330
|
- spec/deployment_patch_spec.rb
|
312
|
-
- spec/
|
331
|
+
- spec/git_credentials_provider_spec.rb
|
313
332
|
- spec/helpers/dns_helper_spec.rb
|
314
|
-
- spec/helpers/
|
333
|
+
- spec/helpers/git_protocol_helper_spec.rb
|
315
334
|
- spec/helpers/project_deployment_helper_spec.rb
|
316
335
|
- spec/helpers/release_helper_spec.rb
|
317
|
-
- spec/helpers/spiff_helper_spec.rb
|
318
336
|
- spec/helpers/stemcell_helper_spec.rb
|
319
337
|
- spec/manifest_builder_spec.rb
|
338
|
+
- spec/merge_tool_spec.rb
|
320
339
|
- spec/project_deployment_spec.rb
|
321
340
|
- spec/release_spec.rb
|
341
|
+
- spec/rugged_spec.rb
|
322
342
|
- spec/schemas/credentials_spec.rb
|
323
343
|
- spec/schemas/deployment_patch_spec.rb
|
324
344
|
- spec/schemas/project_deployment_spec.rb
|
@@ -1,111 +0,0 @@
|
|
1
|
-
module Bosh::Workspace
|
2
|
-
module GitCredentialsHelper
|
3
|
-
REFSPEC = ['HEAD:refs/remotes/origin/HEAD']
|
4
|
-
|
5
|
-
def fetch_or_clone_repo(dir, url)
|
6
|
-
repo = File.exist?(File.join(dir, '.git')) ? open_repo(dir) : init_repo(dir, url)
|
7
|
-
fetch_and_checkout(repo)
|
8
|
-
end
|
9
|
-
|
10
|
-
def fetch_repo(dir)
|
11
|
-
fetch_and_checkout(open_repo(dir))
|
12
|
-
end
|
13
|
-
|
14
|
-
private
|
15
|
-
|
16
|
-
def fetch_and_checkout(repo)
|
17
|
-
url = repo.remotes['origin'].url
|
18
|
-
repo.fetch('origin', REFSPEC, connection_options_for(repo, url))
|
19
|
-
commit = repo.references['refs/remotes/origin/HEAD'].resolve.target_id
|
20
|
-
repo.checkout_tree commit, strategy: :force
|
21
|
-
repo.checkout commit, strategy: :force
|
22
|
-
end
|
23
|
-
|
24
|
-
def connection_options_for(repo, url)
|
25
|
-
return {} if check_connection(repo, url)
|
26
|
-
validate_url_protocol_support!(url)
|
27
|
-
|
28
|
-
options = { credentials: require_credentials_for(url) }
|
29
|
-
unless check_connection(repo, url, options)
|
30
|
-
say "Using credentials from: #{git_credentials_file}"
|
31
|
-
err "Invalid credentials for: #{url}"
|
32
|
-
end
|
33
|
-
options
|
34
|
-
end
|
35
|
-
|
36
|
-
def check_connection(repo, url, options = {})
|
37
|
-
repo.remotes.create_anonymous(url).check_connection(:fetch, options)
|
38
|
-
end
|
39
|
-
|
40
|
-
def init_repo(dir, url)
|
41
|
-
FileUtils.mkdir_p File.dirname(dir)
|
42
|
-
Rugged::Repository.init_at(dir).tap do |repo|
|
43
|
-
repo.remotes.create('origin', url)
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
def open_repo(dir)
|
48
|
-
Rugged::Repository.new(dir)
|
49
|
-
end
|
50
|
-
|
51
|
-
def git_credentials
|
52
|
-
@git_credentials ||= Credentials.new(git_credentials_file)
|
53
|
-
end
|
54
|
-
|
55
|
-
def require_credentials_for(url)
|
56
|
-
unless File.exist? git_credentials_file
|
57
|
-
say("Authentication is required for: #{url}".make_red)
|
58
|
-
err("Credentials file does not exist: #{git_credentials_file}".make_red)
|
59
|
-
end
|
60
|
-
unless git_credentials.valid?
|
61
|
-
say("Validation errors:".make_red)
|
62
|
-
git_credentials.errors.each { |error| say("- #{error}") }
|
63
|
-
err("'#{git_credentials_file}' is not valid".make_red)
|
64
|
-
end
|
65
|
-
if creds = git_credentials.find_by_url(url)
|
66
|
-
load_git_credentials(creds)
|
67
|
-
else
|
68
|
-
say("Credential look up failed in: #{git_credentials_file}")
|
69
|
-
err("No credentials found for: #{url}".make_red)
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
def validate_url_protocol_support!(url)
|
74
|
-
protocol = GitRemoteUrl.new(url).protocol
|
75
|
-
case protocol
|
76
|
-
when :git
|
77
|
-
err("Somthing is wrong, the git protocol does not support authentication")
|
78
|
-
when :https, :ssh
|
79
|
-
unless Rugged.features.include? protocol
|
80
|
-
say("Please reinstall Rugged gem with #{protocol} support: http://git.io/veiyJ")
|
81
|
-
err("Rugged requires #{protocol} support for: #{url}")
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
def git_credentials_file
|
87
|
-
File.join work_dir, '.credentials.yml'
|
88
|
-
end
|
89
|
-
|
90
|
-
def load_git_credentials(git_credentials)
|
91
|
-
case git_credentials.keys
|
92
|
-
when %i(private_key)
|
93
|
-
options = {
|
94
|
-
username: 'git',
|
95
|
-
privatekey: temp_key_file(git_credentials[:private_key])
|
96
|
-
}
|
97
|
-
Rugged::Credentials::SshKey.new(options)
|
98
|
-
when %i(username password)
|
99
|
-
Rugged::Credentials::UserPassword.new(git_credentials)
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
def temp_key_file(key)
|
104
|
-
file = Tempfile.new('sshkey')
|
105
|
-
file.write key
|
106
|
-
file.close
|
107
|
-
File.chmod(0600, file.path)
|
108
|
-
file.path
|
109
|
-
end
|
110
|
-
end
|
111
|
-
end
|
@@ -1,34 +0,0 @@
|
|
1
|
-
module Bosh::Workspace
|
2
|
-
module SpiffHelper
|
3
|
-
include Bosh::Exec
|
4
|
-
|
5
|
-
def spiff_merge(templates, target_file)
|
6
|
-
spiff(:merge, templates) do |output|
|
7
|
-
File.open(target_file, 'w') { |file| file.write(output) }
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
private
|
12
|
-
|
13
|
-
def spiff(verb, params)
|
14
|
-
params.map!(&:shellescape)
|
15
|
-
cmd = ["spiff", verb.to_s] + params + ["2>&1"]
|
16
|
-
sh(cmd.join(" "), :yield => :on_false) do |result|
|
17
|
-
spiff_not_found if result.not_found?
|
18
|
-
spiff_failed(result.command, result.output) if result.failed?
|
19
|
-
yield result.output
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
def spiff_not_found
|
24
|
-
say("Can't find spiff in $PATH".make_red)
|
25
|
-
say("Go to spiff.cfapps.io for installation instructions")
|
26
|
-
err("Please make sure spiff is installed")
|
27
|
-
end
|
28
|
-
|
29
|
-
def spiff_failed(command, output)
|
30
|
-
say("Command failed: '#{command}'")
|
31
|
-
err(output)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
@@ -1,190 +0,0 @@
|
|
1
|
-
module Bosh::Workspace
|
2
|
-
describe GitCredentialsHelper do
|
3
|
-
class GitCredentialsHelperTester
|
4
|
-
include Bosh::Workspace::GitCredentialsHelper
|
5
|
-
|
6
|
-
attr_reader :work_dir
|
7
|
-
|
8
|
-
def initialize(work_dir)
|
9
|
-
@work_dir = work_dir
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
let(:work_dir) { asset_dir("manifests-repo") }
|
14
|
-
let(:url) { "foo/bar.git" }
|
15
|
-
let(:dir) { File.join(work_dir, '.releases', 'foo') }
|
16
|
-
let(:repo) { instance_double 'Rugged::Repository' }
|
17
|
-
let(:remote) { instance_double 'Rugged::Remote', url: url }
|
18
|
-
let(:protocol) { :http }
|
19
|
-
let(:git_url) { instance_double 'GitRemoteURL', protocol: protocol }
|
20
|
-
|
21
|
-
before do
|
22
|
-
allow(Rugged::Repository).to receive(:new).with(dir).and_return(repo)
|
23
|
-
allow(File).to receive(:exist?).with(File.join(dir, '.git')).and_return(dir_exist)
|
24
|
-
allow(repo).to receive_message_chain("remotes.[]").and_return(remote)
|
25
|
-
allow(repo).to receive_message_chain("remotes.create_anonymous")
|
26
|
-
.with(url).and_return(remote)
|
27
|
-
allow(repo).to receive_message_chain("references.[].resolve.target_id")
|
28
|
-
.and_return(:commit_id)
|
29
|
-
allow(remote).to receive(:check_connection).with(:fetch, Hash)
|
30
|
-
.and_return(!auth_required, credentials_auth_valid)
|
31
|
-
allow(repo).to receive(:checkout_tree).with(:commit_id, strategy: :force)
|
32
|
-
allow(repo).to receive(:checkout).with(:commit_id, strategy: :force)
|
33
|
-
allow(GitRemoteUrl).to receive(:new).and_return(git_url)
|
34
|
-
end
|
35
|
-
|
36
|
-
def expect_no_credentials
|
37
|
-
expect(repo).to receive(:fetch).with('origin', Array, {})
|
38
|
-
end
|
39
|
-
|
40
|
-
let(:dir_exist) { true }
|
41
|
-
let(:auth_required) { false }
|
42
|
-
let(:credentials_auth_valid) { true }
|
43
|
-
|
44
|
-
describe "fetch_repo" do
|
45
|
-
subject do
|
46
|
-
GitCredentialsHelperTester.new(work_dir).fetch_repo(dir)
|
47
|
-
end
|
48
|
-
|
49
|
-
context "with existing repo" do
|
50
|
-
it do
|
51
|
-
expect_no_credentials
|
52
|
-
expect(repo).to receive("checkout_tree")
|
53
|
-
expect(repo).to receive("checkout")
|
54
|
-
subject
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
describe "fetch_or_clone_repo" do
|
60
|
-
subject do
|
61
|
-
GitCredentialsHelperTester.new(work_dir).fetch_or_clone_repo(dir, url)
|
62
|
-
end
|
63
|
-
|
64
|
-
context "with existing repo" do
|
65
|
-
it do
|
66
|
-
expect_no_credentials
|
67
|
-
expect(repo).to receive("checkout_tree")
|
68
|
-
expect(repo).to receive("checkout")
|
69
|
-
subject
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
context "repo does not yet exist" do
|
74
|
-
let(:dir_exist) { false }
|
75
|
-
|
76
|
-
it "initializes a new repository and sets up a remote" do
|
77
|
-
expect(Rugged::Repository).to receive(:init_at).with(dir)
|
78
|
-
.and_return(repo)
|
79
|
-
expect(repo).to receive_message_chain("remotes.create")
|
80
|
-
.with('origin', url)
|
81
|
-
expect_no_credentials
|
82
|
-
subject
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
context "repo requires authentication" do
|
87
|
-
let(:auth_required) { true }
|
88
|
-
let(:credentials_file_exist) { true }
|
89
|
-
let(:credentials_valid) { true }
|
90
|
-
let(:credentials) { instance_double "Bosh::Workspace::Credentials" }
|
91
|
-
let(:creds_hash) { {} }
|
92
|
-
|
93
|
-
def expect_credentials(credentials)
|
94
|
-
expect(repo).to receive(:fetch)
|
95
|
-
.with('origin', Array, credentials: credentials)
|
96
|
-
end
|
97
|
-
|
98
|
-
before do
|
99
|
-
allow(File).to receive(:exist?).with(/.credentials.yml/)
|
100
|
-
.and_return(credentials_file_exist)
|
101
|
-
allow(Bosh::Workspace::Credentials).to receive(:new)
|
102
|
-
.with(/.credentials.yml/).and_return(credentials)
|
103
|
-
allow(credentials).to receive(:valid?).and_return(credentials_valid)
|
104
|
-
allow(credentials).to receive(:find_by_url)
|
105
|
-
.with(url).and_return(creds_hash)
|
106
|
-
end
|
107
|
-
|
108
|
-
context "without supported protocol" do
|
109
|
-
before do
|
110
|
-
expect(Rugged).to receive(:features).and_return([])
|
111
|
-
end
|
112
|
-
|
113
|
-
let(:protocol) { :https }
|
114
|
-
|
115
|
-
it "raises" do
|
116
|
-
expect { subject }.to raise_error /rugged requires https/i
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
context "with git protocol" do
|
121
|
-
let(:protocol) { :git }
|
122
|
-
|
123
|
-
it "raises" do
|
124
|
-
expect { subject }.to raise_error /not support authentication/i
|
125
|
-
end
|
126
|
-
end
|
127
|
-
|
128
|
-
context "with sshkey" do
|
129
|
-
let(:creds_hash) { { private_key: "foobarkey" } }
|
130
|
-
|
131
|
-
it "uses a key" do
|
132
|
-
expect(Rugged::Credentials::SshKey).to receive(:new) do |args|
|
133
|
-
expect(IO.read(args[:privatekey])).to eq "foobarkey"
|
134
|
-
:ssh_credentials
|
135
|
-
end
|
136
|
-
expect_credentials :ssh_credentials
|
137
|
-
subject
|
138
|
-
end
|
139
|
-
end
|
140
|
-
|
141
|
-
context "with username/password" do
|
142
|
-
let(:creds_hash) { { username: "foo", password: "bar" } }
|
143
|
-
|
144
|
-
it "uses a username/password" do
|
145
|
-
expect(Rugged::Credentials::UserPassword).to receive(:new) do |args|
|
146
|
-
expect(args[:username]).to eq "foo"
|
147
|
-
expect(args[:password]).to eq "bar"
|
148
|
-
:user_pw_credentials
|
149
|
-
end
|
150
|
-
expect_credentials :user_pw_credentials
|
151
|
-
subject
|
152
|
-
end
|
153
|
-
end
|
154
|
-
|
155
|
-
context "with invalid credentials" do
|
156
|
-
let(:credentials_auth_valid) { false }
|
157
|
-
|
158
|
-
it "raises and error" do
|
159
|
-
expect { subject }.to raise_error /invalid credentials/i
|
160
|
-
end
|
161
|
-
end
|
162
|
-
|
163
|
-
context "without credentials file" do
|
164
|
-
let(:credentials_file_exist) { false }
|
165
|
-
|
166
|
-
it "raises and error" do
|
167
|
-
expect { subject }.to raise_error /credentials file does not exist/i
|
168
|
-
end
|
169
|
-
end
|
170
|
-
|
171
|
-
context "with invalid credentials file" do
|
172
|
-
let(:credentials_valid) { false }
|
173
|
-
|
174
|
-
it "raises and error" do
|
175
|
-
expect(credentials).to receive(:errors) { ["fooerror"] }
|
176
|
-
expect { subject }.to raise_error /not valid/
|
177
|
-
end
|
178
|
-
end
|
179
|
-
|
180
|
-
context "without credentials for given url" do
|
181
|
-
let(:creds_hash) { nil }
|
182
|
-
|
183
|
-
it "raises and error" do
|
184
|
-
expect { subject }.to raise_error /no credentials found/i
|
185
|
-
end
|
186
|
-
end
|
187
|
-
end
|
188
|
-
end
|
189
|
-
end
|
190
|
-
end
|