fea: tests: add switch ut.
Some checks failed
Coverage CI / build (push) Has been cancelled
CodeQL / Analyze (go) (push) Has been cancelled
Ubuntu CI / build (push) Has been cancelled

This commit is contained in:
Daniel Ding
2025-07-25 18:56:57 +08:00
parent b19089559e
commit ae0be2e747
5 changed files with 124 additions and 18 deletions

View File

@@ -1,13 +1,15 @@
# OpenLAN Access UT.
source $PWD/macro.ut
source $PWD/macro.sh
function setup() {
setup_net() {
docker network inspect net1 || {
docker network create net1 \
--driver=bridge --subnet=172.255.0.0/24 --gateway=172.255.0.1
}
}
setup_sw1() {
mkdir -p /opt/openlan/sw1
mkdir -p /opt/openlan/sw1/etc/openlan/switch
cat > /opt/openlan/sw1/etc/openlan/switch/switch.yaml <<EOF
@@ -29,7 +31,9 @@ EOF
# Add users
docker exec sw1 openlan user add --name t1@example --password 123456
docker exec sw1 openlan user add --name t2@example --password 123457
}
setup_ac1() {
mkdir -p /opt/openlan/sw1/etc/openlan/access
# Start access: ac1
cat > /opt/openlan/sw1/etc/openlan/access/t1.yaml <<EOF
@@ -48,6 +52,9 @@ EOF
--name sw1.ac1 $IMAGE /usr/bin/openlan-access -conf /etc/openlan/access/t1.yaml
wait "docker logs -f sw1.ac1" Worker.OnSuccess 30
}
setup_ac2() {
# Start access: ac2
cat > /opt/openlan/sw1/etc/openlan/access/t2.yaml <<EOF
protocol: tcp
@@ -65,12 +72,22 @@ EOF
--name sw1.ac2 $IMAGE /usr/bin/openlan-access -conf /etc/openlan/access/t2.yaml
wait "docker logs -f sw1.ac2" Worker.OnSuccess
}
ping() {
wait "docker exec sw1.ac1 ping -c 3 172.11.0.1" "3 received" 5
wait "docker exec sw1.ac1 ping -c 3 172.11.0.12" "3 received" 5
}
function cleanup() {
setup() {
setup_net
setup_sw1
setup_ac1
setup_ac2
ping
}
cleanup() {
# Stop containd
docker stop sw1.ac1
docker stop sw1.ac2
@@ -80,8 +97,4 @@ function cleanup() {
rm -rvf /opt/openlan/sw1
}
setup
if [[ $PAUSE == true ]]; then
pause
fi
cleanup
main

View File

@@ -5,11 +5,11 @@ export ARCH=amd64
export VERSION=unknown
## functions
function version() {
version() {
$PWD/../dist/version.sh
}
function flush() {
flush() {
local out=$1
while IFS= read -r line; do
@@ -17,7 +17,7 @@ function flush() {
done
}
function wait() {
wait() {
set +x
local cmd=$1; local match=$2
local count=$3; local code=1
@@ -52,10 +52,18 @@ function wait() {
return $code
}
function pause() {
pause() {
echo "Press ENTER to continue: "
read
}
export VERSION=$(version)
export IMAGE="luscis/openlan:$VERSION.$ARCH.deb"
export IMAGE="luscis/openlan:$VERSION.$ARCH.deb"
main() {
setup
if [[ $PAUSE == true ]]; then
pause
fi
cleanup
}

View File

@@ -4,7 +4,7 @@ set -ex
pushd $(dirname $0)
source access.ut
source switch.ut
source access.sh
source switch.sh
popd

88
tests/switch.sh Executable file
View File

@@ -0,0 +1,88 @@
# OpenLAN Access UT.
source $PWD/macro.sh
network_name=net1
setup_net() {
docker network inspect $network_name|| {
docker network create $network_name\
--driver=bridge --subnet=172.255.0.0/24 --gateway=172.255.0.1
}
}
setup_sw1() {
local name=sw1
local address=172.255.0.2
mkdir -p /opt/openlan/$name
mkdir -p /opt/openlan/$name/etc/openlan/switch
cat > /opt/openlan/$name/etc/openlan/switch/switch.yaml <<EOF
protocol: tcp
crypt:
algorithm: aes-128
secret: ea64d5b0c96c
EOF
# Start switch:
docker run -d --rm --privileged --network $network_name --ip $address \
--volume /opt/openlan/$name/etc/openlan:/etc/openlan \
--name $name $IMAGE /usr/bin/openlan-switch -conf:dir /etc/openlan/switch
wait "docker logs -f $name" Http.Start 30
# Add a network.
docker exec $name openlan network add --name example --address 172.11.0.1/24
# Add users
docker exec $name openlan user add --name t1@example --password 123456
}
setup_sw2() {
local name=sw2
local address=172.255.0.3
mkdir -p /opt/openlan/$name
mkdir -p /opt/openlan/$name/etc/openlan/switch
cat > /opt/openlan/$name/etc/openlan/switch/switch.yaml <<EOF
protocol: tcp
crypt:
algorithm: aes-128
secret: ea64d5b0c96c
EOF
# Start switch:
docker run -d --rm --privileged --network $network_name --ip $address \
--volume /opt/openlan/$name/etc/openlan:/etc/openlan \
--name $name $IMAGE /usr/bin/openlan-switch -conf:dir /etc/openlan/switch
wait "docker logs -f $name" Http.Start 30
# Add a network.
docker exec $name openlan network add --name example --address 172.11.0.2/24
# Add a output
docker exec $name openlan network --name example output add --remote 172.255.0.2 --protocol tcp --secret t1:123456 --crypt aes-128:ea64d5b0c96c
docker exec $name openlan network --name example output ls
}
ping() {
wait "docker exec sw2 ping -c 15 172.11.0.1" "bytes from" 15
}
setup() {
setup_net
setup_sw1
setup_sw2
ping
}
cleanup() {
# Stop containd
docker stop sw1
docker stop sw2
# Cleanup files
rm -rvf /opt/openlan/sw1
rm -rvf /opt/openlan/sw2
}
main

View File

@@ -1,3 +0,0 @@
# OpenLAN Switch UT
source $PWD/macro.ut