1
16
17 package main
18
19 import (
20 "fmt"
21 "reflect"
22 "testing"
23 )
24
25 func TestConformance(t *testing.T) {
26 for _, tc := range []struct {
27 desc string
28 filename string
29 code string
30 targetFrame frame
31 output *ConformanceData
32 }{
33 {
34 desc: "Grabs comment above test",
35 filename: "test/list/main_test.go",
36 code: `package test
37
38 var num = 3
39 func Helper(x int) { return x / 0 }
40 var _ = Describe("Feature", func() {
41 /*
42 Testname: Kubelet-OutputToLogs
43 Description: By default the stdout and stderr from the process
44 being executed in a pod MUST be sent to the pod's logs.
45 */
46 framework.ConformanceIt("validates describe with ConformanceIt", func() {})
47 })`,
48 output: &ConformanceData{
49 URL: "https://github.com/kubernetes/kubernetes/tree/master/test/list/main_test.go#L11",
50 TestName: "Kubelet-OutputToLogs",
51 Description: `By default the stdout and stderr from the process being executed in a pod MUST be sent to the pod's logs.`,
52 File: "test/list/main_test.go",
53 },
54 targetFrame: frame{File: "test/list/main_test.go", Line: 11},
55 }, {
56 desc: "Handles extra spaces",
57 filename: "e2e/foo.go",
58 code: `package test
59
60 var _ = SIGDescribe("Feature", func() {
61 Context("with context and extra spaces before It block should still pick up Testname", func() {
62 // Testname: Test with spaces
63 //Description: Should pick up testname even if it is not within 3 spaces
64 //even when executed from memory.
65 framework.ConformanceIt("should work", func() {})
66 })
67 })`,
68 output: &ConformanceData{
69 URL: "https://github.com/kubernetes/kubernetes/tree/master/e2e/foo.go#L8",
70 TestName: "Test with spaces",
71 Description: `Should pick up testname even if it is not within 3 spaces even when executed from memory.`,
72 File: "e2e/foo.go",
73 },
74 targetFrame: frame{File: "e2e/foo.go", Line: 8},
75 }, {
76 desc: "Should target the correct comment based on the line numbers (second)",
77 filename: "e2e/foo.go",
78 code: `package test
79
80 var _ = SIGDescribe("Feature", func() {
81 Context("with context and extra spaces before It block should still pick up Testname", func() {
82 // Testname: First test
83 // Description: Should pick up testname even if it is not within 3 spaces
84 // even when executed from memory.
85 framework.ConformanceIt("should work", func() {})
86
87 // Testname: Second test
88 // Description: Should target the correct test/comment based on the line numbers
89 framework.ConformanceIt("should work", func() {})
90 })
91 })`,
92 output: &ConformanceData{
93 URL: "https://github.com/kubernetes/kubernetes/tree/master/e2e/foo.go#L13",
94 TestName: "Second test",
95 Description: `Should target the correct test/comment based on the line numbers`,
96 File: "e2e/foo.go",
97 },
98 targetFrame: frame{File: "e2e/foo.go", Line: 13},
99 }, {
100 desc: "Should target the correct comment based on the line numbers (first)",
101 filename: "e2e/foo.go",
102 code: `package test
103
104 var _ = SIGDescribe("Feature", func() {
105 Context("with context and extra spaces before It block should still pick up Testname", func() {
106 // Testname: First test
107 // Description: Should target the correct test/comment based on the line numbers
108 framework.ConformanceIt("should work", func() {})
109
110 // Testname: Second test
111 // Description: Should target the correct test/comment based on the line numbers
112 framework.ConformanceIt("should work", func() {})
113 })
114 })`,
115 output: &ConformanceData{
116 URL: "https://github.com/kubernetes/kubernetes/tree/master/e2e/foo.go#L8",
117 TestName: "First test",
118 Description: `Should target the correct test/comment based on the line numbers`,
119 File: "e2e/foo.go",
120 },
121 targetFrame: frame{File: "e2e/foo.go", Line: 8},
122 },
123 } {
124 t.Run(tc.desc, func(t *testing.T) {
125 *confDoc = true
126 cd, err := scanFileForFrame(tc.filename, tc.code, tc.targetFrame)
127 if err != nil {
128 panic(err)
129 }
130 if !reflect.DeepEqual(cd, tc.output) {
131 t.Errorf("code:\n%s\ngot %+v\nwant %+v",
132 tc.code, cd, tc.output)
133 }
134 })
135 }
136 }
137
138 func TestCommentToConformanceData(t *testing.T) {
139 tcs := []struct {
140 desc string
141 input string
142 expected *ConformanceData
143 }{
144 {
145 desc: "Empty comment leads to nil",
146 }, {
147 desc: "No Release or Testname leads to nil",
148 input: "Description: foo",
149 }, {
150 desc: "Release but no Testname should result in nil",
151 input: "Release: v1.1\nDescription: foo",
152 }, {
153 desc: "Testname but no Release does not result in nil",
154 input: "Testname: mytest\nDescription: foo",
155 expected: &ConformanceData{TestName: "mytest", Description: "foo"},
156 }, {
157 desc: "All fields parsed and newlines and whitespace removed from description",
158 input: "Release: v1.1\n\t\tTestname: mytest\n\t\tDescription: foo\n\t\tbar\ndone",
159 expected: &ConformanceData{TestName: "mytest", Release: "v1.1", Description: "foo bar done"},
160 },
161 }
162
163 for _, tc := range tcs {
164 t.Run(tc.desc, func(t *testing.T) {
165 out := commentToConformanceData(tc.input)
166 if !reflect.DeepEqual(out, tc.expected) {
167 t.Errorf("Expected %#v but got %#v", tc.expected, out)
168 }
169 })
170 }
171 }
172
173 func TestValidateTestName(t *testing.T) {
174 testCases := []struct {
175 testName string
176 tagString string
177 }{
178 {
179 "a test case with no tags",
180 "",
181 },
182 {
183 "a test case with valid tags [LinuxOnly] [NodeConformance] [Serial] [Disruptive]",
184 "",
185 },
186 {
187 "a flaky test case that is invalid [Flaky]",
188 "[Flaky]",
189 },
190 {
191 "a feature test case that is invalid [Feature:Awesome]",
192 "[Feature:Awesome]",
193 },
194 {
195 "an alpha test case that is invalid [Alpha]",
196 "[Alpha]",
197 },
198 {
199 "a test case with multiple invalid tags [Flaky] [Feature:Awesome] [Alpha]",
200 "[Flaky],[Feature:Awesome],[Alpha]",
201 },
202 {
203 "[sig-awesome] [Alpha] [Disruptive] a test case with valid and invalid tags [Serial] [Flaky]",
204 "[Alpha],[Flaky]",
205 },
206 }
207 for i, tc := range testCases {
208 err := validateTestName(tc.testName)
209 if err != nil {
210 if tc.tagString == "" {
211 t.Errorf("test case[%d]: expected no validate error, got %q", i, err.Error())
212 } else {
213 expectedMsg := fmt.Sprintf("'%s' cannot have invalid tags %s", tc.testName, tc.tagString)
214 actualMsg := err.Error()
215 if actualMsg != expectedMsg {
216 t.Errorf("test case[%d]: expected error message %q, got %q", i, expectedMsg, actualMsg)
217 }
218 }
219 }
220 }
221 }
222
View as plain text