Test library and type names

This commit is contained in:
Joe Betz 2024-09-10 17:07:29 -04:00
parent e085f3818a
commit 430b1de921
8 changed files with 20 additions and 10 deletions

View File

@ -232,7 +232,7 @@ var authzLib = &authz{}
type authz struct{} type authz struct{}
func (*authz) LibraryName() string { func (*authz) LibraryName() string {
return "k8s.authz" return "kubernetes.authz"
} }
func (*authz) Types() []*cel.Type { func (*authz) Types() []*cel.Type {
@ -337,7 +337,7 @@ var authzSelectorsLib = &authzSelectors{}
type authzSelectors struct{} type authzSelectors struct{}
func (*authzSelectors) LibraryName() string { func (*authzSelectors) LibraryName() string {
return "k8s.authzSelectors" return "kubernetes.authzSelectors"
} }
func (*authzSelectors) Types() []*cel.Type { func (*authzSelectors) Types() []*cel.Type {

View File

@ -91,7 +91,7 @@ var formatLib = &format{}
type format struct{} type format struct{}
func (*format) LibraryName() string { func (*format) LibraryName() string {
return "format" return "kubernetes.format"
} }
func (*format) Types() []*cel.Type { func (*format) Types() []*cel.Type {

View File

@ -20,6 +20,7 @@ import (
"github.com/google/cel-go/cel" "github.com/google/cel-go/cel"
"github.com/google/cel-go/common/decls" "github.com/google/cel-go/common/decls"
"github.com/google/cel-go/common/types" "github.com/google/cel-go/common/types"
"strings"
"testing" "testing"
"k8s.io/apimachinery/pkg/util/sets" "k8s.io/apimachinery/pkg/util/sets"
@ -28,6 +29,9 @@ import (
func TestLibraryCompatibility(t *testing.T) { func TestLibraryCompatibility(t *testing.T) {
functionNames := sets.New[string]() functionNames := sets.New[string]()
for _, lib := range KnownLibraries() { for _, lib := range KnownLibraries() {
if !strings.HasPrefix(lib.LibraryName(), "kubernetes.") {
t.Errorf("Expected all kubernetes CEL libraries to have a name package with a 'kubernetes.' prefix but got %v", lib.LibraryName())
}
for name := range lib.declarations() { for name := range lib.declarations() {
functionNames[name] = struct{}{} functionNames[name] = struct{}{}
} }
@ -93,8 +97,11 @@ func TestTypeRegistration(t *testing.T) {
} }
} }
} }
for _, t := range lib.Types() { for _, lb := range lib.Types() {
registeredTypes.Insert(t) registeredTypes.Insert(lb)
if !strings.HasPrefix(lb.TypeName(), "kubernetes.") && !legacyTypeNames.Has(lb.TypeName()) {
t.Errorf("Expected all types in kubernetes CEL libraries to have a type name packaged with a 'kubernetes.' prefix but got %v", lb.TypeName())
}
} }
unregistered := usedTypes.Difference(registeredTypes) unregistered := usedTypes.Difference(registeredTypes)
if len(unregistered) != 0 { if len(unregistered) != 0 {
@ -102,3 +109,6 @@ func TestTypeRegistration(t *testing.T) {
} }
} }
} }
// TODO: Consider renaming these to "kubernetes.net.IP" and "kubernetes.net.CIDR" if we decide not to promote them to cel-go
var legacyTypeNames = sets.New[string]("net.IP", "net.CIDR")

View File

@ -96,7 +96,7 @@ var listsLib = &lists{}
type lists struct{} type lists struct{}
func (*lists) LibraryName() string { func (*lists) LibraryName() string {
return "k8s.lists" return "kubernetes.lists"
} }
func (*lists) Types() []*cel.Type { func (*lists) Types() []*cel.Type {

View File

@ -143,7 +143,7 @@ var quantityLib = &quantity{}
type quantity struct{} type quantity struct{}
func (*quantity) LibraryName() string { func (*quantity) LibraryName() string {
return "k8s.quantity" return "kubernetes.quantity"
} }
func (*quantity) Types() []*cel.Type { func (*quantity) Types() []*cel.Type {

View File

@ -52,7 +52,7 @@ var regexLib = &regex{}
type regex struct{} type regex struct{}
func (*regex) LibraryName() string { func (*regex) LibraryName() string {
return "k8s.regex" return "kubernetes.regex"
} }
func (*regex) Types() []*cel.Type { func (*regex) Types() []*cel.Type {

View File

@ -38,7 +38,7 @@ type testLib struct {
} }
func (*testLib) LibraryName() string { func (*testLib) LibraryName() string {
return "k8s.test" return "kubernetes.test"
} }
type TestOption func(*testLib) *testLib type TestOption func(*testLib) *testLib

View File

@ -113,7 +113,7 @@ var urlsLib = &urls{}
type urls struct{} type urls struct{}
func (*urls) LibraryName() string { func (*urls) LibraryName() string {
return "k8s.urls" return "kubernetes.urls"
} }
func (*urls) Types() []*cel.Type { func (*urls) Types() []*cel.Type {