k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/hack/conformance/check_conformance_test_requirements.go (about) 1 /* 2 Copyright 2019 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 // This tool is for checking conformance e2e tests follow the requirements 18 // which is https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/conformance-tests.md#conformance-test-requirements 19 package main 20 21 import ( 22 "bufio" 23 "bytes" 24 "errors" 25 "fmt" 26 "os" 27 "regexp" 28 29 "github.com/spf13/cobra" 30 ) 31 32 const ( 33 // e.g. framework.ConformanceIt("should provide secure master service", func(ctx context.Context) { 34 patternStartConformance = `framework.ConformanceIt\(.*, func\(\) {$` 35 patternEndConformance = `}\)$` 36 patternSkip = `e2eskipper.Skip.*\(` 37 ) 38 39 // This function checks the requirement: it works for all providers (e.g., no SkipIfProviderIs/SkipUnlessProviderIs calls) 40 func checkAllProviders(e2eFile string) error { 41 checkFailed := false 42 inConformanceCode := false 43 44 regStartConformance := regexp.MustCompile(patternStartConformance) 45 regEndConformance := regexp.MustCompile(patternEndConformance) 46 regSkip := regexp.MustCompile(patternSkip) 47 48 fileInput, err := os.ReadFile(e2eFile) 49 if err != nil { 50 return fmt.Errorf("Failed to read file %s: %w", e2eFile, err) 51 } 52 scanner := bufio.NewScanner(bytes.NewReader(fileInput)) 53 scanner.Split(bufio.ScanLines) 54 55 for scanner.Scan() { 56 line := scanner.Text() 57 if regStartConformance.MatchString(line) { 58 if inConformanceCode { 59 return errors.New("Missed the end of previous conformance test. There might be a bug in this script.") 60 } 61 inConformanceCode = true 62 } 63 if inConformanceCode { 64 if regSkip.MatchString(line) { 65 // To list all invalid places in a single operation of this tool, here doesn't return error and continues checking. 66 fmt.Fprintf(os.Stderr, "%v: Conformance test should not call any e2eskipper.Skip*()\n", e2eFile) 67 checkFailed = true 68 } 69 if regEndConformance.MatchString(line) { 70 inConformanceCode = false 71 } 72 } 73 } 74 if inConformanceCode { 75 return errors.New("Missed the end of previous conformance test. There might be a bug in this script.") 76 } 77 if checkFailed { 78 return errors.New("We need to fix the above errors.") 79 } 80 return nil 81 } 82 83 func processFile(e2ePath string) error { 84 regGoFile := regexp.MustCompile(`.*\.go`) 85 86 files, err := os.ReadDir(e2ePath) 87 if err != nil { 88 return fmt.Errorf("Failed to read dir %s: %w", e2ePath, err) 89 } 90 for _, file := range files { 91 if file.IsDir() { 92 continue 93 } 94 if !regGoFile.MatchString(file.Name()) { 95 continue 96 } 97 e2eFile := fmt.Sprintf("%s/%s", e2ePath, file.Name()) 98 err = checkAllProviders(e2eFile) 99 if err != nil { 100 return err 101 } 102 } 103 return nil 104 } 105 106 func processDir(e2ePath string) error { 107 err := processFile(e2ePath) 108 if err != nil { 109 return err 110 } 111 112 // Search sub directories if exist 113 files, err := os.ReadDir(e2ePath) 114 if err != nil { 115 return fmt.Errorf("Failed to read dir %s: %w", e2ePath, err) 116 } 117 for _, file := range files { 118 if !file.IsDir() { 119 continue 120 } 121 err = processDir(fmt.Sprintf("%s/%s", e2ePath, file.Name())) 122 if err != nil { 123 return err 124 } 125 } 126 return nil 127 } 128 129 func newCommand() *cobra.Command { 130 cmd := &cobra.Command{ 131 Use: "check_conformance_test_requirements [e2e-test-path]", 132 Short: "Check conformance test code follows the requirements", 133 Run: func(cmd *cobra.Command, args []string) { 134 if len(args) != 1 { 135 cmd.Help() 136 os.Exit(1) 137 } 138 e2eRootPath := args[0] 139 err := processDir(e2eRootPath) 140 if err != nil { 141 fmt.Fprintf(os.Stderr, "Error: %v\n", err) 142 os.Exit(1) 143 } 144 os.Exit(0) 145 }, 146 } 147 return cmd 148 } 149 150 func main() { 151 command := newCommand() 152 if err := command.Execute(); err != nil { 153 os.Exit(1) 154 } 155 }