@@ -307,155 +307,116 @@ func TestDeleteDistributionWaitHandler(t *testing.T) {
307307 }
308308}
309309
310- type response struct {
311- state string
312- status int
313- }
314-
315- var (
316- testProjectId = "testProjectId"
317- testDistributionId = "testDistributionId"
318- testCustomDomain = "testCustomDomain"
319- )
320-
321- type repeatableClientMock struct {
322- responseIndex int
323- responses []response
324- }
310+ func TestUpdateDistributionWaitHandler (t * testing.T ) {
311+ projectId := "test-project-id"
312+ distributionId := "test-distribution-id"
313+ statusActive := DistributionStatusActive
314+ statusUpdating := DistributionStatusUpdating
315+ statusCreating := DistributionStatusCreating
316+ statusError := DistributionStatusError
317+ statusDeleting := DistributionStatusDeleting
325318
326- func (mock * repeatableClientMock ) GetDistributionExecute (_ context.Context , _ , _ string ) (* cdn.GetDistributionResponse , error ) {
327- response := mock .responses [mock .responseIndex ]
328- mock .responseIndex ++
329- mock .responseIndex %= len (mock .responses )
330- if response .status >= http .StatusBadRequest {
331- return nil , & oapierror.GenericOpenAPIError {
332- StatusCode : response .status ,
333- ErrorMessage : "simulated error" ,
319+ mockClientFixture := func (patches ... func (tc * mockApiClient )) * mockApiClient {
320+ client := & mockApiClient {
321+ distributions : []* cdn.Distribution {
322+ {
323+ Id : & distributionId ,
324+ ProjectId : & projectId ,
325+ Status : & statusActive ,
326+ },
327+ },
328+ getDistributionError : nil ,
334329 }
335- }
336- return & cdn.GetDistributionResponse {
337- Distribution : & cdn.Distribution {
338- Id : & testDistributionId ,
339- Status : & response .state ,
340- },
341- }, nil
342- }
343-
344- func (mock * repeatableClientMock ) GetCustomDomainExecute (_ context.Context , _ , _ , _ string ) (* cdn.GetCustomDomainResponse , error ) {
345- response := mock .responses [mock .responseIndex ]
346- mock .responseIndex ++
347- mock .responseIndex %= len (mock .responses )
348- if response .status >= http .StatusBadRequest {
349- return nil , & oapierror.GenericOpenAPIError {
350- StatusCode : response .status ,
351- ErrorMessage : "simulated error" ,
330+ for _ , p := range patches {
331+ p (client )
352332 }
333+ return client
353334 }
354- return & cdn.GetCustomDomainResponse {
355- CustomDomain : & cdn.CustomDomain {
356- Name : & testCustomDomain ,
357- Status : cdn .DOMAINSTATUS_ACTIVE .Ptr (),
358- },
359- }, nil
360- }
361-
362- func TestUpdateDistributionWaitHandler (t * testing.T ) {
363-
364- type args struct {
365- projectId string
366- distributionId string
367- }
368- type fields struct {
369- responses []response
370- }
371- tests := []struct {
372- name string
373- args args
374- fields fields
375- wantStatus string
376- wantErr bool
335+ tests := map [string ]struct {
336+ apiClient * mockApiClient
337+ projectId string
338+ distributionId string
339+ expectedDistribution * cdn.GetDistributionResponse
340+ errCheck func (* testing.T , error )
377341 }{
378- {
379- "success immediately" ,
380- args {
381- projectId : testProjectId ,
382- distributionId : testDistributionId ,
383- },
384- fields {
385- responses : [] response {
386- { DistributionStatusActive , 0 } ,
342+ "happyPath" : {
343+ apiClient : mockClientFixture () ,
344+ projectId : projectId ,
345+ distributionId : distributionId ,
346+ expectedDistribution : & cdn. GetDistributionResponse {
347+ Distribution : & cdn. Distribution {
348+ Id : & distributionId ,
349+ ProjectId : & projectId ,
350+ Status : & statusActive ,
387351 },
388352 },
389- DistributionStatusActive ,
390- false ,
353+ errCheck : isNil ,
391354 },
392- {
393- "success delayed" ,
394- args {
395- projectId : testProjectId ,
396- distributionId : testDistributionId ,
397- } ,
398- fields {
399- responses : [] response {
400- { DistributionStatusUpdating , 0 },
401- { DistributionStatusUpdating , 0 },
402- { DistributionStatusActive , http . StatusOK },
403- },
355+ "statusUpdating" : {
356+ apiClient : mockClientFixture ( func ( tc * mockApiClient ) {
357+ tc . distributions [ 0 ]. Status = & statusUpdating
358+ }) ,
359+ projectId : projectId ,
360+ distributionId : distributionId ,
361+ expectedDistribution : nil ,
362+ errCheck : func ( t * testing. T , err error ) {
363+ if err . Error () == "WaitWithContext() has timed out" {
364+ return
365+ }
366+ t . Fatalf ( "Unexpected error: %v" , err )
404367 },
405- DistributionStatusActive ,
406- false ,
407368 },
408- {
409- "request fails" ,
410- args {
411- projectId : testProjectId ,
412- distributionId : testDistributionId ,
413- } ,
414- fields {
415- responses : [] response {
416- { DistributionStatusUpdating , 0 },
417- { DistributionStatusUpdating , 0 },
418- { DistributionStatusUpdating , http . StatusInternalServerError },
419- },
369+ "statusCreating" : {
370+ apiClient : mockClientFixture ( func ( tc * mockApiClient ) {
371+ tc . distributions [ 0 ]. Status = & statusCreating
372+ }) ,
373+ projectId : projectId ,
374+ distributionId : distributionId ,
375+ expectedDistribution : nil ,
376+ errCheck : func ( t * testing. T , err error ) {
377+ if strings . Contains ( err . Error (), "unexpected status CREATING" ) {
378+ return
379+ }
380+ t . Fatalf ( "Unexpected error: %v" , err )
420381 },
421- "" ,
422- true ,
423382 },
424- {
425- "timeout" ,
426- args {
427- projectId : testProjectId ,
428- distributionId : testDistributionId ,
383+ "statusDeleting" : {
384+ apiClient : mockClientFixture (func (tc * mockApiClient ) {
385+ tc .distributions [0 ].Status = & statusDeleting
386+ }),
387+ projectId : projectId ,
388+ distributionId : distributionId ,
389+ expectedDistribution : nil ,
390+ errCheck : func (t * testing.T , err error ) {
391+ if strings .Contains (err .Error (), "updating CDN distribution failed" ) {
392+ return
393+ }
394+ t .Fatalf ("Unexpected error: %v" , err )
429395 },
430- fields {
431- responses : []response {
432- {DistributionStatusUpdating , 0 },
433- },
396+ },
397+ "statusError" : {
398+ apiClient : mockClientFixture (func (tc * mockApiClient ) {
399+ tc .distributions [0 ].Status = & statusError
400+ }),
401+ projectId : projectId ,
402+ distributionId : distributionId ,
403+ expectedDistribution : nil ,
404+ errCheck : func (t * testing.T , err error ) {
405+ if strings .Contains (err .Error (), "updating CDN distribution failed" ) {
406+ return
407+ }
408+ t .Fatalf ("Unexpected error: %v" , err )
434409 },
435- DistributionStatusUpdating ,
436- true ,
437410 },
438411 }
439- for _ , tt := range tests {
440- t .Run (tt .name , func (t * testing.T ) {
441- apiClientMock := repeatableClientMock {
442- responses : tt .fields .responses ,
443- }
444- ctx := context .Background ()
445- handler := UpdateDistributionWaitHandler (ctx , & apiClientMock , tt .args .projectId , tt .args .distributionId )
446- handler .SetTimeout (2 * time .Second )
447- handler .SetSleepBeforeWait (100 * time .Millisecond )
448- handler .SetThrottle (50 * time .Millisecond )
449- response , err := handler .WaitWithContext (ctx )
450- if (err != nil ) != tt .wantErr {
451- t .Fatalf ("handler error %s, wantErr %v" , err , tt .wantErr )
452- }
453- var actualStatus string
454- if response != nil {
455- actualStatus = * response .Distribution .Status
456- }
457- if tt .wantStatus != actualStatus {
458- t .Errorf ("wrong state want %q but got %q" , tt .wantStatus , actualStatus )
412+
413+ for name , tc := range tests {
414+ t .Run (name , func (t * testing.T ) {
415+ handler := UpdateDistributionWaitHandler (context .Background (), tc .apiClient , tc .projectId , tc .distributionId )
416+ dist , err := handler .SetTimeout (10 * time .Millisecond ).WaitWithContext (context .Background ())
417+ tc .errCheck (t , err )
418+ if diff := cmp .Diff (tc .expectedDistribution , dist ); diff != "" {
419+ t .Fatalf ("Unexpected response (-want, +got):\n %s" , diff )
459420 }
460421 })
461422 }
0 commit comments