|
| 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 | +} |
0 commit comments