|
| 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/services/serviceenablement" |
| 11 | +) |
| 12 | + |
| 13 | +// Used for testing service operations |
| 14 | +type apiClientServiceMocked struct { |
| 15 | + serviceId string |
| 16 | + serviceState string |
| 17 | + getServiceFails bool |
| 18 | +} |
| 19 | + |
| 20 | +func (a *apiClientServiceMocked) GetServiceStatusExecute(_ context.Context, _, _ string) (*serviceenablement.ServiceStatus, error) { |
| 21 | + if a.getServiceFails { |
| 22 | + return nil, &oapierror.GenericOpenAPIError{ |
| 23 | + StatusCode: 500, |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + return &serviceenablement.ServiceStatus{ |
| 28 | + ServiceId: &a.serviceId, |
| 29 | + State: &a.serviceState, |
| 30 | + }, nil |
| 31 | +} |
| 32 | + |
| 33 | +func TestEnableServiceWaitHandler(t *testing.T) { |
| 34 | + tests := []struct { |
| 35 | + desc string |
| 36 | + getServiceFails bool |
| 37 | + serviceState string |
| 38 | + wantErr bool |
| 39 | + wantResp bool |
| 40 | + }{ |
| 41 | + { |
| 42 | + desc: "enable_succeeded", |
| 43 | + getServiceFails: false, |
| 44 | + serviceState: ServiceStateEnabled, |
| 45 | + wantErr: false, |
| 46 | + wantResp: true, |
| 47 | + }, |
| 48 | + { |
| 49 | + desc: "enable_failed", |
| 50 | + getServiceFails: false, |
| 51 | + serviceState: ServiceStateDisabled, |
| 52 | + wantErr: true, |
| 53 | + wantResp: false, |
| 54 | + }, |
| 55 | + { |
| 56 | + desc: "enable_failed_2", |
| 57 | + getServiceFails: false, |
| 58 | + serviceState: ServiceStateDisabling, |
| 59 | + wantErr: true, |
| 60 | + wantResp: false, |
| 61 | + }, |
| 62 | + { |
| 63 | + desc: "timeout", |
| 64 | + getServiceFails: false, |
| 65 | + serviceState: ServiceStateEnabling, |
| 66 | + wantErr: true, |
| 67 | + wantResp: false, |
| 68 | + }, |
| 69 | + { |
| 70 | + desc: "get_service_fails", |
| 71 | + getServiceFails: true, |
| 72 | + serviceState: ServiceStateEnabling, |
| 73 | + wantErr: true, |
| 74 | + wantResp: false, |
| 75 | + }, |
| 76 | + } |
| 77 | + for _, tt := range tests { |
| 78 | + t.Run(tt.desc, func(t *testing.T) { |
| 79 | + serviceId := "serviceId" |
| 80 | + |
| 81 | + apiClient := &apiClientServiceMocked{ |
| 82 | + serviceId: serviceId, |
| 83 | + serviceState: tt.serviceState, |
| 84 | + getServiceFails: tt.getServiceFails, |
| 85 | + } |
| 86 | + |
| 87 | + var wantRes *serviceenablement.ServiceStatus |
| 88 | + if tt.wantResp { |
| 89 | + wantRes = &serviceenablement.ServiceStatus{ |
| 90 | + ServiceId: &serviceId, |
| 91 | + State: &tt.serviceState, |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + ctx := context.Background() |
| 96 | + |
| 97 | + handler := EnableServiceWaitHandler(ctx, apiClient, "projectId", serviceId) |
| 98 | + |
| 99 | + gotRes, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(ctx) |
| 100 | + |
| 101 | + if err != nil { |
| 102 | + if !tt.wantErr { |
| 103 | + t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr) |
| 104 | + } |
| 105 | + return |
| 106 | + } |
| 107 | + if !cmp.Equal(gotRes, wantRes) { |
| 108 | + t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes) |
| 109 | + } |
| 110 | + }, |
| 111 | + ) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func TestDisableServiceWaitHandler(t *testing.T) { |
| 116 | + tests := []struct { |
| 117 | + desc string |
| 118 | + getServiceFails bool |
| 119 | + serviceState string |
| 120 | + wantErr bool |
| 121 | + wantResp bool |
| 122 | + }{ |
| 123 | + { |
| 124 | + desc: "disable_succeeded", |
| 125 | + getServiceFails: false, |
| 126 | + serviceState: ServiceStateDisabled, |
| 127 | + wantErr: false, |
| 128 | + wantResp: true, |
| 129 | + }, |
| 130 | + { |
| 131 | + desc: "disable_failed", |
| 132 | + getServiceFails: false, |
| 133 | + serviceState: ServiceStateEnabled, |
| 134 | + wantErr: true, |
| 135 | + wantResp: false, |
| 136 | + }, |
| 137 | + { |
| 138 | + desc: "disable_failed_2", |
| 139 | + getServiceFails: false, |
| 140 | + serviceState: ServiceStateEnabling, |
| 141 | + wantErr: true, |
| 142 | + wantResp: false, |
| 143 | + }, |
| 144 | + { |
| 145 | + desc: "timeout", |
| 146 | + getServiceFails: false, |
| 147 | + serviceState: ServiceStateDisabling, |
| 148 | + wantErr: true, |
| 149 | + wantResp: false, |
| 150 | + }, |
| 151 | + { |
| 152 | + desc: "get_service_fails", |
| 153 | + getServiceFails: true, |
| 154 | + serviceState: ServiceStateDisabling, |
| 155 | + wantErr: true, |
| 156 | + wantResp: false, |
| 157 | + }, |
| 158 | + } |
| 159 | + for _, tt := range tests { |
| 160 | + t.Run(tt.desc, func(t *testing.T) { |
| 161 | + serviceId := "serviceId" |
| 162 | + |
| 163 | + apiClient := &apiClientServiceMocked{ |
| 164 | + serviceId: serviceId, |
| 165 | + serviceState: tt.serviceState, |
| 166 | + getServiceFails: tt.getServiceFails, |
| 167 | + } |
| 168 | + |
| 169 | + var wantRes *serviceenablement.ServiceStatus |
| 170 | + if tt.wantResp { |
| 171 | + wantRes = &serviceenablement.ServiceStatus{ |
| 172 | + ServiceId: &serviceId, |
| 173 | + State: &tt.serviceState, |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + ctx := context.Background() |
| 178 | + |
| 179 | + handler := DisableServiceWaitHandler(ctx, apiClient, "projectId", serviceId) |
| 180 | + |
| 181 | + gotRes, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(ctx) |
| 182 | + |
| 183 | + if err != nil { |
| 184 | + if !tt.wantErr { |
| 185 | + t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr) |
| 186 | + } |
| 187 | + return |
| 188 | + } |
| 189 | + if !cmp.Equal(gotRes, wantRes) { |
| 190 | + t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes) |
| 191 | + } |
| 192 | + }, |
| 193 | + ) |
| 194 | + } |
| 195 | +} |
0 commit comments