2023-05-16 16:01:50 +00:00
|
|
|
package bundle
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
"github.com/databricks/cli/libs/diag"
|
2023-05-16 16:01:50 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
type mutatorWithError struct {
|
|
|
|
applyCalled int
|
|
|
|
errorMsg string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *mutatorWithError) Name() string {
|
|
|
|
return "mutatorWithError"
|
|
|
|
}
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
func (t *mutatorWithError) Apply(_ context.Context, b *Bundle) diag.Diagnostics {
|
2023-05-16 16:01:50 +00:00
|
|
|
t.applyCalled++
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.Errorf(t.errorMsg)
|
2023-05-16 16:01:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeferredMutatorWhenAllMutatorsSucceed(t *testing.T) {
|
|
|
|
m1 := &testMutator{}
|
|
|
|
m2 := &testMutator{}
|
|
|
|
m3 := &testMutator{}
|
|
|
|
cleanup := &testMutator{}
|
2023-05-24 12:45:19 +00:00
|
|
|
deferredMutator := Defer(Seq(m1, m2, m3), cleanup)
|
2023-05-16 16:01:50 +00:00
|
|
|
|
2023-11-15 14:03:36 +00:00
|
|
|
b := &Bundle{}
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := Apply(context.Background(), b, deferredMutator)
|
|
|
|
assert.NoError(t, diags.Error())
|
2023-05-16 16:01:50 +00:00
|
|
|
|
|
|
|
assert.Equal(t, 1, m1.applyCalled)
|
|
|
|
assert.Equal(t, 1, m2.applyCalled)
|
|
|
|
assert.Equal(t, 1, m3.applyCalled)
|
|
|
|
assert.Equal(t, 1, cleanup.applyCalled)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeferredMutatorWhenFirstFails(t *testing.T) {
|
|
|
|
m1 := &testMutator{}
|
|
|
|
m2 := &testMutator{}
|
|
|
|
mErr := &mutatorWithError{errorMsg: "mutator error occurred"}
|
|
|
|
cleanup := &testMutator{}
|
2023-05-24 12:45:19 +00:00
|
|
|
deferredMutator := Defer(Seq(mErr, m1, m2), cleanup)
|
2023-05-16 16:01:50 +00:00
|
|
|
|
2023-11-15 14:03:36 +00:00
|
|
|
b := &Bundle{}
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := Apply(context.Background(), b, deferredMutator)
|
|
|
|
assert.ErrorContains(t, diags.Error(), "mutator error occurred")
|
2023-05-16 16:01:50 +00:00
|
|
|
|
|
|
|
assert.Equal(t, 1, mErr.applyCalled)
|
|
|
|
assert.Equal(t, 0, m1.applyCalled)
|
|
|
|
assert.Equal(t, 0, m2.applyCalled)
|
|
|
|
assert.Equal(t, 1, cleanup.applyCalled)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeferredMutatorWhenMiddleOneFails(t *testing.T) {
|
|
|
|
m1 := &testMutator{}
|
|
|
|
m2 := &testMutator{}
|
|
|
|
mErr := &mutatorWithError{errorMsg: "mutator error occurred"}
|
|
|
|
cleanup := &testMutator{}
|
2023-05-24 12:45:19 +00:00
|
|
|
deferredMutator := Defer(Seq(m1, mErr, m2), cleanup)
|
2023-05-16 16:01:50 +00:00
|
|
|
|
2023-11-15 14:03:36 +00:00
|
|
|
b := &Bundle{}
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := Apply(context.Background(), b, deferredMutator)
|
|
|
|
assert.ErrorContains(t, diags.Error(), "mutator error occurred")
|
2023-05-16 16:01:50 +00:00
|
|
|
|
|
|
|
assert.Equal(t, 1, m1.applyCalled)
|
|
|
|
assert.Equal(t, 1, mErr.applyCalled)
|
|
|
|
assert.Equal(t, 0, m2.applyCalled)
|
|
|
|
assert.Equal(t, 1, cleanup.applyCalled)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeferredMutatorWhenLastOneFails(t *testing.T) {
|
|
|
|
m1 := &testMutator{}
|
|
|
|
m2 := &testMutator{}
|
|
|
|
mErr := &mutatorWithError{errorMsg: "mutator error occurred"}
|
|
|
|
cleanup := &testMutator{}
|
2023-05-24 12:45:19 +00:00
|
|
|
deferredMutator := Defer(Seq(m1, m2, mErr), cleanup)
|
2023-05-16 16:01:50 +00:00
|
|
|
|
2023-11-15 14:03:36 +00:00
|
|
|
b := &Bundle{}
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := Apply(context.Background(), b, deferredMutator)
|
|
|
|
assert.ErrorContains(t, diags.Error(), "mutator error occurred")
|
2023-05-16 16:01:50 +00:00
|
|
|
|
|
|
|
assert.Equal(t, 1, m1.applyCalled)
|
|
|
|
assert.Equal(t, 1, m2.applyCalled)
|
|
|
|
assert.Equal(t, 1, mErr.applyCalled)
|
|
|
|
assert.Equal(t, 1, cleanup.applyCalled)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeferredMutatorCombinesErrorMessages(t *testing.T) {
|
|
|
|
m1 := &testMutator{}
|
|
|
|
m2 := &testMutator{}
|
|
|
|
mErr := &mutatorWithError{errorMsg: "mutator error occurred"}
|
|
|
|
cleanupErr := &mutatorWithError{errorMsg: "cleanup error occurred"}
|
2023-05-24 12:45:19 +00:00
|
|
|
deferredMutator := Defer(Seq(m1, m2, mErr), cleanupErr)
|
2023-05-16 16:01:50 +00:00
|
|
|
|
2023-11-15 14:03:36 +00:00
|
|
|
b := &Bundle{}
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := Apply(context.Background(), b, deferredMutator)
|
|
|
|
|
|
|
|
var errs []string
|
|
|
|
for _, d := range diags {
|
|
|
|
errs = append(errs, d.Summary)
|
|
|
|
}
|
|
|
|
assert.Contains(t, errs, "mutator error occurred")
|
|
|
|
assert.Contains(t, errs, "cleanup error occurred")
|
2023-05-16 16:01:50 +00:00
|
|
|
|
|
|
|
assert.Equal(t, 1, m1.applyCalled)
|
|
|
|
assert.Equal(t, 1, m2.applyCalled)
|
|
|
|
assert.Equal(t, 1, mErr.applyCalled)
|
|
|
|
assert.Equal(t, 1, cleanupErr.applyCalled)
|
|
|
|
}
|