quality: Add test cases for openflow actions

This commit is contained in:
Eishun Kondoh 2019-04-11 23:52:24 +09:00
parent 92bce907c0
commit b8255f3f81
3 changed files with 113 additions and 10 deletions

View file

@ -13,12 +13,28 @@ defmodule Openflow.Action.NxFlowSpecMatch do
alias __MODULE__ alias __MODULE__
def new(options) do @type t :: %NxFlowSpecMatch{
dst = options[:dst] src: atom(),
dst: atom(),
n_bits: non_neg_integer(),
src_offset: non_neg_integer(),
dst_offset: non_neg_integer()
}
@spec new(
src: atom(),
dst: atom(),
n_bits: non_neg_integer(),
src_offset: non_neg_integer(),
dst_offset: non_neg_integer()
) :: t()
def new(options \\ []) do
dst = options[:dst] || raise ":dst must be specified"
src = options[:src] || raise ":src must be specified"
n_bits = options[:n_bits] || Openflow.Match.Field.n_bits_of(dst) n_bits = options[:n_bits] || Openflow.Match.Field.n_bits_of(dst)
%NxFlowSpecMatch{ %NxFlowSpecMatch{
src: options[:src], src: src,
dst: dst, dst: dst,
n_bits: n_bits, n_bits: n_bits,
src_offset: options[:src_offset] || 0, src_offset: options[:src_offset] || 0,
@ -26,6 +42,7 @@ defmodule Openflow.Action.NxFlowSpecMatch do
} }
end end
@spec to_binary(t()) :: binary()
def to_binary(%NxFlowSpecMatch{} = fsm) do def to_binary(%NxFlowSpecMatch{} = fsm) do
%NxFlowSpecMatch{ %NxFlowSpecMatch{
dst: dst_field, dst: dst_field,
@ -48,6 +65,7 @@ defmodule Openflow.Action.NxFlowSpecMatch do
end end
end end
@spec read(binary()) :: {t(), binary()}
def read( def read(
<<_::2, @learn_src_field::1, @learn_dst::2, n_bits::11, src_bin::4-bytes, src_ofs::16, <<_::2, @learn_src_field::1, @learn_dst::2, n_bits::11, src_bin::4-bytes, src_ofs::16,
dst_bin::4-bytes, dst_ofs::16, rest::bitstring>> dst_bin::4-bytes, dst_ofs::16, rest::bitstring>>

View file

@ -10,23 +10,42 @@ defmodule Openflow.Action.NxFlowSpecOutput do
alias __MODULE__ alias __MODULE__
def new(options) do @type t :: %NxFlowSpecOutput{
src = options[:src] n_bits: non_neg_integer(),
src: atom(),
src_offset: non_neg_integer()
}
@spec new(src: atom(), n_bits: non_neg_integer(), src_offset: non_neg_integer()) :: t()
def new(options \\ []) do
src = options[:src] || raise ":src must be specified"
n_bits = options[:n_bits] || Openflow.Match.Field.n_bits_of(src) n_bits = options[:n_bits] || Openflow.Match.Field.n_bits_of(src)
%NxFlowSpecOutput{n_bits: n_bits, src: src, src_offset: options[:src_offset] || 0} %NxFlowSpecOutput{n_bits: n_bits, src: src, src_offset: options[:src_offset] || 0}
end end
def to_binary(%NxFlowSpecOutput{n_bits: n_bits, src: src, src_offset: src_ofs}) do @spec to_binary(t()) :: binary()
src_bin = Openflow.Match.codec_header(src) def to_binary(%NxFlowSpecOutput{} = flow_spec) do
<<0::2, @learn_src_field::1, @learn_dst::2, n_bits::11, src_bin::4-bytes, src_ofs::16>> <<
0::2,
@learn_src_field::1,
@learn_dst::2,
flow_spec.n_bits::11,
Openflow.Match.codec_header(flow_spec.src)::4-bytes,
flow_spec.src_offset::16
>>
end end
@spec read(binary()) :: {t(), binary()}
def read( def read(
<<0::2, @learn_src_field::1, @learn_dst::2, n_bits::11, src_bin::4-bytes, src_ofs::16, <<0::2, @learn_src_field::1, @learn_dst::2, n_bits::11, src_bin::4-bytes, src_ofs::16,
rest::bitstring>> rest::bitstring>>
) do ) do
src = Openflow.Match.codec_header(src_bin) flow_spec = %NxFlowSpecOutput{
flow_spec = %NxFlowSpecOutput{n_bits: n_bits, src: src, src_offset: src_ofs} n_bits: n_bits,
src: Openflow.Match.codec_header(src_bin),
src_offset: src_ofs
}
{flow_spec, rest} {flow_spec, rest}
end end
end end

View file

@ -733,6 +733,72 @@ defmodule OfpActionTest do
end end
end end
describe "Openflow.Action.NxFlowSpecOutput" do
test "with src = :in_port" do
flow_spec = Openflow.Action.NxFlowSpecOutput.new(src: :in_port)
flow_spec
|> Openflow.Action.NxFlowSpec.to_binary()
|> Openflow.Action.NxFlowSpec.read()
|> Enum.at(0)
|> Kernel.==(flow_spec)
|> assert()
end
test "with no options" do
assert_raise RuntimeError, ":src must be specified", fn ->
Openflow.Action.NxFlowSpecOutput.new()
end
end
end
describe "Openflow.Action.NxFlowSpecMatch" do
test "with src = 0xdeadbeef and dst: :reg0" do
flow_spec = Openflow.Action.NxFlowSpecMatch.new(src: 0xDEADBEEF, dst: :reg0)
flow_spec
|> Openflow.Action.NxFlowSpec.to_binary()
|> Openflow.Action.NxFlowSpec.read()
|> Enum.at(0)
|> Kernel.==(flow_spec)
|> assert()
end
test "with src = :in_port and dst: :reg0" do
flow_spec =
Openflow.Action.NxFlowSpecMatch.new(
src: :in_port,
dst: :reg0,
n_bits: 16
)
flow_spec
|> Openflow.Action.NxFlowSpec.to_binary()
|> Openflow.Action.NxFlowSpec.read()
|> Enum.at(0)
|> Kernel.==(flow_spec)
|> assert()
end
test "with no option" do
assert_raise RuntimeError, ":dst must be specified", fn ->
Openflow.Action.NxFlowSpecMatch.new()
end
end
test "with no dst option" do
assert_raise RuntimeError, ":dst must be specified", fn ->
Openflow.Action.NxFlowSpecMatch.new(src: :in_port)
end
end
test "with no src option" do
assert_raise RuntimeError, ":src must be specified", fn ->
Openflow.Action.NxFlowSpecMatch.new(dst: :reg0)
end
end
end
describe "Openflow.Action.NxMultipath" do describe "Openflow.Action.NxMultipath" do
test "with multipath" do test "with multipath" do
test_file = "test/packet_data/nx_multipath.raw" test_file = "test/packet_data/nx_multipath.raw"