Files
ubicloud/spec/lib/hosting/apis_spec.rb
Junhao Li d23134e509 Add hardware reset for vm host
Calls the Hetzner API to perform a hardware host reset and then reboot.
This enhances remote server management and recovery, particularly
when the host is unresponsive.
2025-01-13 09:38:11 -05:00

84 lines
2.9 KiB
Ruby

# frozen_string_literal: true
RSpec.describe Hosting::Apis do
let(:vm_host) {
instance_double(
VmHost,
ubid: "vhgkz40v22ny2qkf4maddr8xv1",
provider: HetznerHost::PROVIDER_NAME,
hetzner_host: hetzner_host
)
}
let(:connection) { instance_double(Excon::Connection) }
let(:hetzner_apis) { instance_double(Hosting::HetznerApis, pull_ips: []) }
let(:hetzner_host) {
instance_double(
HetznerHost,
connection_string: "str",
user: "user1", password: "pass",
api: hetzner_apis,
server_identifier: 123
)
}
describe "pull_ips" do
it "can pull data from the API" do
expect(hetzner_apis).to receive(:pull_ips).and_return([])
described_class.pull_ips(vm_host)
end
it "raises an error if the provider is unknown" do
expect(vm_host).to receive(:provider).and_return("unknown").at_least(:once)
expect { described_class.pull_ips(vm_host) }.to raise_error RuntimeError, "unknown provider unknown"
end
end
describe "reimage_server" do
it "can reimage a server" do
expect(hetzner_apis).to receive(:reimage).with(123).and_return(true)
described_class.reimage_server(vm_host)
end
it "raises an error if the provider is unknown" do
expect(vm_host).to receive(:provider).and_return("unknown").at_least(:once)
expect { described_class.reimage_server(vm_host) }.to raise_error RuntimeError, "unknown provider unknown"
end
end
describe "hardware_reset_server" do
it "can hardware reset a server" do
expect(hetzner_apis).to receive(:reset).with(123).and_return(true)
described_class.hardware_reset_server(vm_host)
end
it "raises an error if the provider is unknown" do
expect(vm_host).to receive(:provider).and_return("unknown").at_least(:once)
expect { described_class.hardware_reset_server(vm_host) }.to raise_error RuntimeError, "unknown provider unknown"
end
end
describe "pull_dc" do
it "can set dc of a server" do
expect(hetzner_apis).to receive(:pull_dc).with(123).and_return("dc1")
described_class.pull_data_center(vm_host)
end
it "raises an error if the provider is unknown" do
expect(vm_host).to receive(:provider).and_return("unknown").at_least(:once)
expect { described_class.pull_data_center(vm_host) }.to raise_error RuntimeError, "unknown provider unknown"
end
end
describe "set_server_name" do
it "can set server name" do
expect(hetzner_apis).to receive(:set_server_name).with(123, "vhgkz40v22ny2qkf4maddr8xv1").and_return(nil)
described_class.set_server_name(vm_host)
end
it "raises an error if the provider is unknown" do
expect(vm_host).to receive(:provider).and_return("unknown").at_least(:once)
expect { described_class.set_server_name(vm_host) }.to raise_error RuntimeError, "unknown provider unknown"
end
end
end