Skip to content

Commit a8c09e1

Browse files
committed
autoSync by Github Action
1 parent 66c2ada commit a8c09e1

File tree

5 files changed

+344
-0
lines changed

5 files changed

+344
-0
lines changed

.github/workflows/e2e.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: E2E Test Check
2+
on:
3+
pull_request:
4+
branches:
5+
- master
6+
- main
7+
types: [ 'opened', 'synchronize' ]
8+
paths:
9+
- '.github/**'
10+
- '.github/workflows/**'
11+
- '**/*.tf'
12+
13+
jobs:
14+
e2e-check:
15+
# if: github.event.review.state == 'approved' || github.event.review.body == 'approved'
16+
runs-on: ubuntu-latest
17+
name: 'e2e check'
18+
steps:
19+
- name: checkout
20+
uses: actions/checkout@v3
21+
- name: set id
22+
id: set-job-id
23+
uses: ayachensiyuan/get-action-job-id@v1.6
24+
env:
25+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
26+
with:
27+
job-name: 'e2e check'
28+
- name: Get pull request info
29+
run: |
30+
echo "repo name is" ${{github.event.pull_request.head.repo.full_name}}
31+
echo "branch is" ${{github.event.pull_request.head.ref}}
32+
echo "The current job id is ${{ steps.set-job-id.outputs.jobId }}"
33+
- name: e2e test
34+
run: |
35+
objectPath="github-action/${{github.repository}}/e2e/Action-${{github.run_number}}-${{github.run_id}}-${{ steps.set-job-id.outputs.jobId }}"
36+
go run scripts/curl_fc_trigger.go ${{github.event.pull_request.head.ref}} ${{github.event.pull_request.head.repo.full_name}} ${objectPath}
37+
go run scripts/e2e_check.go ${objectPath}

.github/workflows/weekly_e2e.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: Weekly E2E Test Check
2+
on:
3+
workflow_dispatch:
4+
schedule:
5+
- cron: '0 0 * * 0'
6+
7+
jobs:
8+
weekly-e2e-check:
9+
if: github.repository_owner == 'alibabacloud-automation'
10+
name: 'weekly e2e check'
11+
runs-on: ubuntu-latest
12+
permissions: write-all
13+
steps:
14+
- name: checkout
15+
uses: actions/checkout@v3
16+
- name: set id
17+
id: set-job-id
18+
uses: ayachensiyuan/get-action-job-id@v1.6
19+
env:
20+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
21+
with:
22+
job-name: 'weekly e2e check'
23+
- name: Get job id
24+
run: |
25+
echo "The current job id is ${{ steps.set-job-id.outputs.jobId }}"
26+
- name: Extract branch name
27+
shell: bash
28+
run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
29+
id: extract_branch
30+
- name: weekly e2e test
31+
run: |
32+
objectPath="github-action/${{github.repository}}/weekly-e2e/Action-${{github.run_number}}-${{github.run_id}}-${{ steps.set-job-id.outputs.jobId }}"
33+
echo "default branch: ${{ steps.extract_branch.outputs.branch }}"
34+
go run scripts/curl_fc_trigger.go ${{ steps.extract_branch.outputs.branch }} ${{github.repository}} ${objectPath}
35+
go run scripts/e2e_check.go ${objectPath}
36+
# - name: update test record
37+
# run: |
38+
# git add TestRecord.md
39+
# cd .git
40+
# sudo chmod -R a+rwX .
41+
# sudo find . -type d -exec chmod g+s '{}' +
42+
# - name: Commit & Push changes
43+
# uses: actions-js/push@master
44+
# with:
45+
# github_token: ${{ secrets.GITHUB_TOKEN }}
46+
# message: 'Update TestRecord'
47+
# branch: main

scripts/curl_fc_trigger.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package main
2+
3+
import (
4+
"crypto/rand"
5+
"encoding/json"
6+
"fmt"
7+
"io"
8+
"log"
9+
"math/big"
10+
"net/http"
11+
"os"
12+
"strings"
13+
)
14+
15+
var urlPrefix = "https://terraform-fc-test-for-example-module.oss-ap-southeast-1.aliyuncs.com"
16+
17+
func main() {
18+
if len(os.Args)!=4{
19+
log.Println("[ERROR] invalid args")
20+
return
21+
}
22+
branch := strings.TrimSpace(os.Args[1])
23+
repoName := strings.TrimSpace(os.Args[2])
24+
ossObjectPath := strings.TrimSpace(os.Args[3])
25+
26+
// get trigger url
27+
fcTriggerUrl := urlPrefix + "/fcUrls.json"
28+
response, err := http.Get(fcTriggerUrl)
29+
if err != nil {
30+
log.Println("[ERROR] get fc trigger url failed")
31+
}
32+
defer response.Body.Close()
33+
34+
content, _ := io.ReadAll(response.Body)
35+
var data interface{}
36+
json.Unmarshal(content, &data)
37+
triggerMap := data.(map[string]interface{})
38+
39+
n, _ := rand.Int(rand.Reader, big.NewInt(100))
40+
index := int(n.Int64()) % len(triggerMap)
41+
triggerUrl := triggerMap[fmt.Sprintf("%d", index)]
42+
fmt.Println(triggerUrl)
43+
44+
// curl
45+
client := &http.Client{}
46+
req, err := http.NewRequest("GET", triggerUrl.(string),
47+
nil)
48+
if err != nil {
49+
panic(err)
50+
}
51+
req.Header.Add("X-Fc-Invocation-Type", "Async")
52+
53+
query := req.URL.Query()
54+
query.Add("branch", branch)
55+
query.Add("repo_name", repoName)
56+
query.Add("oss_object_path", ossObjectPath)
57+
req.URL.RawQuery = query.Encode()
58+
59+
if _, err := client.Do(req); err != nil {
60+
log.Printf("[ERROR] fail to trigger fc test, err: %s", err)
61+
}
62+
63+
}

scripts/e2e_check.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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+
if strings.Contains(ossObjectPath, "weekly") {
52+
updateTestRecord(ossObjectPath)
53+
exitCode = 0
54+
}
55+
if errResultMessage != "" {
56+
log.Println("[ERROR] run result:", errResultMessage)
57+
}
58+
os.Exit(exitCode)
59+
}
60+
runResultResponse, err := http.Get(runResultUrl)
61+
if err != nil || runResultResponse.StatusCode != 200 {
62+
time.Sleep(5 * time.Second)
63+
continue
64+
}
65+
defer runResultResponse.Body.Close()
66+
runResultContent := make([]byte, 100000)
67+
_, err = runResultResponse.Body.Read(runResultContent)
68+
if err != nil && fmt.Sprint(err) != "EOF" {
69+
log.Println("[ERROR] reading run result response failed:", err)
70+
}
71+
finish = true
72+
if !strings.HasPrefix(string(runResultContent), "PASS") {
73+
errResultMessage = string(runResultContent)
74+
exitCode = 1
75+
}
76+
}
77+
log.Println("[ERROR] Timeout: waiting for job finished timeout after 24 hours.")
78+
}
79+
80+
func updateTestRecord(ossObjectPath string) {
81+
currentTestRecordFileName := "TestRecord.md"
82+
currentTestRecordFileUrl := urlPrefix + "/" + ossObjectPath + "/" + currentTestRecordFileName
83+
response, err := http.Get(currentTestRecordFileUrl)
84+
if err != nil {
85+
log.Println("[ERROR] failed to get test record from oss")
86+
return
87+
}
88+
defer response.Body.Close()
89+
data, _ := io.ReadAll(response.Body)
90+
if response.StatusCode != 200 || len(data) == 0 {
91+
return
92+
}
93+
currentTestRecord := string(data) + "\n"
94+
95+
testRecordFileName := "TestRecord.md"
96+
var testRecordFile *os.File
97+
oldTestRecord := ""
98+
if _, err := os.Stat(testRecordFileName); os.IsNotExist(err) {
99+
testRecordFile, err = os.Create(testRecordFileName)
100+
if err != nil {
101+
log.Println("[ERROR] failed to create test record file")
102+
}
103+
} else {
104+
data, err := os.ReadFile(testRecordFileName)
105+
if err != nil {
106+
log.Println("[ERROR] failed to read test record file")
107+
return
108+
}
109+
oldTestRecord = string(data)
110+
111+
testRecordFile, err = os.OpenFile(testRecordFileName, os.O_TRUNC|os.O_RDWR, 0666)
112+
if err != nil {
113+
log.Println("[ERROR] failed to open test record file")
114+
}
115+
}
116+
defer testRecordFile.Close()
117+
118+
currentTestRecord += oldTestRecord
119+
testRecordFile.WriteString(currentTestRecord)
120+
}

scripts/terraform-test.sh

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env sh
2+
3+
terraformVersionFile="tfversion.md"
4+
echo "" > $terraformVersionFile
5+
version=""
6+
7+
f=${1}
8+
success=true
9+
# echo $f
10+
exitCode=0
11+
echo ""
12+
echo "====> Terraform testing in" $f
13+
terraform -chdir=$f init -upgrade >/dev/null
14+
if [[ $? -ne 0 ]]; then
15+
success=false
16+
exitCode=1
17+
echo -e "\033[31m[ERROR]\033[0m: running terraform init failed."
18+
else
19+
echo ""
20+
echo "----> Plan Testing"
21+
terraform -chdir=$f plan >/dev/null
22+
if [[ $? -ne 0 ]]; then
23+
success=false
24+
exitCode=2
25+
echo -e "\033[31m[ERROR]\033[0m: running terraform plan failed."
26+
else
27+
echo -e "\033[32m - plan check: success\033[0m"
28+
echo ""
29+
echo "----> Apply Testing"
30+
terraform -chdir=$f apply -auto-approve >/dev/null
31+
if [[ $? -ne 0 ]]; then
32+
success=false
33+
exitCode=3
34+
echo -e "\033[31m[ERROR]\033[0m: running terraform apply failed."
35+
else
36+
echo -e "\033[32m - apply check: success\033[0m"
37+
echo ""
38+
echo -e " ----> Apply Diff Checking\n"
39+
terraform -chdir=$f plan -detailed-exitcode
40+
if [[ $? -ne 0 ]]; then
41+
success=false
42+
exitCode=4
43+
echo -e "\033[31m[ERROR]\033[0m: running terraform plan for checking diff failed."
44+
else
45+
echo -e "\033[32m - apply diff check: success\033[0m"
46+
fi
47+
fi
48+
echo ""
49+
echo " ----> Destroying"
50+
terraform -chdir=$f destroy -auto-approve >/dev/null
51+
if [[ $? -ne 0 ]]; then
52+
success=false
53+
if [[ $exitCode -eq 0 ]]; then
54+
exitCode=5
55+
fi
56+
echo -e "\033[31m[ERROR]\033[0m: running terraform destroy failed."
57+
else
58+
echo -e "\033[32m - destroy: success\033[0m"
59+
fi
60+
fi
61+
fi
62+
63+
version=$(terraform -chdir=$f version)
64+
row=`echo -e "$version" | sed -n '/^$/='`
65+
if [ -n "$row" ]; then
66+
version=`echo -e "$version" | sed -n "1,${row}p"`
67+
fi
68+
69+
if [[ $exitCode -ne 1 ]]; then
70+
rm -rf $f/.terraform
71+
rm -rf $f/.terraform.lock.hcl
72+
fi
73+
74+
echo -e "### Versions\n" >> $terraformVersionFile
75+
echo -e "${version}" >> $terraformVersionFile
76+
77+
exit $exitCode

0 commit comments

Comments
 (0)