bosh_warden_cpi 1.5.0.pre.3 → 1.2513.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +3 -11
- data/lib/cloud/warden/cloud.rb +116 -323
- data/lib/cloud/warden/diskutils.rb +107 -0
- data/lib/cloud/warden/helpers.rb +79 -32
- data/lib/cloud/warden/version.rb +2 -4
- data/lib/cloud/warden.rb +14 -32
- data/spec/assets/stemcell-warden-test.tgz +0 -0
- data/spec/spec_helper.rb +20 -0
- data/spec/unit/cloud_spec.rb +264 -0
- data/spec/unit/diskutils_spec.rb +146 -0
- data/spec/unit/helper_spec.rb +106 -0
- metadata +59 -16
- data/db/migrations/20130312211408_initial.rb +0 -22
- data/lib/cloud/warden/device_pool.rb +0 -36
- data/lib/cloud/warden/models/disk.rb +0 -5
- data/lib/cloud/warden/models/vm.rb +0 -5
@@ -0,0 +1,146 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Bosh::WardenCloud::DiskUtils do
|
4
|
+
|
5
|
+
let(:image_path) { asset('stemcell-warden-test.tgz') }
|
6
|
+
let(:bad_image_path) { asset('stemcell-not-existed.tgz') }
|
7
|
+
|
8
|
+
before :each do
|
9
|
+
@disk_root = Dir.mktmpdir('warden-cpi-path')
|
10
|
+
@stemcell_path = Dir.mktmpdir('stemcell-path')
|
11
|
+
@stemcell_root = File.join(@stemcell_path, 'stemcell-uuid')
|
12
|
+
@disk_util = described_class.new(@disk_root, @stemcell_path, 'ext4')
|
13
|
+
allow(@disk_util).to receive(:sleep) {}
|
14
|
+
end
|
15
|
+
|
16
|
+
after :each do
|
17
|
+
FileUtils.rm_rf @disk_root
|
18
|
+
FileUtils.rm_rf @stemcell_path
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'create_stemcell' do
|
22
|
+
it 'will use create stemcell' do
|
23
|
+
mock_sh("tar -C #{@stemcell_root} -xzf #{image_path} 2>&1", true)
|
24
|
+
@disk_util.stemcell_unpack(image_path, 'stemcell-uuid')
|
25
|
+
Dir.chdir(@stemcell_path) do
|
26
|
+
expect(Dir.glob('*').size).to eq(1)
|
27
|
+
expect(Dir.glob('*')).to include('stemcell-uuid')
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should raise error with bad image path' do
|
32
|
+
mock_sh("rm -rf #{@stemcell_root}", true)
|
33
|
+
expect {
|
34
|
+
@disk_util.stemcell_unpack(bad_image_path, 'stemcell-uuid')
|
35
|
+
}.to raise_error
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context 'delete_stemcell' do
|
40
|
+
it 'can delete stemcell' do
|
41
|
+
Dir.chdir(@stemcell_path) do
|
42
|
+
mock_sh("tar -C #{@stemcell_root} -xzf #{image_path} 2>&1", true)
|
43
|
+
@disk_util.stemcell_unpack(image_path, 'stemcell-uuid')
|
44
|
+
expect(Dir.glob('*').size).to eq(1)
|
45
|
+
expect(Dir.glob('*')).to include('stemcell-uuid')
|
46
|
+
mock_sh("rm -rf #{@stemcell_root}", true)
|
47
|
+
@disk_util.stemcell_delete('stemcell-uuid')
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'create_disk' do
|
53
|
+
it 'can create disk' do
|
54
|
+
mock_sh("/sbin/mkfs -t ext4 -F #{@disk_root}/disk-uuid.img 2>&1")
|
55
|
+
@disk_util.create_disk('disk-uuid', 1)
|
56
|
+
Dir.chdir(@disk_root) do
|
57
|
+
image = 'disk-uuid.img'
|
58
|
+
expect(Dir.glob('*').size).to eq(1)
|
59
|
+
expect(Dir.glob('*')).to include(image)
|
60
|
+
expect(File.stat(image).size).to eq(1 << 20)
|
61
|
+
end
|
62
|
+
expect(@disk_util.disk_exist?('disk-uuid')).to be true
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'should raise error if size is 0' do
|
66
|
+
expect {
|
67
|
+
@disk_util.create_disk('disk-uuid', 0)
|
68
|
+
}.to raise_error ArgumentError
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'should raise error if size is smaller than 0' do
|
72
|
+
expect {
|
73
|
+
@disk_util.create_disk('disk-uuid', -1)
|
74
|
+
}.to raise_error ArgumentError
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'should clean up when create disk failed' do
|
78
|
+
allow(@disk_util).to receive(:image_path) { '/path/not/exist' }
|
79
|
+
expect {
|
80
|
+
@disk_util.create_disk('disk-uuid', 1)
|
81
|
+
}.to raise_error
|
82
|
+
Dir.chdir(@disk_root) do
|
83
|
+
expect(Dir.glob('*')).to be_empty
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
context 'delete_disk' do
|
89
|
+
before :each do
|
90
|
+
mock_sh("/sbin/mkfs -t ext4 -F #{@disk_root}/disk-uuid.img 2>&1")
|
91
|
+
@disk_util.create_disk('disk-uuid', 1)
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'can delete disk' do
|
95
|
+
Dir.chdir(@disk_root) do
|
96
|
+
expect(Dir.glob('*').size).to eq(1)
|
97
|
+
expect(Dir.glob('*')).to include('disk-uuid.img')
|
98
|
+
@disk_util.delete_disk('disk-uuid')
|
99
|
+
expect(Dir.glob('*')).to be_empty
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
context 'disk exist' do
|
105
|
+
it 'should detect non-existed disk' do
|
106
|
+
expect(@disk_util.disk_exist?('12345')).to be false
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'should return true for existed disk' do
|
110
|
+
@disk_util.create_disk('disk-uuid', 1)
|
111
|
+
expect(@disk_util.disk_exist?('disk-uuid')).to be true
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
context 'mount & umount disk' do
|
116
|
+
before :each do
|
117
|
+
@vm_path = Dir.mktmpdir('warden-cpi-path')
|
118
|
+
@vm_id_path = File.join(@vm_path, 'vm-id')
|
119
|
+
end
|
120
|
+
|
121
|
+
after :each do
|
122
|
+
FileUtils.rm_rf @vm_path
|
123
|
+
end
|
124
|
+
|
125
|
+
it 'will invoke sudo mount to attach loop device' do
|
126
|
+
mock_sh("mount #{@disk_root}/disk-uuid.img #{@vm_id_path} -o loop", true)
|
127
|
+
@disk_util.mount_disk(@vm_id_path, 'disk-uuid')
|
128
|
+
end
|
129
|
+
|
130
|
+
it 'will sudo invoke umount to detach loop device' do
|
131
|
+
mock_sh("umount #{@vm_id_path}", true)
|
132
|
+
allow(@disk_util).to receive(:mount_entry).and_return('nop', nil)
|
133
|
+
@disk_util.umount_disk(@vm_id_path)
|
134
|
+
end
|
135
|
+
|
136
|
+
it 'will retry umount for detach disk' do
|
137
|
+
mock_sh("umount #{@vm_id_path}", true, Bosh::WardenCloud::DiskUtils::UMOUNT_GUARD_RETRIES + 1, false)
|
138
|
+
allow(@disk_util).to receive(:mount_entry).and_return('nop')
|
139
|
+
|
140
|
+
expect {
|
141
|
+
@disk_util.umount_disk(@vm_id_path)
|
142
|
+
}. to raise_error
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Bosh::WardenCloud::Helpers do
|
4
|
+
include Bosh::WardenCloud::Helpers
|
5
|
+
|
6
|
+
before :each do
|
7
|
+
@warden_client = double('Warden::Client')
|
8
|
+
allow(Warden::Client).to receive(:new).and_return(@warden_client)
|
9
|
+
|
10
|
+
allow(@warden_client).to receive(:connect) {}
|
11
|
+
allow(@warden_client).to receive(:disconnect) {}
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'uuid' do
|
15
|
+
it 'can generate the correct uuid' do
|
16
|
+
expect(uuid('disk')).to start_with 'disk'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'sudo/sh' do
|
21
|
+
it 'run sudo cmd with sudo' do
|
22
|
+
mock_sh('fake', true)
|
23
|
+
sudo('fake')
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'run sh cmd with sh' do
|
27
|
+
mock_sh('fake')
|
28
|
+
sh('fake')
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'generate and get agent env' do
|
33
|
+
before :each do
|
34
|
+
allow(@warden_client).to receive(:call) do |req|
|
35
|
+
res = req.create_response
|
36
|
+
case req
|
37
|
+
when Warden::Protocol::RunRequest
|
38
|
+
expect(req.script).to eq("cat #{agent_settings_file}")
|
39
|
+
res.stdout = %{{"vm":{"name":"vm-name","id":"vm-id"},"agent_id":"vm-agent"}}
|
40
|
+
else
|
41
|
+
raise "#{req} not supported"
|
42
|
+
end
|
43
|
+
res
|
44
|
+
end
|
45
|
+
@agent_properties = { 'ntp' => 'test' }
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'generate agent env from agent_properties' do
|
49
|
+
env = generate_agent_env('vm-id', 'agent-id', {}, { 'password' => 'abc' })
|
50
|
+
expect(env['vm']['name']).to eq('vm-id')
|
51
|
+
expect(env['vm']['id']).to eq('vm-id')
|
52
|
+
expect(env['agent_id']).to eq('agent-id')
|
53
|
+
expect(env['ntp']).to eq('test')
|
54
|
+
expect(env['env']['password']).to eq('abc')
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'invoke warden to cat agent_settings_file' do
|
58
|
+
env = get_agent_env('fake_handle')
|
59
|
+
expect(env['vm']['name']).to eq('vm-name')
|
60
|
+
expect(env['vm']['id']).to eq('vm-id')
|
61
|
+
expect(env['agent_id']).to eq('vm-agent')
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context 'set agent env' do
|
66
|
+
before :each do
|
67
|
+
allow(@warden_client).to receive(:call) do |req|
|
68
|
+
res = req.create_response
|
69
|
+
case req
|
70
|
+
when Warden::Protocol::RunRequest
|
71
|
+
expect(req.script).to eq("mv /tmp/100 #{agent_settings_file}")
|
72
|
+
when Warden::Protocol::CopyInRequest
|
73
|
+
expect(req.dst_path).to eq('/tmp/100')
|
74
|
+
else
|
75
|
+
raise "#{req} not supported"
|
76
|
+
end
|
77
|
+
res
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'generate a random file in tmp and mv to agent_setting_file' do
|
82
|
+
allow(Kernel).to receive(:rand).and_return(100)
|
83
|
+
set_agent_env('fake_handle', {})
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
context 'start agent' do
|
88
|
+
before :each do
|
89
|
+
allow(@warden_client).to receive(:call) do |req|
|
90
|
+
res = req.create_response
|
91
|
+
case req
|
92
|
+
when Warden::Protocol::SpawnRequest
|
93
|
+
expect(req.script).to eq('/usr/sbin/runsvdir-start')
|
94
|
+
else
|
95
|
+
raise "#{req} not supported"
|
96
|
+
end
|
97
|
+
res
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'runs runsvdir-start when start agent' do
|
102
|
+
start_agent('fake_handle')
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
metadata
CHANGED
@@ -1,16 +1,32 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bosh_warden_cpi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
5
|
-
prerelease:
|
4
|
+
version: 1.2513.0
|
5
|
+
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
|
-
-
|
8
|
+
- VMware
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2014-04-30 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bosh_common
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
14
30
|
- !ruby/object:Gem::Dependency
|
15
31
|
name: bosh_cpi
|
16
32
|
requirement: !ruby/object:Gem::Requirement
|
@@ -75,23 +91,42 @@ dependencies:
|
|
75
91
|
- - ! '>='
|
76
92
|
- !ruby/object:Gem::Version
|
77
93
|
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: yajl-ruby
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :runtime
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
78
110
|
description: BOSH Warden CPI
|
79
|
-
email: support@cloudfoundry.
|
111
|
+
email: support@cloudfoundry.com
|
80
112
|
executables: []
|
81
113
|
extensions: []
|
82
114
|
extra_rdoc_files: []
|
83
115
|
files:
|
84
|
-
- db/migrations/20130312211408_initial.rb
|
85
116
|
- lib/cloud/warden.rb
|
86
117
|
- lib/cloud/warden/cloud.rb
|
87
|
-
- lib/cloud/warden/
|
118
|
+
- lib/cloud/warden/diskutils.rb
|
88
119
|
- lib/cloud/warden/helpers.rb
|
89
|
-
- lib/cloud/warden/models/disk.rb
|
90
|
-
- lib/cloud/warden/models/vm.rb
|
91
120
|
- lib/cloud/warden/version.rb
|
92
121
|
- README
|
93
|
-
|
94
|
-
|
122
|
+
- spec/assets/stemcell-warden-test.tgz
|
123
|
+
- spec/spec_helper.rb
|
124
|
+
- spec/unit/cloud_spec.rb
|
125
|
+
- spec/unit/diskutils_spec.rb
|
126
|
+
- spec/unit/helper_spec.rb
|
127
|
+
homepage: https://github.com/cloudfoundry/bosh
|
128
|
+
licenses:
|
129
|
+
- Apache 2.0
|
95
130
|
post_install_message:
|
96
131
|
rdoc_options: []
|
97
132
|
require_paths:
|
@@ -101,17 +136,25 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
101
136
|
requirements:
|
102
137
|
- - ! '>='
|
103
138
|
- !ruby/object:Gem::Version
|
104
|
-
version:
|
139
|
+
version: 1.9.3
|
105
140
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
141
|
none: false
|
107
142
|
requirements:
|
108
|
-
- - ! '
|
143
|
+
- - ! '>='
|
109
144
|
- !ruby/object:Gem::Version
|
110
|
-
version:
|
145
|
+
version: '0'
|
146
|
+
segments:
|
147
|
+
- 0
|
148
|
+
hash: -2647859539726025791
|
111
149
|
requirements: []
|
112
150
|
rubyforge_project:
|
113
|
-
rubygems_version: 1.8.
|
151
|
+
rubygems_version: 1.8.23
|
114
152
|
signing_key:
|
115
153
|
specification_version: 3
|
116
154
|
summary: BOSH Warden CPI
|
117
|
-
test_files:
|
155
|
+
test_files:
|
156
|
+
- spec/assets/stemcell-warden-test.tgz
|
157
|
+
- spec/spec_helper.rb
|
158
|
+
- spec/unit/cloud_spec.rb
|
159
|
+
- spec/unit/diskutils_spec.rb
|
160
|
+
- spec/unit/helper_spec.rb
|
@@ -1,22 +0,0 @@
|
|
1
|
-
Sequel.migration do
|
2
|
-
up do
|
3
|
-
create_table(:warden_vm) do
|
4
|
-
primary_key :id
|
5
|
-
String :container_id
|
6
|
-
end
|
7
|
-
|
8
|
-
create_table(:warden_disk) do
|
9
|
-
primary_key :id
|
10
|
-
foreign_key :vm_id, :warden_vm
|
11
|
-
Integer :device_num
|
12
|
-
String :device_path
|
13
|
-
String :image_path
|
14
|
-
Boolean :attached, :default => false
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
down do
|
19
|
-
drop_table :warden_disk
|
20
|
-
drop_table :warden_vm
|
21
|
-
end
|
22
|
-
end
|
@@ -1,36 +0,0 @@
|
|
1
|
-
module Bosh::WardenCloud
|
2
|
-
|
3
|
-
class DevicePool
|
4
|
-
def initialize(count)
|
5
|
-
@mutex = Mutex.new
|
6
|
-
@pool = []
|
7
|
-
|
8
|
-
@pool = count.times.map { |i| block_given? ? yield(i) : i }
|
9
|
-
end
|
10
|
-
|
11
|
-
def size
|
12
|
-
@mutex.synchronize do
|
13
|
-
@pool.size
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
def acquire
|
18
|
-
@mutex.synchronize do
|
19
|
-
@pool.shift
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
def release(entry)
|
24
|
-
@mutex.synchronize do
|
25
|
-
@pool << entry
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
def delete_if(&blk)
|
30
|
-
@mutex.synchronize do
|
31
|
-
@pool.delete_if &blk
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
end
|