Skip to content

Commit

Permalink
split up tests to support enterprise/non-enterprise tests
Browse files Browse the repository at this point in the history
  • Loading branch information
magiconair committed Sep 25, 2017
1 parent 9f9a251 commit dddc6af
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 105 deletions.
21 changes: 2 additions & 19 deletions agent/config/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,25 +670,8 @@ func (b *Builder) Validate(rt RuntimeConfig) error {
if ipaddr.IsAny(rt.SerfAdvertiseAddrWAN) {
return fmt.Errorf("advertise_addrs.serf_wan cannot be 0.0.0.0, :: or [::]")
}
if rt.ServerMode && rt.SegmentName != "" {
return fmt.Errorf("Segment name can only be set on agents (server = false)")
}
if !rt.ServerMode && len(rt.Segments) > 0 {
return fmt.Errorf("Segments can only be configured on servers (server = true)")
}
if rt.ServerMode && len(rt.Segments) > rt.SegmentLimit {
return fmt.Errorf("cannot configure more than %d segments", rt.SegmentLimit)
}
for _, s := range rt.Segments {
if len(s.Name) > rt.SegmentNameLimit {
return fmt.Errorf("segment[%s].name is too long. Please use %d characters or less", s.Name, rt.SegmentNameLimit)
}
if s.Bind != nil && s.Bind.Port <= 0 {
return fmt.Errorf("segment[%s].port must be > 0", s.Name)
}
if ipaddr.IsAny(s.Advertise) {
return fmt.Errorf("segments[%s].advertise cannot be 0.0.0.0, :: or [::]", s.Name)
}
if err := b.validateSegments(rt); err != nil {
return err
}
for _, a := range rt.DNSAddrs {
if _, ok := a.(*net.UnixAddr); ok {
Expand Down
136 changes: 50 additions & 86 deletions agent/config/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ import (
"github.com/pascaldekloe/goe/verify"
)

type configTest struct {
desc string
flags []string
pre, post func()
json, jsontail []string
hcl, hcltail []string
privatev4 func() ([]*net.IPAddr, error)
publicv6 func() ([]*net.IPAddr, error)
patch func(rt *RuntimeConfig)
err string
warns []string
hostname func() (string, error)
}

// TestConfigFlagsAndEdgecases tests the command line flags and
// edgecases for the config parsing. It provides a test structure which
// checks for warnings on deprecated fields and flags. These tests
Expand All @@ -33,49 +47,7 @@ func TestConfigFlagsAndEdgecases(t *testing.T) {
dataDir := testutil.TempDir(t, "consul")
defer os.RemoveAll(dataDir)

randomString := func(n int) string {
s := ""
for ; n > 0; n-- {
s += "x"
}
return s
}

metaPairs := func(n int, format string) string {
var s []string
for i := 0; i < n; i++ {
switch format {
case "json":
s = append(s, fmt.Sprintf(`"%d":"%d"`, i, i))
case "hcl":
s = append(s, fmt.Sprintf(`"%d"="%d"`, i, i))
default:
panic("invalid format: " + format)
}
}
switch format {
case "json":
return strings.Join(s, ",")
case "hcl":
return strings.Join(s, " ")
default:
panic("invalid format: " + format)
}
}

tests := []struct {
desc string
flags []string
pre, post func()
json, jsontail []string
hcl, hcltail []string
privatev4 func() ([]*net.IPAddr, error)
publicv6 func() ([]*net.IPAddr, error)
patch func(rt *RuntimeConfig)
err string
warns []string
hostname func() (string, error)
}{
tests := []configTest{
// ------------------------------------------------------------
// cmd line flags
//
Expand Down Expand Up @@ -1418,26 +1390,6 @@ func TestConfigFlagsAndEdgecases(t *testing.T) {
hcl: []string{`advertise_addrs = { serf_wan = "0.0.0.0" }`},
err: "advertise_addrs.serf_wan cannot be 0.0.0.0, :: or [::]",
},
{
desc: "segments.advertise any",
flags: []string{
`-data-dir=` + dataDir,
`-server=true`,
},
json: []string{`{ "segments":[{ "name":"x", "advertise": "::", "port": 123 }] }`},
hcl: []string{`segments = [{ name = "x" advertise = "::" port = 123 }]`},
err: `segments[x].advertise cannot be 0.0.0.0, :: or [::]`,
},
{
desc: "segments.advertise socket",
flags: []string{
`-data-dir=` + dataDir,
`-server=true`,
},
json: []string{`{ "segments":[{ "name":"x", "advertise": "unix:///foo" }] }`},
hcl: []string{`segments = [{ name = "x" advertise = "unix:///foo" }]`},
err: `segments[x].advertise cannot be a unix socket`,
},
{
desc: "dns_config.udp_answer_limit invalid",
flags: []string{
Expand Down Expand Up @@ -1707,31 +1659,12 @@ func TestConfigFlagsAndEdgecases(t *testing.T) {
},
warns: []string{`WARNING: WAN keyring exists but -encrypt given, using keyring`},
},
{
desc: "segment name only on agents",
flags: []string{
`-data-dir=` + dataDir,
},
json: []string{`{ "server": true, "segment": "a" }`},
hcl: []string{` server = true segment = "a" `},
err: "Segment name can only be set on agents (server = false)",
},
{
desc: "segments only on servers",
flags: []string{
`-data-dir=` + dataDir,
},
json: []string{`{
"server": false,
"segments": [ { "name:":"a", "port": 123 } ]
}`},
hcl: []string{`
server = false
segments = [ { name = "a" port = 123 } ]`},
err: "Segments can only be configured on servers (server = true)",
},
}

testConfig(t, tests, dataDir)
}

func testConfig(t *testing.T, tests []configTest, dataDir string) {
for _, tt := range tests {
for pass, format := range []string{"json", "hcl"} {
// when we test only flags then there are no JSON or HCL
Expand Down Expand Up @@ -3327,6 +3260,7 @@ func TestFullConfig(t *testing.T) {
rt.DevMode = false
rt.EnableUI = false
rt.SegmentName = ""
rt.Segments = nil

// validate the runtime config
if err := b.Validate(rt); err != nil {
Expand Down Expand Up @@ -3561,3 +3495,33 @@ func writeFile(path string, data []byte) {
panic(err)
}
}

func randomString(n int) string {
s := ""
for ; n > 0; n-- {
s += "x"
}
return s
}

func metaPairs(n int, format string) string {
var s []string
for i := 0; i < n; i++ {
switch format {
case "json":
s = append(s, fmt.Sprintf(`"%d":"%d"`, i, i))
case "hcl":
s = append(s, fmt.Sprintf(`"%d"="%d"`, i, i))
default:
panic("invalid format: " + format)
}
}
switch format {
case "json":
return strings.Join(s, ",")
case "hcl":
return strings.Join(s, " ")
default:
panic("invalid format: " + format)
}
}
17 changes: 17 additions & 0 deletions agent/config/segments_oss.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// +build !ent

package config

import (
"github.com/hashicorp/consul/agent/structs"
)

func (b *Builder) validateSegments(rt RuntimeConfig) error {
if rt.SegmentName != "" {
return structs.ErrSegmentsNotSupported
}
if len(rt.Segments) > 0 {
return structs.ErrSegmentsNotSupported
}
return nil
}
38 changes: 38 additions & 0 deletions agent/config/segments_oss_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// +build !ent

package config

import (
"os"
"testing"

"github.com/hashicorp/consul/testutil"
)

func TestSegments(t *testing.T) {
dataDir := testutil.TempDir(t, "consul")
defer os.RemoveAll(dataDir)

tests := []configTest{
{
desc: "segment name not in OSS",
flags: []string{
`-data-dir=` + dataDir,
},
json: []string{`{ "server": true, "segment": "a" }`},
hcl: []string{` server = true segment = "a" `},
err: `Network segments are not supported in this version of Consul`,
},
{
desc: "segments not in OSS",
flags: []string{
`-data-dir=` + dataDir,
},
json: []string{`{ "segments":[{ "name":"x", "advertise": "unix:///foo" }] }`},
hcl: []string{`segments = [{ name = "x" advertise = "unix:///foo" }]`},
err: `Network segments are not supported in this version of Consul`,
},
}

testConfig(t, tests, dataDir)
}

0 comments on commit dddc6af

Please sign in to comment.