2018-02-05 16:05:59 -05:00
|
|
|
package discovery // import "github.com/docker/docker/daemon/discovery"
|
2015-11-11 19:18:06 -05:00
|
|
|
|
|
|
|
import (
|
2017-03-11 03:03:22 -05:00
|
|
|
"fmt"
|
2015-11-11 19:18:06 -05:00
|
|
|
"testing"
|
|
|
|
"time"
|
2016-08-31 13:07:05 -04:00
|
|
|
|
2018-06-11 09:32:11 -04:00
|
|
|
"gotest.tools/assert"
|
|
|
|
is "gotest.tools/assert/cmp"
|
2017-09-08 18:00:14 -04:00
|
|
|
)
|
2015-11-11 19:18:06 -05:00
|
|
|
|
2017-09-08 18:00:14 -04:00
|
|
|
func TestDiscoveryOptsErrors(t *testing.T) {
|
|
|
|
var testcases = []struct {
|
|
|
|
doc string
|
|
|
|
opts map[string]string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
doc: "discovery.ttl < discovery.heartbeat",
|
|
|
|
opts: map[string]string{"discovery.heartbeat": "10", "discovery.ttl": "5"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "discovery.ttl == discovery.heartbeat",
|
|
|
|
opts: map[string]string{"discovery.heartbeat": "10", "discovery.ttl": "10"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "negative discovery.heartbeat",
|
|
|
|
opts: map[string]string{"discovery.heartbeat": "-10", "discovery.ttl": "10"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "negative discovery.ttl",
|
|
|
|
opts: map[string]string{"discovery.heartbeat": "10", "discovery.ttl": "-10"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "invalid discovery.heartbeat",
|
|
|
|
opts: map[string]string{"discovery.heartbeat": "invalid"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "invalid discovery.ttl",
|
|
|
|
opts: map[string]string{"discovery.ttl": "invalid"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testcase := range testcases {
|
|
|
|
_, _, err := discoveryOpts(testcase.opts)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.ErrorContains(err, ""), testcase.doc)
|
2015-11-11 19:18:06 -05:00
|
|
|
}
|
2017-09-08 18:00:14 -04:00
|
|
|
}
|
2015-11-11 19:18:06 -05:00
|
|
|
|
2017-09-08 18:00:14 -04:00
|
|
|
func TestDiscoveryOpts(t *testing.T) {
|
|
|
|
clusterOpts := map[string]string{"discovery.heartbeat": "10", "discovery.ttl": "20"}
|
|
|
|
heartbeat, ttl, err := discoveryOpts(clusterOpts)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(10*time.Second, heartbeat))
|
|
|
|
assert.Check(t, is.Equal(20*time.Second, ttl))
|
2015-11-11 19:18:06 -05:00
|
|
|
|
|
|
|
clusterOpts = map[string]string{"discovery.heartbeat": "10"}
|
|
|
|
heartbeat, ttl, err = discoveryOpts(clusterOpts)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(10*time.Second, heartbeat))
|
|
|
|
assert.Check(t, is.Equal(10*defaultDiscoveryTTLFactor*time.Second, ttl))
|
2015-11-11 19:18:06 -05:00
|
|
|
|
|
|
|
clusterOpts = map[string]string{"discovery.ttl": "30"}
|
|
|
|
heartbeat, ttl, err = discoveryOpts(clusterOpts)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-11-11 19:18:06 -05:00
|
|
|
|
|
|
|
if ttl != 30*time.Second {
|
|
|
|
t.Fatalf("TTL - Expected : %v, Actual : %v", 30*time.Second, ttl)
|
|
|
|
}
|
|
|
|
|
2017-09-08 18:00:14 -04:00
|
|
|
expected := 30 * time.Second / defaultDiscoveryTTLFactor
|
2015-11-11 19:18:06 -05:00
|
|
|
if heartbeat != expected {
|
2016-08-29 06:37:14 -04:00
|
|
|
t.Fatalf("Heartbeat - Expected : %v, Actual : %v", expected, heartbeat)
|
2015-11-11 19:18:06 -05:00
|
|
|
}
|
|
|
|
|
2017-05-21 19:24:07 -04:00
|
|
|
discoveryTTL := fmt.Sprintf("%d", defaultDiscoveryTTLFactor-1)
|
|
|
|
clusterOpts = map[string]string{"discovery.ttl": discoveryTTL}
|
2017-09-08 18:00:14 -04:00
|
|
|
heartbeat, _, err = discoveryOpts(clusterOpts)
|
2017-03-11 03:03:22 -05:00
|
|
|
if err == nil && heartbeat == 0 {
|
|
|
|
t.Fatal("discovery.heartbeat must be positive")
|
|
|
|
}
|
|
|
|
|
2015-11-11 19:18:06 -05:00
|
|
|
clusterOpts = map[string]string{}
|
|
|
|
heartbeat, ttl, err = discoveryOpts(clusterOpts)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if heartbeat != defaultDiscoveryHeartbeat {
|
2016-08-29 06:37:14 -04:00
|
|
|
t.Fatalf("Heartbeat - Expected : %v, Actual : %v", defaultDiscoveryHeartbeat, heartbeat)
|
2015-11-11 19:18:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
expected = defaultDiscoveryHeartbeat * defaultDiscoveryTTLFactor
|
|
|
|
if ttl != expected {
|
|
|
|
t.Fatalf("TTL - Expected : %v, Actual : %v", expected, ttl)
|
|
|
|
}
|
|
|
|
}
|