walk_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package main
  14. import (
  15. "fmt"
  16. "reflect"
  17. "testing"
  18. )
  19. var conformanceCases = []struct {
  20. filename string
  21. code string
  22. output []conformanceData
  23. }{
  24. // Go unit test
  25. {"test/list/main_test.go", `
  26. var num = 3
  27. func Helper(x int) { return x / 0 }
  28. var _ = Describe("Feature", func() {
  29. /*
  30. Testname: Kubelet-OutputToLogs
  31. Description: By default the stdout and stderr from the process
  32. being executed in a pod MUST be sent to the pod's logs.
  33. */
  34. framework.ConformanceIt("validates describe with ConformanceIt", func() {})
  35. })`, []conformanceData{{URL: "https://github.com/kubernetes/kubernetes/tree/master/test/list/main_test.go#L11", TestName: "Kubelet-OutputToLogs",
  36. Description: `By default the stdout and stderr from the process
  37. being executed in a pod MUST be sent to the pod's logs.` + "\n\n"}},
  38. },
  39. // Describe + It
  40. {"e2e/foo.go", `
  41. var _ = Describe("Feature", func() {
  42. //It should have comment
  43. framework.ConformanceIt("should work properly", func() {})
  44. })`, []conformanceData{{URL: "https://github.com/kubernetes/kubernetes/tree/master/e2e/foo.go#L5", TestName: "Feature should work properly", Description: "It should have comment\n\n"}},
  45. },
  46. // KubeDescribe + It
  47. {"e2e/foo.go", `
  48. var _ = framework.KubeDescribe("Feature", func() {
  49. /*It should have comment*/
  50. framework.ConformanceIt("should work properly", func() {})
  51. })`, []conformanceData{{URL: "https://github.com/kubernetes/kubernetes/tree/master/e2e/foo.go#L5", TestName: "Feature should work properly", Description: "It should have comment\n\n"}},
  52. },
  53. // KubeDescribe + Context + It
  54. {"e2e/foo.go", `
  55. var _ = framework.KubeDescribe("Feature", func() {
  56. Context("when offline", func() {
  57. //Testname: Kubelet-OutputToLogs
  58. //Description: By default the stdout and stderr from the process
  59. //being executed in a pod MUST be sent to the pod's logs.
  60. framework.ConformanceIt("should work", func() {})
  61. })
  62. })`, []conformanceData{{URL: "https://github.com/kubernetes/kubernetes/tree/master/e2e/foo.go#L8", TestName: "Kubelet-OutputToLogs",
  63. Description: `By default the stdout and stderr from the process
  64. being executed in a pod MUST be sent to the pod's logs.` + "\n\n"}},
  65. },
  66. // SIGDescribe + KubeDescribe + It, Describe + KubeDescribe + It
  67. {"e2e/foo.go", `
  68. var _ = framework.SIGDescribe("Feature", func() {
  69. KubeDescribe("Described by", func() {
  70. // Description: description1
  71. framework.ConformanceIt("A ConformanceIt", func() {})
  72. })
  73. Describe("Also described via", func() {
  74. KubeDescribe("A nested", func() {
  75. // Description: description2
  76. framework.ConformanceIt("ConformanceIt", func() {})
  77. })
  78. })
  79. })`, []conformanceData{
  80. {URL: "https://github.com/kubernetes/kubernetes/tree/master/e2e/foo.go#L6", TestName: "Feature Described by A ConformanceIt", Description: "description1\n\n"},
  81. {URL: "https://github.com/kubernetes/kubernetes/tree/master/e2e/foo.go#L11", TestName: "Feature Also described via A nested ConformanceIt", Description: "description2\n\n"},
  82. }},
  83. // KubeDescribe + Context + It
  84. {"e2e/foo.go", `
  85. var _ = framework.KubeDescribe("Feature", func() {
  86. Context("with context", func() {
  87. //Description: By default the stdout and stderr from the process
  88. //being executed in a pod MUST be sent to the pod's logs.
  89. framework.ConformanceIt("should work", func() {})
  90. })
  91. })`, []conformanceData{{URL: "https://github.com/kubernetes/kubernetes/tree/master/e2e/foo.go#L7", TestName: "Feature with context should work",
  92. Description: `By default the stdout and stderr from the process
  93. being executed in a pod MUST be sent to the pod's logs.` + "\n\n"}},
  94. },
  95. {"e2e/foo.go", `
  96. var _ = framework.KubeDescribe("Feature", func() {
  97. Context("with context and extra spaces before It block should still pick up Testname", func() {
  98. // Testname: Test with spaces
  99. //Description: Should pick up testname even if it is not within 3 spaces
  100. //even when executed from memory.
  101. framework.ConformanceIt("should work", func() {})
  102. })
  103. })`, []conformanceData{{URL: "https://github.com/kubernetes/kubernetes/tree/master/e2e/foo.go#L8", TestName: "Test with spaces",
  104. Description: `Should pick up testname even if it is not within 3 spaces
  105. even when executed from memory.` + "\n\n"}},
  106. },
  107. }
  108. func TestConformance(t *testing.T) {
  109. for _, test := range conformanceCases {
  110. code := "package test\n" + test.code
  111. *confDoc = true
  112. tests := scanfile(test.filename, code)
  113. if !reflect.DeepEqual(tests, test.output) {
  114. t.Errorf("code:\n%s\ngot %+v\nwant %+v",
  115. code, tests, test.output)
  116. }
  117. }
  118. }
  119. func TestNormalizeTestNames(t *testing.T) {
  120. testCases := []struct {
  121. rawName string
  122. normalizedName string
  123. }{
  124. {
  125. "should have monotonically increasing restart count [Slow]",
  126. "should have monotonically increasing restart count",
  127. },
  128. {
  129. " should check is all data is printed ",
  130. "should check is all data is printed",
  131. },
  132. }
  133. for i, tc := range testCases {
  134. actualName := normalizeTestName(tc.rawName)
  135. if actualName != tc.normalizedName {
  136. t.Errorf("test case[%d]: expected normalized name %q, got %q", i, tc.normalizedName, actualName)
  137. }
  138. }
  139. }
  140. func TestValidateTestName(t *testing.T) {
  141. testCases := []struct {
  142. testName string
  143. tagString string
  144. }{
  145. {
  146. "a test case with no tags",
  147. "",
  148. },
  149. {
  150. "a test case with valid tags [LinuxOnly] [NodeConformance] [Serial] [Disruptive]",
  151. "",
  152. },
  153. {
  154. "a flaky test case that is invalid [Flaky]",
  155. "[Flaky]",
  156. },
  157. {
  158. "a feature test case that is invalid [Feature:Awesome]",
  159. "[Feature:Awesome]",
  160. },
  161. {
  162. "an alpha test case that is invalid [Alpha]",
  163. "[Alpha]",
  164. },
  165. {
  166. "a test case with multiple invalid tags [Flaky] [Feature:Awesome] [Alpha]",
  167. "[Flaky],[Feature:Awesome],[Alpha]",
  168. },
  169. {
  170. "[sig-awesome] [Alpha] [Disruptive] a test case with valid and invalid tags [Serial] [Flaky]",
  171. "[Alpha],[Flaky]",
  172. },
  173. }
  174. for i, tc := range testCases {
  175. err := validateTestName(tc.testName)
  176. if err != nil {
  177. if tc.tagString == "" {
  178. t.Errorf("test case[%d]: expected no validate error, got %q", i, err.Error())
  179. } else {
  180. expectedMsg := fmt.Sprintf("'%s' cannot have invalid tags %s", tc.testName, tc.tagString)
  181. actualMsg := err.Error()
  182. if actualMsg != expectedMsg {
  183. t.Errorf("test case[%d]: expected error message %q, got %q", i, expectedMsg, actualMsg)
  184. }
  185. }
  186. }
  187. }
  188. }