Skip to content

Commit

Permalink
Executor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CDimonaco committed Jan 8, 2025
1 parent 59352a6 commit 6c99891
Show file tree
Hide file tree
Showing 5 changed files with 449 additions and 1 deletion.
9 changes: 9 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
with-expecter: true
keeptree: False
packages:
github.com/trento-project/workbench/pkg/operator:
config:
outpkg: "operator"
dir: "pkg/operator"
interfaces:
phaser:
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ go 1.23.4
require (
github.com/jessevdk/go-flags v1.6.1
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.9.0
github.com/tidwall/gjson v1.18.0
golang.org/x/mod v0.21.0
)

require (
github.com/stretchr/testify v1.9.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
golang.org/x/sys v0.29.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
Expand All @@ -23,6 +25,7 @@ golang.org/x/mod v0.21.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
184 changes: 184 additions & 0 deletions pkg/operator/executor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
package operator

import (
"context"
"errors"
"testing"

"github.com/stretchr/testify/assert"
)

func TestExecutorHappyFlow(t *testing.T) {
executionContext := context.Background()
phaser := NewMockphaser(t)
emptyDiff := make(map[string]any)

phaserCall := phaser.On("plan", executionContext).
Return(nil)

commitCall := phaser.On("commit", executionContext).
Return(nil).
NotBefore(phaserCall)

verifyCall := phaser.On("verify", executionContext).
Return(nil).
NotBefore(commitCall)

phaser.On("operationDiff", executionContext).
Return(emptyDiff).
NotBefore(verifyCall)

executor := Executor{
phaser: phaser,
operationID: "operation-id",
}

report := executor.Run(executionContext)

assert.Equal(t, "operation-id", report.OperationID)
assert.Equal(t, VERIFY, report.Success.LastPhase)
assert.Equal(t, emptyDiff, report.Success.Diff)
assert.Nil(t, report.Error)
}

func TestExecutorPlanError(t *testing.T) {
executionContext := context.Background()
phaser := NewMockphaser(t)
planError := errors.New("error during plan phase")

phaser.On("plan", executionContext).
Return(planError)

executor := Executor{
phaser: phaser,
operationID: "operation-id",
}

report := executor.Run(executionContext)

assert.Equal(t, planError.Error(), report.Error.Message)
assert.Equal(t, PLAN, report.Error.ErrorPhase)
assert.Nil(t, report.Success)
}

func TestExecutorCommitErrorWithSuccessfulRollback(t *testing.T) {
executionContext := context.Background()
phaser := NewMockphaser(t)
commitError := errors.New("error during error phase")

phaserCall := phaser.On("plan", executionContext).
Return(nil)

commitCall := phaser.On("commit", executionContext).
Return(commitError).
NotBefore(phaserCall)

phaser.On("rollback", executionContext).
Return(nil).
NotBefore(commitCall)

executor := Executor{
phaser: phaser,
operationID: "operation-id",
}

report := executor.Run(executionContext)

assert.Equal(t, commitError.Error(), report.Error.Message)
assert.Equal(t, COMMIT, report.Error.ErrorPhase)
assert.Nil(t, report.Success)
}

func TestExecutorCommitErrorWithFailedRollback(t *testing.T) {
executionContext := context.Background()
phaser := NewMockphaser(t)
commitError := errors.New("error during error phase")
rollbackError := errors.New("error during rollback phase")

phaserCall := phaser.On("plan", executionContext).
Return(nil)

commitCall := phaser.On("commit", executionContext).
Return(commitError).
NotBefore(phaserCall)

phaser.On("rollback", executionContext).
Return(rollbackError).
NotBefore(commitCall)

executor := Executor{
phaser: phaser,
operationID: "operation-id",
}

report := executor.Run(executionContext)

assert.Equal(t, errors.Join(rollbackError, commitError).Error(), report.Error.Message)
assert.Equal(t, ROLLBACK, report.Error.ErrorPhase)
assert.Nil(t, report.Success)
}

func TestExecutorVerifyErrorWithSuccessfulRollback(t *testing.T) {
executionContext := context.Background()
phaser := NewMockphaser(t)
verifyError := errors.New("error during verify phase")

phaserCall := phaser.On("plan", executionContext).
Return(nil)

commitCall := phaser.On("commit", executionContext).
Return(nil).
NotBefore(phaserCall)

verifyCall := phaser.On("verify", executionContext).
Return(verifyError).
NotBefore(commitCall)

phaser.On("rollback", executionContext).
Return(nil).
NotBefore(verifyCall)

executor := Executor{
phaser: phaser,
operationID: "operation-id",
}

report := executor.Run(executionContext)

assert.Equal(t, verifyError.Error(), report.Error.Message)
assert.Equal(t, VERIFY, report.Error.ErrorPhase)
assert.Nil(t, report.Success)
}

func TestExecutorVerifyErrorWithFailedRollback(t *testing.T) {
executionContext := context.Background()
phaser := NewMockphaser(t)
verifyError := errors.New("error during verify phase")
rollbackError := errors.New("error during rollback phase")

phaserCall := phaser.On("plan", executionContext).
Return(nil)

commitCall := phaser.On("commit", executionContext).
Return(nil).
NotBefore(phaserCall)

verifyCall := phaser.On("verify", executionContext).
Return(verifyError).
NotBefore(commitCall)

phaser.On("rollback", executionContext).
Return(rollbackError).
NotBefore(verifyCall)

executor := Executor{
phaser: phaser,
operationID: "operation-id",
}

report := executor.Run(executionContext)

assert.Equal(t, errors.Join(rollbackError, verifyError).Error(), report.Error.Message)
assert.Equal(t, ROLLBACK, report.Error.ErrorPhase)
assert.Nil(t, report.Success)
}
Loading

0 comments on commit 6c99891

Please sign in to comment.