@@ -706,6 +706,123 @@ func TestPlacementManagementOptions(t *testing.T) {
706706 }
707707}
708708
709+ // TestWebhookOptions tests the parsing and validation logic of the webhook options defined in WebhookOptions.
710+ func TestWebhookOptions (t * testing.T ) {
711+ testCases := []struct {
712+ name string
713+ flagSetName string
714+ args []string
715+ wantWebhookOpts WebhookOptions
716+ wantErred bool
717+ wantErrMsgSubStr string
718+ }{
719+ {
720+ name : "all default" ,
721+ flagSetName : "allDefault" ,
722+ args : []string {},
723+ wantWebhookOpts : WebhookOptions {
724+ EnableWebhooks : true ,
725+ ClientConnectionType : "url" ,
726+ ServiceName : "fleetwebhook" ,
727+ EnableGuardRail : false ,
728+ GuardRailWhitelistedUsers : "" ,
729+ GuardRailDenyModifyMemberClusterLabels : false ,
730+ EnableWorkload : false ,
731+ UseCertManager : false ,
732+ },
733+ },
734+ {
735+ name : "all specified" ,
736+ flagSetName : "allSpecified" ,
737+ args : []string {
738+ "--enable-webhook=false" ,
739+ "--webhook-client-connection-type=service" ,
740+ "--webhook-service-name=customwebhook" ,
741+ "--enable-guard-rail=true" ,
742+ "--whitelisted-users=user1,user2" ,
743+ "--deny-modify-member-cluster-labels=true" ,
744+ "--enable-workload=true" ,
745+ "--use-cert-manager=true" ,
746+ },
747+ wantWebhookOpts : WebhookOptions {
748+ EnableWebhooks : false ,
749+ ClientConnectionType : "service" ,
750+ ServiceName : "customwebhook" ,
751+ EnableGuardRail : true ,
752+ GuardRailWhitelistedUsers : "user1,user2" ,
753+ GuardRailDenyModifyMemberClusterLabels : true ,
754+ EnableWorkload : true ,
755+ UseCertManager : true ,
756+ },
757+ },
758+ {
759+ name : "webhook client connection type URL (case-insensitive)" ,
760+ flagSetName : "webhookClientConnTypeURL" ,
761+ args : []string {"--webhook-client-connection-type=URL" },
762+ wantWebhookOpts : WebhookOptions {
763+ EnableWebhooks : true ,
764+ ClientConnectionType : "url" ,
765+ ServiceName : "fleetwebhook" ,
766+ EnableGuardRail : false ,
767+ GuardRailWhitelistedUsers : "" ,
768+ GuardRailDenyModifyMemberClusterLabels : false ,
769+ EnableWorkload : false ,
770+ UseCertManager : false ,
771+ },
772+ },
773+ {
774+ name : "webhook client connection type service (case-insensitive)" ,
775+ flagSetName : "webhookClientConnTypeService" ,
776+ args : []string {"--webhook-client-connection-type=Service" },
777+ wantWebhookOpts : WebhookOptions {
778+ EnableWebhooks : true ,
779+ ClientConnectionType : "service" ,
780+ ServiceName : "fleetwebhook" ,
781+ EnableGuardRail : false ,
782+ GuardRailWhitelistedUsers : "" ,
783+ GuardRailDenyModifyMemberClusterLabels : false ,
784+ EnableWorkload : false ,
785+ UseCertManager : false ,
786+ },
787+ },
788+ {
789+ name : "invalid webhook client connection type" ,
790+ flagSetName : "webhookClientConnTypeInvalid" ,
791+ args : []string {"--webhook-client-connection-type=ftp" },
792+ wantErred : true ,
793+ wantErrMsgSubStr : "invalid webhook client connection type" ,
794+ },
795+ }
796+
797+ for _ , tc := range testCases {
798+ t .Run (tc .name , func (t * testing.T ) {
799+ flags := flag .NewFlagSet (tc .flagSetName , flag .ContinueOnError )
800+ webhookOpts := WebhookOptions {}
801+ webhookOpts .AddFlags (flags )
802+
803+ err := flags .Parse (tc .args )
804+ if tc .wantErred {
805+ if err == nil {
806+ t .Fatalf ("flag Parse() = nil, want erred" )
807+ }
808+
809+ if ! strings .Contains (err .Error (), tc .wantErrMsgSubStr ) {
810+ t .Fatalf ("flag Parse() error = %v, want error msg with sub-string %s" , err , tc .wantErrMsgSubStr )
811+ }
812+ return
813+ }
814+
815+ if err != nil {
816+ t .Fatalf ("flag Parse() = %v, want nil" , err )
817+ }
818+
819+ if diff := cmp .Diff (webhookOpts , tc .wantWebhookOpts ); diff != "" {
820+ t .Errorf ("webhook options diff (-got, +want):\n %s" , diff )
821+ }
822+ })
823+ }
824+ }
825+
709826// TestRateLimitOptions tests the parsing and validation logic of the rate limit options defined in RateLimitOptions.
710827func TestRateLimitOptions (t * testing.T ) {
711828 testCases := []struct {
0 commit comments