forked from IBM-Cloud/terraform-provider-ibm
-
Notifications
You must be signed in to change notification settings - Fork 2
143 lines (125 loc) · 5.65 KB
/
power-acceptance-test.yml
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
name: PowerVS Acceptance Tests
on:
pull_request:
paths:
- 'ibm/service/power/*.go' # Trigger only when test files within power are changed
types: [opened, synchronize, reopened]
jobs:
check-permissions:
runs-on: ubuntu-latest
if: github.repository == 'powervs-ibm/terraform-provider-ibm'
outputs:
is_authorized: ${{ steps.check-permission.outputs.is_authorized }}
steps:
- name: Check if the user is allowed to trigger this workflow
id: check-permission
run: |
echo "REPO=${{ secrets.REPO }}" >> $GITHUB_ENV
# Fetch the GitHub actor (the user who triggered the action)
actor="${GITHUB_ACTOR}"
# Define allowed users
allowed_users="${{ secrets.ALLOWED_USERS }}"
# Convert it to an array
IFS=' ' read -r -a allowed_users_array <<< "$allowed_users"
# Check if the actor is in the allowed list
if [[ ! " ${allowed_users_array[@]} " =~ " ${actor} " ]]; then
echo "User ${actor} is not authorized to run this workflow."
echo "is_authorized=false" >> $GITHUB_ENV # Set output to false
else
echo "User ${actor} is authorized to run the workflow."
echo "is_authorized=true" >> $GITHUB_ENV # Set output to true
fi
acceptance-test:
needs: check-permissions
runs-on: ubuntu-latest
if: ${{ needs.check-permissions.outputs.is_authorized == 'true' && github.repository == env.REPO }}
steps:
# Step 1: Checkout code
- name: Checkout code
uses: actions/checkout@v3
# Step 2: Set up Go environment
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: '1.22'
# Step 3: Cache Go modules
- name: Cache Go modules
uses: actions/cache@v3
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.mod', '**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
# Step 4: Install dependencies
- name: Install dependencies
run: |
go mod tidy
go mod download
# Step 5 : Find modified files, set environment variables, and run tests
- name: Find modified files and run acceptance tests
run: |
# Create report file
mkdir -p test-results
declare -A modified_test_files_map
# Get the list of modified Go files in the PR under the ibm/service/power directory
git fetch origin
modified_go_files=$(git diff --name-only origin/${{ github.event.pull_request.base.ref }} -- 'ibm/service/power/*.go')
echo "Modified files: $modified_go_files"
# Initialize an empty list for test files
modified_test_files=""
# Loop through modified Go files to identify corresponding test files
for file in $modified_go_files; do
# Skip constants.go since it has no corresponding test file
if [[ "$file" == "ibm/service/power/ibm_pi_constants.go" ]]; then
echo "Skipping ibm_pi_constants.go as it has no test file."
continue
fi
if [["$file" == *_test.go ]]; then
modified_test_files_map["$file"]=1
else
# Get the test file corresponding to the modified Go file by replacing .go with _test.go
test_file="${file%.go}_test.go"
# If the test file exists, add it to the list of test files to run
if [ -f "$test_file" ]; then
modified_test_files_map["$test_file"]=1
fi
fi
done
# If there are modified test files, run the tests
if [ ${#modified_test_files_map[@]} -gt 0 ]; then
modified_test_files=$(echo "${!modified_test_files_map[@]}")
echo "Modified test files: $modified_test_files"
go test -v -tags=all -test.v -test.run '^TestAcc' $modified_test_files 2>&1 | tee test-results/test-report.log
if grep -q 'FAIL' test-results/test-report.log; then
exit 1
fi
else
echo "No modified test files detected."
fi
env:
TF_ACC: ${{ secrets.TF_ACC }}
TF_CLI_ARGS_plan: ${{ secrets.TF_CLI_ARGS_plan }}
TF_CLI_ARGS_apply: ${{ secrets.TF_CLI_ARGS_apply }}
IC_API_KEY: ${{ secrets.IC_API_KEY }}
IAAS_CLASSIC_API_KEY: ${{ secrets.IAAS_CLASSIC_API_KEY }}
IAAS_CLASSIC_USERNAME: ${{ secrets.IAAS_CLASSIC_USERNAME }}
# Endpoints
IBMCLOUD_PI_API_ENDPOINT: ${{ vars.IBMCLOUD_PI_API_ENDPOINT }}
IBMCLOUD_IAM_API_ENDPOINT: ${{ vars.IBMCLOUD_IAM_API_ENDPOINT }}
IBMCLOUD_RESOURCE_CATALOG_API_ENDPOINT: ${{ vars.IBMCLOUD_RESOURCE_CATALOG_API_ENDPOINT }}
IBMCLOUD_RESOURCE_MANAGEMENT_API_ENDPOINT: ${{ vars.IBMCLOUD_RESOURCE_MANAGEMENT_API_ENDPOINT }}
IBMCLOUD_RESOURCE_CONTROLLER_API_ENDPOINT: ${{ vars.IBMCLOUD_RESOURCE_CONTROLLER_API_ENDPOINT }}
IBMCLOUD_GS_API_ENDPOINT: ${{ vars.IBMCLOUD_GS_API_ENDPOINT }}
IBMCLOUD_GT_API_ENDPOINT: ${{ vars.IBMCLOUD_GT_API_ENDPOINT }}
# Power
PI_CLOUDINSTANCE_ID: ${{ secrets.PI_CLOUDINSTANCE_ID }}
IBMCLOUD_REGION: ${{ secrets.IBMCLOUD_REGION }}
IBMCLOUD_ZONE: ${{ secrets.IBMCLOUD_ZONE }}
# Step 6: Display Test Results
- name: Display Test Results
run: |
echo "Test Results:"
grep -E '^(=== RUN|--- PASS|--- FAIL|PASS|FAIL)' test-results/test-report.log
if: always()