github.com/LazyboyChen7/engine@v17.12.1-ce-rc2+incompatible/daemon/discovery/discovery_test.go (about) 1 package discovery 2 3 import ( 4 "fmt" 5 "testing" 6 "time" 7 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestDiscoveryOptsErrors(t *testing.T) { 13 var testcases = []struct { 14 doc string 15 opts map[string]string 16 }{ 17 { 18 doc: "discovery.ttl < discovery.heartbeat", 19 opts: map[string]string{"discovery.heartbeat": "10", "discovery.ttl": "5"}, 20 }, 21 { 22 doc: "discovery.ttl == discovery.heartbeat", 23 opts: map[string]string{"discovery.heartbeat": "10", "discovery.ttl": "10"}, 24 }, 25 { 26 doc: "negative discovery.heartbeat", 27 opts: map[string]string{"discovery.heartbeat": "-10", "discovery.ttl": "10"}, 28 }, 29 { 30 doc: "negative discovery.ttl", 31 opts: map[string]string{"discovery.heartbeat": "10", "discovery.ttl": "-10"}, 32 }, 33 { 34 doc: "invalid discovery.heartbeat", 35 opts: map[string]string{"discovery.heartbeat": "invalid"}, 36 }, 37 { 38 doc: "invalid discovery.ttl", 39 opts: map[string]string{"discovery.ttl": "invalid"}, 40 }, 41 } 42 43 for _, testcase := range testcases { 44 _, _, err := discoveryOpts(testcase.opts) 45 assert.Error(t, err, testcase.doc) 46 } 47 } 48 49 func TestDiscoveryOpts(t *testing.T) { 50 clusterOpts := map[string]string{"discovery.heartbeat": "10", "discovery.ttl": "20"} 51 heartbeat, ttl, err := discoveryOpts(clusterOpts) 52 require.NoError(t, err) 53 assert.Equal(t, 10*time.Second, heartbeat) 54 assert.Equal(t, 20*time.Second, ttl) 55 56 clusterOpts = map[string]string{"discovery.heartbeat": "10"} 57 heartbeat, ttl, err = discoveryOpts(clusterOpts) 58 require.NoError(t, err) 59 assert.Equal(t, 10*time.Second, heartbeat) 60 assert.Equal(t, 10*defaultDiscoveryTTLFactor*time.Second, ttl) 61 62 clusterOpts = map[string]string{"discovery.ttl": "30"} 63 heartbeat, ttl, err = discoveryOpts(clusterOpts) 64 require.NoError(t, err) 65 66 if ttl != 30*time.Second { 67 t.Fatalf("TTL - Expected : %v, Actual : %v", 30*time.Second, ttl) 68 } 69 70 expected := 30 * time.Second / defaultDiscoveryTTLFactor 71 if heartbeat != expected { 72 t.Fatalf("Heartbeat - Expected : %v, Actual : %v", expected, heartbeat) 73 } 74 75 discoveryTTL := fmt.Sprintf("%d", defaultDiscoveryTTLFactor-1) 76 clusterOpts = map[string]string{"discovery.ttl": discoveryTTL} 77 heartbeat, _, err = discoveryOpts(clusterOpts) 78 if err == nil && heartbeat == 0 { 79 t.Fatal("discovery.heartbeat must be positive") 80 } 81 82 clusterOpts = map[string]string{} 83 heartbeat, ttl, err = discoveryOpts(clusterOpts) 84 if err != nil { 85 t.Fatal(err) 86 } 87 88 if heartbeat != defaultDiscoveryHeartbeat { 89 t.Fatalf("Heartbeat - Expected : %v, Actual : %v", defaultDiscoveryHeartbeat, heartbeat) 90 } 91 92 expected = defaultDiscoveryHeartbeat * defaultDiscoveryTTLFactor 93 if ttl != expected { 94 t.Fatalf("TTL - Expected : %v, Actual : %v", expected, ttl) 95 } 96 }