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