# frozen_string_literal: true require "rails_helper" RSpec.describe Streams::CloudProviders::Hetzner do let(:stubs) { Faraday::Adapter::Test::Stubs.new } let(:conn) do Faraday.new(url: Streams::CloudProviders::Hetzner::API) do |f| f.request :json f.response :json f.adapter :test, stubs end end let(:provider) { described_class.new(token: "test-token", conn: conn) } def with_env(vars) previous = vars.keys.index_with { |k| ENV[k] } vars.each { |k, v| ENV[k] = v } yield ensure previous.each { |k, v| v.nil? ? ENV.delete(k) : ENV[k] = v } end after { stubs.verify_stubbed_calls } it "creates a server and waits until running" do with_env( "HCLOUD_LOCATION" => "nbg1", "HCLOUD_SERVER_TYPE" => "cpx12", "HCLOUD_IMAGE" => "debian-12", "HCLOUD_SSH_KEY" => "matchlivetv-stream-hetzner" ) do stubs.post("https://api.hetzner.cloud/v1/servers") do |env| body = JSON.parse(env.body) expect(body["name"]).to eq("mltv-stream-ingest-01") expect(body["location"]).to eq("nbg1") [ 201, { "Content-Type" => "application/json" }, { "server" => { "id" => 42, "name" => "mltv-stream-ingest-01", "status" => "initializing", "public_net" => { "ipv4" => { "ip" => "49.13.1.2" } }, "private_net" => [] }, "action" => { "id" => 7, "status" => "running" } } ] end stubs.get("https://api.hetzner.cloud/v1/actions/7") do [200, { "Content-Type" => "application/json" }, { "action" => { "id" => 7, "status" => "success" } }] end stubs.get("https://api.hetzner.cloud/v1/servers/42") do [ 200, { "Content-Type" => "application/json" }, { "server" => { "id" => 42, "name" => "mltv-stream-ingest-01", "status" => "running", "public_net" => { "ipv4" => { "ip" => "49.13.1.2" } }, "private_net" => [] } } ] end instance = provider.create_node(name: "mltv-stream-ingest-01", labels: { role: "stream-node" }) expect(instance.id).to eq("42") expect(instance.public_ip).to eq("49.13.1.2") expect(instance.status).to eq("running") end end it "destroys a server" do stubs.delete("https://api.hetzner.cloud/v1/servers/42") { [204, {}, ""] } expect(provider.destroy_node("42")).to eq(true) end end