suite.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Package suite is a simplified version of testify's suite package which has unnecessary dependencies.
  2. // Please remove this package whenever possible.
  3. package suite
  4. import (
  5. "context"
  6. "flag"
  7. "reflect"
  8. "runtime/debug"
  9. "strings"
  10. "testing"
  11. "github.com/docker/docker/testutil"
  12. )
  13. // TimeoutFlag is the flag to set a per-test timeout when running tests. Defaults to `-timeout`.
  14. var TimeoutFlag = flag.Duration("timeout", 0, "DO NOT USE")
  15. var typTestingT = reflect.TypeOf(new(testing.T))
  16. // Run takes a testing suite and runs all of the tests attached to it.
  17. func Run(ctx context.Context, t *testing.T, suite interface{}) {
  18. defer failOnPanic(t)
  19. ctx = testutil.StartSpan(ctx, t)
  20. suiteCtx := ctx
  21. suiteSetupDone := false
  22. defer func() {
  23. if suiteSetupDone {
  24. if tearDownAllSuite, ok := getTeardownAllSuite(suite); ok {
  25. tearDownAllSuite.TearDownSuite(suiteCtx, t)
  26. }
  27. }
  28. }()
  29. methodFinder := reflect.TypeOf(suite)
  30. for index := 0; index < methodFinder.NumMethod(); index++ {
  31. method := methodFinder.Method(index)
  32. if !methodFilter(method.Name, method.Type) {
  33. continue
  34. }
  35. t.Run(method.Name, func(t *testing.T) {
  36. ctx := testutil.StartSpan(ctx, t)
  37. testutil.SetContext(t, ctx)
  38. t.Cleanup(func() {
  39. testutil.CleanupContext(t)
  40. })
  41. defer failOnPanic(t)
  42. if !suiteSetupDone {
  43. if setupAllSuite, ok := getSetupAllSuite(suite); ok {
  44. setupAllSuite.SetUpSuite(suiteCtx, t)
  45. }
  46. suiteSetupDone = true
  47. }
  48. if setupTestSuite, ok := getSetupTestSuite(suite); ok {
  49. setupTestSuite.SetUpTest(ctx, t)
  50. }
  51. defer func() {
  52. if tearDownTestSuite, ok := getTearDownTestSuite(suite); ok {
  53. tearDownTestSuite.TearDownTest(ctx, t)
  54. }
  55. }()
  56. method.Func.Call([]reflect.Value{reflect.ValueOf(suite), reflect.ValueOf(t)})
  57. })
  58. }
  59. }
  60. func getSetupAllSuite(suite interface{}) (SetupAllSuite, bool) {
  61. setupAllSuite, ok := suite.(SetupAllSuite)
  62. if ok {
  63. return setupAllSuite, ok
  64. }
  65. t := reflect.TypeOf(suite)
  66. for i := 0; i < t.NumMethod(); i++ {
  67. if t.Method(i).Name == "SetUpSuite" {
  68. panic("Wrong SetUpSuite signature")
  69. }
  70. }
  71. return nil, false
  72. }
  73. func getSetupTestSuite(suite interface{}) (SetupTestSuite, bool) {
  74. setupAllTest, ok := suite.(SetupTestSuite)
  75. if ok {
  76. return setupAllTest, ok
  77. }
  78. t := reflect.TypeOf(suite)
  79. for i := 0; i < t.NumMethod(); i++ {
  80. if t.Method(i).Name == "SetUpTest" {
  81. panic("Wrong SetUpTest signature")
  82. }
  83. }
  84. return nil, false
  85. }
  86. func getTearDownTestSuite(suite interface{}) (TearDownTestSuite, bool) {
  87. tearDownTest, ok := suite.(TearDownTestSuite)
  88. if ok {
  89. return tearDownTest, ok
  90. }
  91. t := reflect.TypeOf(suite)
  92. for i := 0; i < t.NumMethod(); i++ {
  93. if t.Method(i).Name == "TearDownTest" {
  94. panic("Wrong TearDownTest signature")
  95. }
  96. }
  97. return nil, false
  98. }
  99. func getTeardownAllSuite(suite interface{}) (TearDownAllSuite, bool) {
  100. tearDownAll, ok := suite.(TearDownAllSuite)
  101. if ok {
  102. return tearDownAll, ok
  103. }
  104. t := reflect.TypeOf(suite)
  105. for i := 0; i < t.NumMethod(); i++ {
  106. if t.Method(i).Name == "TearDownSuite" {
  107. panic("Wrong TearDownSuite signature")
  108. }
  109. }
  110. return nil, false
  111. }
  112. func failOnPanic(t *testing.T) {
  113. r := recover()
  114. if r != nil {
  115. t.Errorf("test suite panicked: %v\n%s", r, debug.Stack())
  116. t.FailNow()
  117. }
  118. }
  119. func methodFilter(name string, typ reflect.Type) bool {
  120. return strings.HasPrefix(name, "Test") && typ.NumIn() == 2 && typ.In(1) == typTestingT // 2 params: method receiver and *testing.T
  121. }