Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
schmichael committed Jan 12, 2022
1 parent 482d975 commit 31a015f
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 3 deletions.
63 changes: 63 additions & 0 deletions command/agent/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/hashicorp/nomad/helper/freeport"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/nomad/structs/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -1441,3 +1442,65 @@ func TestParseMultipleIPTemplates(t *testing.T) {
})
}
}

// TestConfig_Validate_Default asserts the DefaultConfig validates
// successfully.
func TestConfig_Validate(t *testing.T) {
results := DefaultConfig().Validate()
assert.Empty(t, results.Warnings)
assert.Nil(t, results.Errors)
assert.NoError(t, results.Err())
assert.NoError(t, results.ErrSummary())
}

// TestConfig_Validate_Bad asserts a wide range of config validation failures.
func TestConfig_Validate_Bad(t *testing.T) {
config, err := ParseConfigFile("testdata/badagent.hcl")
require.NoError(t, err)

// Intentionally not merging in the DefaultConfig to test every bad
// case.

results := config.Validate()
assert.Empty(t, results.Warnings)
require.NotNil(t, results.Errors)

expected := map[string]struct{}{
"Missing region": {},
"Missing datacenter": {},
"client.cpu_total_compute must be >= 0 but found: -10": {},
"client.memory_total_mb must be >= 0 but found: -11": {},
`client.max_kill_timeout is not a valid duration: time: invalid duration ""`: {},
`client.max_dynamic_port must be <= 65536 but found 999998`: {},
`client.min_dynamic_port must be <= 65536 but found 999999`: {},
`client.min_dynamic_port (999999) must be < client.max_dynamic_port (999998)`: {},
`reserved.disk must be >= 0 but found: -3`: {},
`client.gc_max_allocs must be > 0 but found 0`: {},
`client.gc_inode_usage_threshold must be > 0 but found 0.000000`: {},
`client.gc_interval must be > 0 but found -5m0s`: {},
`client.gc_parallel_destroys must be > 0 but found -12`: {},
`client.gc_disk_usage_threshold must be > 0 but found 0.000000`: {},

`reserved.reserved_ports 1,10-99999999 invalid: port must be < 65536 but found 99999999`: {},
`host_network["bad"].reserved_ports "-10" invalid: strconv.ParseUint: parsing "": invalid syntax`: {},
}
unexpected := []string{}

for _, err := range results.Errors.Errors {
v := err.Error()
if _, ok := expected[v]; ok {
// Error found! Remove and continue
delete(expected, v)
continue
}

// Unexpected
unexpected = append(unexpected, v)
}

// All expected errors should be removed
assert.Len(t, expected, 0)

// There should be no unexpected errors
assert.Len(t, unexpected, 0, strings.Join(unexpected, "\n"))
}
23 changes: 23 additions & 0 deletions command/agent/testdata/badagent.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See command/agent/config_test.go#TestConifg_Validate_Bad

client {
cpu_total_compute = -10
memory_total_mb = -11

max_dynamic_port = 999998
min_dynamic_port = 999999

gc_interval = "-5m"
gc_parallel_destroys = -12

reserved {
cpu = 1
memory = 2
disk = -3
reserved_ports = "1,10-99999999"
}

host_network "bad" {
reserved_ports = "-10"
}
}
6 changes: 3 additions & 3 deletions helper/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (v *ValidationResults) ErrSummary() error {
return nil
}

ne, nw := len(v.Errors.Errors), len(v.Warnings)
ne := len(v.Errors.Errors)

if ne == 0 {
return nil
Expand All @@ -85,7 +85,7 @@ func (v *ValidationResults) ErrSummary() error {
errstr = fmt.Sprintf("%d errors", ne)
}

switch nw {
switch nw := len(v.Warnings); nw {
case 1:
errstr += ", 1 warning"
default:
Expand All @@ -98,7 +98,7 @@ func (v *ValidationResults) ErrSummary() error {
// Problems returns true if there are any errors or warnings.
func (v *ValidationResults) Problems() bool {
if v.Errors == nil {
return false
return len(v.Warnings) > 0
}

return len(v.Errors.Errors) > 0 || len(v.Warnings) > 0
Expand Down
6 changes: 6 additions & 0 deletions nomad/structs/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,12 @@ func ParsePortRanges(spec string) ([]uint64, error) {
return nil, fmt.Errorf("invalid range: starting value (%v) less than ending (%v) value", end, start)
}

// Full range validation is below but prevent creating
// arbitrarily large arrays here
if end > MaxValidPort {
return nil, fmt.Errorf("port must be < %d but found %d", MaxValidPort, end)
}

for i := start; i <= end; i++ {
ports[i] = struct{}{}
}
Expand Down

0 comments on commit 31a015f

Please sign in to comment.