chef-provisioning-vsphere 0.10.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'json'
2
3
  require 'kitchen'
3
4
  require 'chef/provisioning/vsphere_driver'
@@ -6,26 +7,25 @@ require 'chef/provisioning/machine_spec'
6
7
  module Kitchen
7
8
  module Driver
8
9
  class Vsphere < Kitchen::Driver::Base
9
-
10
10
  @@chef_zero_server = false
11
11
 
12
12
  default_config :machine_options,
13
- :start_timeout => 600,
14
- :create_timeout => 600,
15
- :stop_timeout => 600,
16
- :ready_timeout => 90,
17
- :bootstrap_options => {
18
- :use_linked_clone => true,
19
- :ssh => {
20
- :user => 'root',
21
- :paranoid => false,
22
- :port => 22
23
- },
24
- :convergence_options => {},
25
- :customization_spec => {
26
- :domain => 'local'
27
- }
28
- }
13
+ start_timeout: 600,
14
+ create_timeout: 600,
15
+ stop_timeout: 600,
16
+ ready_timeout: 90,
17
+ bootstrap_options: {
18
+ use_linked_clone: true,
19
+ ssh: {
20
+ user: 'root',
21
+ paranoid: false,
22
+ port: 22
23
+ },
24
+ convergence_options: {},
25
+ customization_spec: {
26
+ domain: 'local'
27
+ }
28
+ }
29
29
 
30
30
  default_config(:vsphere_name) do |driver|
31
31
  "#{driver.instance.name}-#{SecureRandom.hex(4)}"
@@ -37,7 +37,7 @@ module Kitchen
37
37
  state[:password] = config[:machine_options][:bootstrap_options][:ssh][:password]
38
38
  config[:server_name] = state[:vsphere_name]
39
39
 
40
- machine = with_provisioning_driver(state) do | action_handler, driver, machine_spec|
40
+ machine = with_provisioning_driver(state) do |action_handler, driver, machine_spec|
41
41
  driver.allocate_machine(action_handler, machine_spec, config[:machine_options])
42
42
  driver.ready_machine(action_handler, machine_spec, config[:machine_options])
43
43
  state[:server_id] = machine_spec.location['server_id']
@@ -49,9 +49,9 @@ module Kitchen
49
49
  def destroy(state)
50
50
  return if state[:server_id].nil?
51
51
 
52
- with_provisioning_driver(state) do | action_handler, driver, machine_spec|
52
+ with_provisioning_driver(state) do |action_handler, driver, machine_spec|
53
53
  machine_spec.location = { 'driver_url' => driver.driver_url,
54
- 'server_id' => state[:server_id]}
54
+ 'server_id' => state[:server_id] }
55
55
  driver.destroy_machine(action_handler, machine_spec, config[:machine_options])
56
56
  end
57
57
 
@@ -60,23 +60,23 @@ module Kitchen
60
60
  state.delete(:vsphere_name)
61
61
  end
62
62
 
63
- def with_provisioning_driver(state, &block)
64
- config[:machine_options][:convergence_options] = {:chef_server => chef_server}
63
+ def with_provisioning_driver(state)
64
+ config[:machine_options][:convergence_options] = { chef_server: chef_server }
65
65
  machine_spec = Chef::Provisioning.chef_managed_entry_store(chef_server).get(:machine, state[:vsphere_name])
66
66
  if machine_spec.nil?
67
67
  machine_spec = Chef::Provisioning.chef_managed_entry_store(chef_server)
68
- .new_entry(:machine, state[:vsphere_name])
68
+ .new_entry(:machine, state[:vsphere_name])
69
69
  end
70
70
  url = URI::VsphereUrl.from_config(@config[:driver_options]).to_s
71
71
  driver = Chef::Provisioning.driver_for_url(url, config)
72
72
  action_handler = Chef::Provisioning::ActionHandler.new
73
- block.call(action_handler, driver, machine_spec)
73
+ yield(action_handler, driver, machine_spec)
74
74
  end
75
75
 
76
76
  def chef_server
77
- if !@@chef_zero_server
77
+ unless @@chef_zero_server
78
78
  vsphere_mutex.synchronize do
79
- if !@@chef_zero_server
79
+ unless @@chef_zero_server
80
80
  Chef::Config.local_mode = true
81
81
  Chef::Config.chef_repo_path = Chef::Config.find_chef_repo_path(Dir.pwd)
82
82
  require 'chef/local_mode'
@@ -92,7 +92,7 @@ module Kitchen
92
92
  def vsphere_mutex
93
93
  @@vsphere_mutex ||= begin
94
94
  Kitchen.mutex.synchronize do
95
- instance.class.mutexes ||= Hash.new
95
+ instance.class.mutexes ||= {}
96
96
  instance.class.mutexes[self.class] = Mutex.new
97
97
  end
98
98
 
@@ -1 +1 @@
1
- config.rb
1
+ config.rb
@@ -1,37 +1,38 @@
1
+ # frozen_string_literal: true
1
2
  require 'chef/provisioning/vsphere_driver'
2
3
  require 'chef/provisioning/machine_spec'
3
4
 
4
- # A file named config.rb in the same directory as this spec file
5
- # must exist containing the driver options to use for the test.
6
- # Here is an example:
7
- # {
8
- # :driver_options => {
9
- # :host => '213.45.67.88',
10
- # :user => 'vmapi',
11
- # :password => 'SuperSecureP@ssw0rd',
12
- # :insecure => true
13
- # },
14
- # :machine_options => {
15
- # :start_timeout => 600,
16
- # :create_timeout => 600,
17
- # :bootstrap_options => {
18
- # :datacenter => 'QA1',
19
- # :template_name => 'UBUNTU-12-64-TEMPLATE',
20
- # :vm_folder => 'DLAB',
21
- # :num_cpus => 2,
22
- # :network_name => 'vlan152_172.21.152',
23
- # :memory_mb => 4096,
24
- # :resource_pool => 'CLSTR02/DLAB',
25
- # :ssh => {
26
- # :user => 'root',
27
- # :password => 'SuperSecureP@ssw0rd',
28
- # :paranoid => false,
29
- # :port => 22
30
- # },
31
- # :convergence_options => {}
32
- # }
33
- # }
34
- # }
5
+ # A file named config.rb in the same directory as this spec file
6
+ # must exist containing the driver options to use for the test.
7
+ # Here is an example:
8
+ # {
9
+ # :driver_options => {
10
+ # :host => '213.45.67.88',
11
+ # :user => 'vmapi',
12
+ # :password => 'SuperSecureP@ssw0rd',
13
+ # :insecure => true
14
+ # },
15
+ # :machine_options => {
16
+ # :start_timeout => 600,
17
+ # :create_timeout => 600,
18
+ # :bootstrap_options => {
19
+ # :datacenter => 'QA1',
20
+ # :template_name => 'UBUNTU-12-64-TEMPLATE',
21
+ # :vm_folder => 'DLAB',
22
+ # :num_cpus => 2,
23
+ # :network_name => 'vlan152_172.21.152',
24
+ # :memory_mb => 4096,
25
+ # :resource_pool => 'CLSTR02/DLAB',
26
+ # :ssh => {
27
+ # :user => 'root',
28
+ # :password => 'SuperSecureP@ssw0rd',
29
+ # :paranoid => false,
30
+ # :port => 22
31
+ # },
32
+ # :convergence_options => {}
33
+ # }
34
+ # }
35
+ # }
35
36
 
36
37
  describe 'vsphere_driver' do
37
38
  before :all do
@@ -45,7 +46,7 @@ describe 'vsphere_driver' do
45
46
  @driver = Chef::Provisioning.driver_for_url(url, @metal_config)
46
47
  action_handler = Chef::Provisioning::ActionHandler.new
47
48
  @driver.allocate_machine(action_handler, @machine_spec, @metal_config[:machine_options])
48
- @metal_config[:machine_options][:convergence_options] = {:chef_server => chef_server}
49
+ @metal_config[:machine_options][:convergence_options] = { chef_server: chef_server }
49
50
  machine = @driver.ready_machine(action_handler, @machine_spec, @metal_config[:machine_options])
50
51
  @server_id = @machine_spec.location['server_id']
51
52
  @vsphere_helper = ChefProvisioningVsphere::VsphereHelper.new(
@@ -57,7 +58,6 @@ describe 'vsphere_driver' do
57
58
  end
58
59
 
59
60
  context 'when allocating a machine' do
60
-
61
61
  it 'adds machine to the correct folder' do
62
62
  expect(@vm.parent.name).to eq(@metal_config[:machine_options][:bootstrap_options][:vm_folder])
63
63
  end
@@ -65,13 +65,13 @@ describe 'vsphere_driver' do
65
65
  expect(@vm.config.instanceUuid).to eq(@machine_spec.location['server_id'])
66
66
  end
67
67
  it 'has the correct name' do
68
- now = Time.now.utc
69
- trimmed_name = @vm.config.guestId.start_with?('win') ? @vm_name.byteslice(0,15) : @vm_name
70
- expected_name="#{trimmed_name}.#{@metal_config[:machine_options][:bootstrap_options][:customization_spec][:domain]}"
71
- until (Time.now.utc - now) > 30 || (@vm.guest.hostName == expected_name) do
72
- print '.'
73
- sleep 5
74
- end
68
+ now = Time.now.utc
69
+ trimmed_name = @vm.config.guestId.start_with?('win') ? @vm_name.byteslice(0, 15) : @vm_name
70
+ expected_name = "#{trimmed_name}.#{@metal_config[:machine_options][:bootstrap_options][:customization_spec][:domain]}"
71
+ until (Time.now.utc - now) > 30 || (@vm.guest.hostName == expected_name)
72
+ print '.'
73
+ sleep 5
74
+ end
75
75
  expect(@vm.guest.hostName).to eq(expected_name)
76
76
  end
77
77
  it 'has the correct number of CPUs' do
@@ -81,24 +81,24 @@ describe 'vsphere_driver' do
81
81
  expect(@vm.config.hardware.memoryMB).to eq(@metal_config[:machine_options][:bootstrap_options][:memory_mb])
82
82
  end
83
83
  it 'is on the correct networks' do
84
- expect(@vm.network.map {|n| n.name}).to include(@metal_config[:machine_options][:bootstrap_options][:network_name][0])
85
- expect(@vm.network.map {|n| n.name}).to include(@metal_config[:machine_options][:bootstrap_options][:network_name][1])
84
+ expect(@vm.network.map(&:name)).to include(@metal_config[:machine_options][:bootstrap_options][:network_name][0])
85
+ expect(@vm.network.map(&:name)).to include(@metal_config[:machine_options][:bootstrap_options][:network_name][1])
86
86
  end
87
87
  it 'is on the correct datastore' do
88
88
  expect(@vm.datastore[0].name).to eq(@metal_config[:machine_options][:bootstrap_options][:datastore])
89
89
  end
90
90
  it 'is in the correct resource pool' do
91
- if @metal_config[:machine_options][:bootstrap_options].has_key?(:resource_pool)
91
+ if @metal_config[:machine_options][:bootstrap_options].key?(:resource_pool)
92
92
  expect(@vm.resourcePool.name).to eq(@metal_config[:machine_options][:bootstrap_options][:resource_pool].split('/')[1])
93
93
  end
94
94
  end
95
95
  it 'is in the correct host' do
96
- if @metal_config[:machine_options][:bootstrap_options].has_key?(:host)
96
+ if @metal_config[:machine_options][:bootstrap_options].key?(:host)
97
97
  expect(@vm.runtime.host.name).to eq(@metal_config[:machine_options][:bootstrap_options][:host].split('/').last)
98
98
  end
99
99
  end
100
100
  it 'is in the correct cluster' do
101
- if @metal_config[:machine_options][:bootstrap_options].has_key?(:resource_pool)
101
+ if @metal_config[:machine_options][:bootstrap_options].key?(:resource_pool)
102
102
  expect(@vm.resourcePool.owner.name).to eq(@metal_config[:machine_options][:bootstrap_options][:resource_pool].split('/')[0])
103
103
  end
104
104
  end
@@ -107,7 +107,7 @@ describe 'vsphere_driver' do
107
107
  end
108
108
  it 'has an added disk of the correct size' do
109
109
  disk_count = @vm.disks.count
110
- expect(@vm.disks[disk_count-1].capacityInKB).to eq(@metal_config[:machine_options][:bootstrap_options][:additional_disk_size_gb][1] * 1024 * 1024)
110
+ expect(@vm.disks[disk_count - 1].capacityInKB).to eq(@metal_config[:machine_options][:bootstrap_options][:additional_disk_size_gb][1] * 1024 * 1024)
111
111
  end
112
112
  it 'has the correct number of disks' do
113
113
  expect(@vm.disks.count).to eq(3)
@@ -120,19 +120,18 @@ describe 'vsphere_driver' do
120
120
  end
121
121
  it 'has hot add memory enabled' do
122
122
  expect(@vm.config.memoryHotAddEnabled).to eq(true)
123
- end
123
+ end
124
124
  it 'has the correct IP address' do
125
125
  now = Time.now.utc
126
- until (Time.now.utc - now) > 30 || (@vm.guest.toolsRunningStatus == 'guestToolsRunning' && @vm.guest.net.count == 2 && @vm.guest.net[1].ipAddress[1] == @metal_config[:machine_options][:bootstrap_options][:customization_spec][:ipsettings][:ip]) do
126
+ until (Time.now.utc - now) > 30 || (@vm.guest.toolsRunningStatus == 'guestToolsRunning' && @vm.guest.net.count == 2 && @vm.guest.net[1].ipAddress[1] == @metal_config[:machine_options][:bootstrap_options][:customization_spec][:ipsettings][:ip])
127
127
  print '.'
128
128
  sleep 5
129
129
  end
130
- expect(@vm.guest.net.map { |net| net.ipAddress}.flatten).to include(@metal_config[:machine_options][:bootstrap_options][:customization_spec][:ipsettings][:ip])
130
+ expect(@vm.guest.net.map(&:ipAddress).flatten).to include(@metal_config[:machine_options][:bootstrap_options][:customization_spec][:ipsettings][:ip])
131
131
  end
132
132
  end
133
133
 
134
134
  context 'destroy_machine' do
135
-
136
135
  it 'removes the machine' do
137
136
  Cheffish.honor_local_mode do
138
137
  chef_server = Cheffish.default_chef_server
@@ -140,7 +139,7 @@ describe 'vsphere_driver' do
140
139
  driver = Chef::Provisioning.driver_for_url(url, @metal_config)
141
140
  action_handler = Chef::Provisioning::ActionHandler.new
142
141
  machine_spec = Chef::Provisioning.chef_managed_entry_store(chef_server)
143
- .new_entry(:machine, @vm_name)
142
+ .new_entry(:machine, @vm_name)
144
143
  machine_spec.location = {
145
144
  'driver_url' => driver.driver_url,
146
145
  'server_id' => @server_id
@@ -1,132 +1,133 @@
1
- require 'chef/provisioning/vsphere_driver'
2
-
3
- describe ChefProvisioningVsphere::VsphereDriver do
4
- subject do
5
- Chef::Provisioning.driver_for_url(
6
- 'vsphere://3.3.3.3:999/crazyapi?use_ssl=false&insecure=true',
7
- metal_config
8
- )
9
- end
10
-
11
- context "when config does not include the properties included in the url" do
12
- let(:metal_config) do
13
- {
14
- :driver_options => {
15
- :user => 'vmapi',
16
- :password => '<password>'
17
- },
18
- :machine_options => {
19
- :ssh => {
20
- :password => '<password>',
21
- :paranoid => false
22
- },
23
- :bootstrap_options => {
24
- :datacenter => 'QA1',
25
- :template_name => 'UBUNTU-12-64-TEMPLATE',
26
- :vm_folder => 'DLAB',
27
- :num_cpus => 2,
28
- :memory_mb => 4096,
29
- :resource_pool => 'CLSTR02/DLAB'
30
- }
31
- },
32
- :log_level => :debug
33
- }
34
- end
35
-
36
- it "populates the connect options with correct host from the driver url" do
37
- expect(subject.connect_options[:host]).to eq('3.3.3.3')
38
- end
39
- it "populates the connect options with correct port from the driver url" do
40
- expect(subject.connect_options[:port]).to eq(999)
41
- end
42
- it "populates the connect options with correct path from the driver url" do
43
- expect(subject.connect_options[:path]).to eq('/crazyapi')
44
- end
45
- it "populates the connect options with correct use_ssl setting from the driver url" do
46
- expect(subject.connect_options[:use_ssl]).to eq(false)
47
- end
48
- it "populates the connect options with correct insecure setting from the driver url" do
49
- expect(subject.connect_options[:insecure]).to eq(true)
50
- end
51
- end
52
-
53
- context "when config keys are stringified" do
54
- let(:metal_config) do
55
- {
56
- 'driver_options' => {
57
- 'user' => 'vmapi',
58
- 'password' => '<driver_password>'
59
- },
60
- 'bootstrap_options' => {
61
- 'machine_options' => {
62
- 'datacenter' => 'QA1',
63
- 'ssh' => {
64
- 'password' => '<machine_password>'
65
- }
66
- }
67
- }
68
- }
69
- end
70
-
71
- it "will symbolize user" do
72
- expect(subject.connect_options[:user]).to eq('vmapi')
73
- end
74
- it "will symbolize password" do
75
- expect(subject.connect_options[:password]).to eq('<driver_password>')
76
- end
77
- it "will symbolize ssh password" do
78
- expect(subject.config[:bootstrap_options][:machine_options][:ssh][:password]).to eq('<machine_password>')
79
- end
80
- it "will symbolize ssh bootstrap options" do
81
- expect(subject.config[:bootstrap_options][:machine_options][:datacenter]).to eq('QA1')
82
- end
83
- end
84
-
85
- describe 'canonicalize_url' do
86
- context "when no url is in the config" do
87
- let(:metal_config) do
88
- {
89
- :user => 'vmapi',
90
- :password => '<password>',
91
- :host => '4.4.4.4',
92
- :port => 888,
93
- :path => '/yoda',
94
- :use_ssl => false,
95
- :insecure => true
96
- }
97
- end
98
-
99
- subject do
100
- ChefProvisioningVsphere::VsphereDriver.canonicalize_url(
101
- nil,
102
- metal_config
103
- )
104
- end
105
-
106
- it "creates the correct driver url from config settings" do
107
- expect(subject[0]).to eq('vsphere://4.4.4.4:888/yoda?use_ssl=false&insecure=true')
108
- end
109
- end
110
-
111
- context "when no url is in the config and config is missing defaulted values" do
112
- let(:metal_config) do
113
- {
114
- :user => 'vmapi',
115
- :password => '<password>',
116
- :host => '4.4.4.4'
117
- }
118
- end
119
-
120
- subject do
121
- ChefProvisioningVsphere::VsphereDriver.canonicalize_url(
122
- nil,
123
- metal_config
124
- )
125
- end
126
-
127
- it "creates the correct driver url from default settings" do
128
- expect(subject[0]).to eq('vsphere://4.4.4.4/sdk?use_ssl=true&insecure=false')
129
- end
130
- end
131
- end
132
- end
1
+ # frozen_string_literal: true
2
+ require 'chef/provisioning/vsphere_driver'
3
+
4
+ describe ChefProvisioningVsphere::VsphereDriver do
5
+ subject do
6
+ Chef::Provisioning.driver_for_url(
7
+ 'vsphere://3.3.3.3:999/crazyapi?use_ssl=false&insecure=true',
8
+ metal_config
9
+ )
10
+ end
11
+
12
+ context 'when config does not include the properties included in the url' do
13
+ let(:metal_config) do
14
+ {
15
+ driver_options: {
16
+ user: 'vmapi',
17
+ password: '<password>'
18
+ },
19
+ machine_options: {
20
+ ssh: {
21
+ password: '<password>',
22
+ paranoid: false
23
+ },
24
+ bootstrap_options: {
25
+ datacenter: 'QA1',
26
+ template_name: 'UBUNTU-12-64-TEMPLATE',
27
+ vm_folder: 'DLAB',
28
+ num_cpus: 2,
29
+ memory_mb: 4096,
30
+ resource_pool: 'CLSTR02/DLAB'
31
+ }
32
+ },
33
+ log_level: :debug
34
+ }
35
+ end
36
+
37
+ it 'populates the connect options with correct host from the driver url' do
38
+ expect(subject.connect_options[:host]).to eq('3.3.3.3')
39
+ end
40
+ it 'populates the connect options with correct port from the driver url' do
41
+ expect(subject.connect_options[:port]).to eq(999)
42
+ end
43
+ it 'populates the connect options with correct path from the driver url' do
44
+ expect(subject.connect_options[:path]).to eq('/crazyapi')
45
+ end
46
+ it 'populates the connect options with correct use_ssl setting from the driver url' do
47
+ expect(subject.connect_options[:use_ssl]).to eq(false)
48
+ end
49
+ it 'populates the connect options with correct insecure setting from the driver url' do
50
+ expect(subject.connect_options[:insecure]).to eq(true)
51
+ end
52
+ end
53
+
54
+ context 'when config keys are stringified' do
55
+ let(:metal_config) do
56
+ {
57
+ 'driver_options' => {
58
+ 'user' => 'vmapi',
59
+ 'password' => '<driver_password>'
60
+ },
61
+ 'bootstrap_options' => {
62
+ 'machine_options' => {
63
+ 'datacenter' => 'QA1',
64
+ 'ssh' => {
65
+ 'password' => '<machine_password>'
66
+ }
67
+ }
68
+ }
69
+ }
70
+ end
71
+
72
+ it 'will symbolize user' do
73
+ expect(subject.connect_options[:user]).to eq('vmapi')
74
+ end
75
+ it 'will symbolize password' do
76
+ expect(subject.connect_options[:password]).to eq('<driver_password>')
77
+ end
78
+ it 'will symbolize ssh password' do
79
+ expect(subject.config[:bootstrap_options][:machine_options][:ssh][:password]).to eq('<machine_password>')
80
+ end
81
+ it 'will symbolize ssh bootstrap options' do
82
+ expect(subject.config[:bootstrap_options][:machine_options][:datacenter]).to eq('QA1')
83
+ end
84
+ end
85
+
86
+ describe 'canonicalize_url' do
87
+ context 'when no url is in the config' do
88
+ let(:metal_config) do
89
+ {
90
+ user: 'vmapi',
91
+ password: '<password>',
92
+ host: '4.4.4.4',
93
+ port: 888,
94
+ path: '/yoda',
95
+ use_ssl: false,
96
+ insecure: true
97
+ }
98
+ end
99
+
100
+ subject do
101
+ ChefProvisioningVsphere::VsphereDriver.canonicalize_url(
102
+ nil,
103
+ metal_config
104
+ )
105
+ end
106
+
107
+ it 'creates the correct driver url from config settings' do
108
+ expect(subject[0]).to eq('vsphere://4.4.4.4:888/yoda?use_ssl=false&insecure=true')
109
+ end
110
+ end
111
+
112
+ context 'when no url is in the config and config is missing defaulted values' do
113
+ let(:metal_config) do
114
+ {
115
+ user: 'vmapi',
116
+ password: '<password>',
117
+ host: '4.4.4.4'
118
+ }
119
+ end
120
+
121
+ subject do
122
+ ChefProvisioningVsphere::VsphereDriver.canonicalize_url(
123
+ nil,
124
+ metal_config
125
+ )
126
+ end
127
+
128
+ it 'creates the correct driver url from default settings' do
129
+ expect(subject[0]).to eq('vsphere://4.4.4.4/sdk?use_ssl=true&insecure=false')
130
+ end
131
+ end
132
+ end
133
+ end