|
| 1 | +package v1alpha1_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "go/ast" |
| 6 | + "go/parser" |
| 7 | + "go/token" |
| 8 | + "io/fs" |
| 9 | + "strconv" |
| 10 | + "strings" |
| 11 | + |
| 12 | + . "github.com/onsi/ginkgo/v2" |
| 13 | + . "github.com/onsi/gomega" |
| 14 | + |
| 15 | + operatorutil "github.com/operator-framework/operator-controller/internal/util" |
| 16 | +) |
| 17 | + |
| 18 | +var _ = Describe("OperatorTypes", func() { |
| 19 | + Describe("Condition Type and Reason constants", func() { |
| 20 | + It("should register types in operatorutil.ConditionTypes", func() { |
| 21 | + types, err := parseConstants("Type") |
| 22 | + Expect(err).NotTo(HaveOccurred()) |
| 23 | + |
| 24 | + for _, t := range types { |
| 25 | + Expect(t).To(BeElementOf(operatorutil.ConditionTypes), "Append Type%s to operatorutil.ConditionTypes in this package's init function.", t) |
| 26 | + } |
| 27 | + for _, t := range operatorutil.ConditionTypes { |
| 28 | + Expect(t).To(BeElementOf(types), "There must be a Type%[1]s string literal constant for type %[1]q (i.e. 'const Type%[1]s = %[1]q')", t) |
| 29 | + } |
| 30 | + }) |
| 31 | + It("should register reasons in operatorutil.ConditionReasons", func() { |
| 32 | + reasons, err := parseConstants("Reason") |
| 33 | + Expect(err).NotTo(HaveOccurred()) |
| 34 | + |
| 35 | + for _, r := range reasons { |
| 36 | + Expect(r).To(BeElementOf(operatorutil.ConditionReasons), "Append Reason%s to operatorutil.ConditionReasons in this package's init function.", r) |
| 37 | + } |
| 38 | + for _, r := range operatorutil.ConditionReasons { |
| 39 | + Expect(r).To(BeElementOf(reasons), "There must be a Reason%[1]s string literal constant for reason %[1]q (i.e. 'const Reason%[1]s = %[1]q')", r) |
| 40 | + } |
| 41 | + }) |
| 42 | + }) |
| 43 | +}) |
| 44 | + |
| 45 | +func parseConstants(prefix string) ([]string, error) { |
| 46 | + fset := token.NewFileSet() |
| 47 | + pkgs, err := parser.ParseDir(fset, ".", func(info fs.FileInfo) bool { |
| 48 | + return !strings.HasSuffix(info.Name(), "_test.go") |
| 49 | + }, 0) |
| 50 | + if err != nil { |
| 51 | + return nil, err |
| 52 | + } |
| 53 | + |
| 54 | + var constValues []string |
| 55 | + for _, pkg := range pkgs { |
| 56 | + for _, f := range pkg.Files { |
| 57 | + for _, d := range f.Decls { |
| 58 | + genDecl, ok := d.(*ast.GenDecl) |
| 59 | + if !ok { |
| 60 | + continue |
| 61 | + } |
| 62 | + for _, s := range genDecl.Specs { |
| 63 | + valueSpec, ok := s.(*ast.ValueSpec) |
| 64 | + if !ok || len(valueSpec.Names) != 1 || valueSpec.Names[0].Obj.Kind != ast.Con || !strings.HasPrefix(valueSpec.Names[0].String(), prefix) { |
| 65 | + continue |
| 66 | + } |
| 67 | + for _, val := range valueSpec.Values { |
| 68 | + lit, ok := val.(*ast.BasicLit) |
| 69 | + if !ok || lit.Kind != token.STRING { |
| 70 | + continue |
| 71 | + } |
| 72 | + v, err := strconv.Unquote(lit.Value) |
| 73 | + if err != nil { |
| 74 | + return nil, fmt.Errorf("unquote literal string %s: %v", lit.Value, err) |
| 75 | + } |
| 76 | + constValues = append(constValues, v) |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + return constValues, nil |
| 83 | +} |
0 commit comments