satellite/overlay/placement: better error message for placement parsing

Given the placement.txt with the content:

```
9:exclude(placement(10))
10:country("DE")
```

Without patch:

```
placement-test --placement=/tmp/placement.txt countrycode=DE
Error: type mismatch: reflect: Call using zero Value argument
```

With the patch:

```
placement-test --placement=/tmp/placement.txt countrycode=DE
Error: Error in line 'exclude(placement(10))' when placement rule is parsed: Placement 10 is referenced before defined. Please define it first!
```

Change-Id: I9ad81016d4a57fdb32f3ff9031b5591f9a7cd2a6
This commit is contained in:
Márton Elek 2023-10-13 10:22:27 +02:00 committed by Storj Robot
parent d8376a2a24
commit c3fbac2e7a
2 changed files with 12 additions and 3 deletions

View File

@ -98,8 +98,12 @@ func (d *PlacementDefinitions) AddPlacementFromString(definitions string) error
"country": func(countries ...string) (nodeselection.NodeFilter, error) {
return nodeselection.NewCountryFilterFromString(countries)
},
"placement": func(ix int64) nodeselection.NodeFilter {
return d.placements[storj.PlacementConstraint(ix)]
"placement": func(ix int64) (nodeselection.NodeFilter, error) {
filter, found := d.placements[storj.PlacementConstraint(ix)]
if !found {
return nil, errs.New("Placement %d is referenced before defined. Please define it first!", ix)
}
return filter, nil
},
"all": func(filters ...nodeselection.NodeFilter) (nodeselection.NodeFilters, error) {
res := nodeselection.NodeFilters{}
@ -177,7 +181,7 @@ func (d *PlacementDefinitions) AddPlacementFromString(definitions string) error
}
val, err := mito.Eval(idDef[1], env)
if err != nil {
return errs.Wrap(err)
return errs.New("Error in line '%s' when placement rule is parsed: %v", idDef[1], err)
}
id, err := strconv.Atoi(idDef[0])
if err != nil {

View File

@ -173,7 +173,12 @@ func TestPlacementFromString(t *testing.T) {
require.True(t, placement2.Match(&nodeselection.SelectedNode{
CountryCode: location.Germany,
}))
})
t.Run("placement reuse wrong", func(t *testing.T) {
p := NewPlacementDefinitions()
err := p.AddPlacementFromString(`1:exclude(placement(2));2:country("DE")`)
require.ErrorContains(t, err, "referenced before defined")
})
t.Run("all rules", func(t *testing.T) {