github.com/sijibomii/docker@v0.0.0-20231230191044-5cf6ca554647/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 } 92 93 func TestModifiedDiscoverySettings(t *testing.T) { 94 cases := []struct { 95 current *Config 96 modified *Config 97 expected bool 98 }{ 99 { 100 current: discoveryConfig("foo", "bar", map[string]string{}), 101 modified: discoveryConfig("foo", "bar", map[string]string{}), 102 expected: false, 103 }, 104 { 105 current: discoveryConfig("foo", "bar", map[string]string{"foo": "bar"}), 106 modified: discoveryConfig("foo", "bar", map[string]string{"foo": "bar"}), 107 expected: false, 108 }, 109 { 110 current: discoveryConfig("foo", "bar", map[string]string{}), 111 modified: discoveryConfig("foo", "bar", nil), 112 expected: false, 113 }, 114 { 115 current: discoveryConfig("foo", "bar", nil), 116 modified: discoveryConfig("foo", "bar", map[string]string{}), 117 expected: false, 118 }, 119 { 120 current: discoveryConfig("foo", "bar", nil), 121 modified: discoveryConfig("baz", "bar", nil), 122 expected: true, 123 }, 124 { 125 current: discoveryConfig("foo", "bar", nil), 126 modified: discoveryConfig("foo", "baz", nil), 127 expected: true, 128 }, 129 { 130 current: discoveryConfig("foo", "bar", nil), 131 modified: discoveryConfig("foo", "bar", map[string]string{"foo": "bar"}), 132 expected: true, 133 }, 134 } 135 136 for _, c := range cases { 137 got := modifiedDiscoverySettings(c.current, c.modified.ClusterStore, c.modified.ClusterAdvertise, c.modified.ClusterOpts) 138 if c.expected != got { 139 t.Fatalf("expected %v, got %v: current config %v, new config %v", c.expected, got, c.current, c.modified) 140 } 141 } 142 } 143 144 func discoveryConfig(backendAddr, advertiseAddr string, opts map[string]string) *Config { 145 return &Config{ 146 CommonConfig: CommonConfig{ 147 ClusterStore: backendAddr, 148 ClusterAdvertise: advertiseAddr, 149 ClusterOpts: opts, 150 }, 151 } 152 }