Files
kubemirror/pkg/discovery/discovery_test.go
T
lukaszraczylo 096dca47d1 improvements jan2025 (#6)
* feat(controller): add lazy watcher, improve resource usage and add pattern validation

- [x] Add cache sync health check for readiness probe verification
- [x] Create namespace lister with API reader support for fresh label queries
- [x] Add pattern validation with warning logs for invalid glob patterns
- [x] Implement lazy watcher initialization mode to scan for active resources
- [x] Add requeue delay to namespace reconciler for cache settlement
- [x] Replace custom containsString with slices.Contains from stdlib
- [x] Add structured logging context to reconcilers (kind, group, version)
- [x] Improve error variable naming for clarity in nested conditions
- [x] Add nil-safe label access in namespace reconciler setup
- [x] Add APIReader to namespace and source reconcilers for fresh data
- [x] Improve type assertions with proper error handling in mirror operations
- [x] Reorder struct fields for consistency and readability
- [x] Add comprehensive pattern validation tests and validation API

* feat(controller): add lazy watcher, improve resource usage and add pattern validation

- [x] Add circuit breaker for reconciliation failure tracking and prevention
- [x] Implement granular registration state tracking (not-registered, source-only, fully-registered)
- [x] Add lazy controller initialization for active resource types only
- [x] Consolidate namespace listing into single API call for efficiency
- [x] Add mirror creation verification to catch webhook rejections
- [x] Implement high-cardinality resource detection and warnings
- [x] Add source deletion check in mirror reconciler to prevent races
- [x] Preserve transformation annotations on errors in mirror reconciliation
- [x] Expand constants documentation with labels vs annotations design rationale
- [x] Add comprehensive test coverage for circuit breaker and registration states
- [x] Add mutation-safety tests for hash computation

* fixup! feat(controller): add lazy watcher, improve resource usage and add pattern validation
2026-01-14 13:07:11 +00:00

119 lines
3.3 KiB
Go

package discovery
import (
"testing"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func TestSupportsRequiredVerbs(t *testing.T) {
tests := []struct {
name string
verbs metav1.Verbs
want bool
}{
{
name: "all required verbs present",
verbs: metav1.Verbs{"get", "list", "watch", "create", "update", "patch", "delete"},
want: true,
},
{
name: "exact required verbs",
verbs: metav1.Verbs{"get", "list", "watch", "create", "update", "delete"},
want: true,
},
{
name: "missing create verb",
verbs: metav1.Verbs{"get", "list", "watch", "update", "delete"},
want: false,
},
{
name: "missing watch verb",
verbs: metav1.Verbs{"get", "list", "create", "update", "delete"},
want: false,
},
{
name: "read-only resource",
verbs: metav1.Verbs{"get", "list", "watch"},
want: false,
},
{
name: "empty verbs",
verbs: metav1.Verbs{},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := supportsRequiredVerbs(tt.verbs)
assert.Equal(t, tt.want, got)
})
}
}
func TestIsDeniedResourceType(t *testing.T) {
tests := []struct {
name string
kind string
want bool
}{
// Should be denied
{name: "Pod", kind: "Pod", want: true},
{name: "Event", kind: "Event", want: true},
{name: "Endpoints", kind: "Endpoints", want: true},
{name: "Node", kind: "Node", want: true},
{name: "Lease", kind: "Lease", want: true},
{name: "Namespace", kind: "Namespace", want: true},
{name: "ClusterRole", kind: "ClusterRole", want: true},
{name: "Certificate", kind: "Certificate", want: true}, // cert-manager resources are denied
// Should NOT be denied
{name: "Secret", kind: "Secret", want: false},
{name: "ConfigMap", kind: "ConfigMap", want: false},
{name: "Service", kind: "Service", want: false},
{name: "Ingress", kind: "Ingress", want: false},
{name: "Deployment", kind: "Deployment", want: false},
{name: "StatefulSet", kind: "StatefulSet", want: false},
{name: "Middleware", kind: "Middleware", want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := isDeniedResourceType(tt.kind)
assert.Equal(t, tt.want, got, "isDeniedResourceType(%s) = %v, want %v", tt.kind, got, tt.want)
})
}
}
func TestIsHighCardinalityResource(t *testing.T) {
tests := []struct {
name string
kind string
want bool
}{
// High cardinality resources (should warn)
{name: "ServiceAccount", kind: "ServiceAccount", want: true},
{name: "Role", kind: "Role", want: true},
{name: "RoleBinding", kind: "RoleBinding", want: true},
{name: "NetworkPolicy", kind: "NetworkPolicy", want: true},
{name: "ServiceMonitor", kind: "ServiceMonitor", want: true},
{name: "VirtualService", kind: "VirtualService", want: true},
// Not high cardinality (no warning needed)
{name: "Secret", kind: "Secret", want: false},
{name: "ConfigMap", kind: "ConfigMap", want: false},
{name: "Service", kind: "Service", want: false},
{name: "Deployment", kind: "Deployment", want: false},
{name: "Middleware", kind: "Middleware", want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := isHighCardinalityResource(tt.kind)
assert.Equal(t, tt.want, got, "isHighCardinalityResource(%s) = %v, want %v", tt.kind, got, tt.want)
})
}
}