Skip to content

Commit

Permalink
Registry tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CDimonaco committed Jan 8, 2025
1 parent 6c99891 commit 6d1adf4
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 4 deletions.
6 changes: 5 additions & 1 deletion .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ packages:
outpkg: "operator"
dir: "pkg/operator"
interfaces:
phaser:
phaser:
Operator:
config:
outpkg: "mocks"
dir: "pkg/operator/mocks"
81 changes: 81 additions & 0 deletions pkg/operator/mocks/mock_Operator.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions pkg/operator/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (e *OperatorNotFoundError) Error() string {
type OperatorBuilder func(operationID string, arguments OperatorArguments) Operator

// map[operatorName]map[operatorVersion]OperatorBuilder
type operatorBuildersTree map[string]map[string]OperatorBuilder
type OperatorBuildersTree map[string]map[string]OperatorBuilder

func extractOperatorNameAndVersion(operatorName string) (string, string, error) {
parts := strings.Split(operatorName, "@")
Expand All @@ -35,7 +35,13 @@ func extractOperatorNameAndVersion(operatorName string) (string, string, error)
}

type Registry struct {
operators operatorBuildersTree
operators OperatorBuildersTree
}

func NewRegistry(operators OperatorBuildersTree) *Registry {
return &Registry{
operators: operators,
}
}

func (m *Registry) GetOperatorBuilder(name string) (OperatorBuilder, error) {
Expand Down Expand Up @@ -92,7 +98,7 @@ func (m *Registry) getLatestVersionForOperator(name string) (string, error) {

func StandardRegistry(options ...BaseOperationOption) *Registry {
return &Registry{
operators: operatorBuildersTree{
operators: OperatorBuildersTree{
SaptuneApplySolutionOperatorName: map[string]OperatorBuilder{
"v1": func(operationID string, arguments OperatorArguments) Operator {
return NewSaptuneApplySolution(arguments, operationID, OperatorOptions[saptuneApplySolution]{
Expand Down
111 changes: 111 additions & 0 deletions pkg/operator/registry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package operator_test

import (
"sort"
"testing"

"github.com/stretchr/testify/suite"
"github.com/trento-project/workbench/pkg/operator"
"github.com/trento-project/workbench/pkg/operator/mocks"
)

type RegistryTest struct {
suite.Suite
}

func TestRegistryTest(t *testing.T) {
suite.Run(t, new(RegistryTest))
}

func (suite *RegistryTest) TestRegistryAvailableOperators() {
registry := operator.NewRegistry(operator.OperatorBuildersTree{
operator.SaptuneApplySolutionOperatorName: map[string]operator.OperatorBuilder{
"v1": func(operationID string, arguments operator.OperatorArguments) operator.Operator { return nil },
"v2": func(operationID string, arguments operator.OperatorArguments) operator.Operator { return nil },
},
"test": map[string]operator.OperatorBuilder{
"v1": func(operationID string, arguments operator.OperatorArguments) operator.Operator { return nil },
"v2": func(operationID string, arguments operator.OperatorArguments) operator.Operator { return nil },
},
"test2": map[string]operator.OperatorBuilder{
"v1": func(operationID string, arguments operator.OperatorArguments) operator.Operator { return nil },
},
})

expectedOperators := []string{
"saptuneapplysolution - v1/v2",
"test - v1/v2",
"test2 - v1",
}

// we sort the array in order to have consistency in the tests
// map keys are not ordered ofc

result := registry.AvailableOperators()
sort.Strings(result)

suite.Equal(expectedOperators, result)
}

func (suite *RegistryTest) TestGetOperatorBuilderNotFound() {
registry := operator.NewRegistry(operator.OperatorBuildersTree{
operator.SaptuneApplySolutionOperatorName: map[string]operator.OperatorBuilder{
"v1": func(operationID string, arguments operator.OperatorArguments) operator.Operator { return nil },
"v2": func(operationID string, arguments operator.OperatorArguments) operator.Operator { return nil },
},
"test": map[string]operator.OperatorBuilder{
"v1": func(operationID string, arguments operator.OperatorArguments) operator.Operator { return nil },
"v2": func(operationID string, arguments operator.OperatorArguments) operator.Operator { return nil },
},
"test2": map[string]operator.OperatorBuilder{
"v1": func(operationID string, arguments operator.OperatorArguments) operator.Operator { return nil },
},
})
_, err := registry.GetOperatorBuilder("other@v1")

suite.EqualError(err, "operator other@v1 not found")
}

func (suite *RegistryTest) TestGetOperatorBuilderFoundWithVersion() {
foundOperator := mocks.NewMockOperator(suite.T())
registry := operator.NewRegistry(operator.OperatorBuildersTree{
operator.SaptuneApplySolutionOperatorName: map[string]operator.OperatorBuilder{
"v1": func(operationID string, arguments operator.OperatorArguments) operator.Operator { return foundOperator },
"v2": func(operationID string, arguments operator.OperatorArguments) operator.Operator { return nil },
},
"test": map[string]operator.OperatorBuilder{
"v1": func(operationID string, arguments operator.OperatorArguments) operator.Operator { return nil },
"v2": func(operationID string, arguments operator.OperatorArguments) operator.Operator { return nil },
},
"test2": map[string]operator.OperatorBuilder{
"v1": func(operationID string, arguments operator.OperatorArguments) operator.Operator { return nil },
},
})

b, err := registry.GetOperatorBuilder("saptuneapplysolution@v1")

suite.NoError(err)
suite.Equal(b("", nil), foundOperator)
}

func (suite *RegistryTest) TestGetOperatorBuilderFoundWithoutVersionGetLast() {
foundOperator := mocks.NewMockOperator(suite.T())
registry := operator.NewRegistry(operator.OperatorBuildersTree{
operator.SaptuneApplySolutionOperatorName: map[string]operator.OperatorBuilder{
"v1": func(operationID string, arguments operator.OperatorArguments) operator.Operator { return nil },
"v2": func(operationID string, arguments operator.OperatorArguments) operator.Operator { return foundOperator },
},
"test": map[string]operator.OperatorBuilder{
"v1": func(operationID string, arguments operator.OperatorArguments) operator.Operator { return nil },
"v2": func(operationID string, arguments operator.OperatorArguments) operator.Operator { return nil },
},
"test2": map[string]operator.OperatorBuilder{
"v1": func(operationID string, arguments operator.OperatorArguments) operator.Operator { return nil },
},
})

b, err := registry.GetOperatorBuilder("saptuneapplysolution")

suite.NoError(err)
suite.Equal(b("", nil), foundOperator)
}

0 comments on commit 6d1adf4

Please sign in to comment.