Skip to content

Commit

Permalink
[PoC] Add routing classes
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs authored and mchf committed Mar 21, 2019
1 parent 25c8bb6 commit 735ab2f
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/lib/y2network/interface.rb
@@ -0,0 +1,29 @@
# Copyright (c) [2019] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.
module Y2Network
# Network interface.
class Interface
# @return [String] Device name (eth0, wlan0, etc.)
attr_reader :name

def initialize(name)
@name = name
end
end
end
49 changes: 49 additions & 0 deletions src/lib/y2network/route.rb
@@ -0,0 +1,49 @@
# Copyright (c) [2019] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.
module Y2Network
# This class represents a network route
class Route
# @return [IPAddr]
attr_reader :to
# @return [Symbol] :local, :multicast, etc.
attr_reader :type
# @return [Device]
attr_reader :device
# @return [IPAddr,nil]
attr_reader :source
# @return [IPAddr,nil]
attr_reader :via

def initialize(to, device, via: nil, source: nil, preference: nil, type: :unicast)
@to = to
@device = device
@via = via
@source = source
@type = type
@preference = preference
end

# Determines whether it is the default route or not
#
# @return [Boolean]
def default?
@to.nil?
end
end
end
35 changes: 35 additions & 0 deletions src/lib/y2network/routing_table.rb
@@ -0,0 +1,35 @@
# Copyright (c) [2019] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.
module Y2Network
class RouteTable
attr_reader :routes

# MAIN_TABLE_ID = 254

def initialize
# @id = MAIN_TABLE_ID
@routes = []
end

# @param route [Y2Network::Route] Route to add
def <<(route)
@routes << route
end
end
end
48 changes: 48 additions & 0 deletions test/y2network/route_test.rb
@@ -0,0 +1,48 @@
# Copyright (c) [2019] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require_relative "../test_helper"
require "y2network/route"
require "y2network/interface"
require "ipaddr"

describe Y2Network::Route do
subject(:route) do
described_class.new(to, interface)
end

let(:to) { IPAddr.new("192.168.122.1") }
let(:interface) { Y2Network::Interface.new("eth0") }

describe "#default?" do
context "when it is the default route" do
let(:to) { nil }

it "returns true" do
expect(route.default?).to eq(true)
end
end

context "when it is not the default route" do
it "returns false" do
expect(route.default?).to eq(false)
end
end
end
end

0 comments on commit 735ab2f

Please sign in to comment.