go.etcd.io/etcd@v3.3.27+incompatible/etcdserver/config_test.go (about)

     1  // Copyright 2015 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package etcdserver
    16  
    17  import (
    18  	"net/url"
    19  	"testing"
    20  
    21  	"github.com/coreos/etcd/pkg/types"
    22  )
    23  
    24  func mustNewURLs(t *testing.T, urls []string) []url.URL {
    25  	if len(urls) == 0 {
    26  		return nil
    27  	}
    28  	u, err := types.NewURLs(urls)
    29  	if err != nil {
    30  		t.Fatalf("error creating new URLs from %q: %v", urls, err)
    31  	}
    32  	return u
    33  }
    34  
    35  func TestConfigVerifyBootstrapWithoutClusterAndDiscoveryURLFail(t *testing.T) {
    36  	c := &ServerConfig{
    37  		Name:               "node1",
    38  		DiscoveryURL:       "",
    39  		InitialPeerURLsMap: types.URLsMap{},
    40  	}
    41  	if err := c.VerifyBootstrap(); err == nil {
    42  		t.Errorf("err = nil, want not nil")
    43  	}
    44  }
    45  
    46  func TestConfigVerifyExistingWithDiscoveryURLFail(t *testing.T) {
    47  	cluster, err := types.NewURLsMap("node1=http://127.0.0.1:2380")
    48  	if err != nil {
    49  		t.Fatalf("NewCluster error: %v", err)
    50  	}
    51  	c := &ServerConfig{
    52  		Name:               "node1",
    53  		DiscoveryURL:       "http://127.0.0.1:2379/abcdefg",
    54  		PeerURLs:           mustNewURLs(t, []string{"http://127.0.0.1:2380"}),
    55  		InitialPeerURLsMap: cluster,
    56  		NewCluster:         false,
    57  	}
    58  	if err := c.VerifyJoinExisting(); err == nil {
    59  		t.Errorf("err = nil, want not nil")
    60  	}
    61  }
    62  
    63  func TestConfigVerifyLocalMember(t *testing.T) {
    64  	tests := []struct {
    65  		clusterSetting string
    66  		apurls         []string
    67  		strict         bool
    68  		shouldError    bool
    69  	}{
    70  		{
    71  			// Node must exist in cluster
    72  			"",
    73  			nil,
    74  			true,
    75  
    76  			true,
    77  		},
    78  		{
    79  			// Initial cluster set
    80  			"node1=http://localhost:7001,node2=http://localhost:7002",
    81  			[]string{"http://localhost:7001"},
    82  			true,
    83  
    84  			false,
    85  		},
    86  		{
    87  			// Default initial cluster
    88  			"node1=http://localhost:2380,node1=http://localhost:7001",
    89  			[]string{"http://localhost:2380", "http://localhost:7001"},
    90  			true,
    91  
    92  			false,
    93  		},
    94  		{
    95  			// Advertised peer URLs must match those in cluster-state
    96  			"node1=http://localhost:7001",
    97  			[]string{"http://localhost:12345"},
    98  			true,
    99  
   100  			true,
   101  		},
   102  		{
   103  			// Advertised peer URLs must match those in cluster-state
   104  			"node1=http://localhost:2380,node1=http://localhost:12345",
   105  			[]string{"http://localhost:12345"},
   106  			true,
   107  
   108  			true,
   109  		},
   110  		{
   111  			// Advertised peer URLs must match those in cluster-state
   112  			"node1=http://localhost:12345",
   113  			[]string{"http://localhost:2380", "http://localhost:12345"},
   114  			true,
   115  
   116  			true,
   117  		},
   118  		{
   119  			// Advertised peer URLs must match those in cluster-state
   120  			"node1=http://localhost:2380",
   121  			[]string{},
   122  			true,
   123  
   124  			true,
   125  		},
   126  		{
   127  			// do not care about the urls if strict is not set
   128  			"node1=http://localhost:2380",
   129  			[]string{},
   130  			false,
   131  
   132  			false,
   133  		},
   134  	}
   135  
   136  	for i, tt := range tests {
   137  		cluster, err := types.NewURLsMap(tt.clusterSetting)
   138  		if err != nil {
   139  			t.Fatalf("#%d: Got unexpected error: %v", i, err)
   140  		}
   141  		cfg := ServerConfig{
   142  			Name:               "node1",
   143  			InitialPeerURLsMap: cluster,
   144  		}
   145  		if tt.apurls != nil {
   146  			cfg.PeerURLs = mustNewURLs(t, tt.apurls)
   147  		}
   148  		if err = cfg.hasLocalMember(); err == nil && tt.strict {
   149  			err = cfg.advertiseMatchesCluster()
   150  		}
   151  		if (err == nil) && tt.shouldError {
   152  			t.Errorf("#%d: Got no error where one was expected", i)
   153  		}
   154  		if (err != nil) && !tt.shouldError {
   155  			t.Errorf("#%d: Got unexpected error: %v", i, err)
   156  		}
   157  	}
   158  }
   159  
   160  func TestSnapDir(t *testing.T) {
   161  	tests := map[string]string{
   162  		"/":            "/member/snap",
   163  		"/var/lib/etc": "/var/lib/etc/member/snap",
   164  	}
   165  	for dd, w := range tests {
   166  		cfg := ServerConfig{
   167  			DataDir: dd,
   168  		}
   169  		if g := cfg.SnapDir(); g != w {
   170  			t.Errorf("DataDir=%q: SnapDir()=%q, want=%q", dd, g, w)
   171  		}
   172  	}
   173  }
   174  
   175  func TestWALDir(t *testing.T) {
   176  	tests := map[string]string{
   177  		"/":            "/member/wal",
   178  		"/var/lib/etc": "/var/lib/etc/member/wal",
   179  	}
   180  	for dd, w := range tests {
   181  		cfg := ServerConfig{
   182  			DataDir: dd,
   183  		}
   184  		if g := cfg.WALDir(); g != w {
   185  			t.Errorf("DataDir=%q: WALDir()=%q, want=%q", dd, g, w)
   186  		}
   187  	}
   188  }
   189  
   190  func TestShouldDiscover(t *testing.T) {
   191  	tests := map[string]bool{
   192  		"":                              false,
   193  		"foo":                           true,
   194  		"http://discovery.etcd.io/asdf": true,
   195  	}
   196  	for durl, w := range tests {
   197  		cfg := ServerConfig{
   198  			DiscoveryURL: durl,
   199  		}
   200  		if g := cfg.ShouldDiscover(); g != w {
   201  			t.Errorf("durl=%q: ShouldDiscover()=%t, want=%t", durl, g, w)
   202  		}
   203  	}
   204  }