test-policy.sh raw
1 #!/bin/bash
2
3 set -e
4
5 echo "=== ORLY Policy Test Script ==="
6 echo ""
7
8 # Colors for output
9 GREEN='\033[0;32m'
10 RED='\033[0;31m'
11 YELLOW='\033[1;33m'
12 NC='\033[0m' # No Color
13
14 # Get the directory where this script is located
15 SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
16 # Get the repository root (two levels up from scripts/docker-policy)
17 REPO_ROOT="$( cd "$SCRIPT_DIR/../.." && pwd )"
18
19 echo "Script directory: $SCRIPT_DIR"
20 echo "Repository root: $REPO_ROOT"
21 echo ""
22
23 echo -e "${YELLOW}Step 1: Building ORLY binary on host...${NC}"
24 cd "$REPO_ROOT" && CGO_ENABLED=0 go build -o orly
25
26 echo ""
27 echo -e "${YELLOW}Step 2: Copying files to test directory...${NC}"
28 cp "$REPO_ROOT/orly" "$SCRIPT_DIR/"
29 cp "$REPO_ROOT/pkg/crypto/p8k/libsecp256k1.so" "$SCRIPT_DIR/"
30
31 echo ""
32 echo -e "${YELLOW}Step 3: Cleaning up old containers...${NC}"
33 cd "$SCRIPT_DIR" && docker-compose down -v 2>/dev/null || true
34
35 echo ""
36 echo -e "${YELLOW}Step 4: Building Docker image...${NC}"
37 cd "$SCRIPT_DIR" && docker-compose build
38
39 echo ""
40 echo -e "${YELLOW}Step 5: Starting ORLY relay container...${NC}"
41 cd "$SCRIPT_DIR" && docker-compose up -d
42
43 echo ""
44 echo -e "${YELLOW}Step 6: Waiting for relay to start (15 seconds)...${NC}"
45 sleep 15
46
47 echo ""
48 echo -e "${YELLOW}Step 7: Checking relay logs...${NC}"
49 docker logs orly-policy-test 2>&1 | tail -20
50
51 echo ""
52 echo -e "${YELLOW}Step 8: Building policytest tool...${NC}"
53 cd "$REPO_ROOT" && CGO_ENABLED=0 go build -o policytest ./cmd/policytest
54
55 echo ""
56 echo -e "${YELLOW}Step 9: Publishing 2 events and querying for them...${NC}"
57
58 # Check which port the relay is listening on
59 RELAY_PORT=$(docker logs orly-policy-test 2>&1 | grep "starting listener" | grep -oP ':\K[0-9]+' | head -1)
60 if [ -z "$RELAY_PORT" ]; then
61 RELAY_PORT="8777"
62 fi
63 echo "Relay is listening on port: $RELAY_PORT"
64
65 # Test publish and query - this will publish 2 events and query for them
66 cd "$REPO_ROOT"
67 echo ""
68 echo "--- Publishing and querying events ---"
69 ./policytest -url "ws://localhost:$RELAY_PORT" -type publish-and-query -kind 1 -count 2 2>&1
70
71 echo ""
72 echo -e "${YELLOW}Step 10: Checking relay logs...${NC}"
73 docker logs orly-policy-test 2>&1 | tail -20
74
75 echo ""
76 echo -e "${YELLOW}Step 11: Waiting for policy script to process (3 seconds)...${NC}"
77 sleep 3
78
79 echo ""
80 echo -e "${YELLOW}Step 12: Checking if cs-policy.js created output file...${NC}"
81
82 # Check if the output file exists in the container
83 if docker exec orly-policy-test test -f /home/orly/cs-policy-output.txt; then
84 echo -e "${GREEN}✓ SUCCESS: cs-policy-output.txt file exists!${NC}"
85 echo ""
86 echo "Output file contents:"
87 docker exec orly-policy-test cat /home/orly/cs-policy-output.txt
88 echo ""
89
90 # Check if we see both read and write access types
91 WRITE_COUNT=$(docker exec orly-policy-test cat /home/orly/cs-policy-output.txt | grep -c "Access: write" || echo "0")
92 READ_COUNT=$(docker exec orly-policy-test cat /home/orly/cs-policy-output.txt | grep -c "Access: read" || echo "0")
93
94 echo "Policy invocations summary:"
95 echo " - Write operations (EVENT): $WRITE_COUNT (expected: 2)"
96 echo " - Read operations (REQ): $READ_COUNT (expected: >=1)"
97 echo ""
98
99 # Analyze results
100 if [ "$WRITE_COUNT" -ge 2 ] && [ "$READ_COUNT" -ge 1 ]; then
101 echo -e "${GREEN}✓ SUCCESS: Policy script processed both write and read operations!${NC}"
102 echo -e "${GREEN} - Published 2 events (write control)${NC}"
103 echo -e "${GREEN} - Queried events (read control)${NC}"
104 EXIT_CODE=0
105 elif [ "$WRITE_COUNT" -gt 0 ] && [ "$READ_COUNT" -gt 0 ]; then
106 echo -e "${YELLOW}⚠ PARTIAL: Policy invoked but counts don't match expected${NC}"
107 echo -e "${YELLOW} - Write count: $WRITE_COUNT (expected 2)${NC}"
108 echo -e "${YELLOW} - Read count: $READ_COUNT (expected >=1)${NC}"
109 EXIT_CODE=0
110 elif [ "$WRITE_COUNT" -gt 0 ]; then
111 echo -e "${YELLOW}⚠ WARNING: Policy script only processed write operations${NC}"
112 echo -e "${YELLOW} Read operations may not have been tested or logged${NC}"
113 EXIT_CODE=0
114 else
115 echo -e "${YELLOW}⚠ WARNING: Policy script is working but access types may not be logged correctly${NC}"
116 EXIT_CODE=0
117 fi
118 else
119 echo -e "${RED}✗ FAILURE: cs-policy-output.txt file not found!${NC}"
120 echo ""
121 echo "Checking relay logs for errors:"
122 docker logs orly-policy-test 2>&1 | grep -i policy || echo "No policy-related logs found"
123 EXIT_CODE=1
124 fi
125
126 echo ""
127 echo -e "${YELLOW}Step 13: Additional debugging info...${NC}"
128 echo "Files in /home/orly directory:"
129 docker exec orly-policy-test ls -la /home/orly/
130
131 echo ""
132 echo "Policy configuration:"
133 docker exec orly-policy-test cat /home/orly/.config/orly/policy.json || echo "Policy config not found"
134
135 echo ""
136 echo "=== Test Complete ==="
137 echo ""
138 echo "To view logs: docker logs orly-policy-test"
139 echo "To stop container: cd scripts/docker-policy && docker-compose down"
140 echo "To clean up: cd scripts/docker-policy && docker-compose down -v"
141
142 exit $EXIT_CODE
143