@@ -7,24 +7,27 @@ import (
77 "github.com/google/go-cmp/cmp"
88 "github.com/google/go-cmp/cmp/cmpopts"
99 "github.com/google/uuid"
10- "github.com/stackitcloud/stackit-sdk-go/services/opensearch"
10+ opensearch "github.com/stackitcloud/stackit-sdk-go/services/opensearch/v2api "
1111
1212 "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
1313 "github.com/stackitcloud/stackit-cli/internal/pkg/testparams"
1414 "github.com/stackitcloud/stackit-cli/internal/pkg/testutils"
15- "github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1615)
1716
1817type testCtxKey struct {}
1918
20- var testCtx = context .WithValue (context .Background (), testCtxKey {}, "foo" )
21- var testClient = & opensearch.APIClient {}
22- var testProjectId = uuid .NewString ()
23- var testInstanceId = uuid .NewString ()
19+ var (
20+ testCtx = context .WithValue (context .Background (), testCtxKey {}, "foo" )
21+ testClient = & opensearch.APIClient {DefaultAPI : & opensearch.DefaultAPIService {}}
22+ testProjectId = uuid .NewString ()
23+ testRegion = "eu01"
24+ testInstanceId = uuid .NewString ()
25+ )
2426
2527func fixtureFlagValues (mods ... func (flagValues map [string ]string )) map [string ]string {
2628 flagValues := map [string ]string {
2729 globalflags .ProjectIdFlag : testProjectId ,
30+ globalflags .RegionFlag : testRegion ,
2831 instanceIdFlag : testInstanceId ,
2932 }
3033 for _ , mod := range mods {
@@ -37,6 +40,7 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
3740 model := & inputModel {
3841 GlobalFlagModel : & globalflags.GlobalFlagModel {
3942 ProjectId : testProjectId ,
43+ Region : testRegion ,
4044 Verbosity : globalflags .VerbosityDefault ,
4145 },
4246 InstanceId : testInstanceId ,
@@ -48,7 +52,7 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
4852}
4953
5054func fixtureRequest (mods ... func (request * opensearch.ApiCreateCredentialsRequest )) opensearch.ApiCreateCredentialsRequest {
51- request := testClient .CreateCredentials (testCtx , testProjectId , testInstanceId )
55+ request := testClient .DefaultAPI . CreateCredentials (testCtx , testProjectId , testRegion , testInstanceId )
5256 for _ , mod := range mods {
5357 mod (& request )
5458 }
@@ -154,7 +158,7 @@ func TestBuildRequest(t *testing.T) {
154158
155159 diff := cmp .Diff (request , tt .expectedRequest ,
156160 cmp .AllowUnexported (tt .expectedRequest ),
157- cmpopts .EquateComparable (testCtx ),
161+ cmpopts .EquateComparable (testCtx , opensearch. DefaultAPIService {} ),
158162 )
159163 if diff != "" {
160164 t .Fatalf ("Data does not match: %s" , diff )
@@ -165,8 +169,7 @@ func TestBuildRequest(t *testing.T) {
165169
166170func TestOutputResult (t * testing.T ) {
167171 type args struct {
168- outputFormat string
169- showPassword bool
172+ model inputModel
170173 instanceLabel string
171174 resp * opensearch.CredentialsResponse
172175 }
@@ -181,40 +184,20 @@ func TestOutputResult(t *testing.T) {
181184 wantErr : true ,
182185 },
183186 {
184- name : "set no raw in response" ,
185- args : args {
186- resp : & opensearch.CredentialsResponse {
187- Uri : utils .Ptr ("https://opensearch.example.com" ),
188- },
189- },
190- wantErr : true ,
191- },
192- {
193- name : "set empty raw in response" ,
194- args : args {
195- resp : & opensearch.CredentialsResponse {
196- Raw : & opensearch.RawCredentials {},
197- Uri : utils .Ptr ("https://opensearch.example.com" ),
198- },
199- },
200- wantErr : true ,
201- },
202- {
203- name : "set raw but no uri in response" ,
187+ name : "set uri but no raw in response" ,
204188 args : args {
205189 resp : & opensearch.CredentialsResponse {
206- Raw : & opensearch.RawCredentials {
207- Credentials : & opensearch.Credentials {},
208- },
190+ Uri : "https://opensearch.example.com" ,
209191 },
210192 },
211193 wantErr : true ,
212194 },
213195 {
214- name : "set uri but no raw in response " ,
196+ name : "empty response should not cause panic when password should be hidden " ,
215197 args : args {
216- resp : & opensearch.CredentialsResponse {
217- Uri : utils .Ptr ("https://opensearch.example.com" ),
198+ model : inputModel {
199+ GlobalFlagModel : & globalflags.GlobalFlagModel {},
200+ ShowPassword : false ,
218201 },
219202 },
220203 wantErr : true ,
@@ -224,9 +207,12 @@ func TestOutputResult(t *testing.T) {
224207 args : args {
225208 resp : & opensearch.CredentialsResponse {
226209 Raw : & opensearch.RawCredentials {
227- Credentials : & opensearch.Credentials {},
210+ Credentials : opensearch.Credentials {},
228211 },
229- Uri : utils .Ptr ("https://opensearch.example.com" ),
212+ Uri : "https://opensearch.example.com" ,
213+ },
214+ model : inputModel {
215+ GlobalFlagModel : & globalflags.GlobalFlagModel {},
230216 },
231217 },
232218 wantErr : false ,
@@ -235,7 +221,7 @@ func TestOutputResult(t *testing.T) {
235221 params := testparams .NewTestParams ()
236222 for _ , tt := range tests {
237223 t .Run (tt .name , func (t * testing.T ) {
238- if err := outputResult (params .Printer , tt .args .outputFormat , tt . args . showPassword , tt .args .instanceLabel , tt .args .resp ); (err != nil ) != tt .wantErr {
224+ if err := outputResult (params .Printer , tt .args .model , tt .args .instanceLabel , tt .args .resp ); (err != nil ) != tt .wantErr {
239225 t .Errorf ("outputResult() error = %v, wantErr %v" , err , tt .wantErr )
240226 }
241227 })
0 commit comments