Validate method, and rename ratio field to percent
This commit is contained in:
parent
0037d72fa8
commit
5eb82b6260
|
@ -5401,9 +5401,9 @@ type Spread struct {
|
|||
}
|
||||
|
||||
type SpreadTarget struct {
|
||||
Value string
|
||||
Ratio uint32
|
||||
str string
|
||||
Value string
|
||||
Percent uint32
|
||||
str string
|
||||
}
|
||||
|
||||
func (s *Spread) Copy() *Spread {
|
||||
|
@ -5439,10 +5439,36 @@ func (s *SpreadTarget) String() string {
|
|||
if s.str != "" {
|
||||
return s.str
|
||||
}
|
||||
s.str = fmt.Sprintf("%s %v", s.Value, s.Ratio)
|
||||
s.str = fmt.Sprintf("%s %v", s.Value, s.Percent)
|
||||
return s.str
|
||||
}
|
||||
|
||||
func (s *Spread) Validate() error {
|
||||
var mErr multierror.Error
|
||||
if s.Attribute == "" {
|
||||
mErr.Errors = append(mErr.Errors, errors.New("Missing spread attribute"))
|
||||
}
|
||||
if s.Weight <= 0 || s.Weight > 100 {
|
||||
mErr.Errors = append(mErr.Errors, errors.New("Spread stanza must have a positive weight from 0 to 100"))
|
||||
}
|
||||
if len(s.SpreadTarget) == 0 {
|
||||
// TODO(preetha): This should go away if we can assume even spread if there are no targets
|
||||
// In that case, the target percentages should be calculated at schedule time
|
||||
mErr.Errors = append(mErr.Errors, errors.New("Atleast one spread target value must be specified"))
|
||||
}
|
||||
seen := make(map[string]struct{})
|
||||
for _, target := range s.SpreadTarget {
|
||||
// Make sure there are no duplicates
|
||||
_, ok := seen[target.Value]
|
||||
if !ok {
|
||||
seen[target.Value] = struct{}{}
|
||||
} else {
|
||||
mErr.Errors = append(mErr.Errors, errors.New(fmt.Sprintf("Spread target value %q already defined", target.Value)))
|
||||
}
|
||||
}
|
||||
return mErr.ErrorOrNil()
|
||||
}
|
||||
|
||||
// EphemeralDisk is an ephemeral disk object
|
||||
type EphemeralDisk struct {
|
||||
// Sticky indicates whether the allocation is sticky to a node
|
||||
|
|
|
@ -3926,3 +3926,91 @@ func TestNode_Copy(t *testing.T) {
|
|||
require.Equal(node.DrainStrategy, node2.DrainStrategy)
|
||||
require.Equal(node.Drivers, node2.Drivers)
|
||||
}
|
||||
|
||||
func TestSpread_Validate(t *testing.T) {
|
||||
type tc struct {
|
||||
spread *Spread
|
||||
err error
|
||||
name string
|
||||
}
|
||||
|
||||
testCases := []tc{
|
||||
{
|
||||
spread: &Spread{},
|
||||
err: fmt.Errorf("Missing spread attribute"),
|
||||
name: "empty spread",
|
||||
},
|
||||
{
|
||||
spread: &Spread{
|
||||
Attribute: "${node.datacenter}",
|
||||
Weight: -1,
|
||||
},
|
||||
err: fmt.Errorf("Spread stanza must have a positive weight from 0 to 100"),
|
||||
name: "Invalid weight",
|
||||
},
|
||||
{
|
||||
spread: &Spread{
|
||||
Attribute: "${node.datacenter}",
|
||||
Weight: 200,
|
||||
},
|
||||
err: fmt.Errorf("Spread stanza must have a positive weight from 0 to 100"),
|
||||
name: "Invalid weight",
|
||||
},
|
||||
{
|
||||
spread: &Spread{
|
||||
Attribute: "${node.datacenter}",
|
||||
Weight: 50,
|
||||
},
|
||||
err: fmt.Errorf("Atleast one spread target value must be specified"),
|
||||
name: "No spread targets",
|
||||
},
|
||||
{
|
||||
spread: &Spread{
|
||||
Attribute: "${node.datacenter}",
|
||||
Weight: 50,
|
||||
SpreadTarget: []*SpreadTarget{
|
||||
{
|
||||
Value: "dc1",
|
||||
Percent: 25,
|
||||
},
|
||||
{
|
||||
Value: "dc1",
|
||||
Percent: 50,
|
||||
},
|
||||
},
|
||||
},
|
||||
err: fmt.Errorf("Spread target value \"dc1\" already defined"),
|
||||
name: "No spread targets",
|
||||
},
|
||||
{
|
||||
spread: &Spread{
|
||||
Attribute: "${node.datacenter}",
|
||||
Weight: 50,
|
||||
SpreadTarget: []*SpreadTarget{
|
||||
{
|
||||
Value: "dc1",
|
||||
Percent: 25,
|
||||
},
|
||||
{
|
||||
Value: "dc2",
|
||||
Percent: 50,
|
||||
},
|
||||
},
|
||||
},
|
||||
err: nil,
|
||||
name: "Valid spread",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := tc.spread.Validate()
|
||||
if tc.err != nil {
|
||||
require.NotNil(t, err)
|
||||
require.Contains(t, err.Error(), tc.err.Error())
|
||||
} else {
|
||||
require.Nil(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue