Merge pull request #3536 from angrycub/b-resource-memory-test-fix

Fixed test and moved constants into standalone func
This commit is contained in:
Preetha
2017-11-13 17:00:14 -06:00
committed by GitHub
3 changed files with 53 additions and 16 deletions

View File

@@ -139,7 +139,7 @@ func TestJobs_Canonicalize(t *testing.T) {
{
KillTimeout: helper.TimeToPtr(5 * time.Second),
LogConfig: DefaultLogConfig(),
Resources: MinResources(),
Resources: DefaultResources(),
},
},
},
@@ -201,7 +201,7 @@ func TestJobs_Canonicalize(t *testing.T) {
{
Name: "task1",
LogConfig: DefaultLogConfig(),
Resources: MinResources(),
Resources: DefaultResources(),
KillTimeout: helper.TimeToPtr(5 * time.Second),
},
},
@@ -550,7 +550,7 @@ func TestJobs_Canonicalize(t *testing.T) {
{
Name: "task1",
LogConfig: DefaultLogConfig(),
Resources: MinResources(),
Resources: DefaultResources(),
KillTimeout: helper.TimeToPtr(5 * time.Second),
},
},
@@ -582,7 +582,7 @@ func TestJobs_Canonicalize(t *testing.T) {
{
Name: "task1",
LogConfig: DefaultLogConfig(),
Resources: MinResources(),
Resources: DefaultResources(),
KillTimeout: helper.TimeToPtr(5 * time.Second),
},
},

View File

@@ -12,28 +12,47 @@ type Resources struct {
Networks []*NetworkResource
}
// Canonicalize will supply missing values in the cases
// where they are not provided.
func (r *Resources) Canonicalize() {
defaultResources := DefaultResources()
if r.CPU == nil {
r.CPU = helper.IntToPtr(100)
r.CPU = defaultResources.CPU
}
if r.MemoryMB == nil {
r.MemoryMB = helper.IntToPtr(300)
r.MemoryMB = defaultResources.MemoryMB
}
if r.IOPS == nil {
r.IOPS = helper.IntToPtr(0)
r.IOPS = defaultResources.IOPS
}
for _, n := range r.Networks {
n.Canonicalize()
}
}
// DefaultResources is a small resources object that contains the
// default resources requests that we will provide to an object.
// --- THIS FUNCTION IS REPLICATED IN nomad/structs/structs.go
// and should be kept in sync.
func DefaultResources() *Resources {
return &Resources{
CPU: helper.IntToPtr(100),
MemoryMB: helper.IntToPtr(300),
IOPS: helper.IntToPtr(0),
}
}
// MinResources is a small resources object that contains the
// absolute minimum resources that we will provide to an object.
// This should not be confused with the defaults which are
// provided in DefaultResources() --- THIS LOGIC IS REPLICATED
// IN nomad/structs/structs.go and should be kept in sync.
func MinResources() *Resources {
return &Resources{
CPU: helper.IntToPtr(100),
MemoryMB: helper.IntToPtr(10),
IOPS: helper.IntToPtr(0),
}
}
// Merge merges this resource with another resource.

View File

@@ -24,7 +24,7 @@ import (
"github.com/gorhill/cronexpr"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/go-multierror"
multierror "github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-version"
"github.com/hashicorp/nomad/acl"
"github.com/hashicorp/nomad/helper"
@@ -1222,8 +1222,24 @@ const (
BytesInMegabyte = 1024 * 1024
)
// DefaultResources returns the default resources for a task.
// DefaultResources is a small resources object that contains the
// default resources requests that we will provide to an object.
// --- THIS FUNCTION IS REPLICATED IN api/resources.go and should
// be kept in sync.
func DefaultResources() *Resources {
return &Resources{
CPU: 100,
MemoryMB: 300,
IOPS: 0,
}
}
// MinResources is a small resources object that contains the
// absolute minimum resources that we will provide to an object.
// This should not be confused with the defaults which are
// provided in Canonicalize() --- THIS FUNCTION IS REPLICATED IN
// api/resources.go and should be kept in sync.
func MinResources() *Resources {
return &Resources{
CPU: 100,
MemoryMB: 10,
@@ -1269,16 +1285,18 @@ func (r *Resources) Canonicalize() {
// MeetsMinResources returns an error if the resources specified are less than
// the minimum allowed.
// This is based on the minimums defined in the Resources type
func (r *Resources) MeetsMinResources() error {
var mErr multierror.Error
if r.CPU < 20 {
mErr.Errors = append(mErr.Errors, fmt.Errorf("minimum CPU value is 20; got %d", r.CPU))
minResources := MinResources()
if r.CPU < minResources.CPU {
mErr.Errors = append(mErr.Errors, fmt.Errorf("minimum CPU value is %d; got %d", minResources.CPU, r.CPU))
}
if r.MemoryMB < 10 {
mErr.Errors = append(mErr.Errors, fmt.Errorf("minimum MemoryMB value is 10; got %d", r.MemoryMB))
if r.MemoryMB < minResources.MemoryMB {
mErr.Errors = append(mErr.Errors, fmt.Errorf("minimum MemoryMB value is %d; got %d", minResources.CPU, r.MemoryMB))
}
if r.IOPS < 0 {
mErr.Errors = append(mErr.Errors, fmt.Errorf("minimum IOPS value is 0; got %d", r.IOPS))
if r.IOPS < minResources.IOPS {
mErr.Errors = append(mErr.Errors, fmt.Errorf("minimum IOPS value is %d; got %d", minResources.CPU, r.IOPS))
}
for i, n := range r.Networks {
if err := n.MeetsMinResources(); err != nil {