Skip to content

Commit 6930c7b

Browse files
committed
update e2e to FC
1 parent d07d846 commit 6930c7b

12 files changed

+318
-185
lines changed

.github/workflows/e2e.yml

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,32 @@ on:
1212

1313
jobs:
1414
e2e-check:
15-
runs-on: terraform-alicloud-landing
16-
environment:
17-
name: acctests
15+
runs-on: ubuntu-latest
16+
name: 'e2e check'
1817
steps:
1918
- name: checkout
2019
uses: actions/checkout@v3
21-
- name: Get changed files
22-
id: changed-files
23-
uses: tj-actions/changed-files@v34
20+
- name: Checking the max commits number
21+
run: |
22+
commitNum=${{ github.event.pull_request.commits }}
23+
if [[ ${commitNum} -gt 1 ]]; then
24+
echo -e "\nERROR: The PR has ${commitNum} commits, and please rebase it to 1.\n"
25+
exit 1
26+
fi
27+
- name: set id
28+
id: set-job-id
29+
uses: ayachensiyuan/[email protected]
30+
env:
31+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2432
with:
25-
dir_names: "true"
26-
separator: ","
27-
files: "quickstarts/*"
28-
files_ignore: "**/TestRecord.md"
29-
dir_names_max_depth: 3
30-
- name: test pr
33+
job-name: 'e2e check'
34+
- name: get pull request info
35+
run: |
36+
echo "repo name is" ${{github.event.pull_request.head.repo.full_name}}
37+
echo "branch is" ${{github.event.pull_request.head.ref}}
38+
echo "the current job id is ${{ steps.set-job-id.outputs.jobId }}"
39+
- name: e2e test
3140
run: |
32-
bash scripts/terraform-install.sh
33-
CHANGED_FOLDERS="${{ steps.changed-files.outputs.all_changed_files }}"
34-
bash scripts/terraform-test.sh ${CHANGED_FOLDERS}
41+
objectPath="github-action/${{github.repository}}/e2e/Action-${{github.run_number}}-${{github.run_id}}-${{ steps.set-job-id.outputs.jobId }}"
42+
curl -H "X-Fc-Invocation-Type:Async" -s https://example-ci-terrafoodule-ci-svrrwvrpnj.ap-southeast-1.fcapp.run/?branch=${{github.event.pull_request.head.ref}}\&repo_name=${{github.event.pull_request.head.repo.full_name}}\&oss_object_path=${objectPath}
43+
go run scripts/e2e_check.go ${objectPath}

.github/workflows/weekly_e2e.yml

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,27 @@ on:
88
jobs:
99
all-e2e-test:
1010
if: github.repository_owner == 'alibabacloud-automation'
11-
runs-on: terraform-alicloud-landing
12-
timeout-minutes: 1440
13-
environment:
14-
name: acctests
11+
name: 'weekly e2e check'
12+
runs-on: ubuntu-latest
1513
permissions: write-all
1614
steps:
1715
- name: checkout
1816
uses: actions/checkout@v3
1917
with:
2018
token: ${{ secrets.GITHUB_TOKEN }}
21-
- name: test all
22-
run: |
23-
bash scripts/terraform-install.sh
24-
bash scripts/terraform-test.sh all examples
25-
- name: update TestRecord
26-
run: |
27-
bash scripts/update-test-record.sh
28-
cd .git
29-
sudo chmod -R a+rwX .
30-
sudo find . -type d -exec chmod g+s '{}' +
31-
- name: Commit & Push changes
32-
uses: actions-js/push@master
19+
- name: set id
20+
id: set-job-id
21+
uses: ayachensiyuan/[email protected]
22+
env:
23+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3324
with:
34-
github_token: ${{ secrets.GITHUB_TOKEN }}
35-
message: 'Update TestRecord'
36-
branch: main
37-
- name: update oss bucket
25+
job-name: 'weekly e2e check'
26+
- name: get pull request info
27+
run: |
28+
echo "repo name is" ${{github.event.pull_request.head.repo.full_name}}
29+
echo "branch is" ${{github.event.pull_request.head.ref}}
30+
echo "the current job id is ${{ steps.set-job-id.outputs.jobId }}"
31+
- name: weekly e2e test
3832
run: |
39-
source ~/.terraform_profile
40-
python3 scripts/upload-test-record-to-oss.py
41-
aliyun oss cp TestRecord.json oss://$BUCKET_EXAMPLE_OBJECT/TestRecord.json -u -e $ossEndpoint --region $ossRegion
33+
objectPath="github-action/${{github.repository}}/weekly-e2e/Action-${{github.run_number}}-${{github.run_id}}-${{ steps.set-job-id.outputs.jobId }}"
34+
curl -H "X-Fc-Invocation-Type:Async" -s https://example-ci-terrafoodule-ci-svrrwvrpnj.ap-southeast-1.fcapp.run/?branch=main\&repo_name=${{github.repository}}\&oss_object_path=${objectPath}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Weekly E2E Update Test Record
2+
on:
3+
repository_dispatch:
4+
types:
5+
- update_record
6+
jobs:
7+
run:
8+
if: github.repository_owner == 'alibabacloud-automation' && github.event.sender.login == 'shanye997'
9+
runs-on: ubuntu-latest
10+
permissions: write-all
11+
steps:
12+
- name: checkout
13+
uses: actions/checkout@v3
14+
with:
15+
token: ${{ secrets.GITHUB_TOKEN }}
16+
# - name: update TestRecord
17+
# run: |
18+
# echo ${{ github.event.client_payload.ossObejctPath }}
19+
# echo ${{ github.event.sender.login }}
20+
# go run scripts/update_test_record.go ${{ github.event.client_payload.ossObejctPath }}
21+
# cd .git
22+
# sudo chmod -R a+rwX .
23+
# sudo find . -type d -exec chmod g+s '{}' +
24+
# - name: Commit & Push changes
25+
# uses: actions-js/push@master
26+
# with:
27+
# github_token: ${{ secrets.GITHUB_TOKEN }}
28+
# message: 'Update TestRecord'
29+
# branch: main

scripts/apply.tftest.hcl

Lines changed: 0 additions & 3 deletions
This file was deleted.

scripts/e2e_check.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"log"
7+
"net/http"
8+
"os"
9+
"strings"
10+
"time"
11+
)
12+
13+
var urlPrefix = "https://terraform-fc-test-for-example-module.oss-ap-southeast-1.aliyuncs.com"
14+
15+
func main() {
16+
ossObjectPath := strings.TrimSpace(os.Args[1])
17+
log.Println("run log path:", ossObjectPath)
18+
runLogFileName := "terraform.run.log"
19+
runResultFileName := "terraform.run.result.log"
20+
runLogUrl := urlPrefix + "/" + ossObjectPath + "/" + runLogFileName
21+
runResultUrl := urlPrefix + "/" + ossObjectPath + "/" + runResultFileName
22+
lastLineNum := 0
23+
deadline := time.Now().Add(time.Duration(24) * time.Hour)
24+
finish := false
25+
exitCode := 0
26+
log.Println(runLogUrl)
27+
errResultMessage := ""
28+
for !time.Now().After(deadline) {
29+
runLogResponse, err := http.Get(runLogUrl)
30+
if err != nil || runLogResponse.StatusCode != 200 {
31+
log.Println("waiting for job running...")
32+
time.Sleep(5 * time.Second)
33+
continue
34+
}
35+
defer runLogResponse.Body.Close()
36+
37+
s, er := io.ReadAll(runLogResponse.Body)
38+
if er != nil && fmt.Sprint(er) != "EOF" {
39+
log.Println("[ERROR] reading run log response failed:", err)
40+
}
41+
lineNum := len(s)
42+
if runLogResponse.StatusCode == 200 {
43+
if lineNum > lastLineNum {
44+
fmt.Printf("%s", s[lastLineNum:lineNum])
45+
lastLineNum = lineNum
46+
}
47+
}
48+
if finish {
49+
log.Println("run log path:", ossObjectPath)
50+
log.Println("run log url:", runLogUrl)
51+
os.Exit(exitCode)
52+
}
53+
runResultResponse, err := http.Get(runResultUrl)
54+
if err != nil || runResultResponse.StatusCode != 200 {
55+
time.Sleep(5 * time.Second)
56+
continue
57+
}
58+
defer runResultResponse.Body.Close()
59+
runResultContent := make([]byte, 100000)
60+
_, err = runResultResponse.Body.Read(runResultContent)
61+
if err != nil && fmt.Sprint(err) != "EOF" {
62+
log.Println("[ERROR] reading run result response failed:", err)
63+
}
64+
finish = true
65+
if !strings.HasPrefix(string(runResultContent), "PASS") {
66+
errResultMessage = string(runResultContent)
67+
exitCode = 1
68+
}
69+
}
70+
log.Println("[ERROR] run result:", errResultMessage)
71+
log.Println("[ERROR] Timeout: waiting for job finished timeout after 24 hours.")
72+
}
73+

scripts/generate-test-record.sh

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
#!/usr/bin/env bash
22

3-
unset TF_LOG_PATH
4-
5-
if [ "$1" == "false" ];then
6-
exit 0
7-
fi
8-
93
success=true
10-
if [ -n "$3" ] ;then
4+
if [ -n "$2" ] ;then
115
success=false
126
fi
137

14-
subcategory=$(dirname $2)
8+
subcategory=$(dirname $1)
159
subcategory=$(basename $subcategory)
16-
fileName=$subcategory/$(basename $2)
10+
fileName=$subcategory/$(basename $1)
1711

1812
testRecordFile=TestRecord/$fileName/TestRecord.md.tmp
1913
if [ ! -d "TestRecord/${fileName}" ]; then
@@ -25,7 +19,7 @@ time=$(date -u "+%d %b %Y %H:%M UTC")
2519
echo -e "## $time\n" > $testRecordFile
2620
echo -e "success: ${success}\n\n### Versions\n" >> $testRecordFile
2721

28-
cd $2
22+
cd $1
2923
row=$(terraform version | sed -n '/^$/=')
3024
if [ -n "$row" ]; then
3125
version=`echo "$(terraform version | sed -n "1,${row}p")"`
@@ -37,8 +31,6 @@ cd - >/dev/null 2>&1
3731
echo "${version}" >> $testRecordFile
3832
echo -e "\n### Error\n" >> $testRecordFile
3933

40-
if [ -n "$3" ] ;then
41-
echo $3 >> $testRecordFile
34+
if [ -n "$2" ] ;then
35+
echo $2 >> $testRecordFile
4236
fi
43-
44-
echo "generate-test-record ${testRecordFile} successfully"

scripts/plan.tftest.hcl

Lines changed: 0 additions & 3 deletions
This file was deleted.

scripts/terraform-install.sh

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,5 @@ else
88
echo "terraform exists!"
99
fi
1010

11-
if [ ! -f /usr/bin/aliyun ]; then
12-
echo "aliyun does not exist!"
13-
wget -q https://github.com/aliyun/aliyun-cli/releases/download/v3.0.186/aliyun-cli-linux-3.0.186-amd64.tgz
14-
tar xzvf aliyun-cli-linux-3.0.186-amd64.tgz
15-
mv aliyun /usr/bin
16-
else
17-
echo "aliyun exists!"
18-
fi
1911

2012
exit 0

scripts/terraform-test.sh

Lines changed: 62 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,79 @@
11
#!/usr/bin/env sh
2-
error=false
32

4-
success=true
5-
allSuccess=true
6-
record=false
7-
folders=$1
8-
if [ ! -n "$1" ] ;then
9-
exit 0
10-
fi
11-
if [ -n "$2" ] ;then
12-
record=true
13-
folders=$(find quickstarts -maxdepth 2 -mindepth 2 -type d)
3+
if [ ! $# -eq 1 ];then
4+
exit 6
145
fi
15-
for f in ${folders//,/ }
16-
do
17-
success=true
18-
echo $f
19-
f=$(echo $f | xargs echo -n)
20-
export TF_LOG_PATH=${f}/terraform.log
21-
echo ""
22-
echo "====> Terraform testing in" $f
23-
terraform -chdir=$f init -upgrade
24-
~/init_env.sh
25-
source ~/.terraform_profile
6+
7+
f=$1
8+
success=true
9+
10+
f=$(echo $f | xargs echo -n)
11+
12+
exitCode=0
13+
echo ""
14+
echo "====> Terraform testing in" $f
15+
terraform -chdir=$f init -upgrade >/dev/null
16+
if [[ $? -ne 0 ]]; then
17+
success=false
18+
exitCode=1
19+
echo -e "\033[31m[ERROR]\033[0m: running terraform init failed."
20+
bash scripts/generate-test-record.sh $f "Init: running terraform init failed."
21+
else
2622
echo ""
27-
echo "----> Plan Testing"
28-
cp scripts/plan.tftest.hcl $f/
29-
terraform -chdir=$f test test -verbose
23+
echo " ----> Plan Testing"
24+
terraform -chdir=$f plan >/dev/null
3025
if [[ $? -ne 0 ]]; then
3126
success=false
32-
allSuccess=false
33-
echo -e "\033[31m[ERROR]\033[0m: running terraform test for plan failed."
34-
bash scripts/generate-test-record.sh $record $f "Plan: running terraform test for plan failed."
27+
exitCode=2
28+
echo -e "\033[31m[ERROR]\033[0m: running terraform plan failed."
29+
bash scripts/generate-test-record.sh $f "Plan: running terraform plan failed."
3530
else
31+
echo -e "\033[32m - plan check: success\033[0m"
3632
echo ""
37-
echo "----> Apply Testing"
38-
rm -rf $f/plan.tftest.hcl
39-
cp scripts/apply.tftest.hcl $f/
40-
terraform -chdir=$f test test
33+
echo " ----> Apply Testing"
34+
terraform -chdir=$f apply -auto-approve >/dev/null
4135
if [[ $? -ne 0 ]]; then
4236
success=false
43-
allSuccess=false
44-
echo -e "\033[31m[ERROR]\033[0m: running terraform test for apply failed."
45-
bash scripts/generate-test-record.sh $record $f "Apply: running terraform test for apply failed."
37+
exitCode=3
38+
echo -e "\033[31m[ERROR]\033[0m: running terraform apply failed."
39+
bash scripts/generate-test-record.sh $f "Apply: running terraform apply failed."
40+
else
41+
echo -e "\033[32m - apply check: success\033[0m"
42+
echo ""
43+
echo -e " ----> Apply Diff Checking\n"
44+
terraform -chdir=$f plan -detailed-exitcode
45+
if [[ $? -ne 0 ]]; then
46+
success=false
47+
exitCode=4
48+
echo -e "\033[31m[ERROR]\033[0m: running terraform plan for checking diff failed."
49+
bash scripts/generate-test-record.sh $f "Checking diff: running terraform plan for checking diff failed."
50+
else
51+
echo -e "\033[32m - apply diff check: success\033[0m"
52+
fi
53+
fi
54+
echo ""
55+
echo " ----> Destroying"
56+
terraform -chdir=$f destroy -auto-approve >/dev/null
57+
if [[ $? -ne 0 ]]; then
58+
success=false
59+
if [[ $exitCode -eq 0 ]]; then
60+
exitCode=5
61+
fi
62+
echo -e "\033[31m[ERROR]\033[0m: running terraform destroy failed."
63+
bash scripts/generate-test-record.sh $f "Destroy: running terraform destroy failed."
64+
else
65+
echo -e "\033[32m - destroy: success\033[0m"
4666
fi
47-
rm -rf $f/apply.tftest.hcl
4867
fi
68+
4969
if [[ $success == "true" ]]; then
50-
bash scripts/generate-test-record.sh $record $f
70+
bash scripts/generate-test-record.sh $f
5171
fi
52-
done
5372

54-
# e2e
55-
if [[ $allSuccess == "false" && $record == "false" ]]; then
56-
exit 1
73+
rm -rf $f/.terraform
74+
rm -rf $f/.terraform.lock.hcl
5775
fi
5876

59-
exit 0
77+
echo -e "\n"
78+
79+
exit $exitCode

0 commit comments

Comments
 (0)