Skip to content

Commit

Permalink
add e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
shanye997 committed Jan 4, 2024
1 parent d6f460d commit 96fb0d3
Show file tree
Hide file tree
Showing 7 changed files with 299 additions and 0 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: E2E Test Check
on:
pull_request:
branches:
- master
- main
types: [ 'opened', 'synchronize' ]
paths:
- '.github/**'
- '.github/workflows/**'
- '**/*.tf'

jobs:
e2e-check:
# if: github.event.review.state == 'approved' || github.event.review.body == 'approved'
runs-on: ubuntu-latest
name: 'e2e check'
steps:
- name: checkout
uses: actions/checkout@v3
- name: set id
id: set-job-id
uses: ayachensiyuan/get-action-job-id@v1.6
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
job-name: 'e2e check'
- name: Get pull request info
run: |
echo "repo name is" ${{github.event.pull_request.head.repo.full_name}}
echo "branch is" ${{github.event.pull_request.head.ref}}
echo "The current job id is ${{ steps.set-job-id.outputs.jobId }}"
- name: e2e test
run: |
objectPath="github-action/${{github.repository}}/e2e/Action-${{github.run_number}}-${{github.run_id}}-${{ steps.set-job-id.outputs.jobId }}"
go run scripts/curl_fc_trigger.go ${{github.event.pull_request.head.ref}} ${{github.event.pull_request.head.repo.full_name}} ${objectPath}
go run scripts/e2e_check.go ${objectPath}
31 changes: 31 additions & 0 deletions .github/workflows/weekly_e2e.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Weekly E2E Test Check
on:
workflow_dispatch:
schedule:
- cron: '0 0 * * 0'

jobs:
weekly-e2e-check:
if: github.repository_owner == 'alibabacloud-automation'
runs-on: ubuntu-latest
permissions: write-all
steps:
- name: checkout
uses: actions/checkout@v3
- name: weekly e2e test
run: |
objectPath="github-action/${{github.repository}}/weekly-e2e/Action-${{github.run_number}}-${{github.run_id}}-${{ steps.set-job-id.outputs.jobId }}"
go run scripts/curl_fc_trigger.go main ${{github.repository}} ${objectPath}
go run scripts/e2e_check.go ${objectPath}
- name: update test record
run: |
git add TestRecord.md
cd .git
sudo chmod -R a+rwX .
sudo find . -type d -exec chmod g+s '{}' +
- name: Commit & Push changes
uses: actions-js/push@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
message: 'Update TestRecord'
branch: main
3 changes: 3 additions & 0 deletions scripts/apply.tftest.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
run "apply" {
command = apply
}
63 changes: 63 additions & 0 deletions scripts/curl_fc_trigger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package main

import (
"crypto/rand"
"encoding/json"
"fmt"
"io"
"log"
"math/big"
"net/http"
"os"
"strings"
)

var urlPrefix = "https://terraform-fc-test-for-example-module.oss-ap-southeast-1.aliyuncs.com"

func main() {
if len(os.Args)!=4{
log.Println("[ERROR] invalid args")
return
}
branch := strings.TrimSpace(os.Args[1])
repoName := strings.TrimSpace(os.Args[2])
ossObjectPath := strings.TrimSpace(os.Args[3])

// get trigger url
fcTriggerUrl := urlPrefix + "/fcUrls.json"
response, err := http.Get(fcTriggerUrl)
if err != nil {
log.Println("[ERROR] get fc trigger url failed")
}
defer response.Body.Close()

content, _ := io.ReadAll(response.Body)
var data interface{}
json.Unmarshal(content, &data)
triggerMap := data.(map[string]interface{})

n, _ := rand.Int(rand.Reader, big.NewInt(100))
index := int(n.Int64()) % len(triggerMap)
triggerUrl := triggerMap[fmt.Sprintf("%d", index)]
fmt.Println(triggerUrl)

// curl
client := &http.Client{}
req, err := http.NewRequest("GET", triggerUrl.(string),
nil)
if err != nil {
panic(err)
}
req.Header.Add("X-Fc-Invocation-Type", "Async")

query := req.URL.Query()
query.Add("branch", branch)
query.Add("repo_name", repoName)
query.Add("oss_object_path", ossObjectPath)
req.URL.RawQuery = query.Encode()

if _, err := client.Do(req); err != nil {
log.Printf("[ERROR] fail to trigger fc test, err: %s", err)
}

}
113 changes: 113 additions & 0 deletions scripts/e2e_check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package main

import (
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"time"
)

var urlPrefix = "https://terraform-fc-test-for-example-module.oss-ap-southeast-1.aliyuncs.com"

func main() {
ossObjectPath := strings.TrimSpace(os.Args[1])
log.Println("run log path:", ossObjectPath)
runLogFileName := "terraform.run.log"
runResultFileName := "terraform.run.result.log"
runLogUrl := urlPrefix + "/" + ossObjectPath + "/" + runLogFileName
runResultUrl := urlPrefix + "/" + ossObjectPath + "/" + runResultFileName
lastLineNum := 0
deadline := time.Now().Add(time.Duration(24) * time.Hour)
finish := false
exitCode := 0
log.Println(runLogUrl)
for !time.Now().After(deadline) {
runLogResponse, err := http.Get(runLogUrl)
if err != nil || runLogResponse.StatusCode != 200 {
log.Println("waiting for job running...")
time.Sleep(5 * time.Second)
continue
}
defer runLogResponse.Body.Close()

s, er := io.ReadAll(runLogResponse.Body)
if er != nil && fmt.Sprint(er) != "EOF" {
log.Println("[ERROR] reading run log response failed:", err)
}
lineNum := len(s)
if runLogResponse.StatusCode == 200 {
if lineNum > lastLineNum {
fmt.Printf("%s", s[lastLineNum:lineNum])
lastLineNum = lineNum
}
}
if finish {
log.Println("run log path:", ossObjectPath)
log.Println("run log url:", runLogUrl)
if strings.Contains(ossObjectPath, "weekly") {
updateTestRecord(ossObjectPath)
exitCode = 0
}
os.Exit(exitCode)
}
runResultResponse, err := http.Get(runResultUrl)
if err != nil || runResultResponse.StatusCode != 200 {
time.Sleep(5 * time.Second)
continue
}
defer runResultResponse.Body.Close()
runResultContent := make([]byte, 100000)
_, err = runResultResponse.Body.Read(runResultContent)
if err != nil && fmt.Sprint(err) != "EOF" {
log.Println("[ERROR] reading run result response failed:", err)
}
finish = true
if !strings.HasPrefix(string(runResultContent), "PASS") {
log.Println("[ERROR] run result:", string(runResultContent))
exitCode = 1
}
}
log.Println("[ERROR] Timeout: waiting for job finished timeout after 24 hours.")
}

func updateTestRecord(ossObjectPath string) {
currentTestRecordFileName := "TestRecord.md"
currentTestRecordFileUrl := urlPrefix + "/" + ossObjectPath + "/" + currentTestRecordFileName
response, err := http.Get(currentTestRecordFileUrl)
if err != nil {
log.Println("[ERROR] failed to get test record from oss")
return
}
defer response.Body.Close()
data, _ := io.ReadAll(response.Body)
currentTestRecord := string(data) + "\n"

testRecordFileName := "TestRecord.md"
var testRecordFile *os.File
oldTestRecord := ""
if _, err := os.Stat(testRecordFileName); os.IsNotExist(err) {
testRecordFile, err = os.Create(testRecordFileName)
if err != nil {
log.Println("[ERROR] failed to create test record file")
}
} else {
data, err := os.ReadFile(testRecordFileName)
if err != nil {
log.Println("[ERROR] failed to read test record file")
return
}
oldTestRecord = string(data)

testRecordFile, err = os.OpenFile(testRecordFileName, os.O_TRUNC|os.O_RDWR, 0666)
if err != nil {
log.Println("[ERROR] failed to open test record file")
}
}
defer testRecordFile.Close()

currentTestRecord += oldTestRecord
testRecordFile.WriteString(currentTestRecord)
}
3 changes: 3 additions & 0 deletions scripts/plan.tftest.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
run "plan" {
command = plan
}
49 changes: 49 additions & 0 deletions scripts/terraform-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env sh

planFile=${1}"/scripts/plan.tftest.hcl"
applyFile=${1}"/scripts/apply.tftest.hcl"

terraformVersionFile=${1}"/tfversion.md"
echo "" > $terraformVersionFile
version=""

f=${2}
success=true
echo $f
echo ""
echo "====> Terraform testing in" $f
./terraform -chdir=$f init -upgrade
echo ""
echo "----> Plan Testing"
cp $planFile $f/
./terraform -chdir=$f test test -verbose
if [[ $? -ne 0 ]]; then
success=false
echo -e "\033[31m[ERROR]\033[0m: running terraform test for plan failed."
else
echo ""
echo "----> Apply Testing"
rm -rf $f/plan.tftest.hcl
cp $applyFile $f/
./terraform -chdir=$f test test
if [[ $? -ne 0 ]]; then
success=false
echo -e "\033[31m[ERROR]\033[0m: running terraform test for apply failed."
fi
rm -rf $f/apply.tftest.hcl
fi

version=$(./terraform -chdir=$f version)
row=`echo -e "$version" | sed -n '/^$/='`
if [ -n "$row" ]; then
version=`echo -e "$version" | sed -n "1,${row}p"`
fi

echo -e "### Versions\n" >> $terraformVersionFile
echo -e "${version}" >> $terraformVersionFile

if [[ $success == false ]]; then
exit 1
fi

exit 0

0 comments on commit 96fb0d3

Please sign in to comment.