consul/sdk/testutil/retry/retry.go

233 lines
4.5 KiB
Go
Raw Normal View History

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package retry
import (
"fmt"
"os"
)
var _ TestingTB = &R{}
2020-08-14 20:54:44 +00:00
type R struct {
wrapped TestingTB
retryer Retryer
done bool
fullOutput bool
immediateCleanup bool
attempts []*attempt
}
func (r *R) Cleanup(clean func()) {
if r.immediateCleanup {
a := r.getCurrentAttempt()
a.cleanups = append(a.cleanups, clean)
} else {
r.wrapped.Cleanup(clean)
}
}
func (r *R) Error(args ...any) {
r.Log(args...)
r.Fail()
}
func (r *R) Errorf(format string, args ...any) {
r.Logf(format, args...)
r.Fail()
}
func (r *R) Fail() {
r.getCurrentAttempt().failed = true
}
func (r *R) FailNow() {
r.Fail()
panic(attemptFailed{})
}
func (r *R) Failed() bool {
return r.getCurrentAttempt().failed
}
func (r *R) Fatal(args ...any) {
r.Log(args...)
r.FailNow()
}
func (r *R) Fatalf(format string, args ...any) {
r.Logf(format, args...)
r.FailNow()
}
func (r *R) Helper() {
// *testing.T will just record which functions are helpers by their addresses and
// it doesn't much matter where where we record that they are helpers
r.wrapped.Helper()
}
func (r *R) Log(args ...any) {
r.log(fmt.Sprintln(args...))
}
func (r *R) Logf(format string, args ...any) {
r.log(fmt.Sprintf(format, args...))
}
// Name will return the name of the underlying TestingT.
func (r *R) Name() string {
return r.wrapped.Name()
}
// Setenv will save the current value of the specified env var, set it to the
// specified value and then restore it to the original value in a cleanup function
// once the retry attempt has finished.
func (r *R) Setenv(key, value string) {
prevValue, ok := os.LookupEnv(key)
if err := os.Setenv(key, value); err != nil {
r.wrapped.Fatalf("cannot set environment variable: %v", err)
}
if ok {
r.Cleanup(func() {
os.Setenv(key, prevValue)
})
} else {
r.Cleanup(func() {
os.Unsetenv(key)
})
}
New ACLs (#4791) This PR is almost a complete rewrite of the ACL system within Consul. It brings the features more in line with other HashiCorp products. Obviously there is quite a bit left to do here but most of it is related docs, testing and finishing the last few commands in the CLI. I will update the PR description and check off the todos as I finish them over the next few days/week. Description At a high level this PR is mainly to split ACL tokens from Policies and to split the concepts of Authorization from Identities. A lot of this PR is mostly just to support CRUD operations on ACLTokens and ACLPolicies. These in and of themselves are not particularly interesting. The bigger conceptual changes are in how tokens get resolved, how backwards compatibility is handled and the separation of policy from identity which could lead the way to allowing for alternative identity providers. On the surface and with a new cluster the ACL system will look very similar to that of Nomads. Both have tokens and policies. Both have local tokens. The ACL management APIs for both are very similar. I even ripped off Nomad's ACL bootstrap resetting procedure. There are a few key differences though. Nomad requires token and policy replication where Consul only requires policy replication with token replication being opt-in. In Consul local tokens only work with token replication being enabled though. All policies in Nomad are globally applicable. In Consul all policies are stored and replicated globally but can be scoped to a subset of the datacenters. This allows for more granular access management. Unlike Nomad, Consul has legacy baggage in the form of the original ACL system. The ramifications of this are: A server running the new system must still support other clients using the legacy system. A client running the new system must be able to use the legacy RPCs when the servers in its datacenter are running the legacy system. The primary ACL DC's servers running in legacy mode needs to be a gate that keeps everything else in the entire multi-DC cluster running in legacy mode. So not only does this PR implement the new ACL system but has a legacy mode built in for when the cluster isn't ready for new ACLs. Also detecting that new ACLs can be used is automatic and requires no configuration on the part of administrators. This process is detailed more in the "Transitioning from Legacy to New ACL Mode" section below.
2018-10-19 16:04:07 +00:00
}
// TempDir will use the wrapped TestingT to create a temporary directory
// that will be cleaned up when ALL RETRYING has finished.
func (r *R) TempDir() string {
return r.wrapped.TempDir()
}
// Check will call r.Fatal(err) if err is not nil
func (r *R) Check(err error) {
if err != nil {
r.Fatal(err)
}
}
func (r *R) Stop(err error) {
r.log(err.Error())
r.done = true
}
func (r *R) failCurrentAttempt() {
r.getCurrentAttempt().failed = true
}
func (r *R) log(s string) {
a := r.getCurrentAttempt()
a.output = append(a.output, decorate(s))
}
func (r *R) getCurrentAttempt() *attempt {
if len(r.attempts) == 0 {
panic("no retry attempts have been started yet")
}
return r.attempts[len(r.attempts)-1]
}
// cleanupAttempt will perform all the register cleanup operations recorded
// during execution of the single round of the test function.
func (r *R) cleanupAttempt(a *attempt) {
// Make sure that if a cleanup function panics,
// we still run the remaining cleanup functions.
defer func() {
err := recover()
if err != nil {
r.Stop(fmt.Errorf("error when performing test cleanup: %v", err))
}
if len(a.cleanups) > 0 {
r.cleanupAttempt(a)
}
}()
for len(a.cleanups) > 0 {
var cleanup func()
if len(a.cleanups) > 0 {
last := len(a.cleanups) - 1
cleanup = a.cleanups[last]
a.cleanups = a.cleanups[:last]
}
if cleanup != nil {
cleanup()
}
}
}
// runAttempt will execute one round of the test function and handle cleanups and panic recovery
// of a failed attempt that should not stop retrying.
func (r *R) runAttempt(f func(r *R)) {
r.Helper()
a := &attempt{}
r.attempts = append(r.attempts, a)
defer r.cleanupAttempt(a)
defer func() {
if p := recover(); p != nil && p != (attemptFailed{}) {
panic(p)
}
}()
f(r)
}
func (r *R) run(f func(r *R)) {
r.Helper()
for r.retryer.Continue() {
r.runAttempt(f)
switch {
case r.done:
r.recordRetryFailure()
return
case !r.Failed():
// the current attempt did not fail so we can go ahead and return
return
}
}
// We cannot retry any more and no attempt has succeeded yet.
r.recordRetryFailure()
}
func (r *R) recordRetryFailure() {
r.Helper()
output := r.getCurrentAttempt().output
if r.fullOutput {
var combined []string
for _, attempt := range r.attempts {
combined = append(combined, attempt.output...)
}
output = combined
}
out := dedup(output)
if out != "" {
r.wrapped.Log(out)
}
r.wrapped.FailNow()
2018-02-08 01:29:30 +00:00
}
type attempt struct {
failed bool
output []string
cleanups []func()
}
// attemptFailed is a sentinel value to indicate that the func itself
// didn't panic, rather that `FailNow` was called.
type attemptFailed struct{}