github.com/endocode/docker@v1.4.2-0.20160113120958-46eb4700391e/daemon/discovery_test.go (about) 1 package daemon 2 3 import ( 4 "testing" 5 "time" 6 ) 7 8 func TestDiscoveryOpts(t *testing.T) { 9 clusterOpts := map[string]string{"discovery.heartbeat": "10", "discovery.ttl": "5"} 10 heartbeat, ttl, err := discoveryOpts(clusterOpts) 11 if err == nil { 12 t.Fatalf("discovery.ttl < discovery.heartbeat must fail") 13 } 14 15 clusterOpts = map[string]string{"discovery.heartbeat": "10", "discovery.ttl": "10"} 16 heartbeat, ttl, err = discoveryOpts(clusterOpts) 17 if err == nil { 18 t.Fatalf("discovery.ttl == discovery.heartbeat must fail") 19 } 20 21 clusterOpts = map[string]string{"discovery.heartbeat": "invalid"} 22 heartbeat, ttl, err = discoveryOpts(clusterOpts) 23 if err == nil { 24 t.Fatalf("invalid discovery.heartbeat must fail") 25 } 26 27 clusterOpts = map[string]string{"discovery.ttl": "invalid"} 28 heartbeat, ttl, err = discoveryOpts(clusterOpts) 29 if err == nil { 30 t.Fatalf("invalid discovery.ttl must fail") 31 } 32 33 clusterOpts = map[string]string{"discovery.heartbeat": "10", "discovery.ttl": "20"} 34 heartbeat, ttl, err = discoveryOpts(clusterOpts) 35 if err != nil { 36 t.Fatal(err) 37 } 38 39 if heartbeat != 10*time.Second { 40 t.Fatalf("Heatbeat - Expected : %v, Actual : %v", 10*time.Second, heartbeat) 41 } 42 43 if ttl != 20*time.Second { 44 t.Fatalf("TTL - Expected : %v, Actual : %v", 20*time.Second, ttl) 45 } 46 47 clusterOpts = map[string]string{"discovery.heartbeat": "10"} 48 heartbeat, ttl, err = discoveryOpts(clusterOpts) 49 if err != nil { 50 t.Fatal(err) 51 } 52 53 if heartbeat != 10*time.Second { 54 t.Fatalf("Heatbeat - Expected : %v, Actual : %v", 10*time.Second, heartbeat) 55 } 56 57 expected := 10 * defaultDiscoveryTTLFactor * time.Second 58 if ttl != expected { 59 t.Fatalf("TTL - Expected : %v, Actual : %v", expected, ttl) 60 } 61 62 clusterOpts = map[string]string{"discovery.ttl": "30"} 63 heartbeat, ttl, err = discoveryOpts(clusterOpts) 64 if err != nil { 65 t.Fatal(err) 66 } 67 68 if ttl != 30*time.Second { 69 t.Fatalf("TTL - Expected : %v, Actual : %v", 30*time.Second, ttl) 70 } 71 72 expected = 30 * time.Second / defaultDiscoveryTTLFactor 73 if heartbeat != expected { 74 t.Fatalf("Heatbeat - Expected : %v, Actual : %v", expected, heartbeat) 75 } 76 77 clusterOpts = map[string]string{} 78 heartbeat, ttl, err = discoveryOpts(clusterOpts) 79 if err != nil { 80 t.Fatal(err) 81 } 82 83 if heartbeat != defaultDiscoveryHeartbeat { 84 t.Fatalf("Heatbeat - Expected : %v, Actual : %v", defaultDiscoveryHeartbeat, heartbeat) 85 } 86 87 expected = defaultDiscoveryHeartbeat * defaultDiscoveryTTLFactor 88 if ttl != expected { 89 t.Fatalf("TTL - Expected : %v, Actual : %v", expected, ttl) 90 } 91 }