-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Daniel Weiße <[email protected]>
- Loading branch information
1 parent
7376c6a
commit 93aab8a
Showing
4 changed files
with
119 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
Copyright (c) Edgeless Systems GmbH | ||
SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
|
||
package helm | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/edgelesssys/constellation/v2/internal/logger" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRetryApply(t *testing.T) { | ||
testCases := map[string]struct { | ||
applier *stubRetriableApplier | ||
wantErr bool | ||
}{ | ||
"success": { | ||
applier: &stubRetriableApplier{ | ||
atomic: true, | ||
}, | ||
}, | ||
"two errors": { | ||
applier: &stubRetriableApplier{ | ||
applyErrs: []error{ | ||
assert.AnError, | ||
assert.AnError, | ||
nil, | ||
}, | ||
atomic: true, | ||
}, | ||
}, | ||
"retries are aborted after maximumRetryAttempts": { | ||
applier: &stubRetriableApplier{ | ||
applyErrs: []error{ | ||
assert.AnError, | ||
assert.AnError, | ||
assert.AnError, | ||
assert.AnError, | ||
}, | ||
atomic: true, | ||
}, | ||
wantErr: true, | ||
}, | ||
"non atomic actions are not retried": { | ||
applier: &stubRetriableApplier{ | ||
atomic: false, | ||
applyErrs: []error{ | ||
assert.AnError, | ||
assert.AnError, | ||
nil, | ||
}, | ||
}, | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for name, tc := range testCases { | ||
t.Run(name, func(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
err := retryApply(context.Background(), tc.applier, time.Millisecond, logger.NewTest(t)) | ||
if tc.wantErr { | ||
assert.Error(err) | ||
} else { | ||
assert.NoError(err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
type stubRetriableApplier struct { | ||
atomic bool | ||
applyErrs []error | ||
} | ||
|
||
func (s *stubRetriableApplier) apply(context.Context) error { | ||
if len(s.applyErrs) == 0 { | ||
return nil | ||
} | ||
|
||
// return the first error in the list | ||
// and remove it from the list | ||
err := s.applyErrs[0] | ||
if len(s.applyErrs) > 1 { | ||
s.applyErrs = s.applyErrs[1:] | ||
} else { | ||
s.applyErrs = nil | ||
} | ||
return err | ||
} | ||
|
||
func (s *stubRetriableApplier) ReleaseName() string { | ||
return "" | ||
} | ||
|
||
func (s *stubRetriableApplier) IsAtomic() bool { | ||
return s.atomic | ||
} |