-
Notifications
You must be signed in to change notification settings - Fork 2
192 lines (155 loc) Β· 7.22 KB
/
test-utils.yml
File metadata and controls
192 lines (155 loc) Β· 7.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
name: Test Utils Actions
on:
pull_request:
branches:
- "*"
push:
branches:
- "main"
- "release-*"
permissions:
contents: read
jobs:
test-check-permissions:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
# Install dependencies
- name: Install act
run: |
# Install act for workflow testing
curl -s https://raw.githubusercontent.com/nektos/act/master/install.sh | sudo bash
sudo install -m 0755 ./bin/act /usr/local/bin/act
- name: Install yq
uses: ./.github/actions/dependencies/install-yq
with:
version: "v4.6.3"
# Test all check-permissions scenarios
- name: Test check-permissions action scenarios
run: |
set -e
SCENARIOS_FILE=".github/tests/scenarios/check-permissions.yaml"
WORKFLOW=".github/tests/workflows/check-permissions-template.yaml"
echo "π Running check-permissions action tests..."
echo "π Loading scenarios from: $SCENARIOS_FILE"
# Get total number of scenarios
TOTAL=$(yq eval '.scenarios | length' "$SCENARIOS_FILE")
echo "π Found $TOTAL test scenarios"
echo
echo "β οΈ Note: These tests validate action structure and input handling."
echo " GitHub API permission checks depend on actual repository permissions."
echo
# Initialize overall result tracker
overall_result=true
# Loop through each scenario
for i in $(seq 0 $((TOTAL - 1))); do
# Extract scenario details using yq
id=$(yq eval ".scenarios[$i].id" "$SCENARIOS_FILE")
description=$(yq eval ".scenarios[$i].description" "$SCENARIOS_FILE")
event=$(yq eval ".scenarios[$i].event" "$SCENARIOS_FILE")
fixture=$(yq eval ".scenarios[$i].fixture" "$SCENARIOS_FILE")
echo "βββββββββββββββββββββββββββββββββββββββ"
echo "π Scenario $((i + 1))/$TOTAL: $id"
echo " Description: $description"
echo " Event: $event"
echo " Fixture: $fixture"
# Build environment variables from expectations
env_args=""
# Get all expectation keys and build env vars
expectation_keys=$(yq eval ".scenarios[$i].expectations | keys | .[]" "$SCENARIOS_FILE")
for key in $expectation_keys; do
value=$(yq eval ".scenarios[$i].expectations.$key" "$SCENARIOS_FILE")
env_key="EXPECT_$(echo "$key" | tr '[:lower:]' '[:upper:]')"
env_args="$env_args --env $env_key=\"$value\""
done
# Extract actor from event fixture for proper GITHUB_ACTOR override
actor=$(yq eval '.actor' "$fixture")
echo " π€ Actor from fixture: $actor"
# Run act with the scenario
echo "βΆοΈ Running test..."
# Override GITHUB_ACTOR to match the actor in the event fixture
actor_args="--env GITHUB_ACTOR=\"$actor\""
token_args="-s GITHUB_TOKEN=\"${{ secrets.GITHUB_TOKEN }}\""
if eval "act '$event' -W '$WORKFLOW' -e '$fixture' -P ubuntu-latest=ghcr.io/catthehacker/ubuntu:act-22.04 --pull=false $env_args $actor_args $token_args"; then
echo "β
$id completed"
else
echo "β $id failed"
overall_result=false
fi
# Print blank line for better readability
echo
done
if [ "$overall_result" = "false" ]; then
echo "There are some test β. Please check the logs for more details."
exit 1
fi
echo "βββββββββββββββββββββββββββββββββββββββ"
echo "π All $TOTAL check-permissions scenarios completed!"
echo " Action structure and input handling validated β
"
test-should-run:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
# Install dependencies
- name: Install act
run: |
# Install act for workflow testing
curl -s https://raw.githubusercontent.com/nektos/act/master/install.sh | sudo bash
sudo install -m 0755 ./bin/act /usr/local/bin/act
- name: Install yq
uses: ./.github/actions/dependencies/install-yq
with:
version: "v4.6.3"
# Test all should-run scenarios
- name: Test should-run action scenarios
run: |
set -e
SCENARIOS_FILE=".github/tests/scenarios/should-run.yaml"
WORKFLOW=".github/tests/workflows/should-run-template.yaml"
echo "π¦ Running should-run action tests..."
echo "π Loading scenarios from: $SCENARIOS_FILE"
# Get total number of scenarios
TOTAL=$(yq eval '.scenarios | length' "$SCENARIOS_FILE")
echo "π Found $TOTAL test scenarios"
echo
# Initialize overall result tracker
overall_result=true
# Loop through each scenario
for i in $(seq 0 $((TOTAL - 1))); do
# Extract scenario details using yq
id=$(yq eval ".scenarios[$i].id" "$SCENARIOS_FILE")
description=$(yq eval ".scenarios[$i].description" "$SCENARIOS_FILE")
event=$(yq eval ".scenarios[$i].event" "$SCENARIOS_FILE")
fixture=$(yq eval ".scenarios[$i].fixture" "$SCENARIOS_FILE")
echo "βββββββββββββββββββββββββββββββββββββββ"
echo "π Scenario $((i + 1))/$TOTAL: $id"
echo " Description: $description"
echo " Event: $event"
echo " Fixture: $fixture"
# Build environment variables from expectations
env_args=""
# Get all expectation keys and build env vars
expectation_keys=$(yq eval ".scenarios[$i].expectations | keys | .[]" "$SCENARIOS_FILE")
for key in $expectation_keys; do
value=$(yq eval ".scenarios[$i].expectations.$key" "$SCENARIOS_FILE")
env_key="EXPECT_$(echo "$key" | tr '[:lower:]' '[:upper:]')"
env_args="$env_args --env $env_key=\"$value\""
done
token_args="-s GITHUB_TOKEN=\"${{ secrets.GITHUB_TOKEN }}\""
# Run act with the scenario
echo "βΆοΈ Running test..."
if eval "act '$event' -W '$WORKFLOW' -e '$fixture' -P ubuntu-latest=ghcr.io/catthehacker/ubuntu:act-22.04 --pull=false $env_args $token_args"; then
echo "β
$id passed"
else
echo "β $id failed"
overall_result=false
fi
# Print blank line for better readability
echo
done
if [ "$overall_result" = "false" ]; then
echo "There are some test β. Please check the logs for more details."
exit 1
fi
echo "βββββββββββββββββββββββββββββββββββββββ"
echo "π All $TOTAL should-run scenarios passed!"