|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Test that ontology imports of uploaded files do not cause deadlock |
| 5 | +# This verifies the fix for circular dependency when: |
| 6 | +# 1. Request arrives for /uploads/xyz |
| 7 | +# 2. OntologyFilter intercepts it and loads ontology |
| 8 | +# 3. Ontology has owl:imports for /uploads/xyz |
| 9 | +# 4. Jena FileManager makes HTTP request to /uploads/xyz |
| 10 | +# 5. Would cause infinite loop/deadlock without the fix |
| 11 | + |
| 12 | +initialize_dataset "$END_USER_BASE_URL" "$TMP_END_USER_DATASET" "$END_USER_ENDPOINT_URL" |
| 13 | +initialize_dataset "$ADMIN_BASE_URL" "$TMP_ADMIN_DATASET" "$ADMIN_ENDPOINT_URL" |
| 14 | +purge_cache "$END_USER_VARNISH_SERVICE" |
| 15 | +purge_cache "$ADMIN_VARNISH_SERVICE" |
| 16 | +purge_cache "$FRONTEND_VARNISH_SERVICE" |
| 17 | + |
| 18 | +pwd=$(realpath "$PWD") |
| 19 | + |
| 20 | +# add agent to the writers group so they can upload files |
| 21 | + |
| 22 | +add-agent-to-group.sh \ |
| 23 | + -f "$OWNER_CERT_FILE" \ |
| 24 | + -p "$OWNER_CERT_PWD" \ |
| 25 | + --agent "$AGENT_URI" \ |
| 26 | + "${ADMIN_BASE_URL}acl/groups/writers/" |
| 27 | + |
| 28 | +# Step 1: Upload an RDF file |
| 29 | + |
| 30 | +file_content_type="text/turtle" |
| 31 | + |
| 32 | +file_doc=$(create-file.sh \ |
| 33 | + -f "$AGENT_CERT_FILE" \ |
| 34 | + -p "$AGENT_CERT_PWD" \ |
| 35 | + -b "$END_USER_BASE_URL" \ |
| 36 | + --title "Test ontology for upload import" \ |
| 37 | + --file "$pwd/test-ontology-import.ttl" \ |
| 38 | + --file-content-type "${file_content_type}") |
| 39 | + |
| 40 | +# Step 2: Extract the uploaded file URI (content-addressed) |
| 41 | + |
| 42 | +file_doc_ntriples=$(get.sh \ |
| 43 | + -f "$AGENT_CERT_FILE" \ |
| 44 | + -p "$AGENT_CERT_PWD" \ |
| 45 | + --accept 'application/n-triples' \ |
| 46 | + "$file_doc") |
| 47 | + |
| 48 | +upload_uri=$(echo "$file_doc_ntriples" | sed -rn "s/<${file_doc//\//\\/}> <http:\/\/xmlns.com\/foaf\/0.1\/primaryTopic> <(.*)> \./\1/p") |
| 49 | + |
| 50 | +echo "Uploaded file URI: $upload_uri" |
| 51 | + |
| 52 | +# Verify the uploaded file is accessible before we add it as an import |
| 53 | +curl -k -f -s \ |
| 54 | + -E "$AGENT_CERT_FILE":"$AGENT_CERT_PWD" \ |
| 55 | + -H "Accept: ${file_content_type}" \ |
| 56 | + "$upload_uri" > /dev/null |
| 57 | + |
| 58 | +echo "Upload file is accessible" |
| 59 | + |
| 60 | +# Step 3: Add the uploaded file as an owl:import to the namespace ontology |
| 61 | + |
| 62 | +namespace_doc="${END_USER_BASE_URL}ns" |
| 63 | +namespace="${namespace_doc}#" |
| 64 | +ontology_doc="${ADMIN_BASE_URL}ontologies/namespace/" |
| 65 | + |
| 66 | +add-ontology-import.sh \ |
| 67 | + -f "$OWNER_CERT_FILE" \ |
| 68 | + -p "$OWNER_CERT_PWD" \ |
| 69 | + --import "$upload_uri" \ |
| 70 | + "$ontology_doc" |
| 71 | + |
| 72 | +echo "Added owl:import of uploaded file to namespace ontology" |
| 73 | + |
| 74 | +# Step 4: Clear the namespace ontology from memory to force reload on next request |
| 75 | + |
| 76 | +clear-ontology.sh \ |
| 77 | + -f "$OWNER_CERT_FILE" \ |
| 78 | + -p "$OWNER_CERT_PWD" \ |
| 79 | + -b "$ADMIN_BASE_URL" \ |
| 80 | + --ontology "$namespace" |
| 81 | + |
| 82 | +echo "Cleared ontology cache to force reload" |
| 83 | + |
| 84 | +# Step 5: Make a request that triggers ontology loading |
| 85 | +# This would cause a deadlock without the OntologyFilter fix |
| 86 | +# Use portable timeout implementation (works on both macOS and Linux) |
| 87 | + |
| 88 | +echo "Making request to trigger ontology loading (testing for deadlock)..." |
| 89 | + |
| 90 | +# Portable timeout function - works on both macOS and Linux |
| 91 | +request_pid="" |
| 92 | +( |
| 93 | + curl -k -f -s \ |
| 94 | + -E "$OWNER_CERT_FILE":"$OWNER_CERT_PWD" \ |
| 95 | + -H "Accept: application/n-triples" \ |
| 96 | + "$namespace_doc" > /dev/null |
| 97 | +) & |
| 98 | +request_pid=$! |
| 99 | + |
| 100 | +# Wait up to 30 seconds for the request to complete |
| 101 | +timeout_seconds=30 |
| 102 | +elapsed=0 |
| 103 | +while kill -0 "$request_pid" 2>/dev/null; do |
| 104 | + if [ $elapsed -ge $timeout_seconds ]; then |
| 105 | + kill -9 "$request_pid" 2>/dev/null || true |
| 106 | + echo "ERROR: Request timed out after ${timeout_seconds} seconds - deadlock detected!" |
| 107 | + exit 1 |
| 108 | + fi |
| 109 | + sleep 1 |
| 110 | + ((elapsed++)) |
| 111 | +done |
| 112 | + |
| 113 | +# Check if curl succeeded |
| 114 | +wait "$request_pid" |
| 115 | +curl_exit_code=$? |
| 116 | +if [ $curl_exit_code -ne 0 ]; then |
| 117 | + echo "ERROR: Request failed with exit code $curl_exit_code" |
| 118 | + exit 1 |
| 119 | +fi |
| 120 | + |
| 121 | +echo "Request completed successfully in ${elapsed}s (no deadlock)" |
| 122 | + |
| 123 | +# Step 6: Verify the import is present in the loaded ontology |
| 124 | + |
| 125 | +curl -k -f -s \ |
| 126 | + -H "Accept: application/n-triples" \ |
| 127 | + "$namespace_doc" \ |
| 128 | +| grep "<${namespace}> <http://www.w3.org/2002/07/owl#imports> <${upload_uri}>" > /dev/null |
| 129 | + |
| 130 | +echo "Verified owl:import is present in namespace ontology" |
| 131 | + |
| 132 | +# Step 7: Verify the uploaded file is still accessible after ontology loading |
| 133 | + |
| 134 | +curl -k -f -s \ |
| 135 | + -E "$AGENT_CERT_FILE":"$AGENT_CERT_PWD" \ |
| 136 | + -H "Accept: ${file_content_type}" \ |
| 137 | + "$upload_uri" > /dev/null |
| 138 | + |
| 139 | +echo "Uploaded file is still accessible after ontology import" |
| 140 | + |
| 141 | +# Step 8: Verify that the imported ontology content is accessible via the namespace document |
| 142 | +# This confirms the import was actually loaded (not just skipped) |
| 143 | + |
| 144 | +curl -k -f -s \ |
| 145 | + -G \ |
| 146 | + -E "$OWNER_CERT_FILE":"$OWNER_CERT_PWD" \ |
| 147 | + -H 'Accept: application/sparql-results+xml' \ |
| 148 | + --data-urlencode "query=SELECT * { <https://example.org/test#TestClass> ?p ?o }" \ |
| 149 | + "$namespace_doc" \ |
| 150 | +| grep '<literal>Test Class</literal>' > /dev/null |
| 151 | + |
| 152 | +echo "Verified imported ontology content is accessible via SPARQL" |
| 153 | + |
| 154 | +echo "✓ All tests passed - no deadlock detected when importing uploaded files in ontology" |
0 commit comments