@@ -112,11 +112,15 @@ func TestParseClusterConfigWithoutExecClusterInfo(t *testing.T) {
112112 }
113113}
114114
115- func TestParseClusterConfigUsesExecClusterInfo (t * testing.T ) {
115+ func TestParseClusterConfigPrefersExplicitConfiguration (t * testing.T ) {
116116 viper .Reset ()
117117 t .Cleanup (viper .Reset )
118- viper .Set (config .ProjectIdKey , uuid .NewString ())
119- viper .Set (config .RegionKey , "conflicting-region" )
118+ explicitProjectID := uuid .NewString ()
119+ explicitRegion := "explicit-region"
120+ explicitClusterName := "explicit-cluster"
121+ explicitOrganizationID := uuid .NewString ()
122+ viper .Set (config .ProjectIdKey , explicitProjectID )
123+ viper .Set (config .RegionKey , explicitRegion )
120124 t .Setenv (envServiceAccountEmail , "workload@sa.stackit.cloud" )
121125
122126 configJSON , err := json .Marshal (fixtureClusterConfig ())
@@ -131,16 +135,48 @@ func TestParseClusterConfigUsesExecClusterInfo(t *testing.T) {
131135 params := testparams .NewTestParams ()
132136 cmd := & cobra.Command {}
133137 configureFlags (cmd )
134- if err := cmd .Flags ().Set (clusterNameFlag , "conflicting-cluster" ); err != nil {
138+ if err := cmd .Flags ().Set (clusterNameFlag , explicitClusterName ); err != nil {
135139 t .Fatalf ("set cluster name flag: %v" , err )
136140 }
137- if err := cmd .Flags ().Set (organizationFlag , uuid . NewString () ); err != nil {
141+ if err := cmd .Flags ().Set (organizationFlag , explicitOrganizationID ); err != nil {
138142 t .Fatalf ("set organization flag: %v" , err )
139143 }
140144 actual , err := parseClusterConfig (params .Printer , cmd , true , true , true )
141145 if err != nil {
142146 t .Fatalf ("parse cluster config: %v" , err )
143147 }
148+ expected := fixtureClusterConfig (func (config * clusterConfig ) {
149+ config .STACKITProjectID = explicitProjectID
150+ config .Region = explicitRegion
151+ config .ClusterName = explicitClusterName
152+ config .OrganizationID = explicitOrganizationID
153+ })
154+ if diff := cmp .Diff (actual , expected , cmpopts .IgnoreFields (clusterConfig {}, "cacheKey" )); diff != "" {
155+ t .Fatalf ("Data does not match: %s" , diff )
156+ }
157+ }
158+
159+ func TestParseClusterConfigUsesExecClusterInfoAsFallback (t * testing.T ) {
160+ viper .Reset ()
161+ t .Cleanup (viper .Reset )
162+ t .Setenv (envServiceAccountEmail , "workload@sa.stackit.cloud" )
163+
164+ configJSON , err := json .Marshal (fixtureClusterConfig ())
165+ if err != nil {
166+ t .Fatalf ("marshal cluster config: %v" , err )
167+ }
168+ setExecCredentialEnv (t , & clientauthenticationv1.Cluster {
169+ Server : "https://api.example.stackit.cloud" ,
170+ Config : runtime.RawExtension {Raw : configJSON },
171+ })
172+
173+ params := testparams .NewTestParams ()
174+ cmd := & cobra.Command {}
175+ configureFlags (cmd )
176+ actual , err := parseClusterConfig (params .Printer , cmd , true , true , true )
177+ if err != nil {
178+ t .Fatalf ("parse cluster config: %v" , err )
179+ }
144180 expected := fixtureClusterConfig ()
145181 if diff := cmp .Diff (actual , expected , cmpopts .IgnoreFields (clusterConfig {}, "cacheKey" )); diff != "" {
146182 t .Fatalf ("Data does not match: %s" , diff )
0 commit comments