walk_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. // KubeDescribe + Context + It
  67. {"e2e/foo.go", `
  68. var _ = framework.KubeDescribe("Feature", func() {
  69. Context("with context", func() {
  70. //Description: By default the stdout and stderr from the process
  71. //being executed in a pod MUST be sent to the pod's logs.
  72. framework.ConformanceIt("should work", func() {})
  73. })
  74. })`, []conformanceData{{URL: "https://github.com/kubernetes/kubernetes/tree/master/e2e/foo.go#L7", TestName: "Feature with context should work",
  75. Description: `By default the stdout and stderr from the process
  76. being executed in a pod MUST be sent to the pod's logs.` + "\n\n"}},
  77. },
  78. {"e2e/foo.go", `
  79. var _ = framework.KubeDescribe("Feature", func() {
  80. Context("with context and extra spaces before It block should still pick up Testname", func() {
  81. // Testname: Test with spaces
  82. //Description: Should pick up testname even if it is not within 3 spaces
  83. //even when executed from memory.
  84. framework.ConformanceIt("should work", func() {})
  85. })
  86. })`, []conformanceData{{URL: "https://github.com/kubernetes/kubernetes/tree/master/e2e/foo.go#L8", TestName: "Test with spaces",
  87. Description: `Should pick up testname even if it is not within 3 spaces
  88. even when executed from memory.` + "\n\n"}},
  89. },
  90. }
  91. func TestConformance(t *testing.T) {
  92. for _, test := range conformanceCases {
  93. code := "package test\n" + test.code
  94. *confDoc = true
  95. tests := scanfile(test.filename, code)
  96. if !reflect.DeepEqual(tests, test.output) {
  97. t.Errorf("code:\n%s\ngot %v\nwant %v",
  98. code, tests, test.output)
  99. }
  100. }
  101. }
  102. func TestNormalizeTestNames(t *testing.T) {
  103. testCases := []struct {
  104. rawName string
  105. normalizedName string
  106. }{
  107. {
  108. "should have monotonically increasing restart count [Slow]",
  109. "should have monotonically increasing restart count",
  110. },
  111. {
  112. " should check is all data is printed ",
  113. "should check is all data is printed",
  114. },
  115. }
  116. for i, tc := range testCases {
  117. actualName := normalizeTestName(tc.rawName)
  118. if actualName != tc.normalizedName {
  119. t.Errorf("test case[%d]: expected normalized name %q, got %q", i, tc.normalizedName, actualName)
  120. }
  121. }
  122. }
  123. func TestValidateTestName(t *testing.T) {
  124. testCases := []struct {
  125. testName string
  126. tagString string
  127. }{
  128. {
  129. "a test case with no tags",
  130. "",
  131. },
  132. {
  133. "a test case with valid tags [LinuxOnly] [NodeConformance] [Serial]",
  134. "",
  135. },
  136. {
  137. "a flaky test case that is invalid [Flaky]",
  138. "[Flaky]",
  139. },
  140. {
  141. "a disruptive test case that is invalid [Disruptive]",
  142. "[Disruptive]",
  143. },
  144. {
  145. "a feature test case that is invalid [Feature:Awesome]",
  146. "[Feature:Awesome]",
  147. },
  148. {
  149. "an alpha test case that is invalid [Alpha]",
  150. "[Alpha]",
  151. },
  152. {
  153. "a test case with multiple invalid tags [Flaky][Disruptive] [Feature:Awesome] [Alpha]",
  154. "[Flaky],[Disruptive],[Feature:Awesome],[Alpha]",
  155. },
  156. {
  157. "[sig-awesome] [Disruptive] a test case with valid and invalid tags [Alpha] [Serial] [Flaky]",
  158. "[Disruptive],[Alpha],[Flaky]",
  159. },
  160. }
  161. for i, tc := range testCases {
  162. err := validateTestName(tc.testName)
  163. if err != nil {
  164. if tc.tagString == "" {
  165. t.Errorf("test case[%d]: expected no validate error, got %q", i, err.Error())
  166. } else {
  167. expectedMsg := fmt.Sprintf("'%s' cannot have invalid tags %s", tc.testName, tc.tagString)
  168. actualMsg := err.Error()
  169. if actualMsg != expectedMsg {
  170. t.Errorf("test case[%d]: expected error message %q, got %q", i, expectedMsg, actualMsg)
  171. }
  172. }
  173. }
  174. }
  175. }