Skip to content

Commit

Permalink
Merge branch 'feature-gce-v1-api'; closes #6.
Browse files Browse the repository at this point in the history
  • Loading branch information
anl committed Mar 28, 2014
2 parents 3e9f9db + b5e9d32 commit d895171
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 15 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ launching instances into a zone that is down for maintenance. If
"any" is specified, kitchen-gce will select a zone from all areas.
Default: `us` (lowest cost area); valid values: `any`, `europe`, `us`

### autodelete_disk

Boolean specifying whether or not to automatically delete boot disk
for test instance. Default: true

### disk_size

Size, in gigabytes of boot disk. Default: 10.

### google_client_email

**Required** Email address associated with your GCE service account.
Expand Down Expand Up @@ -131,7 +140,7 @@ driver_config:
platforms:
- name: debian-7
driver_config:
image_name: debian-7-wheezy-v20130926
image_name: debian-7-wheezy-v20140318
require_chef_omnibus: true
public_key_path: '/home/alice/.ssh/google_compute_engine.pub'
tags: ["somerole"]
Expand Down
2 changes: 1 addition & 1 deletion kitchen-gce.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Gem::Specification.new do |s|

s.add_dependency 'test-kitchen'
s.add_dependency 'faraday', '~> 0.8.9'
s.add_dependency 'fog', '1.19.0'
s.add_dependency 'fog', '>= 1.20.0'
s.add_dependency 'google-api-client'

s.add_development_dependency 'bundler'
Expand Down
39 changes: 29 additions & 10 deletions lib/kitchen/driver/gce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ module Driver
# @author Andrew Leonard <[email protected]>
class Gce < Kitchen::Driver::SSHBase
default_config :area, 'us'
default_config :autodelete_disk, true
default_config :disk_size, 10
default_config :machine_type, 'n1-standard-1'
default_config :network, 'default'
default_config :inst_name, nil
Expand All @@ -43,12 +45,12 @@ class Gce < Kitchen::Driver::SSHBase
def create(state)
return if state[:server_id]

server = create_instance
state[:server_id] = server.identity
instance = create_instance
state[:server_id] = instance.identity

info("GCE instance <#{state[:server_id]}> created.")

wait_for_up_instance(server, state)
wait_for_up_instance(instance, state)

rescue Fog::Errors::Error, Excon::Errors::Error => ex
raise ActionFailed, ex.message
Expand All @@ -57,8 +59,8 @@ def create(state)
def destroy(state)
return if state[:server_id].nil?

server = connection.servers.get(state[:server_id])
server.destroy unless server.nil?
instance = connection.servers.get(state[:server_id])
instance.destroy unless instance.nil?
info("GCE instance <#{state[:server_id]}> destroyed.")
state.delete(:server_id)
state.delete(:hostname)
Expand All @@ -75,13 +77,30 @@ def connection
)
end

def create_disk
disk = connection.disks.create(
name: config[:inst_name],
size_gb: config[:disk_size],
zone_name: config[:zone_name],
source_image: config[:image_name]
)

disk.wait_for { disk.ready? }
disk
end

def create_instance
config[:inst_name] ||= generate_inst_name
config[:zone_name] ||= select_zone

disk = create_disk
create_server(disk)
end

def create_server(disk)
connection.servers.create(
name: config[:inst_name],
image_name: config[:image_name],
disks: [disk.get_as_boot_disk(true, config[:autodelete_disk])],
machine_type: config[:machine_type],
network: config[:network],
tags: config[:tags],
Expand Down Expand Up @@ -110,14 +129,14 @@ def select_zone
zones.sample.name
end

def wait_for_up_instance(server, state)
server.wait_for do
def wait_for_up_instance(instance, state)
instance.wait_for do
print '.'
ready?
end
print '(server ready)'
state[:hostname] = server.public_ip_address ||
server.private_ip_address
state[:hostname] = instance.public_ip_address ||
instance.private_ip_address
wait_for_sshd(state[:hostname], config[:username])
puts '(ssh ready)'
end
Expand Down
50 changes: 47 additions & 3 deletions spec/kitchen/driver/gce_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,23 @@
d
end

let(:fog) do
Fog::Compute::Google::Mock.new
end

let(:disk) do
fog.disks.create(
name: 'rspec-test-disk',
size_gb: 10,
zone_name: 'us-central1-b',
source_image: 'debian-7-wheezy-v20131120'
)
end

let(:server) do
fog = Fog::Compute::Google::Mock.new
fog.servers.create(
name: 'rspec-test-instance',
disks: [disk],
machine_type: 'n1-standard-1',
zone_name: 'us-central1-b'
)
Expand All @@ -52,6 +65,8 @@

defaults = {
area: 'us',
autodelete_disk: true,
disk_size: 10,
inst_name: nil,
machine_type: 'n1-standard-1',
network: 'default',
Expand All @@ -69,6 +84,8 @@
context 'with overriden options' do
overrides = {
area: 'europe',
autodelete_disk: false,
disk_size: 15,
inst_name: 'ci-instance',
machine_type: 'n1-highmem-8',
network: 'dev-net',
Expand All @@ -93,9 +110,9 @@
expect(driver.send(:connection)).to be_a(Fog::Compute::Google::Mock)
end

it 'uses the v1beta16 api version' do
it 'uses the v1 api version' do
conn = driver.send(:connection)
expect(conn.api_version).to eq('v1beta16')
expect(conn.api_version).to eq('v1')
end
end

Expand Down Expand Up @@ -143,6 +160,24 @@

end

describe '#create_disk' do
context 'with defaults and required options' do
it 'returns a Google Disk object' do
config[:image_name] = 'debian-7-wheezy-v20131120'
config[:inst_name] = 'rspec-disk'
config[:zone_name] = 'us-central1-a'
expect(driver.send(:create_disk)).to be_a(Fog::Compute::Google::Disk)
end
end

context 'without required options' do
it 'returns a Fog NotFound Error' do
expect { driver.send(:create_disk) }.to raise_error(
Fog::Errors::NotFound)
end
end
end

describe '#create_instance' do
context 'with default options' do
it 'returns a Fog Compute Server object' do
Expand All @@ -152,6 +187,15 @@
end
end

describe '#create_server' do
context 'with default options' do
it 'returns a Fog Compute Server object' do
expect(driver.send(:create_instance)).to be_a(
Fog::Compute::Google::Server)
end
end
end

describe '#destroy' do
let(:state) do
s = Hash.new
Expand Down

0 comments on commit d895171

Please sign in to comment.