...
1
16
17
18
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
34 patternStartConformance = `framework.ConformanceIt\(.*, func\(\) {$`
35 patternEndConformance = `}\)$`
36 patternSkip = `e2eskipper.Skip.*\(`
37 )
38
39
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
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
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 }
156
View as plain text