Files
ubicloud/spec/routes/api/cli/vm/destroy_spec.rb
Jeremy Evans 9d5f0a2cbc Validate Vm#public_key when saving
This implements a fairly loose validation of the public key format.
It does not check specific key types, nor try to decode the base64
key and check it for validity.  It does check that the format in
general is valid, and that at least one valid key is present.
It will reject public keys, because those don't have a space.

This requires changes to many specs to have them use a valid public
key format.  In many cases, that is done by changing a single
dash to a space.
2025-04-26 05:34:41 +09:00

42 lines
1.8 KiB
Ruby

# frozen_string_literal: true
require_relative "../spec_helper"
RSpec.describe Clover, "cli vm destroy" do
before do
expect(Vm.count).to eq 0
expect(PrivateSubnet.count).to eq 0
cli(%w[vm eu-central-h1/test-vm create] << "a a")
expect(Vm.count).to eq 1
@vm = Vm.first
expect(@vm).to be_a Vm
end
it "destroys vm directly if -f option is given" do
expect(Semaphore.where(strand_id: @vm.id, name: "destroy")).to be_empty
expect(cli(%w[vm eu-central-h1/test-vm destroy -f])).to eq "Virtual machine, if it exists, is now scheduled for destruction\n"
expect(Semaphore.where(strand_id: @vm.id, name: "destroy")).not_to be_empty
end
it "asks for confirmation if -f option is not given" do
expect(Semaphore.where(strand_id: @vm.id, name: "destroy")).to be_empty
expect(cli(%w[vm eu-central-h1/test-vm destroy], confirm_prompt: "Confirmation")).to eq <<~END
Destroying this virtual machine is not recoverable.
Enter the following to confirm destruction of the virtual machine: #{@vm.name}
END
expect(Semaphore.where(strand_id: @vm.id, name: "destroy")).to be_empty
end
it "works on correct confirmation" do
expect(Semaphore.where(strand_id: @vm.id, name: "destroy")).to be_empty
expect(cli(%w[--confirm test-vm vm eu-central-h1/test-vm destroy])).to eq "Virtual machine, if it exists, is now scheduled for destruction\n"
expect(Semaphore.where(strand_id: @vm.id, name: "destroy")).not_to be_empty
end
it "fails on incorrect confirmation" do
expect(Semaphore.where(strand_id: @vm.id, name: "destroy")).to be_empty
expect(cli(%w[--confirm foo vm eu-central-h1/test-vm destroy], status: 400)).to eq "! Confirmation of virtual machine name not successful.\n"
expect(Semaphore.where(strand_id: @vm.id, name: "destroy")).to be_empty
end
end