-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
256 lines (226 loc) · 6.45 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This script generates the matrix.md file from most recent build of each
// trigger:
//
// ```bash
// go run cmd/testmatrix/main.go > matrix.md
// ```
package main
import (
"bufio"
"context"
"fmt"
"html/template"
"log"
"os"
"regexp"
"sort"
"strings"
"cloud.google.com/go/storage"
e2e_testing "github.com/GoogleCloudPlatform/opentelemetry-operations-e2e-testing"
"github.com/alexflint/go-arg"
"golang.org/x/sync/errgroup"
"google.golang.org/api/cloudbuild/v1"
)
const (
pass status = ":white_check_mark:"
skip status = ":leftwards_arrow_with_hook:"
templateTxt = `# Matrix of supported scenarios in each ops repo
<table>
<thead>
<tr>
<th>Repo Name</th>
<th>Platform</th>
{{- range $.Scenarios }}
<th>{{ . }}</th>
{{- end }}
</tr>
</thead>
<tbody>
{{- range $repoName := $.RepoNames }}
{{- range $i, $platform := $.Platforms }}
<tr>
{{- if eq $i 0 }}
<td rowspan={{ len $.Platforms }}>
<a href="https://github.com/GoogleCloudPlatform/{{ $repoName }}">{{ $repoName }}</a>
</td>
{{- end }}
<td>{{ $platform }}</td>
{{- range $scenario := $.Scenarios }}
<td>{{ index $.RepoToPlatformToScenario $repoName $platform $scenario }}</td>
{{- end }}
</tr>
{{- end }}
{{- end }}
</tbody>
</table>
- *{{ .Pass }} means passing*
- *{{ .Skip }} means not implemented (skipped)*
## Regenerate
To regenerate this matrix, run from the repo root:
` + "```sh" + `
go run cmd/testmatrix/main.go --project-id=opentelemetry-ops-e2e > matrix.md
` + "```" + `
This will fetch recent Cloud Build logs to automatically update the statuses in this matrix.
`
)
type Args struct {
e2e_testing.CmdWithProjectId
}
type status string
type result struct {
RepoName string
Platform string
Statuses map[string]status
}
var (
triggerNameRe = regexp.MustCompile(`^ops-\w+-e2e-.*$`)
scenarioPassRe = regexp.MustCompile(`: --- PASS:\s+([\w_]+)`)
scenarioSkipRe = regexp.MustCompile(`: --- SKIP:\s+([\w_]+)`)
)
func main() {
args := Args{}
arg.MustParse(&args)
ctx := context.Background()
cloudbuildService, err := cloudbuild.NewService(ctx)
if err != nil {
panic(err)
}
storageClient, err := storage.NewClient(ctx)
if err != nil {
panic(err)
}
// Don't bother going over pages, just use a large page size and look at the
// first page
listTriggersRes, err := cloudbuildService.Projects.Triggers.List(args.ProjectID).
Context(ctx).
PageSize(128).
Do()
if err != nil {
panic(err)
}
g, egCtx := errgroup.WithContext(ctx)
results := make([]*result, len(listTriggersRes.Triggers))
for i, trigger := range listTriggersRes.Triggers {
i := i
trigger := trigger
g.Go(func() error {
res, err := handleTrigger(egCtx, args.ProjectID, trigger, cloudbuildService, storageClient)
if err != nil {
return err
}
results[i] = res
return nil
})
}
if err := g.Wait(); err != nil {
panic(err)
}
repoToPlatformToScenario := map[string]map[string]map[string]status{}
repoNameSet := map[string]struct{}{}
scenarioSet := map[string]struct{}{}
platformSet := map[string]struct{}{}
for _, result := range results {
if result == nil || result.Platform == "build" {
continue
}
repoNameSet[result.RepoName] = struct{}{}
if repoToPlatformToScenario[result.RepoName] == nil {
repoToPlatformToScenario[result.RepoName] = map[string]map[string]status{}
}
platformToScenario := repoToPlatformToScenario[result.RepoName]
platformToScenario[result.Platform] = result.Statuses
platformSet[result.Platform] = struct{}{}
for scenario := range result.Statuses {
scenarioSet[scenario] = struct{}{}
}
}
repoNames := sortStringSet(repoNameSet)
scenarios := sortStringSet(scenarioSet)
platforms := sortStringSet(platformSet)
template := template.Must(template.New("table").Parse(templateTxt))
err = template.Execute(os.Stdout, struct {
RepoNames []string
Scenarios []string
Platforms []string
RepoToPlatformToScenario map[string]map[string]map[string]status
Pass status
Skip status
}{repoNames, scenarios, platforms, repoToPlatformToScenario, pass, skip})
if err != nil {
panic(err)
}
}
// handleTrigger returns the latest results for the given trigger by querying
// builds and logs.
func handleTrigger(
ctx context.Context,
projectId string,
trigger *cloudbuild.BuildTrigger,
cloudbuildService *cloudbuild.Service,
storageClient *storage.Client,
) (*result, error) {
if !triggerNameRe.MatchString(trigger.Name) {
log.Printf("Skipping trigger %v which doesn't match regex", trigger.Name)
return nil, nil
}
res := &result{
RepoName: trigger.Github.Name,
Platform: trigger.Tags[1],
Statuses: make(map[string]status),
}
// fetch the latest successful build
listRes, err := cloudbuildService.Projects.Builds.List(projectId).
Context(ctx).
Filter(fmt.Sprintf(`trigger_id="%v" AND status="SUCCESS"`, trigger.Id)).
PageSize(1).
Do()
if err != nil {
return nil, err
}
if len(listRes.Builds) == 0 {
log.Printf("trigger %v had no successful builds, skipping the trigger", trigger.Name)
return res, nil
}
build := listRes.Builds[0]
reader, err := storageClient.Bucket(strings.TrimPrefix(build.LogsBucket, "gs://")).
Object(fmt.Sprintf("log-%v.txt", build.Id)).
NewReader(ctx)
if err != nil {
return nil, err
}
defer reader.Close()
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
line := scanner.Text()
if passMatches := scenarioPassRe.FindStringSubmatch(line); passMatches != nil {
res.Statuses[passMatches[1]] = pass
} else if skipMatches := scenarioSkipRe.FindStringSubmatch(line); skipMatches != nil {
res.Statuses[skipMatches[1]] = skip
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return res, nil
}
func sortStringSet(set map[string]struct{}) []string {
out := []string{}
for k := range set {
out = append(out, k)
}
sort.Strings(out)
return out
}