Skip to content

Commit

Permalink
Force the hash returned by Fog::Compute::Google::Disks#get to contain…
Browse files Browse the repository at this point in the history
… a :users key (fixes #611)
  • Loading branch information
Rob Chekaluk committed Nov 10, 2023
1 parent 2081621 commit 0ad3fdd
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/fog/compute/google/models/disks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def all(zone: nil, filter: nil, max_results: nil, order_by: nil,
def get(identity, zone = nil)
if zone
disk = service.get_disk(identity, zone).to_h

# Force the hash to contain a :users key so that it will override any :users key in the existing object
disk[:users] = nil unless disk.include?(:users)

return new(disk)
elsif identity
response = all(:filter => "name eq #{identity}",
Expand Down
74 changes: 74 additions & 0 deletions test/integration/compute/core_compute/test_servers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,80 @@ def test_start_stop_reboot
assert server.ready?
end

def test_attach_disk
# Creating server
server = @factory.create
server.wait_for { ready? }

disk_name = "fog-test-1-testservers-test-attach-disk-attachable" # suffix forces disk name to differ from the existing disk
# Creating disk #{disk_name}
disk = @disks.create(
:name => disk_name,
:source_image => TEST_IMAGE,
:size_gb => 64
)
device_name = "#{disk.name}-device"

# Attaching disk #{disk.name} as device #{device_name}
self_link = "https://www.googleapis.com/compute/v1/projects/#{TEST_PROJECT}/zones/#{TEST_ZONE}/disks/#{disk.name}"
server.attach_disk(self_link, true, device_name: device_name)

# Waiting for attachment
disk.wait_for { ! users.nil? && users != []}

assert_equal "https://www.googleapis.com/compute/v1/projects/#{TEST_PROJECT}/zones/#{TEST_ZONE}/instances/#{server.name}", disk.users[0]

server.reload
server_attached_disk = server.disks.select{|d| d[:boot] == false}[0]
assert_equal device_name, server_attached_disk[:device_name]
end

def test_detach_disk
# Creating server
server = @factory.create
server.wait_for { ready? }

disk_name = "fog-test-1-testservers-test-detach-attachable" # suffix forces disk name to differ from the existing disk
# Creating disk #{disk_name}
disk = @disks.create(
:name => disk_name,
:source_image => TEST_IMAGE,
:size_gb => 64
)
device_name = "#{disk.name}-device"

# Attaching disk #{disk.name} as device #{device_name}
self_link = "https://www.googleapis.com/compute/v1/projects/#{TEST_PROJECT}/zones/#{TEST_ZONE}/disks/#{disk.name}"
server.attach_disk(self_link, true, device_name: device_name)
disk.wait_for { ! users.nil? && users != []}

server.reload
server_attached_disk = server.disks.select{|d| d[:boot] == false}[0]
assert_equal device_name, server_attached_disk[:device_name]

# Detaching (synchronous) disk #{disk.name}
server.detach_disk(device_name, false)

disk.reload
assert disk.users.nil? || disk.users == []

# Re-attaching disk #{disk.name} as device #{device_name}
server.attach_disk(self_link, true, device_name: device_name)
disk.wait_for { ! users.nil? && users != []}

server.reload
server_attached_disk = server.disks.select{|d| d[:boot] == false}[0]
assert_equal device_name, server_attached_disk[:device_name]

# Detaching (async) disk #{disk.name}
server.detach_disk(device_name, true)

# Waiting for detachment
disk.wait_for { users.nil? || users == []}

assert disk.users.nil? || disk.users == []
end

def test_reset_windows_password
win_disk = @disks.create(
:name => "fog-test-1-testservers-test-reset-windows-password-2",
Expand Down
26 changes: 26 additions & 0 deletions test/unit/compute/test_disk.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "helpers/test_helper"

class UnitTestDisk < Minitest::Test
def setup
Fog.mock!
@client = Fog::Compute.new(provider: "google",
google_project: "foo")
end

def teardown
Fog.unmock!
end

def test_new_disk
disk = Fog::Compute::Google::Disk.new(
:name => "fog-1",
:size_gb => 10,
:zone => "us-central1-a",
:source_image => "debian-7-wheezy-v20131120"
)
assert_equal("fog-1", disk.name, "Fog::Compute::Google::Disk name is incorrect: #{disk.name}")
assert_equal(10, disk.size_gb, "Fog::Compute::Google::Disk size_gb is incorrect: #{disk.size_gb}")
assert_equal("us-central1-a", disk.zone, "Fog::Compute::Google::Disk zone is incorrect: #{disk.zone}")
assert_equal("debian-7-wheezy-v20131120", disk.source_image, "Fog::Compute::Google::Disk source_image is incorrect: #{disk.source_image}")
end
end

0 comments on commit 0ad3fdd

Please sign in to comment.