check_conformance_test_requirements.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. Copyright 2019 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. // This tool is for checking conformance e2e tests follow the requirements
  14. // which is https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/conformance-tests.md#conformance-test-requirements
  15. package main
  16. import (
  17. "bufio"
  18. "bytes"
  19. "fmt"
  20. "io/ioutil"
  21. "os"
  22. "regexp"
  23. "github.com/pkg/errors"
  24. "github.com/spf13/cobra"
  25. )
  26. const (
  27. //e.g. framework.ConformanceIt("should provide secure master service ", func() {
  28. patternStartConformance = `framework.ConformanceIt\(.*, func\(\) {$`
  29. patternEndConformance = `}\)$`
  30. patternSkip = `e2eskipper.Skip.*\(`
  31. )
  32. // This function checks the requirement: it works for all providers (e.g., no SkipIfProviderIs/SkipUnlessProviderIs calls)
  33. func checkAllProviders(e2eFile string) error {
  34. checkFailed := false
  35. inConformanceCode := false
  36. regStartConformance := regexp.MustCompile(patternStartConformance)
  37. regEndConformance := regexp.MustCompile(patternEndConformance)
  38. regSkip := regexp.MustCompile(patternSkip)
  39. fileInput, err := ioutil.ReadFile(e2eFile)
  40. if err != nil {
  41. return errors.Wrapf(err, "Failed to read file %s", e2eFile)
  42. }
  43. scanner := bufio.NewScanner(bytes.NewReader(fileInput))
  44. scanner.Split(bufio.ScanLines)
  45. for scanner.Scan() {
  46. line := scanner.Text()
  47. if regStartConformance.MatchString(line) {
  48. if inConformanceCode {
  49. return errors.Errorf("Missed the end of previous conformance test. There might be a bug in this script.")
  50. }
  51. inConformanceCode = true
  52. }
  53. if inConformanceCode {
  54. if regSkip.MatchString(line) {
  55. // To list all invalid places in a single operation of this tool, here doesn't return error and continues checking.
  56. fmt.Fprintf(os.Stderr, "%v: Conformance test should not call any e2eskipper.Skip*()\n", e2eFile)
  57. checkFailed = true
  58. }
  59. if regEndConformance.MatchString(line) {
  60. inConformanceCode = false
  61. }
  62. }
  63. }
  64. if inConformanceCode {
  65. return errors.Errorf("Missed the end of previous conformance test. There might be a bug in this script.")
  66. }
  67. if checkFailed {
  68. return errors.Errorf("We need to fix the above errors.")
  69. }
  70. return nil
  71. }
  72. func processFile(e2ePath string) error {
  73. regGoFile := regexp.MustCompile(`.*\.go`)
  74. files, err := ioutil.ReadDir(e2ePath)
  75. if err != nil {
  76. return errors.Wrapf(err, "Failed to read dir %s", e2ePath)
  77. }
  78. for _, file := range files {
  79. if file.IsDir() {
  80. continue
  81. }
  82. if !regGoFile.MatchString(file.Name()) {
  83. continue
  84. }
  85. e2eFile := fmt.Sprintf("%s/%s", e2ePath, file.Name())
  86. err = checkAllProviders(e2eFile)
  87. if err != nil {
  88. return err
  89. }
  90. }
  91. return nil
  92. }
  93. func processDir(e2ePath string) error {
  94. err := processFile(e2ePath)
  95. if err != nil {
  96. return err
  97. }
  98. // Search sub directories if exist
  99. files, err := ioutil.ReadDir(e2ePath)
  100. if err != nil {
  101. return errors.Wrapf(err, "Failed to read dir %s", e2ePath)
  102. }
  103. for _, file := range files {
  104. if !file.IsDir() {
  105. continue
  106. }
  107. err = processDir(fmt.Sprintf("%s/%s", e2ePath, file.Name()))
  108. if err != nil {
  109. return err
  110. }
  111. }
  112. return nil
  113. }
  114. func newCommand() *cobra.Command {
  115. cmd := &cobra.Command{
  116. Use: "check_conformance_test_requirements [e2e-test-path]",
  117. Short: "Check conformance test code follows the requirements",
  118. Run: func(cmd *cobra.Command, args []string) {
  119. if len(args) != 1 {
  120. cmd.Help()
  121. os.Exit(1)
  122. }
  123. e2eRootPath := args[0]
  124. err := processDir(e2eRootPath)
  125. if err != nil {
  126. fmt.Fprintf(os.Stderr, "Error: %v\n", err)
  127. os.Exit(1)
  128. }
  129. os.Exit(0)
  130. },
  131. }
  132. return cmd
  133. }
  134. func main() {
  135. command := newCommand()
  136. if err := command.Execute(); err != nil {
  137. os.Exit(1)
  138. }
  139. }