Skip to content

Commit 7a74ff8

Browse files
author
Szymon Mentel
committed
Refactor system tests run with real dobby server
NOTE: should_publish_flow_path is still not passing.
1 parent 24b2872 commit 7a74ff8

3 files changed

Lines changed: 244 additions & 46 deletions

File tree

src/dofl_identifier.erl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ flow_table(DatapahtId, {_Matches, _Actions, Opts}) ->
4949
{skip, []}
5050
end
5151
end,
52-
dby:search(TableIdFun, [], [], [breadth, {max_depth, 1}]).
52+
dby:search(TableIdFun, [], DatapahtId, [breadth, {max_depth, 1}]).
5353

5454
%%%=============================================================================
5555
%%% Internal functions
@@ -64,11 +64,12 @@ table_found(IdMetadataInfo, TableNo) ->
6464
end.
6565

6666
get_metadata_value(Key, Metadatainfo) ->
67-
KeyMap = maps:get(atom_to_binary(Key, utf8), Metadatainfo),
68-
Value = maps:get(value, KeyMap),
69-
case is_binary(Value) of
70-
true ->
71-
binary_to_atom(Value, utf8);
72-
_ ->
73-
Value
67+
KeyMap = maps:get(atom_to_binary(Key, utf8), Metadatainfo, undefined),
68+
case KeyMap =/= undefined andalso maps:get(value, KeyMap) of
69+
false ->
70+
undefined;
71+
V when is_binary(V) ->
72+
binary_to_atom(V, utf8);
73+
V ->
74+
V
7475
end.

test/dofl_with_server_SUITE.erl

Lines changed: 95 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ suite() ->
2525
[{timetrap,{minutes,10}}].
2626

2727
init_per_suite(Config) ->
28-
mock_flow_table_identifiers(),
2928
start_applications(),
3029
case is_dobby_server_running() of
3130
false ->
@@ -35,8 +34,22 @@ init_per_suite(Config) ->
3534
Config
3635
end.
3736

37+
init_per_testcase(should_find_flow_table_identifers, Config) ->
38+
TopFilename = ?config(data_dir, Config) ++ "topo.json",
39+
dby_bulk:import(json0, TopFilename),
40+
Config;
41+
init_per_testcase(_, Config) ->
42+
mock_flow_table_identifiers(),
43+
Config.
44+
45+
end_per_testcase(_, Config) ->
46+
meck:unload(),
47+
Config.
48+
3849
all() ->
39-
[should_publish_net_flow].
50+
[should_publish_net_flow,
51+
should_find_flow_table_identifers,
52+
should_publish_flow_path].
4053

4154
%%%=============================================================================
4255
%%% Testcases
@@ -45,19 +58,43 @@ all() ->
4558
should_publish_net_flow(_Config) ->
4659
%% GIVEN
4760
FlowPath = dofl_test_utils:flow_path(),
48-
FlowPathIds = dofl_test_utils:flow_path_to_identifiers(FlowPath),
4961
publish_endpoints(),
5062

5163
%% WHEN
5264
{ok, NetFlowId} = dobby_oflib:publish_new_flow(?PUBLISHER_ID, ?SRC_EP, ?DST_EP, FlowPath),
53-
Expected = lists:flatten(
54-
[?SRC_EP, NetFlowId, FlowPathIds, NetFlowId, ?DST_EP]),
65+
Expected = [?SRC_EP, NetFlowId, ?DST_EP],
5566

5667
%% %% THEN
57-
Fun = mk_net_flow_with_flow_path_fun(?DST_EP),
68+
Fun = mk_net_flow_fun(?DST_EP),
5869
Actual = dby:search(Fun, [], ?SRC_EP, [depth, {max_depth, 10}, {loop, link}]),
5970
?assertEqual(Expected, Actual).
6071

72+
should_publish_flow_path(_Config) ->
73+
%% GIVEN
74+
FlowPath = dofl_test_utils:flow_path(),
75+
FlowPathIds = dofl_test_utils:flow_path_to_identifiers(FlowPath),
76+
publish_endpoints(),
77+
78+
%% WHEN
79+
{ok, NetFlowId} = dobby_oflib:publish_new_flow(?PUBLISHER_ID, ?SRC_EP, ?DST_EP, FlowPath),
80+
Expected = lists:flatten([NetFlowId, FlowPathIds, NetFlowId]),
81+
82+
%% %% THEN
83+
Fun = mk_flow_path_fun(NetFlowId),
84+
Actual = dby:search(Fun, [], NetFlowId, [breadth, {max_depth, 10}, {loop, link}]),
85+
?assertEqual(Expected, Actual).
86+
87+
should_find_flow_table_identifers(_Config) ->
88+
%% GIVEN
89+
Dpid = <<"OFS1">>,
90+
FlowMod = {_Matches = [], _Actions = [], [{table_id, 0}]},
91+
92+
%% WHEN
93+
Id = dofl_identifier:flow_table(Dpid, FlowMod),
94+
95+
%% THEN
96+
?assertEqual(<<"OFS1-table-0">>, Id).
97+
6198
%%%=============================================================================
6299
%%% Internal functions
63100
%%%=============================================================================
@@ -82,45 +119,65 @@ publish_endpoints() ->
82119
?PUBLISHER_ID, {EP, [{<<"type">>, <<"endpoint">>}]}, [persistent])
83120
|| EP <- [?SRC_EP, ?DST_EP]].
84121

85-
mk_net_flow_with_flow_path_fun(DstEndpoint) ->
122+
mk_net_flow_fun(DstEndpoint) ->
86123
fun(Identifier, _IdMetadataInfo, [], _) ->
87-
{continue, [allowed_transitions(init, []), [Identifier]]};
88-
(Identifier, IdMetadataInfo, [{_, PrevIdMetadataInfo, _} | _], Acc) ->
89-
[AllowedT, IdentifiersAcc] = Acc,
90-
T = transition(PrevIdMetadataInfo, IdMetadataInfo),
91-
case is_transition_allowed(T, AllowedT) of
124+
{continue, {net_flow_next_trasitions(init), [Identifier]}};
125+
(Identifier, IdMetadataInfo, [PrevPathElement | _], Acc) ->
126+
{NextTs, IdAcc} = Acc,
127+
T = transition(PrevPathElement, IdMetadataInfo),
128+
case transition_allowed(T, NextTs) of
129+
true when Identifier =:= DstEndpoint ->
130+
{stop, lists:reverse([Identifier | IdAcc])};
131+
true ->
132+
{continue, {net_flow_next_trasitions(IdMetadataInfo),
133+
[Identifier | IdAcc]}};
92134
false ->
93-
{skip, Acc};
94-
true when Identifier == DstEndpoint ->
95-
{stop, [Identifier | IdentifiersAcc]};
135+
{skip, Acc}
136+
end
137+
end.
138+
139+
mk_flow_path_fun(NetFlowId) ->
140+
fun(Identifier, _IdMetadataInfo, [], _) ->
141+
{continue, {flow_path_next_trasitions(init), [Identifier]}};
142+
(Identifier, IdMetadataInfo, [PrevPathElement | _], Acc) ->
143+
{NextTs, IdAcc} = Acc,
144+
T = transition(PrevPathElement, IdMetadataInfo),
145+
case transition_allowed(T, NextTs) of
146+
true when Identifier =:= NetFlowId ->
147+
{stop, lists:reverse([Identifier | IdAcc])};
96148
true ->
97-
NewAllowedT = allowed_transitions(T, AllowedT),
98-
{continue, [NewAllowedT, [Identifier | IdentifiersAcc]]}
149+
{continue, {flow_path_next_trasitions(IdMetadataInfo),
150+
[Identifier | IdAcc]}};
151+
false ->
152+
{skip, Acc}
99153
end
100154
end.
101155

102-
transition(PrevIdMetadataInfo, IdMetadataInfo) ->
103-
[PrevT, T] = [begin
104-
TypeMap = maps:get(<<"type">>, MetadataInfo),
105-
maps:get(<<"value">>, TypeMap)
106-
end || MetadataInfo <- [PrevIdMetadataInfo, IdMetadataInfo]],
107-
{binary_to_atom(PrevT, utf8), binary_to_atom(T, utf8)}.
108-
109-
is_transition_allowed(Transition, AllowedTransitions) ->
110-
lists:member(Transition, AllowedTransitions).
111-
112-
allowed_transitions({endpoint, of_net_flow}, _CurrentAllowedT) ->
113-
[{of_net_flow, of_flow_mod},
114-
{of_flow_mod, of_flow_mod},
115-
{of_flow_mod, of_net_flow}];
116-
allowed_transitions({of_flow_mod, net_flow}, _CurrentAllowedT) ->
117-
[{net_flow, endpoint}];
118-
allowed_transitions(init, _CurrentAllowedT) ->
119-
[{endpoint, of_net_flow}];
120-
allowed_transitions(_, CurrentAllowedT) ->
121-
CurrentAllowedT.
156+
net_flow_next_trasitions(init) ->
157+
[{ep_to_nf, of_net_flow}];
158+
net_flow_next_trasitions(#{<<"type">> := IdType}) ->
159+
net_flow_next_trasitions(binary_to_atom(maps:get(value, IdType), utf8));
160+
net_flow_next_trasitions(of_net_flow) ->
161+
[{ep_to_nf, endpoint}].
162+
163+
flow_path_next_trasitions(init) ->
164+
[{LinkT, of_flow_mod} || LinkT <- [of_path_starts_at, of_path_ends_at]];
165+
flow_path_next_trasitions(#{<<"type">> := IdType}) ->
166+
flow_path_next_trasitions(binary_to_atom(maps:get(value, IdType), utf8));
167+
flow_path_next_trasitions(of_flow_mod) ->
168+
[{of_path_forwards_to, of_flow_mod} |
169+
[{LinkT, of_net_flow} || LinkT <- [of_path_starts_at, of_path_ends_at]]].
170+
171+
transition_allowed(T, AllowedTs) ->
172+
lists:member(T, AllowedTs).
173+
174+
transition({_, _, #{<<"type">> := LinkType}}, #{<<"type">> := IdType}) ->
175+
F = fun(T) -> binary_to_atom(maps:get(value, T), utf8) end,
176+
{F(LinkType), F(IdType)};
177+
transition(_, _) ->
178+
unknown.
122179

123180
trace_dby_publish() ->
124181
{module, M} = code:ensure_loaded(M = dby),
125182
ct:pal("Matched traces: ~p~n",
126-
[recon_trace:calls({dby, publish, '_'}, 10, [{pid, all}])]).
183+
[recon_trace:calls({dby, publish, '_'}, 20, [{pid, all}])]).
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
[
2+
{
3+
"identifier": "EP1",
4+
"metadata": { "type": "endpoint" }
5+
},
6+
{
7+
"identifier": "OFS1",
8+
"metadata": {
9+
"type": "of_switch",
10+
"datapath_id": "00:00:00:00:00:01:00:01"
11+
}
12+
},
13+
{
14+
"identifier": "OFS1-table-0",
15+
"metadata": {
16+
"type": "of_flow_table",
17+
"table_no": 0
18+
}
19+
},
20+
{
21+
"identifier": "OFS1/OFP1",
22+
"metadata": { "type": "of_port" }
23+
},
24+
{
25+
"identifier": "OFS1/OFP2",
26+
"metadata": { "type": "of_port" }
27+
},
28+
{
29+
"link": ["EP1", "OFS1/OFP1"],
30+
"metadata": { "type": "connected_to" }
31+
},
32+
{
33+
"link": ["OFS1/OFP1", "OFS1"],
34+
"metadata": { "type": "port_of" }
35+
},
36+
{
37+
"link": ["OFS1/OFP2", "OFS1"],
38+
"metadata": { "type": "port_of" }
39+
},
40+
{
41+
"identifier": "OFS2",
42+
"metadata": {
43+
"type": "of_switch",
44+
"datapath_id": "00:00:00:00:00:01:00:02"
45+
}
46+
},
47+
{
48+
"identifier": "OFS2-table-0",
49+
"metadata": {
50+
"type": "of_flow_table",
51+
"table_no": 0
52+
}
53+
},
54+
{
55+
"identifier": "OFS2/OFP1",
56+
"metadata": { "type": "of_port" }
57+
},
58+
{
59+
"identifier": "OFS2/OFP2",
60+
"metadata": { "type": "of_port" }
61+
},
62+
{
63+
"identifier": "OFS2/OFP3",
64+
"metadata": { "type": "of_port" }
65+
},
66+
{
67+
"link": ["OFS2/OFP1", "OFS2"],
68+
"metadata": { "type": "port_of" }
69+
},
70+
{
71+
"link": ["OFS2/OFP2", "OFS2"],
72+
"metadata": { "type": "port_of" }
73+
},
74+
{
75+
"link": ["OFS2/OFP3", "OFS2"],
76+
"metadata": { "type": "port_of" }
77+
},
78+
{
79+
"link": ["OFS1/OFP2", "OFS2/OFP2"],
80+
"metadata": { "type": "connected_to" }
81+
},
82+
{
83+
"identifier": "EP2",
84+
"metadata": { "type": "endpoint" }
85+
},
86+
{
87+
"link": ["EP2", "OFS2/OFP1"],
88+
"metadata": { "type": "connected_to" }
89+
},
90+
{
91+
"identifier": "OFS3",
92+
"metadata": {
93+
"type": "of_switch",
94+
"datapath_id": "00:00:00:00:00:01:00:03"
95+
}
96+
},
97+
{
98+
"identifier": "OFS3-table-0",
99+
"metadata": {
100+
"type": "of_flow_table",
101+
"table_no": 0
102+
}
103+
},
104+
{
105+
"identifier": "OFS3/OFP1",
106+
"metadata": { "type": "of_port" }
107+
},
108+
{
109+
"identifier": "OFS3/OFP2",
110+
"metadata": { "type": "of_port" }
111+
},
112+
{
113+
"link": ["OFS3/OFP1", "OFS3"],
114+
"metadata": { "type": "port_of" }
115+
},
116+
{
117+
"link": ["OFS3/OFP2", "OFS3"],
118+
"metadata": { "type": "port_of" }
119+
},
120+
{
121+
"link": ["OFS2/OFP3", "OFS3/OFP2"],
122+
"metadata": { "type": "connected_to" }
123+
},
124+
{
125+
"identifier": "EP3",
126+
"metadata": { "type": "endpoint" }
127+
},
128+
{
129+
"link": ["EP3", "OFS3/OFP1"],
130+
"metadata": { "type": "connected_to" }
131+
},
132+
{
133+
"link": ["OFS1", "OFS1-table-0"],
134+
"metadata": { "type": "of_resource" }
135+
},
136+
{
137+
"link": ["OFS2", "OFS2-table-0"],
138+
"metadata": { "type": "of_resource" }
139+
}
140+
]

0 commit comments

Comments
 (0)