fea: support tests.
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-18 15:58:27 +08:00
parent 154c7b5f7b
commit b8e02e71fa
4 changed files with 161 additions and 0 deletions

87
tests/access.ut Executable file
View File

@@ -0,0 +1,87 @@
# OpenLAN Access UT.
source $PWD/macro.ut
function setup() {
docker network inspect net1 || {
docker network create net1 \
--driver=bridge --subnet=172.255.0.0/24 --gateway=172.255.0.1
}
mkdir -p /opt/openlan/sw1
mkdir -p /opt/openlan/sw1/etc/openlan/switch
cat > /opt/openlan/sw1/etc/openlan/switch/switch.yaml <<EOF
protocol: tcp
crypt:
algorithm: aes-128
secret: ea64d5b0c96c
EOF
# Start switch: sw1
docker run -d --rm --privileged --network net1 --ip 172.255.0.2 \
--volume /opt/openlan/sw1/etc/openlan:/etc/openlan \
--name sw1 $IMAGE /usr/bin/openlan-switch -conf:dir /etc/openlan/switch
wait "docker logs -f sw1" Http.Start 30
# Add a network.
docker exec sw1 openlan network add --name example --address 172.11.0.1/24
# Add users
docker exec sw1 openlan user add --name t1@example --password 123456
docker exec sw1 openlan user add --name t2@example --password 123457
mkdir -p /opt/openlan/sw1/etc/openlan/access
# Start access: ac1
cat > /opt/openlan/sw1/etc/openlan/access/t1.yaml <<EOF
protocol: tcp
crypt:
algorithm: aes-128
secret: ea64d5b0c96c
connection: 172.255.0.2
username: t1@example
password: 123456
interface:
address: 172.11.0.11/24
EOF
docker run -d --rm --privileged --network net1 \
--volume /opt/openlan/sw1/etc/openlan:/etc/openlan \
--name sw1.ac1 $IMAGE /usr/bin/openlan-access -conf /etc/openlan/access/t1.yaml
wait "docker logs -f sw1.ac1" Worker.OnSuccess 30
# Start access: ac2
cat > /opt/openlan/sw1/etc/openlan/access/t2.yaml <<EOF
protocol: tcp
crypt:
algorithm: aes-128
secret: ea64d5b0c96c
connection: 172.255.0.2
username: t2@example
password: 123457
interface:
address: 172.11.0.12/24
EOF
docker run -d --rm --privileged --network net1 \
--volume /opt/openlan/sw1/etc/openlan:/etc/openlan \
--name sw1.ac2 $IMAGE /usr/bin/openlan-access -conf /etc/openlan/access/t2.yaml
wait "docker logs -f sw1.ac2" Worker.OnSuccess
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() {
# Stop containd
docker stop sw1.ac1
docker stop sw1.ac2
docker stop sw1
# Cleanup files
rm -rvf /opt/openlan/sw1
}
setup
if [[ $PAUSE == true ]]; then
pause
fi
cleanup

61
tests/macro.ut Normal file
View File

@@ -0,0 +1,61 @@
## variables
export ARCH=amd64
export VERSION=unknown
## functions
function version() {
$PWD/../dist/version.sh
}
function flush() {
local out=$1
while IFS= read -r line; do
echo "$line" >> $out
done
}
function wait() {
set +x
local cmd=$1; local match=$2
local count=$3; local code=1
local out=/tmp/wait.1
if [[ $count == "" ]]; then
count=3
fi
$cmd 2>&1 | flush $out & disown
local pid=$!
for i in $(seq 1 $count); do
if [ ! -e $out ]; then
sleep 1
continue
fi
if cat $out | grep "$match"; then
code=0; break
fi
if ! ps -p $pid > /dev/null; then
break
fi
sleep 1
done
if ps -p $pid > /dev/null; then
kill $pid;
fi
rm -f $out
set -x
return $code
}
function pause() {
echo "Press ENTER to continue: "
read
}
export VERSION=$(version)
export IMAGE="luscis/openlan:$VERSION.$ARCH.deb"

10
tests/run.sh Executable file
View File

@@ -0,0 +1,10 @@
#!/bin/bash
set -ex
pushd $(dirname $0)
source access.ut
source switch.ut
popd

3
tests/switch.ut Normal file
View File

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