|
| 1 | +/* |
| 2 | +* Copyright 2025 Google LLC. All Rights Reserved. |
| 3 | +* |
| 4 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +* you may not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at |
| 7 | +* |
| 8 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software |
| 11 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | + */ |
| 16 | +package cmd |
| 17 | + |
| 18 | +import ( |
| 19 | + "fmt" |
| 20 | + "magician/cloudstorage" |
| 21 | + "magician/provider" |
| 22 | + "magician/teamcity" |
| 23 | + utils "magician/utility" |
| 24 | + "os" |
| 25 | + "strconv" |
| 26 | + "strings" |
| 27 | + "time" |
| 28 | + |
| 29 | + "github.com/spf13/cobra" |
| 30 | +) |
| 31 | + |
| 32 | +const ( |
| 33 | + NIGHTLY_DATA_BUCKET = "nightly-test-data" |
| 34 | +) |
| 35 | + |
| 36 | +var cntsRequiredEnvironmentVariables = [...]string{ |
| 37 | + "TEAMCITY_TOKEN", |
| 38 | +} |
| 39 | + |
| 40 | +type TestInfo struct { |
| 41 | + Name string `json:"name"` |
| 42 | + Status string `json:"status"` |
| 43 | + Service string `json:"service"` |
| 44 | + ErrorMessage string `json:"error_message"` |
| 45 | + LogLink string `json"log_link` |
| 46 | +} |
| 47 | + |
| 48 | +// collectNightlyTestStatusCmd represents the collectNightlyTestStatus command |
| 49 | +var collectNightlyTestStatusCmd = &cobra.Command{ |
| 50 | + Use: "collect-nightly-test-status", |
| 51 | + Short: "Collects and stores nightly test status", |
| 52 | + Long: `This command collects nightly test status, stores the data in JSON files and upload the files to GCS. |
| 53 | +
|
| 54 | +
|
| 55 | + The command expects the following argument(s): |
| 56 | + 1. Custom test date in YYYY-MM-DD format. default: ""(current time when the job is executed) |
| 57 | +
|
| 58 | + It then performs the following operations: |
| 59 | + 1. Collects nightly test status of the execution day or the specified test date (if provided) |
| 60 | + 2. Stores the collected data in JSON files |
| 61 | + 3. Uploads the JSON files to GCS |
| 62 | +
|
| 63 | + The following environment variables are required: |
| 64 | +` + listCNTSRequiredEnvironmentVariables(), |
| 65 | + Args: cobra.ExactArgs(1), |
| 66 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 67 | + env := make(map[string]string) |
| 68 | + for _, ev := range cntsRequiredEnvironmentVariables { |
| 69 | + val, ok := os.LookupEnv(ev) |
| 70 | + if !ok { |
| 71 | + return fmt.Errorf("did not provide %s environment variable", ev) |
| 72 | + } |
| 73 | + env[ev] = val |
| 74 | + } |
| 75 | + |
| 76 | + tc := teamcity.NewClient(env["TEAMCITY_TOKEN"]) |
| 77 | + gcs := cloudstorage.NewClient() |
| 78 | + |
| 79 | + now := time.Now() |
| 80 | + |
| 81 | + loc, err := time.LoadLocation("America/Los_Angeles") |
| 82 | + if err != nil { |
| 83 | + return fmt.Errorf("Error loading location: %s", err) |
| 84 | + } |
| 85 | + date := now.In(loc) |
| 86 | + customDate := args[0] |
| 87 | + // check if a specific date is provided |
| 88 | + if customDate != "" { |
| 89 | + parsedDate, err := time.Parse("2006-01-02", customDate) // input format YYYY-MM-DD |
| 90 | + // Set the time to 6pm PT |
| 91 | + date = time.Date(parsedDate.Year(), parsedDate.Month(), parsedDate.Day(), 18, 0, 0, 0, loc) |
| 92 | + if err != nil { |
| 93 | + return fmt.Errorf("invalid input time format: %w", err) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return execCollectNightlyTestStatus(date, tc, gcs) |
| 98 | + }, |
| 99 | +} |
| 100 | + |
| 101 | +func listCNTSRequiredEnvironmentVariables() string { |
| 102 | + var result string |
| 103 | + for i, ev := range cntsRequiredEnvironmentVariables { |
| 104 | + result += fmt.Sprintf("\t%2d. %s\n", i+1, ev) |
| 105 | + } |
| 106 | + return result |
| 107 | +} |
| 108 | + |
| 109 | +func execCollectNightlyTestStatus(now time.Time, tc TeamcityClient, gcs CloudstorageClient) error { |
| 110 | + lastday := now.AddDate(0, 0, -1) |
| 111 | + formattedStartCut := lastday.Format(time.RFC3339) |
| 112 | + formattedFinishCut := now.Format(time.RFC3339) |
| 113 | + date := now.Format("2006-01-02") |
| 114 | + |
| 115 | + err := createTestReport(provider.GA, tc, gcs, formattedStartCut, formattedFinishCut, date) |
| 116 | + if err != nil { |
| 117 | + return fmt.Errorf("Error getting GA nightly test status: %w", err) |
| 118 | + } |
| 119 | + |
| 120 | + err = createTestReport(provider.Beta, tc, gcs, formattedStartCut, formattedFinishCut, date) |
| 121 | + if err != nil { |
| 122 | + return fmt.Errorf("Error getting Beta nightly test status: %w", err) |
| 123 | + } |
| 124 | + |
| 125 | + return nil |
| 126 | +} |
| 127 | + |
| 128 | +func createTestReport(pVersion provider.Version, tc TeamcityClient, gcs CloudstorageClient, formattedStartCut, formattedFinishCut, date string) error { |
| 129 | + // Get all service test builds |
| 130 | + builds, err := tc.GetBuilds(pVersion.TeamCityNightlyProjectName(), formattedFinishCut, formattedStartCut) |
| 131 | + if err != nil { |
| 132 | + return err |
| 133 | + } |
| 134 | + |
| 135 | + var testInfoList []TestInfo |
| 136 | + for _, build := range builds.Builds { |
| 137 | + // Get service package name |
| 138 | + serviceName, err := convertServiceName(build.BuildTypeId) |
| 139 | + if err != nil { |
| 140 | + return fmt.Errorf("failed to convert test service name for %s: %v", build.BuildTypeId, err) |
| 141 | + } |
| 142 | + // Skip sweeper package |
| 143 | + if serviceName == "sweeper" { |
| 144 | + continue |
| 145 | + } |
| 146 | + |
| 147 | + // Get test results |
| 148 | + serviceTestResults, err := tc.GetTestResults(build) |
| 149 | + if err != nil { |
| 150 | + return fmt.Errorf("failed to get test results: %v", err) |
| 151 | + } |
| 152 | + if len(serviceTestResults.TestResults) == 0 { |
| 153 | + fmt.Printf("Service %s has no tests\n", serviceName) |
| 154 | + continue |
| 155 | + } |
| 156 | + |
| 157 | + for _, testResult := range serviceTestResults.TestResults { |
| 158 | + var errorMessage string |
| 159 | + // Get test debug log gcs link |
| 160 | + logLink := fmt.Sprintf("https://storage.cloud.google.com/teamcity-logs/nightly/%s/%s/%s/debug-%s-%s-%s-%s.txt", pVersion.TeamCityNightlyProjectName(), date, build.Number, pVersion.ProviderName(), build.Number, strconv.Itoa(build.Id), testResult.Name) |
| 161 | + // Get concise error message |
| 162 | + if testResult.Status == "FAILURE" { |
| 163 | + errorMessage = convertErrorMessage(testResult.ErrorMessage) |
| 164 | + } |
| 165 | + testInfoList = append(testInfoList, TestInfo{ |
| 166 | + Name: testResult.Name, |
| 167 | + Status: testResult.Status, |
| 168 | + Service: serviceName, |
| 169 | + ErrorMessage: errorMessage, |
| 170 | + LogLink: logLink, |
| 171 | + }) |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + // Write test status data to a JSON file |
| 176 | + fmt.Println("Write test status") |
| 177 | + testStatusFileName := fmt.Sprintf("%s-%s.json", date, pVersion.String()) |
| 178 | + err = utils.WriteToJson(testInfoList, testStatusFileName) |
| 179 | + if err != nil { |
| 180 | + return err |
| 181 | + } |
| 182 | + |
| 183 | + // Upload test status data file to gcs bucket |
| 184 | + objectName := pVersion.String() + "/" + testStatusFileName |
| 185 | + err = gcs.WriteToGCSBucket(NIGHTLY_DATA_BUCKET, objectName, testStatusFileName) |
| 186 | + if err != nil { |
| 187 | + return err |
| 188 | + } |
| 189 | + |
| 190 | + return nil |
| 191 | +} |
| 192 | + |
| 193 | +// convertServiceName extracts service package name from teamcity build type id |
| 194 | +// input: TerraformProviders_GoogleCloud_GOOGLE_NIGHTLYTESTS_GOOGLE_PACKAGE_SECRETMANAGER |
| 195 | +// output: secretmanager |
| 196 | +func convertServiceName(servicePath string) (string, error) { |
| 197 | + idx := strings.LastIndex(servicePath, "_") |
| 198 | + |
| 199 | + if idx != -1 { |
| 200 | + return strings.ToLower(servicePath[idx+1:]), nil |
| 201 | + } |
| 202 | + return "", fmt.Errorf("wrong service path format for %s", servicePath) |
| 203 | +} |
| 204 | + |
| 205 | +// convertErrorMessage returns concise error message |
| 206 | +func convertErrorMessage(rawErrorMessage string) string { |
| 207 | + |
| 208 | + startMarker := "------- Stdout: -------" |
| 209 | + endMarker := "------- Stderr: -------" |
| 210 | + startIndex := strings.Index(rawErrorMessage, startMarker) |
| 211 | + endIndex := strings.Index(rawErrorMessage, endMarker) |
| 212 | + |
| 213 | + if startIndex != -1 { |
| 214 | + startIndex += len(startMarker) |
| 215 | + } else { |
| 216 | + startIndex = 0 |
| 217 | + } |
| 218 | + |
| 219 | + if endIndex == -1 { |
| 220 | + endIndex = len(rawErrorMessage) |
| 221 | + } |
| 222 | + |
| 223 | + return strings.TrimSpace(rawErrorMessage[startIndex:endIndex]) |
| 224 | +} |
| 225 | + |
| 226 | +func init() { |
| 227 | + rootCmd.AddCommand(collectNightlyTestStatusCmd) |
| 228 | +} |
0 commit comments