2015-01-25 06:19:16 +03:00
|
|
|
// Copyright 2015 CoreOS, Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2014-10-18 02:41:22 +04:00
|
|
|
|
2014-10-07 03:42:13 +04:00
|
|
|
package etcdserver
|
|
|
|
|
2014-11-11 00:45:29 +03:00
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/coreos/etcd/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
func mustNewURLs(t *testing.T, urls []string) []url.URL {
|
|
|
|
u, err := types.NewURLs(urls)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error creating new URLs from %q: %v", urls, err)
|
|
|
|
}
|
|
|
|
return u
|
|
|
|
}
|
2014-10-07 03:42:13 +04:00
|
|
|
|
2014-10-18 02:43:56 +04:00
|
|
|
func TestBootstrapConfigVerify(t *testing.T) {
|
2014-10-07 04:05:53 +04:00
|
|
|
tests := []struct {
|
|
|
|
clusterSetting string
|
2014-11-06 22:09:33 +03:00
|
|
|
newclst bool
|
2014-11-11 00:45:29 +03:00
|
|
|
apurls []string
|
2014-10-18 02:43:56 +04:00
|
|
|
disc string
|
2014-10-07 04:05:53 +04:00
|
|
|
shouldError bool
|
|
|
|
}{
|
2014-10-18 02:43:56 +04:00
|
|
|
{
|
2014-10-24 09:39:53 +04:00
|
|
|
// Node must exist in cluster
|
|
|
|
"",
|
2014-11-06 22:09:33 +03:00
|
|
|
true,
|
2014-11-11 00:45:29 +03:00
|
|
|
nil,
|
2014-10-24 09:39:53 +04:00
|
|
|
"",
|
2014-11-11 00:45:29 +03:00
|
|
|
|
2014-10-24 09:39:53 +04:00
|
|
|
true,
|
2014-10-18 02:43:56 +04:00
|
|
|
},
|
|
|
|
{
|
2014-10-24 09:39:53 +04:00
|
|
|
// Cannot have duplicate URLs in cluster config
|
|
|
|
"node1=http://localhost:7001,node2=http://localhost:7001,node2=http://localhost:7002",
|
2014-11-06 22:09:33 +03:00
|
|
|
true,
|
2014-11-11 00:45:29 +03:00
|
|
|
nil,
|
2014-10-24 09:39:53 +04:00
|
|
|
"",
|
2014-11-11 00:45:29 +03:00
|
|
|
|
2014-10-24 09:39:53 +04:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Node defined, ClusterState OK
|
2014-10-18 02:43:56 +04:00
|
|
|
"node1=http://localhost:7001,node2=http://localhost:7002",
|
2014-11-06 22:09:33 +03:00
|
|
|
true,
|
2014-11-11 00:45:29 +03:00
|
|
|
[]string{"http://localhost:7001"},
|
2014-10-24 09:39:53 +04:00
|
|
|
"",
|
2014-11-11 00:45:29 +03:00
|
|
|
|
2014-10-24 09:39:53 +04:00
|
|
|
false,
|
2014-10-18 02:43:56 +04:00
|
|
|
},
|
|
|
|
{
|
2014-10-24 09:39:53 +04:00
|
|
|
// Node defined, discovery OK
|
2014-10-18 02:43:56 +04:00
|
|
|
"node1=http://localhost:7001",
|
2014-11-06 22:09:33 +03:00
|
|
|
false,
|
2014-11-11 00:45:29 +03:00
|
|
|
[]string{"http://localhost:7001"},
|
2014-10-24 09:39:53 +04:00
|
|
|
"http://discovery",
|
2014-11-11 00:45:29 +03:00
|
|
|
|
2014-10-24 09:39:53 +04:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Cannot have ClusterState!=new && !discovery
|
|
|
|
"node1=http://localhost:7001",
|
2014-11-06 22:09:33 +03:00
|
|
|
false,
|
2014-11-11 00:45:29 +03:00
|
|
|
nil,
|
2014-10-24 09:39:53 +04:00
|
|
|
"",
|
2014-11-11 00:45:29 +03:00
|
|
|
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Advertised peer URLs must match those in cluster-state
|
|
|
|
"node1=http://localhost:7001",
|
|
|
|
true,
|
|
|
|
[]string{"http://localhost:12345"},
|
|
|
|
"",
|
|
|
|
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Advertised peer URLs must match those in cluster-state
|
|
|
|
"node1=http://localhost:7001,node1=http://localhost:12345",
|
|
|
|
true,
|
|
|
|
[]string{"http://localhost:12345"},
|
|
|
|
"",
|
|
|
|
|
2014-10-24 09:39:53 +04:00
|
|
|
true,
|
2014-10-18 02:43:56 +04:00
|
|
|
},
|
2014-10-07 03:42:13 +04:00
|
|
|
}
|
|
|
|
|
2014-10-07 04:05:53 +04:00
|
|
|
for i, tt := range tests {
|
2014-10-23 03:15:37 +04:00
|
|
|
cluster, err := NewClusterFromString("", tt.clusterSetting)
|
2014-10-24 09:39:53 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("#%d: Got unexpected error: %v", i, err)
|
2014-10-21 21:25:34 +04:00
|
|
|
}
|
2014-10-07 04:05:53 +04:00
|
|
|
cfg := ServerConfig{
|
2014-10-23 01:31:27 +04:00
|
|
|
Name: "node1",
|
2014-10-18 02:43:56 +04:00
|
|
|
DiscoveryURL: tt.disc,
|
|
|
|
Cluster: cluster,
|
2014-11-06 22:09:33 +03:00
|
|
|
NewCluster: tt.newclst,
|
2014-10-07 04:05:53 +04:00
|
|
|
}
|
2014-11-11 00:45:29 +03:00
|
|
|
if tt.apurls != nil {
|
|
|
|
cfg.PeerURLs = mustNewURLs(t, tt.apurls)
|
|
|
|
}
|
2014-10-21 21:25:34 +04:00
|
|
|
err = cfg.VerifyBootstrapConfig()
|
2014-10-07 04:05:53 +04:00
|
|
|
if (err == nil) && tt.shouldError {
|
2014-10-21 21:25:34 +04:00
|
|
|
t.Errorf("%#v", *cluster)
|
2014-10-07 04:05:53 +04:00
|
|
|
t.Errorf("#%d: Got no error where one was expected", i)
|
|
|
|
}
|
|
|
|
if (err != nil) && !tt.shouldError {
|
|
|
|
t.Errorf("#%d: Got unexpected error: %v", i, err)
|
|
|
|
}
|
2014-10-07 03:42:13 +04:00
|
|
|
}
|
|
|
|
}
|
2014-10-24 09:39:53 +04:00
|
|
|
|
|
|
|
func TestSnapDir(t *testing.T) {
|
|
|
|
tests := map[string]string{
|
2015-02-12 20:45:12 +03:00
|
|
|
"/": "/member/snap",
|
|
|
|
"/var/lib/etc": "/var/lib/etc/member/snap",
|
2014-10-24 09:39:53 +04:00
|
|
|
}
|
|
|
|
for dd, w := range tests {
|
|
|
|
cfg := ServerConfig{
|
|
|
|
DataDir: dd,
|
|
|
|
}
|
|
|
|
if g := cfg.SnapDir(); g != w {
|
|
|
|
t.Errorf("DataDir=%q: SnapDir()=%q, want=%q", dd, g, w)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWALDir(t *testing.T) {
|
|
|
|
tests := map[string]string{
|
2015-02-12 20:45:12 +03:00
|
|
|
"/": "/member/wal",
|
|
|
|
"/var/lib/etc": "/var/lib/etc/member/wal",
|
2014-10-24 09:39:53 +04:00
|
|
|
}
|
|
|
|
for dd, w := range tests {
|
|
|
|
cfg := ServerConfig{
|
|
|
|
DataDir: dd,
|
|
|
|
}
|
|
|
|
if g := cfg.WALDir(); g != w {
|
|
|
|
t.Errorf("DataDir=%q: WALDir()=%q, want=%q", dd, g, w)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldDiscover(t *testing.T) {
|
|
|
|
tests := map[string]bool{
|
|
|
|
"": false,
|
|
|
|
"foo": true,
|
|
|
|
"http://discovery.etcd.io/asdf": true,
|
|
|
|
}
|
|
|
|
for durl, w := range tests {
|
|
|
|
cfg := ServerConfig{
|
|
|
|
DiscoveryURL: durl,
|
|
|
|
}
|
|
|
|
if g := cfg.ShouldDiscover(); g != w {
|
|
|
|
t.Errorf("durl=%q: ShouldDiscover()=%t, want=%t", durl, g, w)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|