chore: run golang tests

This commit is contained in:
Aveline 2025-05-16 19:53:01 +02:00 committed by GitHub
parent d54568642b
commit fea89a0d23
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 127 additions and 8 deletions

View File

@ -32,6 +32,13 @@ jobs:
- name: Build application - name: Build application
run: | run: |
make build_dev make build_dev
- name: Run tests
run: |
go test ./... -json > testreport.json
- name: Golang Test Report
uses: becheran/go-testreport@v0.3.2
with:
output: "testreport.json"
- name: Upload artifact - name: Upload artifact
uses: actions/upload-artifact@v4 uses: actions/upload-artifact@v4
with: with:

3
.gitignore vendored
View File

@ -1,3 +1,6 @@
bin/* bin/*
static/* static/*
.idea .idea
.DS_Store
device-tests.tar.gz

View File

@ -22,6 +22,29 @@ build_dev: hash_resource
@echo "Building..." @echo "Building..."
GOOS=linux GOARCH=arm GOARM=7 go build -ldflags="$(GO_LDFLAGS) -X $(KVM_PKG_NAME).builtAppVersion=$(VERSION_DEV)" -o bin/jetkvm_app cmd/main.go GOOS=linux GOARCH=arm GOARM=7 go build -ldflags="$(GO_LDFLAGS) -X $(KVM_PKG_NAME).builtAppVersion=$(VERSION_DEV)" -o bin/jetkvm_app cmd/main.go
build_test2json:
GOOS=linux GOARCH=arm GOARM=7 go build -o bin/test2json cmd/test2json
build_dev_test: build_test2json
# collect all directories that contain tests
@echo "Building tests for devices ..."
@rm -rf bin/tests && mkdir -p bin/tests
GOOS=linux GOARCH=arm GOARM=7 \
go test -v \
-ldflags="$(GO_LDFLAGS) -X $(KVM_PKG_NAME).builtAppVersion=$(VERSION_DEV)" \
-c -o bin/tests ./...; \
@cat resource/dev_test.sh > bin/tests/run_all_tests
@for test in bin/tests/*.test; do \
chmod +x $$test; \
base_name=$$(basename $$test); \
echo "runTest ./$$base_name" >> bin/tests/run_all_tests; \
done; \
chmod +x bin/tests/run_all_tests; \
cp bin/test2json bin/tests/; \
chmod +x bin/tests/test2json; \
tar czfv device-tests.tar.gz -C bin/tests .
frontend: frontend:
cd ui && npm ci && npm run build:device cd ui && npm ci && npm run build:device

View File

@ -3,6 +3,19 @@
# Exit immediately if a command exits with a non-zero status # Exit immediately if a command exits with a non-zero status
set -e set -e
C_RST="$(tput sgr0)"
C_ERR="$(tput setaf 1)"
C_OK="$(tput setaf 2)"
C_WARN="$(tput setaf 3)"
C_INFO="$(tput setaf 5)"
msg() { printf '%s%s%s\n' $2 "$1" $C_RST; }
msg_info() { msg "$1" $C_INFO; }
msg_ok() { msg "$1" $C_OK; }
msg_err() { msg "$1" $C_ERR; }
msg_warn() { msg "$1" $C_WARN; }
# Function to display help message # Function to display help message
show_help() { show_help() {
echo "Usage: $0 [options] -r <remote_ip>" echo "Usage: $0 [options] -r <remote_ip>"
@ -12,6 +25,7 @@ show_help() {
echo echo
echo "Optional:" echo "Optional:"
echo " -u, --user <remote_user> Remote username (default: root)" echo " -u, --user <remote_user> Remote username (default: root)"
echo " --run-go-tests Run go tests"
echo " --skip-ui-build Skip frontend/UI build" echo " --skip-ui-build Skip frontend/UI build"
echo " --help Display this help message" echo " --help Display this help message"
echo echo
@ -26,6 +40,7 @@ REMOTE_PATH="/userdata/jetkvm/bin"
SKIP_UI_BUILD=false SKIP_UI_BUILD=false
RESET_USB_HID_DEVICE=false RESET_USB_HID_DEVICE=false
LOG_TRACE_SCOPES="${LOG_TRACE_SCOPES:-jetkvm,cloud,websocket,native,jsonrpc}" LOG_TRACE_SCOPES="${LOG_TRACE_SCOPES:-jetkvm,cloud,websocket,native,jsonrpc}"
RUN_GO_TESTS=false
# Parse command line arguments # Parse command line arguments
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
@ -46,6 +61,10 @@ while [[ $# -gt 0 ]]; do
RESET_USB_HID_DEVICE=true RESET_USB_HID_DEVICE=true
shift shift
;; ;;
--run-go-tests)
RUN_GO_TESTS=true
shift
;;
--help) --help)
show_help show_help
exit 0 exit 0
@ -60,24 +79,41 @@ done
# Verify required parameters # Verify required parameters
if [ -z "$REMOTE_HOST" ]; then if [ -z "$REMOTE_HOST" ]; then
echo "Error: Remote IP is a required parameter" msg_err "Error: Remote IP is a required parameter"
show_help show_help
exit 1
fi fi
# Build the development version on the host # Build the development version on the host
if [ "$SKIP_UI_BUILD" = false ]; then if [ "$SKIP_UI_BUILD" = false ]; then
msg_info "▶ Building frontend"
make frontend make frontend
fi fi
msg_info "▶ Building go binary"
make build_dev make build_dev
# Change directory to the binary output directory if [ "$RUN_GO_TESTS" = true ]; then
cd bin msg_info "▶ Building go tests"
make build_dev_test
msg_info "▶ Copying device-tests.tar.gz to remote host"
ssh "${REMOTE_USER}@${REMOTE_HOST}" "cat > ${REMOTE_PATH}/device-tests.tar.gz" < device-tests.tar.gz
msg_info "▶ Running go tests"
ssh "${REMOTE_USER}@${REMOTE_HOST}" ash << EOF
set -e
cd ${REMOTE_PATH}
tar zxvf device-tests.tar.gz
./run_all_tests -test.v
EOF
fi
# Kill any existing instances of the application # Kill any existing instances of the application
ssh "${REMOTE_USER}@${REMOTE_HOST}" "killall jetkvm_app_debug || true" ssh "${REMOTE_USER}@${REMOTE_HOST}" "killall jetkvm_app_debug || true"
# Copy the binary to the remote host # Copy the binary to the remote host
ssh "${REMOTE_USER}@${REMOTE_HOST}" "cat > ${REMOTE_PATH}/jetkvm_app_debug" < jetkvm_app ssh "${REMOTE_USER}@${REMOTE_HOST}" "cat > ${REMOTE_PATH}/jetkvm_app_debug" < jetkvm_app_debug
if [ "$RESET_USB_HID_DEVICE" = true ]; then if [ "$RESET_USB_HID_DEVICE" = true ]; then
# Remove the old USB gadget configuration # Remove the old USB gadget configuration
@ -106,4 +142,4 @@ chmod +x jetkvm_app_debug
PION_LOG_TRACE=${LOG_TRACE_SCOPES} ./jetkvm_app_debug PION_LOG_TRACE=${LOG_TRACE_SCOPES} ./jetkvm_app_debug
EOF EOF
echo "Deployment complete." echo "Deployment complete."

View File

@ -57,7 +57,22 @@ func TestValidateConfig(t *testing.T) {
} }
} }
func TestValidateIPv4StaticConfigRequired(t *testing.T) { func TestValidateIPv4StaticConfigNetmaskRequiredIfStatic(t *testing.T) {
config := &testNetworkConfig{
IPv4Static: &testIPv4StaticConfig{
Address: null.StringFrom("192.168.1.1"),
Gateway: null.StringFrom("192.168.1.1"),
},
IPv4Mode: null.StringFrom("static"),
}
err := SetDefaultsAndValidate(config)
if err == nil {
t.Fatalf("expected error, got nil")
}
}
func TestValidateIPv4StaticConfigNetmaskNotRequiredIfStatic(t *testing.T) {
config := &testNetworkConfig{ config := &testNetworkConfig{
IPv4Static: &testIPv4StaticConfig{ IPv4Static: &testIPv4StaticConfig{
Address: null.StringFrom("192.168.1.1"), Address: null.StringFrom("192.168.1.1"),
@ -66,8 +81,8 @@ func TestValidateIPv4StaticConfigRequired(t *testing.T) {
} }
err := SetDefaultsAndValidate(config) err := SetDefaultsAndValidate(config)
if err == nil { if err != nil {
t.Fatalf("expected error, got nil") t.Fatalf("expected no error, got %v", err)
} }
} }

35
resource/dev_test.sh Normal file
View File

@ -0,0 +1,35 @@
#!/bin/sh
JSON_OUTPUT=false
if [ "$1" = "-json" ]; then
JSON_OUTPUT=true
shift
fi
ADDITIONAL_ARGS=$@
EXIT_CODE=0
runTest() {
if [ "$JSON_OUTPUT" = true ]; then
./test2json $1 -test.v $ADDITIONAL_ARGS | tee $1.result.json
if [ $? -ne 0 ]; then
EXIT_CODE=1
fi
else
$@
if [ $? -ne 0 ]; then
EXIT_CODE=1
fi
fi
}
function exit_with_code() {
if [ $EXIT_CODE -ne 0 ]; then
printf "\e[0;31m❌ Test failed\e[0m\n"
else
printf "\e[0;32m✅ All tests passed\e[0m\n"
fi
exit $EXIT_CODE
}
trap exit_with_code EXIT