Skip to content

Commit aca36f9

Browse files
dimuoncursoragent
andauthored
test(acc): ignore deployment_not_found in the sweeper (#1058)
Search lag and concurrent leftover sweeps can 404 Shutdown after the suite already passed; treat that as success like resource delete. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent f11a0c6 commit aca36f9

2 files changed

Lines changed: 108 additions & 2 deletions

File tree

‎ec/acc/deployment_sweep_test.go‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,19 @@
1818
package acc
1919

2020
import (
21+
"errors"
2122
"log"
23+
"net/http"
2224
"strings"
2325
"sync"
2426
"time"
2527

2628
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
2729

2830
"github.com/elastic/cloud-sdk-go/pkg/api"
31+
"github.com/elastic/cloud-sdk-go/pkg/api/apierror"
2932
"github.com/elastic/cloud-sdk-go/pkg/api/deploymentapi"
33+
"github.com/elastic/cloud-sdk-go/pkg/client/deployments"
3034
"github.com/elastic/cloud-sdk-go/pkg/models"
3135
"github.com/elastic/cloud-sdk-go/pkg/multierror"
3236
"github.com/elastic/cloud-sdk-go/pkg/plan"
@@ -105,12 +109,33 @@ func shutdownDeployment(c *api.API, dep string, wg *sync.WaitGroup) error {
105109
API: c, DeploymentID: dep,
106110
})
107111
if err != nil {
112+
if alreadyDestroyed(err) {
113+
return nil
114+
}
108115
return err
109116
}
110117

111-
return planutil.Wait(plan.TrackChangeParams{
118+
if err := planutil.Wait(plan.TrackChangeParams{
112119
API: c, DeploymentID: dep,
113-
})
120+
}); err != nil && !alreadyDestroyed(err) {
121+
return err
122+
}
123+
return nil
124+
}
125+
126+
func alreadyDestroyed(err error) bool {
127+
if err == nil {
128+
return false
129+
}
130+
var shutdownNF *deployments.ShutdownDeploymentNotFound
131+
if errors.As(err, &shutdownNF) {
132+
return true
133+
}
134+
var getNF *deployments.GetDeploymentNotFound
135+
if errors.As(err, &getNF) {
136+
return true
137+
}
138+
return apierror.IsRuntimeStatusCode(err, http.StatusNotFound)
114139
}
115140

116141
func staleDeployment(lastModified time.Time) bool {

‎ec/acc/deployment_sweep_test_test.go‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,18 @@
1818
package acc
1919

2020
import (
21+
"errors"
22+
"net/http"
23+
"sync"
2124
"testing"
2225
"time"
26+
27+
"github.com/go-openapi/runtime"
28+
29+
"github.com/elastic/cloud-sdk-go/pkg/api"
30+
"github.com/elastic/cloud-sdk-go/pkg/api/apierror"
31+
"github.com/elastic/cloud-sdk-go/pkg/api/mock"
32+
"github.com/elastic/cloud-sdk-go/pkg/client/deployments"
2333
)
2434

2535
func Test_staleDeployment(t *testing.T) {
@@ -59,3 +69,74 @@ func Test_staleDeployment(t *testing.T) {
5969
})
6070
}
6171
}
72+
73+
func Test_alreadyDestroyed(t *testing.T) {
74+
tests := []struct {
75+
name string
76+
err error
77+
want bool
78+
}{
79+
{
80+
name: "nil is not destroyed",
81+
},
82+
{
83+
name: "shutdown 404 is destroyed",
84+
err: &deployments.ShutdownDeploymentNotFound{},
85+
want: true,
86+
},
87+
{
88+
name: "wrapped shutdown 404 is destroyed",
89+
err: apierror.Wrap(&deployments.ShutdownDeploymentNotFound{}),
90+
want: true,
91+
},
92+
{
93+
name: "get 404 is destroyed",
94+
err: &deployments.GetDeploymentNotFound{},
95+
want: true,
96+
},
97+
{
98+
name: "generic 404 is destroyed",
99+
err: &runtime.APIError{Code: http.StatusNotFound},
100+
want: true,
101+
},
102+
{
103+
name: "403 is not destroyed",
104+
err: &runtime.APIError{Code: http.StatusForbidden},
105+
},
106+
{
107+
name: "500 is not destroyed",
108+
err: &runtime.APIError{Code: http.StatusInternalServerError},
109+
},
110+
{
111+
name: "unrelated error is not destroyed",
112+
err: errors.New("boom"),
113+
},
114+
}
115+
for _, tt := range tests {
116+
t.Run(tt.name, func(t *testing.T) {
117+
if got := alreadyDestroyed(tt.err); got != tt.want {
118+
t.Errorf("alreadyDestroyed() = %v, want %v", got, tt.want)
119+
}
120+
})
121+
}
122+
}
123+
124+
func Test_shutdownDeployment_notFound(t *testing.T) {
125+
var wg sync.WaitGroup
126+
wg.Add(1)
127+
err := shutdownDeployment(api.NewMock(mock.SampleNotFoundError()), mock.ValidClusterID, &wg)
128+
wg.Wait()
129+
if err != nil {
130+
t.Errorf("shutdownDeployment() not-found error = %v, want nil", err)
131+
}
132+
}
133+
134+
func Test_shutdownDeployment_serverError(t *testing.T) {
135+
var wg sync.WaitGroup
136+
wg.Add(1)
137+
err := shutdownDeployment(api.NewMock(mock.SampleInternalError()), mock.ValidClusterID, &wg)
138+
wg.Wait()
139+
if err == nil {
140+
t.Fatal("shutdownDeployment() 500 error = nil, want error")
141+
}
142+
}

0 commit comments

Comments
 (0)