Move status tracking CR to rsync
[multicloud/k8s.git] / kud / tests / sdwan / test.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 pods to be ready ..."
65 wait_for_pod $ovn_pod_name
66 wait_for_pod $sdwan_pod_name
67 echo "* Create pods success"
68
69 sdwan_pod_ip=$(kubectl get pods -o wide | grep ^$sdwan_pod_name | awk '{print $6}')
70 ovn_pod_ip=$(kubectl get pods -o wide | grep ^$ovn_pod_name | awk '{print $6}')
71 echo "SDWAN pod ip:"$sdwan_pod_ip
72 echo "OVN pod ip:"$ovn_pod_ip
73
74 echo "Login to sdwan ..."
75 security_token=""
76 while [[ "$security_token" == "" ]]; do
77     echo "Get Security Token ..."
78     security_token=$(login $sdwan_pod_ip)
79     sleep 2
80 done
81 echo "* Security Token: "$security_token
82
83 kubectl exec $sdwan_pod_name ifconfig
84
85 sdwan_pod_wan_ip=$(kubectl exec $sdwan_pod_name ifconfig $wan_interface  | awk '/inet/{print $2}' | cut -f2 -d ":" | awk 'NR==1 {print $1}')
86 echo "Verify ping is work through wan interface between $sdwan_pod_name and $ovn_pod_name"
87 ping_result=$(kubectl exec $ovn_pod_name -- ping -c 3 $sdwan_pod_wan_ip)
88 if [[ $ping_result == *", 0% packet loss"* ]]; then
89     echo "* Ping is work through wan interface"
90 else
91     echo "* Test failed!"
92     exit 1
93 fi
94
95 echo "Disable ping rule of wan interface ..."
96 ret=$(disable_ping $security_token $sdwan_pod_ip)
97
98 echo "Verify ping is not work through wan interface after ping rule disabled"
99 ping_result=$(kubectl exec $ovn_pod_name -- ping -c 3 $sdwan_pod_wan_ip 2>&1 || true)
100 if [[ $ping_result == *", 100% packet loss"* ]]; then
101     echo "* Ping is disabled"
102 else
103     echo "* Test failed!"
104     exit 1
105 fi
106
107 echo "Enable ping rule of wan interface ..."
108 ret=$(enable_ping $security_token $sdwan_pod_ip)
109
110 echo "Verify ping is work through wan interface after ping rule enabled"
111 ping_result=$(kubectl exec $ovn_pod_name -- ping -c 3 $sdwan_pod_wan_ip)
112 if [[ $ping_result == *", 0% packet loss"* ]]; then
113     echo "* Ping is enabled"
114 else
115     echo "* Test failed!"
116     exit 1
117 fi
118
119
120 echo "Test Completed!"