Skip to content

Commit 57ee78d

Browse files
Dynmon test
1 parent a039a87 commit 57ee78d

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env bash
2+
3+
function test_fail {
4+
set +e
5+
res=$($@)
6+
local status=$?
7+
set -e
8+
if [ $status -ne 0 ]; then
9+
return 0
10+
else
11+
return 1
12+
fi
13+
}
14+
15+
function create_veth {
16+
for i in `seq 1 $1`;
17+
do
18+
sudo ip netns add ns${i}
19+
sudo ip link add veth${i}_ type veth peer name veth${i}
20+
sudo ip link set veth${i}_ netns ns${i}
21+
sudo ip netns exec ns${i} ip link set dev veth${i}_ up
22+
sudo ip link set dev veth${i} up
23+
sudo ip netns exec ns${i} ifconfig veth${i}_ 10.0.0.${i}/24
24+
done
25+
}
26+
27+
function create_link {
28+
for i in `seq 1 $1`;
29+
do
30+
sudo ip link add link${i}1 type veth peer name link${i}2
31+
sudo ip link set dev link${i}1 up
32+
sudo ip link set dev link${i}2 up
33+
done
34+
}
35+
36+
function delete_veth {
37+
for i in `seq 1 $1`;
38+
do
39+
sudo ip link del veth${i}
40+
sudo ip netns del ns${i}
41+
done
42+
}
43+
44+
function delete_link {
45+
for i in `seq 1 $1`;
46+
do
47+
sudo ip link del link${i}1
48+
done
49+
}

src/services/pcn-dynmon/test/test1.sh

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#! /bin/bash
2+
3+
# include helper.bash file: used to provide some common function across testing scripts
4+
source "${BASH_SOURCE%/*}/helpers.bash"
5+
6+
# function cleanup: is invoked each time script exit (with or without errors)
7+
# please remember to cleanup all entities previously created:
8+
# namespaces, veth, cubes, ..
9+
function cleanup {
10+
set +e
11+
polycubectl dynmon del dm
12+
delete_veth 2
13+
}
14+
trap cleanup EXIT
15+
16+
# Enable verbose output
17+
set -x
18+
19+
# Makes the script exit, at first error
20+
# Errors are thrown by commands returning not 0 value
21+
set -e
22+
23+
DIR=$(dirname "$0")
24+
25+
TYPE="TC"
26+
27+
if [ -n "$1" ]; then
28+
TYPE=$1
29+
fi
30+
31+
# helper.bash function, creates namespaces and veth connected to them
32+
create_veth 2
33+
34+
# create instance of service dynmon
35+
polycubectl dynmon add dm type=$TYPE
36+
polycubectl dm show
37+
38+
# attaching the monitor to veth1
39+
polycubectl attach dm veth1
40+
41+
42+
43+
# injecting a dataplane configuration
44+
curl -H "Content-Type: application/json" "localhost:9000/polycube/v1/dynmon/dm/dataplane" --upload-file $DIR/test_dataplane.json
45+
polycubectl dm show
46+
47+
set +e
48+
ping_result=$(sudo ip netns exec ns1 ping 10.0.0.2 -c 2 -w 2 | grep -Po '[0-9]+(?= +packets transmitted)')
49+
set -e
50+
51+
polycubectl dm metrics show
52+
polycubectl dm open-metrics show
53+
polycubectl dm metrics packets_total show
54+
55+
metric_value=$(polycubectl dm metrics packets_total value show | sed -n "s/^\[\([0-9]*\)\]$/\1/p")
56+
57+
if [ $metric_value -eq 0 ] || [ $metric_value -lt $ping_result ];
58+
then
59+
echo "Error: expected packets_value > 0 and >= $ping_result"
60+
echo "packets_total: $packets_total"
61+
exit 1
62+
else
63+
echo "packets_total: $packets_total"
64+
echo "TEST: OK"
65+
fi;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "Packets counter probe",
3+
"code": "\r\n BPF_ARRAY(PKT_COUNTER, uint64_t, 1);\r\n static __always_inline int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *md) {\r\n unsigned int key = 0;\r\n uint64_t *pkt_counter = PKT_COUNTER.lookup(&key);\r\n if (!pkt_counter){\r\n /*counter map not found !? */\r\n return RX_OK;\r\n }\r\n *pkt_counter+=1;\r\n pcn_log(ctx, LOG_TRACE, \"counter: %d\", *pkt_counter);\r\n return RX_OK;\r\n }",
4+
"metrics": [
5+
{
6+
"name": "packets_total",
7+
"map-name": "PKT_COUNTER",
8+
"open-metrics-metadata": {
9+
"help": "This metric represents the number of packets that has traveled trough this probe.",
10+
"type": "counter",
11+
"labels": []
12+
}
13+
}
14+
]
15+
}

0 commit comments

Comments
 (0)