Files
ubicloud/spec/model/load_balancer_spec.rb
Jeremy Evans 215f09541a Make access_tag only for project <-> accounts join table
For the includers of HyperTagMethods, this changes the authorization
code and object_tag member validation code to look at the project_id
column for the object, instead of looking a row with the project and
object in the access_tag table.

This removes all calls to associate_with_project, other than
those for Account.  It removes the projects association for
the includers of HyperTagMethods, and adds a project association to
the models that didn't already have one, since there is only a single
project for each object now.

Most HyperTagMethods code is inlined into Account, since it is only
user of the code now.  Temporarily, other models will still include
HyperTagMethods for the before_destroy hook, but eventually it will
go away completely.

The associations in Projects that previous used access_tag as a join
table, are changed from many_to_many to one_to_many, except for
Account (which still uses the join table).

Project#has_resources now needs separate queries for all of the
resource classes to see if there any associated objects.

This causes a lot of fallout in the specs, but unfortunately that is
unavoidable due the extensive use of projects.first in the specs to
get the related project for the objects, as well as the extensive
use of associate_with_project.
2025-01-17 08:32:46 -08:00

125 lines
4.0 KiB
Ruby

# frozen_string_literal: true
require_relative "spec_helper"
RSpec.describe LoadBalancer do
subject(:lb) {
prj = Project.create_with_id(name: "test-prj")
ps = Prog::Vnet::SubnetNexus.assemble(prj.id, name: "test-ps")
Prog::Vnet::LoadBalancerNexus.assemble(ps.id, name: "test-lb", src_port: 80, dst_port: 80).subject
}
let(:vm1) {
prj = lb.private_subnet.project
Prog::Vm::Nexus.assemble("pub-key", prj.id, name: "test-vm1", private_subnet_id: lb.private_subnet.id).subject
}
describe "util funcs" do
before do
allow(Config).to receive(:load_balancer_service_hostname).and_return("lb.ubicloud.com")
end
it "returns hostname" do
expect(lb.hostname).to eq("test-lb.#{lb.private_subnet.ubid[-5...]}.lb.ubicloud.com")
end
end
describe "add_vm" do
it "increments update_load_balancer and rewrite_dns_records" do
expect(lb).to receive(:incr_rewrite_dns_records)
dz = DnsZone.create_with_id(name: "test-dns-zone", project_id: lb.project_id)
cert = Prog::Vnet::CertNexus.assemble("test-host-name", dz.id).subject
lb.add_cert(cert)
lb.add_vm(vm1)
expect(lb.load_balancers_vms.count).to eq(1)
end
end
describe "evacuate_vm" do
let(:ce) {
dz = DnsZone.create_with_id(name: "test-dns-zone", project_id: lb.project_id)
Prog::Vnet::CertNexus.assemble("test-host-name", dz.id).subject
}
before do
lb.add_cert(ce)
lb.add_vm(vm1)
end
it "increments update_load_balancer and rewrite_dns_records" do
expect(lb).to receive(:incr_update_load_balancer)
expect(lb).to receive(:incr_rewrite_dns_records)
health_probe = instance_double(Strand, stack: [{"subject_id" => lb.id, "vm_id" => vm1.id}])
expect(lb.strand).to receive(:children_dataset).and_return(instance_double(Sequel::Dataset, where: instance_double(Sequel::Dataset, all: [health_probe])))
expect(health_probe).to receive(:destroy)
lb.evacuate_vm(vm1)
expect(lb.load_balancers_vms.first[:state]).to eq("evacuating")
end
end
describe "remove_vm" do
let(:ce) {
dz = DnsZone.create_with_id(name: "test-dns-zone", project_id: lb.project_id)
Prog::Vnet::CertNexus.assemble("test-host-name", dz.id).subject
}
before do
lb.add_cert(ce)
lb.add_vm(vm1)
end
it "deletes the vm" do
lb.remove_vm(vm1)
expect(lb.load_balancers_vms.count).to eq(0)
end
end
describe "need_certificates?" do
let(:dns_zone) {
DnsZone.create_with_id(name: "test-dns-zone", project_id: lb.private_subnet.project_id)
}
it "returns true if there are no certs" do
expect(lb.need_certificates?).to be(true)
end
it "returns false if there are certs but either expired or close to expiry" do
cert = Prog::Vnet::CertNexus.assemble(lb.hostname, dns_zone.id).subject
lb.add_cert(cert)
cert.update(created_at: Time.now - 1 * 365 * 24 * 60 * 60)
expect(lb.need_certificates?).to be(true)
end
it "returns false if there are certs and they are not expired" do
cert = Prog::Vnet::CertNexus.assemble(lb.hostname, dns_zone.id).subject
lb.add_cert(cert)
expect(lb.need_certificates?).to be(false)
end
end
describe "active_cert" do
let(:dns_zone) {
DnsZone.create_with_id(name: "test-dns-zone", project_id: lb.private_subnet.project_id)
}
it "returns the cert that is not expired" do
cert1 = Prog::Vnet::CertNexus.assemble(lb.hostname, dns_zone.id).subject
cert2 = Prog::Vnet::CertNexus.assemble(lb.hostname, dns_zone.id).subject
lb.add_cert(cert1)
lb.add_cert(cert2)
cert1.update(created_at: Time.now - 1 * 365 * 24 * 60 * 60)
expect(lb.active_cert.id).to eq(cert2.id)
end
it "returns nil if all certs are expired" do
cert = Prog::Vnet::CertNexus.assemble(lb.hostname, dns_zone.id).subject
lb.add_cert(cert)
cert.update(created_at: Time.now - 1 * 365 * 24 * 60 * 60)
expect(lb.active_cert).to be_nil
end
end
end