Fix test data

This commit is contained in:
Katos
2025-12-27 21:36:26 +00:00
committed by GitHub
parent 570ad8fbf8
commit 87c1f0333f
2 changed files with 111 additions and 104 deletions

View File

@@ -31,7 +31,7 @@ api_call() {
"$API_URL$endpoint")
fi
# Validate if the response is valid JSON
# Validate response is proper JSON
if ! echo "$response" | jq '.' > /dev/null 2>&1; then
echo "Invalid API response for $endpoint: $response" >&2
exit 1
@@ -40,17 +40,25 @@ api_call() {
echo "$response"
}
# Function to initialize test data storage
# Function to initialize the test data JSON file
initialize_test_data() {
echo "Initializing test data file: $TEST_DATA_FILE"
echo "Initializing test data JSON file: $TEST_DATA_FILE"
if [ -f "$TEST_DATA_FILE" ]; then
echo "Found existing test data file. Removing..."
echo "Removing existing test data file..."
rm -f "$TEST_DATA_FILE"
fi
echo "{\"users\":[],\"locations\":[],\"labels\":[],\"notifiers\":[],\"items\":[]}" > "$TEST_DATA_FILE"
echo "{\"users\":[],\"locations\":[],\"labels\":[],\"items\":[],\"attachments\":[],\"notifiers\":[]}" > "$TEST_DATA_FILE"
}
# Function to register a user and get token
# Function to add content to JSON data file
add_to_test_data() {
local key=$1
local value=$2
jq --argjson data "$value" ".${key} += [\$data]" "$TEST_DATA_FILE" > "${TEST_DATA_FILE}.tmp" && mv "${TEST_DATA_FILE}.tmp" "$TEST_DATA_FILE"
}
# Register a user and get their auth token
register_user() {
local email=$1
local name=$2
@@ -58,88 +66,88 @@ register_user() {
local group_token=$4
echo "Registering user: $email"
local payload="{\"email\":\"$email\",\"name\":\"$name\",\"password\":\"$password\""
if [ -n "$group_token" ]; then
payload="$payload,\"groupToken\":\"$group_token\""
fi
payload="$payload}"
local response
response=$(api_call "POST" "/users/register" "$payload")
echo "$response"
api_call "POST" "/users/register" "$payload"
}
# Function to append user data to TEST_DATA_FILE
store_user() {
local email=$1
local password=$2
local token=$3
local group=$4
jq --arg email "$email" \
--arg password "$password" \
--arg token "$token" \
--arg group "$group" \
'.users += [{"email":$email, "password":$password, "token":$token, "group":$group}]' \
"$TEST_DATA_FILE" > "${TEST_DATA_FILE}.tmp" && mv "${TEST_DATA_FILE}.tmp" "$TEST_DATA_FILE"
}
# Initialize the test data file
# Main logic for creating test data
initialize_test_data
# Step 1: Register the first user and create the first group
echo "=== Step 1: Create first group with 5 users ==="
user1_response=$(register_user "user1@homebox.test" "User One" "TestPassword123!")
user1_token=$(echo "$user1_response" | jq -r '.token // empty')
group_token=$(echo "$user1_response" | jq -r '.group.inviteToken // empty')
# Group 1: Create 5 users
echo "=== Creating Group 1 Users ==="
group1_user1_response=$(register_user "user1@homebox.test" "User One" "password123")
group1_user1_token=$(echo "$group1_user1_response" | jq -r '.token // empty')
group1_invite_token=$(echo "$group1_user1_response" | jq -r '.group.inviteToken // empty')
if [ -z "$user1_token" ]; then
echo "Failed to register first user"
echo "Response: $user1_response"
if [ -z "$group1_user1_token" ]; then
echo "Failed to register the first group user" >&2
exit 1
fi
add_to_test_data "users" "{\"email\": \"user1@homebox.test\", \"token\": \"$group1_user1_token\", \"group\": 1}"
# Store the first user
store_user "user1@homebox.test" "TestPassword123!" "$user1_token" "1"
# Register 4 more users in the same group
for i in {2..5}; do
echo "Registering user$i in group 1..."
user_response=$(register_user "user${i}@homebox.test" "User $i" "TestPassword123!" "$group_token")
user_token=$(echo "$user_response" | jq -r '.token // empty')
if [ -z "$user_token" ]; then
echo "Failed to register user$i"
echo "Response: $user_response"
else
store_user "user${i}@homebox.test" "TestPassword123!" "$user_token" "1"
fi
# Add 4 more users to the same group
for user in 2 3 4 5; do
response=$(register_user "user$user@homebox.test" "User $user" "password123" "$group1_invite_token")
token=$(echo "$response" | jq -r '.token // empty')
add_to_test_data "users" "{\"email\": \"user$user@homebox.test\", \"token\": \"$token\", \"group\": 1}"
done
# Step 2: Create second group with 2 users
echo "=== Step 2: Create second group with 2 users ==="
user6_response=$(register_user "user6@homebox.test" "User Six" "TestPassword123!")
user6_token=$(echo "$user6_response" | jq -r '.token // empty')
group2_token=$(echo "$user6_response" | jq -r '.group.inviteToken // empty')
# Group 2: Create 2 users
echo "=== Creating Group 2 Users ==="
group2_user1_response=$(register_user "user6@homebox.test" "User Six" "password123")
group2_user1_token=$(echo "$group2_user1_response" | jq -r '.token // empty')
group2_invite_token=$(echo "$group2_user1_response" | jq -r '.group.inviteToken // empty')
add_to_test_data "users" "{\"email\": \"user6@homebox.test\", \"token\": \"$group2_user1_token\", \"group\": 2}"
if [ -z "$user6_token" ]; then
echo "Failed to register user6"
echo "Response: $user6_response"
exit 1
fi
response=$(register_user "user7@homebox.test" "User Seven" "password123" "$group2_invite_token")
group2_user2_token=$(echo "$response" | jq -r '.token // empty')
add_to_test_data "users" "{\"email\": \"user7@homebox.test\", \"token\": \"$group2_user2_token\", \"group\": 2}"
# Store user6
store_user "user6@homebox.test" "TestPassword123!" "$user6_token" "2"
# Create Locations
echo "=== Creating Locations ==="
group1_locations=()
group1_locations+=("$(api_call "POST" "/locations" "{ \"name\": \"Living Room\", \"description\": \"Family area\" }" "$group1_user1_token")")
group1_locations+=("$(api_call "POST" "/locations" "{ \"name\": \"Garage\", \"description\": \"Storage area\" }" "$group1_user1_token")")
group2_locations=()
group2_locations+=("$(api_call "POST" "/locations" "{ \"name\": \"Office\", \"description\": \"Workspace\" }" "$group2_user1_token")")
user7_response=$(register_user "user7@homebox.test" "User Seven" "TestPassword123!" "$group2_token")
user7_token=$(echo "$user7_response" | jq -r '.token // empty')
if [ -z "$user7_token" ]; then
echo "Failed to register user7"
echo "Response: $user7_response"
else
store_user "user7@homebox.test" "TestPassword123!" "$user7_token" "2"
fi
# Add Locations to Test Data
for loc in "${group1_locations[@]}"; do
loc_id=$(echo "$loc" | jq -r '.id // empty')
add_to_test_data "locations" "{\"id\": \"$loc_id\", \"group\": 1}"
done
# Final Step: Log the created users for debugging
echo "=== Users Created ==="
cat "$TEST_DATA_FILE" | jq '.users'
for loc in "${group2_locations[@]}"; do
loc_id=$(echo "$loc" | jq -r '.id // empty')
add_to_test_data "locations" "{\"id\": \"$loc_id\", \"group\": 2}"
done
# Create Labels
echo "=== Creating Labels ==="
label1=$(api_call "POST" "/labels" "{ \"name\": \"Electronics\", \"description\": \"Devices\" }" "$group1_user1_token")
add_to_test_data "labels" "$label1"
label2=$(api_call "POST" "/labels" "{ \"name\": \"Important\", \"description\": \"High Priority\" }" "$group1_user1_token")
add_to_test_data "labels" "$label2"
# Create Items and Attachments
echo "=== Creating Items and Attachments ==="
item1=$(api_call "POST" "/items" "{ \"name\": \"Laptop\", \"description\": \"Work laptop\", \"locationId\": \"$(echo ${group1_locations[0]} | jq -r '.id // empty')\" }" "$group1_user1_token")
item1_id=$(echo "$item1" | jq -r '.id // empty')
add_to_test_data "items" "{\"id\": \"$item1_id\", \"group\": 1}"
attachment1=$(api_call "POST" "/items/$item1_id/attachments" "" "$group1_user1_token")
add_to_test_data "attachments" "{\"id\": \"$(echo $attachment1 | jq -r '.id // empty')\", \"itemId\": \"$item1_id\"}"
# Create Test Notifier
echo "=== Creating Notifiers ==="
notifier=$(api_call "POST" "/notifiers" "{ \"name\": \"TESTING\", \"url\": \"https://example.com/webhook\", \"isActive\": true }" "$group1_user1_token")
add_to_test_data "notifiers" "$notifier"
echo "=== Test Data Creation Complete ==="
cat "$TEST_DATA_FILE" | jq

View File

@@ -18,9 +18,9 @@ jobs:
runs-on: ubuntu-latest
timeout-minutes: 60
permissions:
contents: read # Read repository contents
packages: read # Pull Docker images from GHCR
contents: read
packages: read
steps:
# Step 1: Checkout repository
- name: Checkout repository
@@ -28,27 +28,27 @@ jobs:
with:
fetch-depth: 0
# Step 2: Setup dependencies (Node.js, Docker, pnpm, Playwright)
# Step 2: Setup dependencies (Node.js, Docker, pnpm, and Playwright)
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: lts/*
- name: Install pnpm
uses: pnpm/action-setup@v3.0.0
with:
version: 9.12.2
- name: Install Playwright
run: |
cd frontend
pnpm install
pnpm exec playwright install --with-deps chromium
# Step 3: Create /tmp data directories and prepare environment
# Step 3: Prepare environment and /tmp directories
- name: Create test data directories
run: |
mkdir -p /tmp/homebox-data-old
@@ -62,7 +62,7 @@ jobs:
- name: Pull latest stable HomeBox image
run: |
docker pull ghcr.io/sysadminsmedia/homebox:latest
- name: Start HomeBox (stable version)
run: |
docker run -d \
@@ -78,29 +78,27 @@ jobs:
# Wait for the service to be ready
timeout 60 bash -c 'until curl -f http://localhost:7745/api/v1/status; do sleep 2; done'
echo "HomeBox stable version is ready"
# Step 5: Run the test data creation script and validate output
- name: Create test data
# Step 5: Run test data script
- name: Create test data (users, items, locations, labels)
run: |
chmod +x .github/scripts/upgrade-test/create-test-data.sh
.github/scripts/upgrade-test/create-test-data.sh
env:
HOMEBOX_URL: http://localhost:7745
- name: Verify initial data creation
- name: Validate test data creation
run: |
echo "Verifying test data was created..."
# Check if the database file exists and contains valid users
if [ -f /tmp/homebox-data-old/homebox.db ]; then
ls -lh /tmp/homebox-data-old/homebox.db
echo "Database file exists"
else
echo "Database file not found!"
exit 1
# Check test-users.json content
cat /tmp/test-users.json | jq || echo "Test-users file is empty or malformed!"
# Check the database file exists
if [ ! -f /tmp/homebox-data-old/homebox.db ]; then
echo "No database found in the old instance directory!" && exit 1
fi
cat /tmp/test-users.json | jq '.users' || echo "Test users file not created properly!"
echo "Test data creation validated successfully!"
# Step 6: Stop the HomeBox stable version
# Step 6: Stop the HomeBox stable instance
- name: Stop old HomeBox instance
run: |
docker stop homebox-old
@@ -117,12 +115,12 @@ jobs:
-f Dockerfile \
.
# Step 8: Run the new version on copied test data
# Step 8: Start the new HomeBox version with migrated data
- name: Copy data to new location
run: |
cp -r /tmp/homebox-data-old/* /tmp/homebox-data-new/
chmod -R 777 /tmp/homebox-data-new
- name: Start HomeBox (new version)
run: |
docker run -d \
@@ -135,23 +133,23 @@ jobs:
-v /tmp/homebox-data-new:/data \
homebox:test
# Wait for the service to be ready
# Wait for the updated service to be ready
timeout 60 bash -c 'until curl -f http://localhost:7745/api/v1/status; do sleep 2; done'
echo "HomeBox new version is ready"
# Step 9: Verification tests using Playwright
- name: Run verification tests
# Step 9: Execute Playwright verification tests
- name: Run Playwright verification tests
run: |
cd frontend
TEST_DATA_FILE=/tmp/test-users.json \
E2E_BASE_URL=http://localhost:7745 \
pnpm exec playwright test \
-c ./test/playwright.config.ts \
--project=chromium \
test/upgrade/upgrade-verification.spec.ts
env:
HOMEBOX_URL: http://localhost:7745
# Step 10: Upload reports for review
- name: Upload Playwright report
uses: actions/upload-artifact@v4
if: always()
@@ -168,15 +166,16 @@ jobs:
path: frontend/test-results/
retention-days: 7
# Step 10: Cleanup resources
- name: Collect Docker logs
# Step 11: Collect logs for failed instances
- name: Collect logs on failure
if: failure()
run: |
echo "=== Docker logs for new version ==="
docker logs homebox-new || true
echo "=== Database content ==="
ls -la /tmp/homebox-data-new/ || true
ls -la /tmp/homebox-data-new || true
# Step 12: Cleanup resources
- name: Cleanup
if: always()
run: |