ubicloud/spec/routes/api/cli/vm/ssh_spec.rb
Jeremy Evans e78a7bb897 Allow using ubid without location in the cli
For example:

```
ubi vm vmdzyppz6j166jh5e9t2dwrfas show
```

This adds an object-info api route under project that returns the type,
location, and name of a supported object (currently the objects supported
by the api).  If there is no slash in the object reference in the cli,
and the object reference is a supported ubid format, then assume the
object reference is a ubid, and have the cli use the object-info endpoint
to get the location, and if a location is returned, it then uses that
location for the request.

Fix the error message for invalid references to not include an
underscore for the ubid, and to separate the location/name and ubid
formats to make it apparent to the user that location is not needed for
ubid (it is still accepted, but we don't need to document that).

Add LoadBalancer#display_location to avoid having different code for
LoadBalancer.
2025-02-26 09:23:01 -08:00

85 lines
3.4 KiB
Ruby
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# frozen_string_literal: true
require_relative "../spec_helper"
RSpec.describe Clover, "cli vm ssh" do
before do
@vm = create_vm(project_id: @project.id, ephemeral_net6: "128:1234::0/64")
@ref = [@vm.display_location, @vm.name].join("/")
subnet = @project.default_private_subnet(@vm.location)
nic = Prog::Vnet::NicNexus.assemble(subnet.id, name: "test-nic").subject
nic.update(vm_id: @vm.id)
end
after do
@socket&.close
end
it "provides headers to connect to vm" do
expect(cli_exec(["vm", @ref, "ssh"])).to eq %w[ssh -- ubi@128:1234::2]
end
it "IPv4 address is used by default if available" do
add_ipv4_to_vm(@vm, "128.0.0.1")
expect(cli_exec(["vm", @ref, "ssh"])).to eq %w[ssh -- ubi@128.0.0.1]
end
it "uses IPv4 address if available and connection is made via IPv4" do
add_ipv4_to_vm(@vm, "128.0.0.1")
@socket = UDPSocket.new(Socket::AF_INET)
expect(cli_exec(["vm", @ref, "ssh"], env: {"puma.socket" => @socket})).to eq %w[ssh -- ubi@128.0.0.1]
end
it "uses IPv6 address if connection is made via IPv6" do
add_ipv4_to_vm(@vm, "128.0.0.1")
@socket = UDPSocket.new(Socket::AF_INET6)
expect(cli_exec(["vm", @ref, "ssh"], env: {"puma.socket" => @socket})).to eq %w[ssh -- ubi@128:1234::2]
end
it "-4 option fails if VM has no IPv4 address" do
expect(cli(["vm", @ref, "-4", "ssh"], status: 400)).to eq "! No valid IPv4 address for requested VM\n"
end
it "-4 option uses IPv4 even if connection is made via IPv6" do
add_ipv4_to_vm(@vm, "128.0.0.1")
@socket = UDPSocket.new(Socket::AF_INET6)
expect(cli_exec(["vm", @ref, "-4", "ssh"], env: {"puma.socket" => @socket})).to eq %w[ssh -- ubi@128.0.0.1]
end
it "-6 option uses IPv6 even if connection is made via IPv4" do
add_ipv4_to_vm(@vm, "128.0.0.1")
@socket = UDPSocket.new(Socket::AF_INET)
expect(cli_exec(["vm", @ref, "-6", "ssh"], env: {"puma.socket" => @socket})).to eq %w[ssh -- ubi@128:1234::2]
end
it "-u option overrides user to connect with" do
expect(cli_exec(["vm", @ref, "-ufoo", "ssh"])).to eq %w[ssh -- foo@128:1234::2]
end
it "handles ssh cmd without args" do
expect(cli_exec(["vm", @ref, "ssh", "id"])).to eq %w[ssh -- ubi@128:1234::2 id]
end
it "handles ssh cmd with args" do
expect(cli_exec(["vm", @ref, "ssh", "uname", "-a"])).to eq %w[ssh -- ubi@128:1234::2 uname -a]
end
it "handles ssh cmd with options and without args" do
expect(cli_exec(["vm", @ref, "ssh", "-A", "--"])).to eq %w[ssh -A -- ubi@128:1234::2]
end
it "handles ssh cmd with options and args" do
expect(cli_exec(["vm", @ref, "ssh", "-A", "--", "uname", "-a"])).to eq %w[ssh -A -- ubi@128:1234::2 uname -a]
end
it "handles multiple options" do
add_ipv4_to_vm(@vm, "128.0.0.1")
expect(cli_exec(["vm", @ref, "-6u", "foo", "ssh"])).to eq %w[ssh -- foo@128:1234::2]
end
it "handles invalid vm reference" do
expect(cli(["vm", "#{@vm.display_location}/foo", "ssh"], status: 404)).to eq "! Unexpected response status: 404\nDetails: Sorry, we couldnt find the resource youre looking for.\n"
expect(cli(["vm", "foo/#{@vm.name}", "ssh"], status: 404)).to eq "! Unexpected response status: 404\nDetails: Sorry, we couldnt find the resource youre looking for.\n"
expect(cli(["vm", "#{@vm.display_location}/#{@vm.name}/bar", "ssh"], status: 400)).to eq "! Invalid vm reference, should be in location/vm-name or vm-id format\n"
end
end