Files
ubicloud/spec/prog/learn_network_spec.rb
Enes Cakir 60b69117dd Clean up nap/hop/exit expectations in specs
We have custom `RSpec::Matchers` for flow control exceptions. There's no
need to manually expect to receive these functions. Additionally, we use
these matchers in our codebase as a single line in general. There's no
need to extend them to 3 lines unless it's too long.

This cleanup has revealed some issues that need fixing:

- learn_network_spec.rb: Our default stack value is `[{}]`. `[]` is not
  expected, and fails our code when popped.

- rekey_nic_tunnel.rb: `pop` raises an exception, so the code can't run
  after it. `return` is unnecessary.

- base.rb: Our default stack value is `[{}]`, so `frame&` is
  unnecessary. We don't need to write extra test to cover this branch.
  It was covered previously because of the wrong stack value at
  `learn_network_spec.rb`.
2023-10-31 18:04:34 +03:00

90 lines
2.0 KiB
Ruby

# frozen_string_literal: true
require_relative "../model/spec_helper"
RSpec.describe Prog::LearnNetwork do
subject(:lm) { described_class.new(Strand.new(stack: [{}])) }
let(:ip6_interface_output) do
<<JSON
[
{
"ifindex": 2,
"ifname": "enp0s31f6",
"flags": [
"BROADCAST",
"MULTICAST",
"UP",
"LOWER_UP"
],
"mtu": 1500,
"operstate": "UP",
"txqlen": 1000,
"addr_info": [
{
"family": "inet6",
"local": "2a01:4f8:173:1ed3::2",
"prefixlen": 64,
"scope": "global",
"valid_life_time": 4294967295,
"preferred_life_time": 4294967295
},
{}
]
}
]
JSON
end
describe "#start" do
it "exits, saving the ip6 address" do
sshable = instance_double(Sshable)
expect(sshable).to receive(:cmd).with("/usr/sbin/ip -j -6 addr show scope global").and_return(ip6_interface_output)
vm_host = instance_double(VmHost)
expect(vm_host).to receive(:update).with(ip6: "2a01:4f8:173:1ed3::2", net6: "2a01:4f8:173:1ed3::/64")
expect(lm).to receive(:sshable).and_return(sshable)
expect(lm).to receive(:vm_host).and_return(vm_host)
expect { lm.start }.to exit({"msg" => "learned network information"})
end
end
describe "#parse_ip_addr_j" do
it "crashes if more than one interface provided" do
expect {
lm.parse_ip_addr_j(<<JSON)
[
{
"ifindex": 1
},
{
"ifindex": 2
}
]
JSON
}.to raise_error RuntimeError, "only one one interface supported"
end
it "crashes if more than one global unique address prefix is provided" do
expect {
lm.parse_ip_addr_j(<<JSON)
[
{
"ifindex": 2,
"addr_info": [
{
"local": "2a01:4f8:173:1ed3::2",
"prefixlen": 64
},
{
"local": "2a01:4f8:173:1ed3::3",
"prefixlen": 64
}
]
}
]
JSON
}.to raise_error RuntimeError, "only one global unique address prefix supported on interface"
end
end
end