main_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. "errors"
  16. "path/filepath"
  17. "reflect"
  18. "testing"
  19. )
  20. var collectCases = []struct {
  21. filename string
  22. code string
  23. output []Test
  24. }{
  25. // Empty: no tests
  26. {"e2e/util_test.go", "", []Test{}},
  27. // Go unit test
  28. {"test/list/main_test.go", `
  29. var num = 3
  30. func Helper(x int) { return x / 0 }
  31. func TestStuff(t *Testing.T) {
  32. }`, []Test{{"test/list/main_test.go:5:1", "k8s.io/kubernetes/test/list", "TestStuff"}},
  33. },
  34. // Describe + It
  35. {"e2e/foo.go", `
  36. var _ = Describe("Feature", func() {
  37. It("should work properly", func() {})
  38. })`, []Test{{"e2e/foo.go:4:2", "[k8s.io] Feature", "should work properly"}},
  39. },
  40. // KubeDescribe + It
  41. {"e2e/foo.go", `
  42. var _ = framework.KubeDescribe("Feature", func() {
  43. It("should work properly", func() {})
  44. })`, []Test{{"e2e/foo.go:4:2", "[k8s.io] Feature", "should work properly"}},
  45. },
  46. // KubeDescribe + Context + It
  47. {"e2e/foo.go", `
  48. var _ = framework.KubeDescribe("Feature", func() {
  49. Context("when offline", func() {
  50. It("should work", func() {})
  51. })
  52. })`, []Test{{"e2e/foo.go:5:3", "[k8s.io] Feature when offline", "should work"}},
  53. },
  54. // KubeDescribe + It(Sprintf)
  55. {"e2e/foo.go", `
  56. var _ = framework.KubeDescribe("Feature", func() {
  57. It(fmt.Sprintf("handles %d nodes", num), func() {})
  58. })`, []Test{{"e2e/foo.go:4:2", "[k8s.io] Feature", "handles * nodes"}},
  59. },
  60. // KubeDescribe + Sprintf + It(var)
  61. {"e2e/foo.go", `
  62. var _ = framework.KubeDescribe("Feature", func() {
  63. arg := fmt.Sprintf("does %s and %v at %d", task, desc, num)
  64. It(arg, func() {})
  65. })`, []Test{{"e2e/foo.go:5:2", "[k8s.io] Feature", "does * and * at *"}},
  66. },
  67. // KubeDescribe + string + It(var)
  68. {"e2e/foo.go", `
  69. var _ = framework.KubeDescribe("Feature", func() {
  70. arg := "does stuff"
  71. It(arg, func() {})
  72. })`, []Test{{"e2e/foo.go:5:2", "[k8s.io] Feature", "does stuff"}},
  73. },
  74. // KubeDescribe + It(unknown)
  75. {"e2e/foo.go", `
  76. var _ = framework.KubeDescribe("Feature", func() {
  77. It(mysteryFunc(), func() {})
  78. })`, []Test{{"e2e/foo.go:4:2", "[k8s.io] Feature", "*"}},
  79. },
  80. }
  81. func TestCollect(t *testing.T) {
  82. for _, test := range collectCases {
  83. code := "package test\n" + test.code
  84. tests := collect(test.filename, code)
  85. if !reflect.DeepEqual(tests, test.output) {
  86. t.Errorf("code:\n%s\ngot %v\nwant %v",
  87. code, tests, test.output)
  88. }
  89. }
  90. }
  91. func TestHandlePath(t *testing.T) {
  92. tl := testList{}
  93. e := errors.New("ex")
  94. if tl.handlePath("foo", nil, e) != e {
  95. t.Error("handlePath not returning errors")
  96. }
  97. if tl.handlePath("foo.txt", nil, nil) != nil {
  98. t.Error("should skip random files")
  99. }
  100. if tl.handlePath("third_party/a_test.go", nil, nil) != filepath.SkipDir {
  101. t.Error("should skip third_party")
  102. }
  103. }