Skip to content

Commit fd82ca0

Browse files
authored
fix(e2e): remove redundant process ready checks (#606)
Signed-off-by: Chetan Banavikalmutt <[email protected]>
1 parent ffd22fa commit fd82ca0

File tree

2 files changed

+111
-40
lines changed

2 files changed

+111
-40
lines changed

test/e2e/fixture/fixture.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"os"
2121
"time"
2222

23+
"github.com/argoproj-labs/argocd-agent/internal/manager"
2324
"github.com/argoproj-labs/argocd-agent/internal/manager/appproject"
2425
"github.com/argoproj/argo-cd/v3/common"
2526
argoapp "github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
@@ -150,6 +151,22 @@ func EnsureDeletion(ctx context.Context, kclient KubeClient, obj KubeObject) err
150151
return fmt.Errorf("EnsureDeletion: timeout waiting for deletion of %s/%s", key.Namespace, key.Name)
151152
}
152153

154+
// WaitForDeletion will wait for a resource to be deleted
155+
func WaitForDeletion(ctx context.Context, kclient KubeClient, obj KubeObject) error {
156+
key := types.NamespacedName{Name: obj.GetName(), Namespace: obj.GetNamespace()}
157+
for count := 0; count < 60; count++ {
158+
err := kclient.Get(ctx, key, obj, metav1.GetOptions{})
159+
if err != nil {
160+
if errors.IsNotFound(err) {
161+
return nil
162+
}
163+
return err
164+
}
165+
time.Sleep(1 * time.Second)
166+
}
167+
return fmt.Errorf("WaitForDeletion: timeout waiting for deletion of %s/%s", key.Namespace, key.Name)
168+
}
169+
153170
func CleanUp(ctx context.Context, principalClient KubeClient, managedAgentClient KubeClient, autonomousAgentClient KubeClient, clusterDetails *ClusterDetails) error {
154171

155172
var list argoapp.ApplicationList
@@ -158,17 +175,38 @@ func CleanUp(ctx context.Context, principalClient KubeClient, managedAgentClient
158175
// Remove any previously configured env variables from the config file
159176
os.Remove(EnvVariablesFromE2EFile)
160177

178+
// Deletion should always propagate from the source of truth to the managed cluster
179+
// Skip deletion of resources that have been created from a source
180+
isFromSource := func(annotations map[string]string) bool {
181+
if annotations == nil {
182+
return false
183+
}
184+
_, ok := annotations[manager.SourceUIDAnnotation]
185+
return ok
186+
}
187+
161188
// Delete all applications from the autonomous agent
162189
list = argoapp.ApplicationList{}
163190
err = autonomousAgentClient.List(ctx, "argocd", &list, metav1.ListOptions{})
164191
if err != nil {
165192
return err
166193
}
167194
for _, app := range list.Items {
195+
if isFromSource(app.GetAnnotations()) {
196+
continue
197+
}
198+
168199
err = EnsureDeletion(ctx, autonomousAgentClient, &app)
169200
if err != nil {
170201
return err
171202
}
203+
204+
// Wait for the app to be deleted from the control plane
205+
app.SetNamespace("agent-autonomous")
206+
err = WaitForDeletion(ctx, principalClient, &app)
207+
if err != nil {
208+
return err
209+
}
172210
}
173211

174212
// Delete all managed applications from the principal
@@ -178,10 +216,21 @@ func CleanUp(ctx context.Context, principalClient KubeClient, managedAgentClient
178216
return err
179217
}
180218
for _, app := range list.Items {
219+
if isFromSource(app.GetAnnotations()) {
220+
continue
221+
}
222+
181223
err = EnsureDeletion(ctx, principalClient, &app)
182224
if err != nil {
183225
return err
184226
}
227+
228+
// Wait for the app to be deleted from the managed cluster
229+
app.SetNamespace("argocd")
230+
err = WaitForDeletion(ctx, managedAgentClient, &app)
231+
if err != nil {
232+
return err
233+
}
185234
}
186235

187236
// Delete any remaining managed applications left on the managed agent
@@ -220,10 +269,23 @@ func CleanUp(ctx context.Context, principalClient KubeClient, managedAgentClient
220269
if appProject.Name == appproject.DefaultAppProjectName {
221270
continue
222271
}
272+
273+
if isFromSource(appProject.GetAnnotations()) {
274+
continue
275+
}
276+
223277
err = EnsureDeletion(ctx, autonomousAgentClient, &appProject)
224278
if err != nil {
225279
return err
226280
}
281+
282+
// Wait for the appProject to be deleted from the control plane
283+
appProject.SetName("agent-autonomous-" + appProject.Name)
284+
appProject.SetNamespace("argocd")
285+
err = WaitForDeletion(ctx, principalClient, &appProject)
286+
if err != nil {
287+
return err
288+
}
227289
}
228290

229291
// Delete all appProjects from the principal
@@ -236,10 +298,22 @@ func CleanUp(ctx context.Context, principalClient KubeClient, managedAgentClient
236298
if appProject.Name == appproject.DefaultAppProjectName {
237299
continue
238300
}
301+
302+
if isFromSource(appProject.GetAnnotations()) {
303+
continue
304+
}
305+
239306
err = EnsureDeletion(ctx, principalClient, &appProject)
240307
if err != nil {
241308
return err
242309
}
310+
311+
// Wait for the appProject to be deleted from the managed cluster
312+
appProject.SetNamespace("argocd")
313+
err = WaitForDeletion(ctx, managedAgentClient, &appProject)
314+
if err != nil {
315+
return err
316+
}
243317
}
244318

245319
// Delete all appProjects from the managed agent
@@ -278,6 +352,13 @@ func CleanUp(ctx context.Context, principalClient KubeClient, managedAgentClient
278352
if err != nil {
279353
return err
280354
}
355+
356+
// Wait for the repository to be deleted from the managed cluster
357+
repo.SetNamespace("argocd")
358+
err = WaitForDeletion(ctx, managedAgentClient, &repo)
359+
if err != nil {
360+
return err
361+
}
281362
}
282363

283364
// Delete all repositories from the autonomous agent

test/e2e/resync_test.go

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (suite *ResyncTestSuite) TearDownTest() {
4545
}
4646

4747
// Ensure that all the components are running after runnings the tests
48-
if !fixture.IsProcessRunning("process") {
48+
if !fixture.IsProcessRunning("principal") {
4949
err := fixture.StartProcess("principal")
5050
requires.NoError(err)
5151
}
@@ -94,9 +94,7 @@ func (suite *ResyncTestSuite) Test_ResyncDeletionOnPrincipalStartupManaged() {
9494
err = fixture.StartProcess("principal")
9595
requires.NoError(err)
9696

97-
requires.Eventually(func() bool {
98-
return fixture.IsProcessRunning("principal")
99-
}, 30*time.Second, 1*time.Second)
97+
fixture.CheckReadiness(suite.T(), "principal")
10098

10199
requires.Eventually(func() bool {
102100
app := argoapp.Application{}
@@ -137,9 +135,7 @@ func (suite *ResyncTestSuite) Test_ResyncUpdatesOnPrincipalStartupManaged() {
137135
err = fixture.StartProcess("principal")
138136
requires.NoError(err)
139137

140-
requires.Eventually(func() bool {
141-
return fixture.IsProcessRunning("principal")
142-
}, 30*time.Second, 1*time.Second)
138+
fixture.CheckReadiness(suite.T(), "principal")
143139

144140
requires.Eventually(func() bool {
145141
app := argoapp.Application{}
@@ -177,9 +173,7 @@ func (suite *ResyncTestSuite) Test_ResyncDeletionOnAgentStartupManaged() {
177173
err = fixture.StartProcess("agent-managed")
178174
requires.NoError(err)
179175

180-
requires.Eventually(func() bool {
181-
return fixture.IsProcessRunning("agent-managed")
182-
}, 30*time.Second, 1*time.Second)
176+
fixture.CheckReadiness(suite.T(), "agent-managed")
183177

184178
requires.Eventually(func() bool {
185179
app := argoapp.Application{}
@@ -214,9 +208,7 @@ func (suite *ResyncTestSuite) Test_ResyncUpdatesOnAgentStartupManaged() {
214208
err = fixture.StartProcess("agent-managed")
215209
requires.NoError(err)
216210

217-
requires.Eventually(func() bool {
218-
return fixture.IsProcessRunning("agent-managed")
219-
}, 30*time.Second, 1*time.Second)
211+
fixture.CheckReadiness(suite.T(), "agent-managed")
220212

221213
// updates to the app on the agent side should be reverted since principal is the source of truth
222214
requires.Eventually(func() bool {
@@ -253,9 +245,7 @@ func (suite *ResyncTestSuite) Test_ResyncDeletionOnAgentStartupAutonomous() {
253245
err = fixture.StartProcess("agent-autonomous")
254246
requires.NoError(err)
255247

256-
requires.Eventually(func() bool {
257-
return fixture.IsProcessRunning("agent-autonomous")
258-
}, 30*time.Second, 1*time.Second)
248+
fixture.CheckReadiness(suite.T(), "agent-autonomous")
259249

260250
requires.Eventually(func() bool {
261251
app := argoapp.Application{}
@@ -336,9 +326,7 @@ func (suite *ResyncTestSuite) Test_ResyncDeletionOnPrincipalStartupAutonomous()
336326
err = fixture.StartProcess("principal")
337327
requires.NoError(err)
338328

339-
requires.Eventually(func() bool {
340-
return fixture.IsProcessRunning("principal")
341-
}, 30*time.Second, 1*time.Second)
329+
fixture.CheckReadiness(suite.T(), "principal")
342330

343331
requires.Eventually(func() bool {
344332
err := suite.PrincipalClient.Get(suite.Ctx, principalKey, principalApp, metav1.GetOptions{})
@@ -375,9 +363,7 @@ func (suite *ResyncTestSuite) Test_ResyncUpdatesOnPrincipalStartupAutonomous() {
375363
err = fixture.StartProcess("principal")
376364
requires.NoError(err)
377365

378-
requires.Eventually(func() bool {
379-
return fixture.IsProcessRunning("principal")
380-
}, 30*time.Second, 1*time.Second)
366+
fixture.CheckReadiness(suite.T(), "principal")
381367

382368
// updates to the app on the principal side should be reverted since agent is the source of truth in autonomous mode
383369
requires.Eventually(func() bool {
@@ -398,6 +384,8 @@ func (suite *ResyncTestSuite) Test_ResyncOnConnectionLostManagedMode() {
398384
// Restart the agent process so that the agent talks to the principal via Toxiproxy
399385
fixture.RestartAgent(suite.T(), "agent-managed")
400386

387+
fixture.CheckReadiness(suite.T(), "agent-managed")
388+
401389
// Create a managed app
402390
app := suite.createManagedApp()
403391
requires.NotNil(app)
@@ -435,6 +423,8 @@ func (suite *ResyncTestSuite) Test_ResyncOnConnectionLostAutonomousMode() {
435423
// Restart the agent process so that the agent talks to the principal via Toxiproxy
436424
fixture.RestartAgent(suite.T(), "agent-autonomous")
437425

426+
fixture.CheckReadiness(suite.T(), "agent-autonomous")
427+
438428
// Create an autonomous app
439429
app := suite.createAutonomousApp()
440430
requires.NotNil(app)
@@ -493,9 +483,7 @@ func (suite *ResyncTestSuite) Test_RepositoryResync_OnAppProjectUpdate() {
493483
err = fixture.StartProcess("principal")
494484
requires.NoError(err)
495485

496-
requires.Eventually(func() bool {
497-
return fixture.IsProcessRunning("principal")
498-
}, 30*time.Second, 1*time.Second)
486+
fixture.CheckReadiness(suite.T(), "principal")
499487

500488
requires.Eventually(func() bool {
501489
appProject := argoapp.AppProject{}
@@ -542,9 +530,7 @@ func (suite *ResyncTestSuite) Test_RepositoryResync_OnCreation() {
542530
err = fixture.StartProcess("principal")
543531
requires.NoError(err)
544532

545-
requires.Eventually(func() bool {
546-
return fixture.IsProcessRunning("principal")
547-
}, 30*time.Second, 1*time.Second)
533+
fixture.CheckReadiness(suite.T(), "principal")
548534

549535
// Ensure the appProject has been created on the workload cluster
550536
requires.Eventually(func() bool {
@@ -649,6 +635,8 @@ func (suite *ResyncTestSuite) Test_RepositoryResync_OnDeletion() {
649635
err = fixture.StartProcess("principal")
650636
requires.NoError(err)
651637

638+
fixture.CheckReadiness(suite.T(), "principal")
639+
652640
// Ensure the appProject is still present on the workload cluster
653641
requires.Eventually(func() bool {
654642
appProject := argoapp.AppProject{}
@@ -680,9 +668,7 @@ func (suite *ResyncTestSuite) Test_AppProjectResync_OnCreate() {
680668
err = fixture.StartProcess("principal")
681669
requires.NoError(err)
682670

683-
requires.Eventually(func() bool {
684-
return fixture.IsProcessRunning("principal")
685-
}, 30*time.Second, 1*time.Second)
671+
fixture.CheckReadiness(suite.T(), "principal")
686672

687673
projKey := fixture.ToNamespacedName(appProject)
688674

@@ -724,6 +710,8 @@ func (suite *ResyncTestSuite) Test_AppProjectResync_OnUpdate() {
724710
err = fixture.StartProcess("principal")
725711
requires.NoError(err)
726712

713+
fixture.CheckReadiness(suite.T(), "principal")
714+
727715
// Ensure the appProject is updated on the workload cluster
728716
requires.Eventually(func() bool {
729717
appProject := argoapp.AppProject{}
@@ -757,6 +745,8 @@ func (suite *ResyncTestSuite) Test_AppProjectResync_OnDeletion() {
757745
err = fixture.StartProcess("principal")
758746
requires.NoError(err)
759747

748+
fixture.CheckReadiness(suite.T(), "principal")
749+
760750
// Ensure the appProject is deleted from the workload cluster
761751
requires.Eventually(func() bool {
762752
appProject := argoapp.AppProject{}
@@ -766,7 +756,7 @@ func (suite *ResyncTestSuite) Test_AppProjectResync_OnDeletion() {
766756
}
767757

768758
// Delete the AppProject from the control plane when the agent is down
769-
// and ensure that the AppProject is created again when the agent is restarted
759+
// and ensure that the AppProject is deleted from the workload cluster when the agent is restarted
770760
func (suite *ResyncTestSuite) Test_AppProjectResync_DeleteOnAgentDelete() {
771761
requires := suite.Require()
772762

@@ -785,19 +775,21 @@ func (suite *ResyncTestSuite) Test_AppProjectResync_DeleteOnAgentDelete() {
785775
err = suite.PrincipalClient.Delete(suite.Ctx, appProject, metav1.DeleteOptions{})
786776
requires.NoError(err)
787777

788-
// Start the agent and ensure that the appProject is created on the workload cluster
778+
// AppProject should still exist on the workload cluster
779+
err = suite.ManagedAgentClient.Get(suite.Ctx, projKey, appProject, metav1.GetOptions{})
780+
requires.NoError(err)
781+
782+
// Start the agent and ensure that the appProject is deleted from the workload cluster
789783
err = fixture.StartProcess("agent-managed")
790784
requires.NoError(err)
791785

792-
requires.Eventually(func() bool {
793-
return fixture.IsProcessRunning("agent-managed")
794-
}, 30*time.Second, 1*time.Second)
786+
fixture.CheckReadiness(suite.T(), "agent-managed")
795787

796788
// Ensure the appProject has been created on the workload cluster
797789
requires.Eventually(func() bool {
798790
appProject := argoapp.AppProject{}
799791
err := suite.ManagedAgentClient.Get(suite.Ctx, projKey, &appProject, metav1.GetOptions{})
800-
return err == nil
792+
return errors.IsNotFound(err)
801793
}, 30*time.Second, 1*time.Second)
802794
}
803795

@@ -825,9 +817,7 @@ func (suite *ResyncTestSuite) Test_AppProjectResync_CreateOnAgentDelete() {
825817
err = fixture.StartProcess("agent-managed")
826818
requires.NoError(err)
827819

828-
requires.Eventually(func() bool {
829-
return fixture.IsProcessRunning("agent-managed")
830-
}, 30*time.Second, 1*time.Second)
820+
fixture.CheckReadiness(suite.T(), "agent-managed")
831821

832822
// Ensure the appProject has been created on the workload cluster
833823
requires.Eventually(func() bool {

0 commit comments

Comments
 (0)