Skip to content

Commit

Permalink
fix: InsertAfterSameScopeValidator now takes project into account.
Browse files Browse the repository at this point in the history
The `InsertAfterSameScopeValidator` mistakenly did not take other projects into account, while it's absolutely valid to reference those.
  • Loading branch information
Laubi committed Dec 19, 2024
1 parent 3dbe7c5 commit 7815785
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 51 deletions.
2 changes: 1 addition & 1 deletion pkg/deploy/internal/classic/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func NewValidator() *validator {

// Validate checks that for each classic config API type, only one config exists with any given name.
// As classic configs are identified by name, ValidateUniqueConfigNames returns errors if a name is used more than once for the same type.
func (v *validator) Validate(_ project.Project, c config.Config) error {
func (v *validator) Validate(_ []project.Project, c config.Config) error {
if v.uniqueNames == nil {
v.uniqueNames = make(map[environmentName]map[classicEndpoint][]config.Config)
}
Expand Down
40 changes: 20 additions & 20 deletions pkg/deploy/internal/classic/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,10 @@ func TestValidate_ValidateCompoundParameterName(t *testing.T) {
config.NameParameter: compoundParam2,
}}

err1 := validator.Validate(project.Project{}, c1)
err1 := validator.Validate([]project.Project{}, c1)
assert.NoError(t, err1)

err2 := validator.Validate(project.Project{}, c2)
err2 := validator.Validate([]project.Project{}, c2)
assert.NoError(t, err2)
})

Expand Down Expand Up @@ -208,10 +208,10 @@ func TestValidate_ValidateCompoundParameterName(t *testing.T) {
config.NameParameter: compoundParam2,
}}

err1 := validator.Validate(project.Project{}, c1)
err1 := validator.Validate([]project.Project{}, c1)
assert.NoError(t, err1)

err2 := validator.Validate(project.Project{}, c2)
err2 := validator.Validate([]project.Project{}, c2)
assert.NoError(t, err2)
})

Expand Down Expand Up @@ -249,10 +249,10 @@ func TestValidate_ValidateCompoundParameterName(t *testing.T) {
//compound value == "forrest gump"
// names equal -> error

err1 := validator.Validate(project.Project{}, c1)
err1 := validator.Validate([]project.Project{}, c1)
assert.NoError(t, err1)

err2 := validator.Validate(project.Project{}, c2)
err2 := validator.Validate([]project.Project{}, c2)
assert.Error(t, err2)
})

Expand Down Expand Up @@ -286,10 +286,10 @@ func TestValidate_ValidateCompoundParameterName(t *testing.T) {
config.NameParameter: compoundParam2,
}}

err1 := validator.Validate(project.Project{}, c1)
err1 := validator.Validate([]project.Project{}, c1)
assert.NoError(t, err1)

err2 := validator.Validate(project.Project{}, c2)
err2 := validator.Validate([]project.Project{}, c2)
assert.Error(t, err2)
})

Expand Down Expand Up @@ -320,13 +320,13 @@ func TestValidate_ValidateCompoundParameterName(t *testing.T) {
Coordinate: coordinate.Coordinate{ConfigId: "SECOND", Project: "project", Type: api.ApplicationMobile},
Parameters: map[string]parameter.Parameter{config.NameParameter: compoundParam1}}

err1 := validator.Validate(project.Project{}, c0)
err1 := validator.Validate([]project.Project{}, c0)
assert.NoError(t, err1)

err2 := validator.Validate(project.Project{}, c1)
err2 := validator.Validate([]project.Project{}, c1)
assert.NoError(t, err2)

err3 := validator.Validate(project.Project{}, c2)
err3 := validator.Validate([]project.Project{}, c2)
assert.Error(t, err3)

})
Expand Down Expand Up @@ -355,13 +355,13 @@ func TestValidate_ValidateCompoundParameterName(t *testing.T) {
Coordinate: coordinate.Coordinate{ConfigId: "SECOND", Project: "project", Type: api.ApplicationMobile},
Parameters: map[string]parameter.Parameter{config.NameParameter: ref1}}

err1 := validator.Validate(project.Project{}, c0)
err1 := validator.Validate([]project.Project{}, c0)
assert.NoError(t, err1)

err2 := validator.Validate(project.Project{}, c1)
err2 := validator.Validate([]project.Project{}, c1)
assert.NoError(t, err2)

err3 := validator.Validate(project.Project{}, c2)
err3 := validator.Validate([]project.Project{}, c2)
assert.Error(t, err3)

})
Expand Down Expand Up @@ -391,21 +391,21 @@ func TestValidate_ValidateCompoundParameterName(t *testing.T) {
Coordinate: coordinate.Coordinate{ConfigId: "SECOND", Project: "project", Type: api.ApplicationMobile},
Parameters: map[string]parameter.Parameter{config.NameParameter: ref2}}

err1 := validator.Validate(project.Project{}, c0)
err1 := validator.Validate([]project.Project{}, c0)
assert.NoError(t, err1)

err2 := validator.Validate(project.Project{}, c1)
err2 := validator.Validate([]project.Project{}, c1)
assert.NoError(t, err2)

err3 := validator.Validate(project.Project{}, c2)
err3 := validator.Validate([]project.Project{}, c2)
assert.NoError(t, err3)

})

}

func newTestConfigForValidation(t *testing.T, coordinate coordinate.Coordinate, configType config.Type, parameters map[string]parameter.Parameter) (project.Project, config.Config) {
return project.Project{}, config.Config{
func newTestConfigForValidation(t *testing.T, coordinate coordinate.Coordinate, configType config.Type, parameters map[string]parameter.Parameter) ([]project.Project, config.Config) {
return []project.Project{}, config.Config{
Coordinate: coordinate,
Type: configType,
Environment: "dev",
Expand All @@ -414,7 +414,7 @@ func newTestConfigForValidation(t *testing.T, coordinate coordinate.Coordinate,
}
}

func newTestClassicConfigForValidation(t *testing.T, configId string, apiID string, parameters map[string]parameter.Parameter) (project.Project, config.Config) {
func newTestClassicConfigForValidation(t *testing.T, configId string, apiID string, parameters map[string]parameter.Parameter) ([]project.Project, config.Config) {
return newTestConfigForValidation(
t,
coordinate.Coordinate{Project: "project", Type: apiID, ConfigId: configId},
Expand Down
28 changes: 22 additions & 6 deletions pkg/deploy/internal/setting/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var deprecatedSchemas = map[string]string{
}

// Validate checks for each settings type whether it is using a deprecated schema.
func (v *DeprecatedSchemaValidator) Validate(_ project.Project, c config.Config) error {
func (v *DeprecatedSchemaValidator) Validate(_ []project.Project, c config.Config) error {

s, ok := c.Type.(config.SettingsType)
if !ok {
Expand All @@ -53,9 +53,10 @@ func (v *DeprecatedSchemaValidator) Validate(_ project.Project, c config.Config)
}

var (
errDiffSchema = errors.New("different schemas")
errDiffScope = errors.New("different scopes")
errReferencedNotFound = errors.New("reference not found")
errDiffSchema = errors.New("different schemas")
errDiffScope = errors.New("different scopes")
errReferencedProjectNotFound = errors.New("referenced project does not exist")
errReferencedNotFound = errors.New("reference not found")
)

type insertAfterSameScopeError struct {
Expand Down Expand Up @@ -84,7 +85,7 @@ func NewInsertAfterSameScopeError(source, target coordinate.Coordinate, cause er
// This only works if both scopes are 'static' data and not references or something similar.
type InsertAfterSameScopeValidator struct{}

func (_ InsertAfterSameScopeValidator) Validate(p project.Project, conf config.Config) error {
func (_ InsertAfterSameScopeValidator) Validate(projects []project.Project, conf config.Config) error {

if conf.Skip {
return nil
Expand All @@ -103,7 +104,12 @@ func (_ InsertAfterSameScopeValidator) Validate(p project.Project, conf config.C
return NewInsertAfterSameScopeError(conf.Coordinate, targetCoordinate, errDiffSchema)
}

targetConf, f := p.GetConfigFor(conf.Environment, targetCoordinate)
proj, f := findProjectByName(projects, targetCoordinate.Project)
if !f {
return NewInsertAfterSameScopeError(conf.Coordinate, targetCoordinate, errReferencedProjectNotFound)
}

targetConf, f := proj.GetConfigFor(conf.Environment, targetCoordinate)
if !f {
return NewInsertAfterSameScopeError(conf.Coordinate, targetCoordinate, errReferencedNotFound)
}
Expand All @@ -125,6 +131,16 @@ func (_ InsertAfterSameScopeValidator) Validate(p project.Project, conf config.C
return nil
}

func findProjectByName(projects []project.Project, projectName string) (project.Project, bool) {
for _, p := range projects {
if p.Id == projectName {
return p, true
}
}

return project.Project{}, false
}

func extractInsertAfterReference(c config.Config) coordinate.Coordinate {
param, f := c.Parameters[config.InsertAfterParameter]
if !f {
Expand Down
Loading

0 comments on commit 7815785

Please sign in to comment.