|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +describe Facter::Util::Resolvers::Networking::PrimaryInterface do |
| 4 | + subject(:primary_interface) { Facter::Util::Resolvers::Networking::PrimaryInterface } |
| 5 | + |
| 6 | + describe '#read_from_route' do |
| 7 | + before do |
| 8 | + allow(Facter::Core::Execution).to receive(:execute) |
| 9 | + .and_return(load_fixture('route_n_get_default').read) |
| 10 | + end |
| 11 | + |
| 12 | + it 'parses output from `route -n get default`' do |
| 13 | + allow(Facter::Core::Execution).to receive(:which).with('route').and_return('/some/path') |
| 14 | + expect(primary_interface.read_from_route).to eq('net0') |
| 15 | + end |
| 16 | + |
| 17 | + it 'returns nil if route command does not exist' do |
| 18 | + allow(Facter::Core::Execution).to receive(:which).with('route').and_return(nil) |
| 19 | + expect(primary_interface.read_from_route).to be_nil |
| 20 | + end |
| 21 | + end |
| 22 | + |
| 23 | + describe '#read_from_proc_route' do |
| 24 | + before do |
| 25 | + allow(Facter::Util::FileHelper).to receive(:safe_read).with('/proc/net/route', '') \ |
| 26 | + .and_return(load_fixture('proc_net_route')) |
| 27 | + end |
| 28 | + |
| 29 | + it 'parses output /proc/net/route file' do |
| 30 | + expect(primary_interface.read_from_proc_route).to eq('ens160') |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + describe '#read_from_ip_route' do |
| 35 | + before do |
| 36 | + allow(Facter::Core::Execution).to receive(:execute) |
| 37 | + .and_return(load_fixture('ip_route_show_default').read) |
| 38 | + end |
| 39 | + |
| 40 | + it 'parses output from `ip route show default`' do |
| 41 | + allow(Facter::Core::Execution).to receive(:which).with('ip').and_return('/some/path') |
| 42 | + expect(primary_interface.read_from_ip_route).to eq('ens160') |
| 43 | + end |
| 44 | + |
| 45 | + it 'returns nil if route command does not exist' do |
| 46 | + allow(Facter::Core::Execution).to receive(:which).with('ip').and_return(nil) |
| 47 | + expect(primary_interface.read_from_ip_route).to be_nil |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + describe '#find_in_interfaces' do |
| 52 | + let(:interfaces) do |
| 53 | + { 'lo' => |
| 54 | + { bindings: [{ address: '127.0.0.1', netmask: '255.0.0.0', network: '127.0.0.0' }], |
| 55 | + bindings6: [{ |
| 56 | + address: '::1', |
| 57 | + netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', |
| 58 | + network: '::1', |
| 59 | + scope6: 'host' |
| 60 | + }], |
| 61 | + scope6: 'host', |
| 62 | + mtu: 65_536 }, |
| 63 | + 'ens160' => |
| 64 | + { bindings: [{ address: '10.16.124.1', netmask: '255.255.240.0', network: '10.16.112.0' }], |
| 65 | + dhcp: '10.32.22.9', |
| 66 | + bindings6: [{ |
| 67 | + address: 'fe80::250:56ff:fe9a:41fc', |
| 68 | + netmask: 'ffff:ffff:ffff:ffff::', |
| 69 | + network: 'fe80::', |
| 70 | + scope6: 'link' |
| 71 | + }], |
| 72 | + scope6: 'link', |
| 73 | + mac: '00:50:56:9a:41:fc', |
| 74 | + mtu: 1500 } } |
| 75 | + end |
| 76 | + |
| 77 | + it 'parses interfaces to find primary interface' do |
| 78 | + expect(primary_interface.find_in_interfaces(interfaces)).to eq('ens160') |
| 79 | + end |
| 80 | + end |
| 81 | +end |
0 commit comments