@@ -2,6 +2,8 @@ package istiocsr
22
33import (
44 "context"
5+ "fmt"
6+ "slices"
57 "testing"
68
79 appsv1 "k8s.io/api/apps/v1"
@@ -736,3 +738,81 @@ func TestCreateOrApplyDeployments(t *testing.T) {
736738 })
737739 }
738740}
741+
742+ func TestUpdateArgList (t * testing.T ) {
743+ tests := []struct {
744+ name string
745+ updateIstioCSR func (* v1alpha1.IstioCSR )
746+ expectedArgs map [string ]string // key is arg name (without --), value is expected value
747+ }{
748+ {
749+ name : "clusterID not provided should default to Kubernetes" ,
750+ updateIstioCSR : func (istiocsr * v1alpha1.IstioCSR ) {
751+ // Server config is nil, so clusterID should default
752+ },
753+ expectedArgs : map [string ]string {
754+ "cluster-id" : "Kubernetes" ,
755+ },
756+ },
757+ {
758+ name : "clusterID empty string should default to Kubernetes" ,
759+ updateIstioCSR : func (istiocsr * v1alpha1.IstioCSR ) {
760+ istiocsr .Spec .IstioCSRConfig .Server = & v1alpha1.ServerConfig {
761+ ClusterID : "" ,
762+ }
763+ },
764+ expectedArgs : map [string ]string {
765+ "cluster-id" : "Kubernetes" ,
766+ },
767+ },
768+ {
769+ name : "clusterID provided should use custom value" ,
770+ updateIstioCSR : func (istiocsr * v1alpha1.IstioCSR ) {
771+ istiocsr .Spec .IstioCSRConfig .Server = & v1alpha1.ServerConfig {
772+ ClusterID : "cluster-123_dev.local" ,
773+ }
774+ },
775+ expectedArgs : map [string ]string {
776+ "cluster-id" : "cluster-123_dev.local" ,
777+ },
778+ },
779+ }
780+
781+ for _ , tt := range tests {
782+ t .Run (tt .name , func (t * testing.T ) {
783+ deployment := testDeployment ()
784+ istiocsr := testIstioCSR ()
785+ if tt .updateIstioCSR != nil {
786+ tt .updateIstioCSR (istiocsr )
787+ }
788+
789+ updateArgList (deployment , istiocsr )
790+
791+ // Find the istio-csr container and check its arguments
792+ var containerArgs []string
793+ for _ , container := range deployment .Spec .Template .Spec .Containers {
794+ if container .Name == istiocsrContainerName {
795+ containerArgs = container .Args
796+ break
797+ }
798+ }
799+
800+ if len (containerArgs ) == 0 {
801+ t .Fatalf ("Expected container args to be set, but got empty args" )
802+ }
803+
804+ // Verify each expected argument
805+ for argName , expectedValue := range tt .expectedArgs {
806+ expectedArg := fmt .Sprintf ("--%s=%s" , argName , expectedValue )
807+ if ! containsArg (containerArgs , expectedArg ) {
808+ t .Errorf ("Expected to find argument %q in container args, but it was not found. Args: %v" , expectedArg , containerArgs )
809+ }
810+ }
811+ })
812+ }
813+ }
814+
815+ // containsArg checks if the given argument is present in the args list
816+ func containsArg (args []string , targetArg string ) bool {
817+ return slices .Contains (args , targetArg )
818+ }
0 commit comments