-
Notifications
You must be signed in to change notification settings - Fork 39.6k
/
walk.go
319 lines (278 loc) · 9.19 KB
/
walk.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
Copyright 2019 The Kubernetes Authors.
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
http://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.
*/
package main
import (
"encoding/json"
"flag"
"fmt"
"go/ast"
"go/parser"
"go/token"
"gopkg.in/yaml.v2"
"io"
"log"
"os"
"regexp"
"sort"
"strconv"
"strings"
"text/template"
"github.com/onsi/ginkgo/types"
)
var (
baseURL = flag.String("url", "https://github.com/kubernetes/kubernetes/tree/master/", "location of the current source")
k8sPath = flag.String("source", "", "location of the current source on the current machine")
confDoc = flag.Bool("docs", false, "write a conformance document")
version = flag.String("version", "v1.9", "version of this conformance document")
// If a test name contains any of these tags, it is ineligble for promotion to conformance
regexIneligibleTags = regexp.MustCompile(`\[(Alpha|Feature:[^\]]+|Flaky)\]`)
// Conformance comments should be within this number of lines to the call itself.
// Allowing for more than one in case a spare comment or two is below it.
conformanceCommentsLineWindow = 5
seenLines map[string]struct{}
)
type frame struct {
Function string
// File and Line are the file name and line number of the
// location in this frame. For non-leaf frames, this will be
// the location of a call. These may be the empty string and
// zero, respectively, if not known.
File string
Line int
}
type conformanceData struct {
// A URL to the line of code in the kube src repo for the test. Omitted from the YAML to avoid exposing line number.
URL string `yaml:"-"`
// Extracted from the "Testname:" comment before the test
TestName string
// CodeName is taken from the actual ginkgo descriptions, e.g. `[sig-apps] Foo should bar [Conformance]`
CodeName string
// Extracted from the "Description:" comment before the test
Description string
// Version when this test is added or modified ex: v1.12, v1.13
Release string
// File is the filename where the test is defined. We intentionally don't save the line here to avoid meaningless changes.
File string
}
func main() {
flag.Parse()
if len(flag.Args()) < 1 {
log.Fatalln("Requires the name of the test details file as first and only argument.")
}
testDetailsFile := flag.Args()[0]
f, err := os.Open(testDetailsFile)
if err != nil {
log.Fatalf("Failed to open file %v: %v", testDetailsFile, err)
}
defer f.Close()
seenLines = map[string]struct{}{}
dec := json.NewDecoder(f)
testInfos := []*conformanceData{}
for {
var spec *types.SpecSummary
if err := dec.Decode(&spec); err == io.EOF {
break
} else if err != nil {
log.Fatal(err)
}
if isConformance(spec) {
testInfo := getTestInfo(spec)
if testInfo != nil {
testInfos = append(testInfos, testInfo)
}
if err := validateTestName(testInfo.CodeName); err != nil {
log.Fatal(err)
}
}
}
sort.Slice(testInfos, func(i, j int) bool { return testInfos[i].CodeName < testInfos[j].CodeName })
saveAllTestInfo(testInfos)
}
func isConformance(spec *types.SpecSummary) bool {
return strings.Contains(getTestName(spec), "[Conformance]")
}
func getTestInfo(spec *types.SpecSummary) *conformanceData {
var c *conformanceData
var err error
// The key to this working is that we don't need to parse every file or walk
// every componentCodeLocation. The last componentCodeLocation is going to typically start
// with the ConformanceIt(...) call and the next call in that callstack will be the
// ast.Node which is attached to the comment that we want.
for i := len(spec.ComponentCodeLocations) - 1; i > 0; i-- {
fullstacktrace := spec.ComponentCodeLocations[i].FullStackTrace
c, err = getConformanceDataFromStackTrace(fullstacktrace)
if err != nil {
log.Printf("Error looking for conformance data: %v", err)
}
if c != nil {
break
}
}
if c == nil {
log.Printf("Did not find test info for spec: %#v\n", getTestName(spec))
return nil
}
c.CodeName = getTestName(spec)
return c
}
func getTestName(spec *types.SpecSummary) string {
return strings.Join(spec.ComponentTexts[1:], " ")
}
func saveAllTestInfo(dataSet []*conformanceData) {
if *confDoc {
// Note: this assumes that you're running from the root of the kube src repo
templ, err := template.ParseFiles("./test/conformance/cf_header.md")
if err != nil {
fmt.Printf("Error reading the Header file information: %s\n\n", err)
}
data := struct {
Version string
}{
Version: *version,
}
templ.Execute(os.Stdout, data)
for _, data := range dataSet {
fmt.Printf("## [%s](%s)\n\n", data.TestName, data.URL)
fmt.Printf("- Added to conformance in release %s\n", data.Release)
fmt.Printf("- Defined in code as: %s\n\n", data.CodeName)
fmt.Printf("%s\n\n", data.Description)
}
return
}
// Serialize the list as a whole. Generally meant to end up as conformance.txt which tracks the set of tests.
b, err := yaml.Marshal(dataSet)
if err != nil {
log.Printf("Error marshalling data into YAML: %v", err)
}
fmt.Println(string(b))
}
func getConformanceDataFromStackTrace(fullstackstrace string) (*conformanceData, error) {
// The full stacktrace to parse from ginkgo is of the form:
// k8s.io/kubernetes/test/e2e/storage/utils.SIGDescribe(0x51f4c4f, 0xf, 0x53a0dd8, 0xc000ab6e01)\n\ttest/e2e/storage/utils/framework.go:23 +0x75\n ... ...
// So we need to split it into lines, remove whitespace, and then grab the files/lines.
stack := strings.Replace(fullstackstrace, "\t", "", -1)
calls := strings.Split(stack, "\n")
frames := []frame{}
i := 0
for i < len(calls) {
fileLine := strings.Split(calls[i+1], " ")
lineinfo := strings.Split(fileLine[0], ":")
line, err := strconv.Atoi(lineinfo[1])
if err != nil {
panic(err)
}
frames = append(frames, frame{
Function: calls[i],
File: lineinfo[0],
Line: line,
})
i += 2
}
// filenames have `/go/src/k8s.io` prefix which dont exist locally
for i, v := range frames {
frames[i].File = strings.Replace(v.File,
"/go/src/k8s.io/kubernetes/_output/dockerized/go/src/k8s.io/kubernetes",
*k8sPath, 1)
}
for _, curFrame := range frames {
if _, seen := seenLines[fmt.Sprintf("%v:%v", curFrame.File, curFrame.Line)]; seen {
continue
}
freader, err := os.Open(curFrame.File)
if err != nil {
return nil, err
}
defer freader.Close()
cd, err := scanFileForFrame(curFrame.File, freader, curFrame)
if err != nil {
return nil, err
}
if cd != nil {
return cd, nil
}
}
return nil, nil
}
// scanFileForFrame will scan the target and look for a conformance comment attached to the function
// described by the target frame. If the comment can't be found then nil, nil is returned.
func scanFileForFrame(filename string, src interface{}, targetFrame frame) (*conformanceData, error) {
fset := token.NewFileSet() // positions are relative to fset
f, err := parser.ParseFile(fset, filename, src, parser.ParseComments)
if err != nil {
return nil, err
}
cmap := ast.NewCommentMap(fset, f, f.Comments)
for _, cs := range cmap {
for _, c := range cs {
if cd := tryCommentGroupAndFrame(fset, c, targetFrame); cd != nil {
return cd, nil
}
}
}
return nil, nil
}
func validateTestName(s string) error {
matches := regexIneligibleTags.FindAllString(s, -1)
if matches != nil {
return fmt.Errorf("'%s' cannot have invalid tags %v", s, strings.Join(matches, ","))
}
return nil
}
func tryCommentGroupAndFrame(fset *token.FileSet, cg *ast.CommentGroup, f frame) *conformanceData {
if !shouldProcessCommentGroup(fset, cg, f) {
return nil
}
// Each file/line will either be some helper function (not a conformance comment) or apply to just a single test. Don't revisit.
if seenLines != nil {
seenLines[fmt.Sprintf("%v:%v", f.File, f.Line)] = struct{}{}
}
cd := commentToConformanceData(cg.Text())
if cd == nil {
return nil
}
cd.URL = fmt.Sprintf("%s%s#L%d", *baseURL, f.File, f.Line)
cd.File = f.File
return cd
}
func shouldProcessCommentGroup(fset *token.FileSet, cg *ast.CommentGroup, f frame) bool {
lineDiff := f.Line - fset.Position(cg.End()).Line
return lineDiff > 0 && lineDiff <= conformanceCommentsLineWindow
}
func commentToConformanceData(comment string) *conformanceData {
lines := strings.Split(comment, "\n")
descLines := []string{}
cd := &conformanceData{}
for _, line := range lines {
line = strings.TrimSpace(line)
if len(line) == 0 {
continue
}
if sline := regexp.MustCompile("^Testname\\s*:\\s*").Split(line, -1); len(sline) == 2 {
cd.TestName = sline[1]
continue
}
if sline := regexp.MustCompile("^Release\\s*:\\s*").Split(line, -1); len(sline) == 2 {
cd.Release = sline[1]
continue
}
if sline := regexp.MustCompile("^Description\\s*:\\s*").Split(line, -1); len(sline) == 2 {
line = sline[1]
}
descLines = append(descLines, line)
}
if cd.Release == "" && cd.TestName == "" {
return nil
}
cd.Description = strings.Join(descLines, " ")
return cd
}