Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust network calls to hcloud 1.2 #30

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

bastelfreak
Copy link
Member

Some methods and their return values changed within the 1.2 release.

@bastelfreak
Copy link
Member Author

This requires tonobo/hcloud-ruby#84

Copy link

codecov bot commented Nov 1, 2023

Codecov Report

Attention: 3 lines in your changes are missing coverage. Please review.

Comparison is base (2e97539) 92.94% compared to head (95b06e4) 88.50%.

❗ Current head 95b06e4 differs from pull request most recent head 4d64c64. Consider uploading reports for the commit 4d64c64 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #30      +/-   ##
==========================================
- Coverage   92.94%   88.50%   -4.44%     
==========================================
  Files           3        3              
  Lines          85       87       +2     
==========================================
- Hits           79       77       -2     
- Misses          6       10       +4     
Files Coverage Δ
lib/beaker/hypervisor/hcloud.rb 88.88% <66.66%> (-6.20%) ⬇️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@bastelfreak bastelfreak force-pushed the hcloud12 branch 2 times, most recently from e26c3e8 to 286eb43 Compare November 1, 2023 15:03
@@ -100,8 +100,8 @@ def create_server(host)
action = @client.actions.find(action.id)
server = @client.servers.find(server.id)
end
host[:ip] = server.public_net['ipv4']['ip']
host[:vmhostname] = server.public_net['ipv4']['dns_ptr']
host[:ip] = server.public_net['ipv4'].ip
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mhm I'm not sure how to mock .ip properly

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

irb(main):013:0> server.public_net['ipv4']
=> #<Hcloud::PrimaryIP:0xd5c {"assignee_id"=>38840073, "assignee_type"=>"server", "auto_delete"=>true, "blocked"=>false, "created"=>2023-11-01 15:10:39 UTC, "datacenter"=>#<Hcloud::Datacenter:0xd70 {"description"=>"Nuremberg 1 virtual DC 3", "id"=>2, "location"=>#<Hcloud::Location:0xd84 {"city"=>"Nuremberg", "country"=>"DE", "description"=>"Nuremberg DC Park 1", "id"=>2, "latitude"=>49.452102, "longitude"=>11.076665, "name"=>"nbg1", "network_zone"=>"eu-central"}>, "name"=>"nbg1-dc3", "server_types"=>{"available"=>[1, 3, 5, 7, 9, 22, 23, 24, 25, 26, 45, 93, 94, 95, 96, 97, 98, 99, 100, 101], "available_for_migration"=>[1, 3, 5, 7, 9, 22, 23, 24, 25, 26, 45, 93, 94, 95, 96, 97, 98, 99, 100, 101], "supported"=>[9, 7, 5, 3, 1, 22, 23, 24, 25, 26, 45, 93, 94, 95, 96, 97, 98, 99, 100, 101]}}>, "dns_ptr"=>[{"ip"=>"128.140.40.189", "dns_ptr"=>"static.189.40.140.128.clients.your-server.de"}], "id"=>42635150, "ip"=>"128.140.40.189", "labels"=>{}, "name"=>"primary_ip-42635150", "protection"=>{"delete"=>false}, "type"=>"ipv4"}>
irb(main):014:0> server.public_net['ipv4'].ip
=> "128.140.40.189"
irb(main):015:0> server.public_net['ipv4'].dns_ptr
=> [{"ip"=>"128.140.40.189", "dns_ptr"=>"static.189.40.140.128.clients.your-server.de"}]
irb(main):016:0> server.public_net['ipv4'].dns_ptr.first
=> {"ip"=>"128.140.40.189", "dns_ptr"=>"static.189.40.140.128.clients.your-server.de"}
irb(main):017:0>

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

irb(main):015:0> server.public_net['ipv4'].dns_ptr.find { |hash| hash['ip'] == server.public_net['ipv4'].ip }
=> {"ip"=>"128.140.40.189", "dns_ptr"=>"static.189.40.140.128.clients.your-server.de"}
irb(main):016:0> server.public_net['ipv4'].dns_ptr.find { |hash| hash['ip'] == server.public_net['ipv4'].ip }['dns_ptr']
=> "static.189.40.140.128.clients.your-server.de"
irb(main):017:0>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd mock the client. So

let(:mock_client) { double(::Hcloud::Client) }
before do
  allow(::Hcloud::Client).to receive(new).and_return(mock_client)
end

A tricky part is that you need to mock all responses on mock_client then. It may be easier to create another helper method:

def self.create_hcloud_server(client, **kwargs)
  action, server, _password, _next_action = client.servers.create(**kwargs)

  while action.status == 'running'
    sleep 5
    action = client.actions.find(action.id)
    server = client.servers.find(server.id)
  end

  server
end

Then you already reduce it a lot: you now can use:

result = Mock(...)
allow(Beaker::Hypervisor::Hcloud).to receive(create_hcloud_server).with(....).and_return(result)

And if you then look at create_hcloud_server, you can also consider if this is also sufficient:

def create_hcloud_server(client, **kwargs)
  action, server, _password, _next_action = client.servers.create(**kwargs)

  while action.status == 'running'
    sleep 5 
    action = client.actions.find(action.id)
  end

  client.servers.find(server.id)
end

It saves a find action on every call.

Some methods and their return values changed within the 1.2 release.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants