Skip to content

Commit

Permalink
Refactor convenience functions for getting interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
fhunleth committed May 9, 2019
1 parent 16fbb2f commit 9b809ed
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
46 changes: 38 additions & 8 deletions lib/vintage_net.ex
Original file line number Diff line number Diff line change
@@ -1,23 +1,53 @@
defmodule VintageNet do
@moduledoc """
VintageNet configures network interfaces using Linux utilities
`VintageNet` is network configuration library built specifically for [Nerves
Project](https://nerves-project.org) devices. It has the following features:
* Ethernet and WiFi support included. Extendible to other technologies
* Default configurations specified in your Application config
* Runtime updates to configurations are persisted and applied on next boot (can
be disabled)
* Simple subscription to network status change events
* Connect to multiple networks at a time and prioritize which interfaces are
used (Ethernet over WiFi over cellular)
* Internet connection monitoring and failure detection (currently slow and
simplistic)
See [github.com/nerves-networking/vintage_net](https://github.com/nerves-networking/vintage_net) for
more information.
"""
alias VintageNet.{Interface, Persistence, PropertyTable}

@typedoc """
A name for the network interface
Names depend on the device drivers and any software that may rename them.
Typical names on Nerves are:
* "eth0", "eth1", etc. for wired Ethernet interfaces
* "wlan0", etc. for WiFi interfaces
* "ppp0" for cellular modems
* "usb0" for gadget USB virtual Ethernet interfaces
"""
@type ifname :: String.t()

@spec all_interfaces() :: [ifname()]
def all_interfaces() do
for {["interface", ifname, "present"], true} <-
get_by_prefix(["interface"]) do
ifname
end
end

@doc """
Return a list of interface names that have been configured
Return a list of configured interface
"""
@spec get_interfaces() :: [ifname()]
def get_interfaces() do
for {[_interface, ifname | _rest], _value} <-
@spec configured_interfaces() :: [ifname()]
def configured_interfaces() do
for {["interface", ifname, "type"], _value} <-
get_by_prefix(["interface"]) do
ifname
end
|> Enum.uniq()
end

@doc """
Expand Down
2 changes: 1 addition & 1 deletion test/vintage_net/interface_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ defmodule VintageNet.InterfaceTest do
{:ok, raw_config} = VintageNet.Technology.Null.to_raw_config(@ifname)
start_and_configure(raw_config)

assert [@ifname] == VintageNet.get_interfaces()
assert [@ifname] == VintageNet.configured_interfaces()
end)
end

Expand Down
13 changes: 13 additions & 0 deletions test/vintage_net_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ defmodule VintageNetTest do
assert {:error, :type_missing} == VintageNet.configure("eth0", %{})
end

test "interfaces exist" do
interfaces = VintageNet.all_interfaces()

# The loopback interface always exists, so check for it
assert interfaces != []

assert Enum.any?(interfaces, &String.starts_with?(&1, "lo"))
end

test "no interfaces are configured when testing" do
assert [] == VintageNet.configured_interfaces()
end

test "verify system works", context do
# create files here at some tmp place
in_tmp(context.test, fn ->
Expand Down

0 comments on commit 9b809ed

Please sign in to comment.