|  | 
|  | 1 | +package rbac | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"strings" | 
|  | 5 | +	"testing" | 
|  | 6 | + | 
|  | 7 | +	"github.com/onsi/gomega" | 
|  | 8 | +	"sigs.k8s.io/controller-tools/pkg/genall" | 
|  | 9 | +	"sigs.k8s.io/controller-tools/pkg/loader" | 
|  | 10 | +	"sigs.k8s.io/controller-tools/pkg/markers" | 
|  | 11 | +	rbacv1 "k8s.io/api/rbac/v1" | 
|  | 12 | +) | 
|  | 13 | + | 
|  | 14 | +func TestAdvancedFeatureGates(t *testing.T) { | 
|  | 15 | +	g := gomega.NewWithT(t) | 
|  | 16 | + | 
|  | 17 | +	// Load test packages | 
|  | 18 | +	pkgs, err := loader.LoadRoots("./testdata/advanced_feature_gates") | 
|  | 19 | +	g.Expect(err).NotTo(gomega.HaveOccurred()) | 
|  | 20 | + | 
|  | 21 | +	// Set up generation context | 
|  | 22 | +	reg := &markers.Registry{} | 
|  | 23 | +	g.Expect(reg.Register(RuleDefinition)).To(gomega.Succeed()) | 
|  | 24 | +	 | 
|  | 25 | +	ctx := &genall.GenerationContext{ | 
|  | 26 | +		Collector: &markers.Collector{Registry: reg}, | 
|  | 27 | +		Roots:     pkgs, | 
|  | 28 | +	} | 
|  | 29 | + | 
|  | 30 | +	tests := []struct { | 
|  | 31 | +		name           string | 
|  | 32 | +		featureGates   string | 
|  | 33 | +		expectedRules  int | 
|  | 34 | +		shouldContain  []string | 
|  | 35 | +		shouldNotContain []string | 
|  | 36 | +	}{ | 
|  | 37 | +		{ | 
|  | 38 | +			name:         "OR logic - alpha enabled", | 
|  | 39 | +			featureGates: "alpha=true,beta=false", | 
|  | 40 | +			expectedRules: 3, // always-on + OR rule (alpha|beta) | 
|  | 41 | +			shouldContain: []string{"pods", "configmaps", "secrets"}, | 
|  | 42 | +			shouldNotContain: []string{"services"}, | 
|  | 43 | +		}, | 
|  | 44 | +		{ | 
|  | 45 | +			name:         "OR logic - beta enabled", | 
|  | 46 | +			featureGates: "alpha=false,beta=true", | 
|  | 47 | +			expectedRules: 3, // always-on + OR rule (alpha|beta) | 
|  | 48 | +			shouldContain: []string{"pods", "configmaps", "secrets"}, | 
|  | 49 | +			shouldNotContain: []string{"services"}, | 
|  | 50 | +		}, | 
|  | 51 | +		{ | 
|  | 52 | +			name:         "OR logic - both enabled", | 
|  | 53 | +			featureGates: "alpha=true,beta=true", | 
|  | 54 | +			expectedRules: 4, // always-on + OR rule + AND rule | 
|  | 55 | +			shouldContain: []string{"pods", "configmaps", "secrets", "services"}, | 
|  | 56 | +			shouldNotContain: []string{}, | 
|  | 57 | +		}, | 
|  | 58 | +		{ | 
|  | 59 | +			name:         "OR logic - neither enabled", | 
|  | 60 | +			featureGates: "alpha=false,beta=false", | 
|  | 61 | +			expectedRules: 2, // only always-on | 
|  | 62 | +			shouldContain: []string{"pods", "configmaps"}, | 
|  | 63 | +			shouldNotContain: []string{"secrets", "services"}, | 
|  | 64 | +		}, | 
|  | 65 | +		{ | 
|  | 66 | +			name:         "AND logic - only alpha enabled", | 
|  | 67 | +			featureGates: "alpha=true,beta=false", | 
|  | 68 | +			expectedRules: 3, // always-on + OR rule (alpha|beta) | 
|  | 69 | +			shouldContain: []string{"pods", "configmaps", "secrets"}, | 
|  | 70 | +			shouldNotContain: []string{"services"}, | 
|  | 71 | +		}, | 
|  | 72 | +		{ | 
|  | 73 | +			name:         "AND logic - both enabled", | 
|  | 74 | +			featureGates: "alpha=true,beta=true", | 
|  | 75 | +			expectedRules: 4, // always-on + OR rule + AND rule | 
|  | 76 | +			shouldContain: []string{"pods", "configmaps", "secrets", "services"}, | 
|  | 77 | +			shouldNotContain: []string{}, | 
|  | 78 | +		}, | 
|  | 79 | +	} | 
|  | 80 | + | 
|  | 81 | +	for _, tt := range tests { | 
|  | 82 | +		t.Run(tt.name, func(t *testing.T) { | 
|  | 83 | +			g := gomega.NewWithT(t) | 
|  | 84 | +			 | 
|  | 85 | +			objs, err := GenerateRoles(ctx, "test-role", tt.featureGates) | 
|  | 86 | +			g.Expect(err).NotTo(gomega.HaveOccurred()) | 
|  | 87 | +			g.Expect(objs).To(gomega.HaveLen(1)) | 
|  | 88 | + | 
|  | 89 | +			role, ok := objs[0].(rbacv1.ClusterRole) | 
|  | 90 | +			g.Expect(ok).To(gomega.BeTrue()) | 
|  | 91 | +			g.Expect(role.Rules).To(gomega.HaveLen(tt.expectedRules)) | 
|  | 92 | + | 
|  | 93 | +			// Convert rules to string for easier checking | 
|  | 94 | +			rulesStr := "" | 
|  | 95 | +			for _, rule := range role.Rules { | 
|  | 96 | +				rulesStr += strings.Join(rule.Resources, ",") + " " | 
|  | 97 | +			} | 
|  | 98 | + | 
|  | 99 | +			for _, resource := range tt.shouldContain { | 
|  | 100 | +				g.Expect(rulesStr).To(gomega.ContainSubstring(resource),  | 
|  | 101 | +					"Expected resource %s to be present", resource) | 
|  | 102 | +			} | 
|  | 103 | + | 
|  | 104 | +			for _, resource := range tt.shouldNotContain { | 
|  | 105 | +				g.Expect(rulesStr).NotTo(gomega.ContainSubstring(resource), | 
|  | 106 | +					"Expected resource %s to be absent", resource) | 
|  | 107 | +			} | 
|  | 108 | +		}) | 
|  | 109 | +	} | 
|  | 110 | +} | 
|  | 111 | + | 
|  | 112 | +func TestFeatureGateValidation(t *testing.T) { | 
|  | 113 | +	tests := []struct { | 
|  | 114 | +		name        string | 
|  | 115 | +		expression  string | 
|  | 116 | +		shouldError bool | 
|  | 117 | +	}{ | 
|  | 118 | +		{name: "empty expression", expression: "", shouldError: false}, | 
|  | 119 | +		{name: "single gate", expression: "alpha", shouldError: false}, | 
|  | 120 | +		{name: "OR expression", expression: "alpha|beta", shouldError: false}, | 
|  | 121 | +		{name: "AND expression", expression: "alpha&beta", shouldError: false}, | 
|  | 122 | +		{name: "mixed operators", expression: "alpha&beta|gamma", shouldError: true}, | 
|  | 123 | +		{name: "invalid character", expression: "alpha@beta", shouldError: true}, | 
|  | 124 | +		{name: "hyphenated gate", expression: "feature-alpha", shouldError: false}, | 
|  | 125 | +		{name: "underscore gate", expression: "feature_alpha", shouldError: false}, | 
|  | 126 | +		{name: "numeric gate", expression: "v1beta1", shouldError: false}, | 
|  | 127 | +	} | 
|  | 128 | + | 
|  | 129 | +	for _, tt := range tests { | 
|  | 130 | +		t.Run(tt.name, func(t *testing.T) { | 
|  | 131 | +			err := validateFeatureGateExpression(tt.expression) | 
|  | 132 | +			if tt.shouldError { | 
|  | 133 | +				if err == nil { | 
|  | 134 | +					t.Errorf("Expected error for expression %s, but got none", tt.expression) | 
|  | 135 | +				} | 
|  | 136 | +			} else { | 
|  | 137 | +				if err != nil { | 
|  | 138 | +					t.Errorf("Expected no error for expression %s, but got: %v", tt.expression, err) | 
|  | 139 | +				} | 
|  | 140 | +			} | 
|  | 141 | +		}) | 
|  | 142 | +	} | 
|  | 143 | +} | 
0 commit comments