quality: Add test cases for aggregate stats

This commit is contained in:
Eishun Kondoh 2019-05-02 12:18:09 +09:00
parent b32cfff395
commit 794f2f3fca
2 changed files with 93 additions and 3 deletions

View file

@ -13,9 +13,59 @@ defmodule Openflow.Multipart.Aggregate.Reply do
alias __MODULE__ alias __MODULE__
def ofp_type, do: 18 @type t :: %Reply{
version: 4,
xid: 0..0xFFFFFFFF,
datapath_id: String.t() | nil,
aux_id: 0..0xFF,
flags: [],
packet_count: 0..0xFFFFFFFFFFFFFFFF,
byte_count: 0..0xFFFFFFFFFFFFFFFF,
flow_count: 0..0xFFFFFFFF
}
def read(<<packet_count::64, byte_count::64, flow_count::32, _::size(4)-unit(8)>>) do @spec ofp_type() :: 19
%Reply{packet_count: packet_count, byte_count: byte_count, flow_count: flow_count} def ofp_type, do: 19
@spec new(
xid: 0..0xFFFFFFFF,
flags: [],
packet_count: 0..0xFFFFFFFFFFFFFFFF,
byte_count: 0..0xFFFFFFFFFFFFFFFF,
flow_count: 0..0xFFFFFFFF
) :: t()
def new(options \\ []) do
%Reply{
xid: options[:xid] || 0,
flags: options[:flags] || [],
packet_count: options[:packet_count] || 0,
byte_count: options[:byte_count] || 0,
flow_count: options[:flow_count] || 0
}
end
@spec to_binary(t()) :: <<_::192>>
def to_binary(aggregate) do
<<
Openflow.Multipart.Reply.header(aggregate)::bytes,
aggregate.packet_count::64,
aggregate.byte_count::64,
aggregate.flow_count::32,
0::size(4)-unit(8)
>>
end
@spec read(<<_::192>>) :: t()
def read(<<
packet_count::64,
byte_count::64,
flow_count::32,
_::size(4)-unit(8)
>>) do
%Reply{
packet_count: packet_count,
byte_count: byte_count,
flow_count: flow_count
}
end end
end end

View file

@ -0,0 +1,40 @@
defmodule OfpAggregateTest do
use ExUnit.Case
describe "Openflow.Multipart.Aggregate.Request" do
test "with default values" do
aggregate =
%Openflow.Multipart.Aggregate.Request{}
|> Map.to_list()
|> Openflow.Multipart.Aggregate.Request.new()
|> Openflow.to_binary()
|> Openflow.read()
|> Kernel.elem(1)
assert aggregate.xid == 0
assert aggregate.table_id == :all
assert aggregate.out_port == :any
assert aggregate.out_group == :any
assert aggregate.cookie == 0
assert aggregate.cookie_mask == 0
assert aggregate.match == []
end
end
describe "Openflow.Multipart.Aggregate.Reply" do
test "with default values" do
aggregate =
%Openflow.Multipart.Aggregate.Reply{}
|> Map.to_list()
|> Openflow.Multipart.Aggregate.Reply.new()
|> Openflow.to_binary()
|> Openflow.read()
|> Kernel.elem(1)
assert aggregate.xid == 0
assert aggregate.packet_count == 0
assert aggregate.byte_count == 0
assert aggregate.flow_count == 0
end
end
end