Add SDWAN build and test script
[icn.git] / sdwan / test / sdwan_verifier.sh
1 #!/bin/bash
2 # SPDX-license-identifier: Apache-2.0
3 ##############################################################################
4 # Copyright (c) 2018
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10
11 set -o errexit
12 set -o nounset
13 set -o pipefail
14
15 sdwan_pod_name=sdwan-ovn-pod
16 ovn_pod_name=ovn-pod
17 wan_interface=net0
18
19 function login {
20     login_url=http://$1/cgi-bin/luci/
21     echo $(wget -S --spider --post-data "luci_username=root&luci_password=" $login_url 2>&1 | grep sysauth= | sed -r 's/.*sysauth=([^;]+);.*/\1/')
22 }
23
24 function disable_ping {
25     command_url=http://$2/cgi-bin/luci/admin/config/command
26     command="uci set firewall.@rule[1].target='REJECT';fw3 reload"
27     echo $(wget -S --spider --header="Cookie:sysauth=$1" --post-data "command=$command" $command_url 2>&1)
28 }
29
30 function enable_ping {
31     command_url=http://$2/cgi-bin/luci/admin/config/command
32     command="uci set firewall.@rule[1].target='ACCEPT';fw3 reload"
33     echo $(wget -S --spider --header="Cookie:sysauth=$1" --post-data "command=$command" $command_url 2>&1)
34 }
35
36 function wait_for_pod {
37     status_phase=""
38     while [[ "$status_phase" != "Running" ]]; do
39         new_phase="$(kubectl get pods -o wide | grep ^$1 | awk '{print $3}')"
40         if [[ "$new_phase" != "$status_phase" ]]; then
41             status_phase="$new_phase"
42         fi
43         if [[ "$new_phase" == "Err"* ]]; then
44             exit 1
45         fi
46         sleep 2
47     done
48 }
49
50 function wait_for_pod_namespace {
51     status_phase=""
52     while [[ "$status_phase" != "Running" ]]; do
53         new_phase="$(kubectl get pods -o wide -n $2 | grep ^$1 | awk '{print $3}')"
54         if [[ "$new_phase" != "$status_phase" ]]; then
55             status_phase="$new_phase"
56         fi
57         if [[ "$new_phase" == "Err"* ]]; then
58             exit 1
59         fi
60         sleep 2
61     done
62 }
63
64 echo "Waiting for multus CNI to be ready ..."
65 wait_for_pod_namespace kube-multus-ds kube-system
66
67 echo "Create pods ..."
68 kubectl apply -f ovn-pod.yml
69 kubectl apply -f sdwan-openwrt-ovn.yml
70
71 echo "Waiting for pods to be ready ..."
72 wait_for_pod $ovn_pod_name
73 wait_for_pod $sdwan_pod_name
74 echo "* Create pods success"
75
76 sdwan_pod_ip=$(kubectl get pods -o wide | grep ^$sdwan_pod_name | awk '{print $6}')
77 ovn_pod_ip=$(kubectl get pods -o wide | grep ^$ovn_pod_name | awk '{print $6}')
78 echo "SDWAN pod ip:"$sdwan_pod_ip
79 echo "OVN pod ip:"$ovn_pod_ip
80
81 echo "Login to sdwan ..."
82 security_token=""
83 while [[ "$security_token" == "" ]]; do
84     echo "Get Security Token ..."
85     security_token=$(login $sdwan_pod_ip)
86     sleep 2
87 done
88 echo "* Security Token: "$security_token
89
90 kubectl exec $sdwan_pod_name ifconfig
91
92 sdwan_pod_wan_ip=$(kubectl exec $sdwan_pod_name ifconfig $wan_interface  | awk '/inet/{print $2}' | cut -f2 -d ":" | awk 'NR==1 {print $1}')
93 echo "Verify ping is work through wan interface between $sdwan_pod_name and $ovn_pod_name"
94 ping_result=$(kubectl exec $ovn_pod_name -- ping -c 3 $sdwan_pod_wan_ip)
95 if [[ $ping_result == *", 0% packet loss"* ]]; then
96     echo "* Ping is work through wan interface"
97 else
98     echo "* Test failed!"
99     exit 1
100 fi
101
102 echo "Disable ping rule of wan interface ..."
103 ret=$(disable_ping $security_token $sdwan_pod_ip)
104
105 echo "Verify ping is not work through wan interface after ping rule disabled"
106 ping_result=$(kubectl exec $ovn_pod_name -- ping -c 3 $sdwan_pod_wan_ip 2>&1 || true)
107 if [[ $ping_result == *", 100% packet loss"* ]]; then
108     echo "* Ping is disabled"
109 else
110     echo "* Test failed!"
111     exit 1
112 fi
113
114 echo "Enable ping rule of wan interface ..."
115 ret=$(enable_ping $security_token $sdwan_pod_ip)
116
117 echo "Verify ping is work through wan interface after ping rule enabled"
118 ping_result=$(kubectl exec $ovn_pod_name -- ping -c 3 $sdwan_pod_wan_ip)
119 if [[ $ping_result == *", 0% packet loss"* ]]; then
120     echo "* Ping is enabled"
121 else
122     echo "* Test failed!"
123     exit 1
124 fi
125
126 echo "Clear pods ..."
127 kubectl delete pod $ovn_pod_name
128 kubectl delete -f sdwan-openwrt-ovn.yml
129
130 echo "Test Completed!"