|
| 1 | +package wait |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/google/go-cmp/cmp" |
| 9 | + "github.com/stackitcloud/stackit-sdk-go/core/oapierror" |
| 10 | + "github.com/stackitcloud/stackit-sdk-go/core/utils" |
| 11 | + loadbalancer "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer/v2api" |
| 12 | +) |
| 13 | + |
| 14 | +const testRegion = "eu01" |
| 15 | + |
| 16 | +type mockSettings struct { |
| 17 | + instanceName string |
| 18 | + instanceStatus *string |
| 19 | + instanceIsDeleted bool |
| 20 | + instanceGetFails bool |
| 21 | +} |
| 22 | + |
| 23 | +func newAPIMock(settings *mockSettings) loadbalancer.DefaultAPI { |
| 24 | + return &loadbalancer.DefaultAPIServiceMock{ |
| 25 | + GetLoadBalancerExecuteMock: utils.Ptr(func(_ loadbalancer.ApiGetLoadBalancerRequest) (*loadbalancer.LoadBalancer, error) { |
| 26 | + if settings.instanceGetFails { |
| 27 | + return nil, &oapierror.GenericOpenAPIError{ |
| 28 | + StatusCode: 500, |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + if settings.instanceIsDeleted { |
| 33 | + return nil, &oapierror.GenericOpenAPIError{ |
| 34 | + StatusCode: 404, |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + return &loadbalancer.LoadBalancer{ |
| 39 | + Name: &settings.instanceName, |
| 40 | + Status: settings.instanceStatus, |
| 41 | + }, nil |
| 42 | + }), |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func TestCreateInstanceWaitHandler(t *testing.T) { |
| 47 | + tests := []struct { |
| 48 | + desc string |
| 49 | + instanceGetFails bool |
| 50 | + instanceStatus *string |
| 51 | + wantErr bool |
| 52 | + wantResp bool |
| 53 | + }{ |
| 54 | + { |
| 55 | + desc: "create_succeeded", |
| 56 | + instanceGetFails: false, |
| 57 | + instanceStatus: utils.Ptr(LOADBALANCERSTATUS_READY), |
| 58 | + wantErr: false, |
| 59 | + wantResp: true, |
| 60 | + }, |
| 61 | + { |
| 62 | + desc: "create_failed", |
| 63 | + instanceGetFails: false, |
| 64 | + instanceStatus: utils.Ptr(LOADBALANCERSTATUS_ERROR), |
| 65 | + wantErr: true, |
| 66 | + wantResp: true, |
| 67 | + }, |
| 68 | + { |
| 69 | + desc: "create_failed_2", |
| 70 | + instanceGetFails: false, |
| 71 | + instanceStatus: utils.Ptr(LOADBALANCERSTATUS_TERMINATING), |
| 72 | + wantErr: true, |
| 73 | + wantResp: true, |
| 74 | + }, |
| 75 | + { |
| 76 | + desc: "instance_get_fails", |
| 77 | + instanceGetFails: true, |
| 78 | + wantErr: true, |
| 79 | + wantResp: false, |
| 80 | + }, |
| 81 | + { |
| 82 | + desc: "timeout", |
| 83 | + instanceGetFails: false, |
| 84 | + instanceStatus: utils.Ptr(LOADBALANCERSTATUS_PENDING), |
| 85 | + wantErr: true, |
| 86 | + wantResp: false, |
| 87 | + }, |
| 88 | + } |
| 89 | + for _, tt := range tests { |
| 90 | + t.Run(tt.desc, func(t *testing.T) { |
| 91 | + instanceName := "foo-bar" |
| 92 | + |
| 93 | + apiClient := newAPIMock(&mockSettings{ |
| 94 | + instanceName: instanceName, |
| 95 | + instanceStatus: tt.instanceStatus, |
| 96 | + instanceGetFails: tt.instanceGetFails, |
| 97 | + }) |
| 98 | + |
| 99 | + var wantRes *loadbalancer.LoadBalancer |
| 100 | + if tt.wantResp { |
| 101 | + wantRes = &loadbalancer.LoadBalancer{ |
| 102 | + Name: &instanceName, |
| 103 | + Status: tt.instanceStatus, |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + handler := CreateLoadBalancerWaitHandler(context.Background(), apiClient, "", testRegion, instanceName) |
| 108 | + |
| 109 | + gotRes, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background()) |
| 110 | + |
| 111 | + if (err != nil) != tt.wantErr { |
| 112 | + t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr) |
| 113 | + } |
| 114 | + if !cmp.Equal(gotRes, wantRes) { |
| 115 | + t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes) |
| 116 | + } |
| 117 | + }) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +func TestDeleteInstanceWaitHandler(t *testing.T) { |
| 122 | + tests := []struct { |
| 123 | + desc string |
| 124 | + instanceGetFails bool |
| 125 | + instanceIsDeleted bool |
| 126 | + wantErr bool |
| 127 | + }{ |
| 128 | + { |
| 129 | + desc: "delete_succeeded", |
| 130 | + instanceGetFails: false, |
| 131 | + instanceIsDeleted: true, |
| 132 | + wantErr: false, |
| 133 | + }, |
| 134 | + { |
| 135 | + desc: "delete_failed", |
| 136 | + instanceGetFails: false, |
| 137 | + instanceIsDeleted: false, |
| 138 | + wantErr: true, |
| 139 | + }, |
| 140 | + { |
| 141 | + desc: "get_fails", |
| 142 | + instanceGetFails: true, |
| 143 | + wantErr: true, |
| 144 | + }, |
| 145 | + } |
| 146 | + for _, tt := range tests { |
| 147 | + t.Run(tt.desc, func(t *testing.T) { |
| 148 | + instanceName := "foo-bar" |
| 149 | + |
| 150 | + apiClient := newAPIMock(&mockSettings{ |
| 151 | + instanceGetFails: tt.instanceGetFails, |
| 152 | + instanceName: instanceName, |
| 153 | + instanceIsDeleted: tt.instanceIsDeleted, |
| 154 | + }) |
| 155 | + |
| 156 | + handler := DeleteLoadBalancerWaitHandler(context.Background(), apiClient, "", testRegion, instanceName) |
| 157 | + |
| 158 | + _, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background()) |
| 159 | + |
| 160 | + if (err != nil) != tt.wantErr { |
| 161 | + t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr) |
| 162 | + } |
| 163 | + }) |
| 164 | + } |
| 165 | +} |
0 commit comments