...

Source file src/go.etcd.io/etcd/server/v3/config/config_test.go

Documentation: go.etcd.io/etcd/server/v3/config

     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 config
    16  
    17  import (
    18  	"net/url"
    19  	"testing"
    20  
    21  	"go.etcd.io/etcd/client/pkg/v3/types"
    22  
    23  	"go.uber.org/zap"
    24  )
    25  
    26  func mustNewURLs(t *testing.T, urls []string) []url.URL {
    27  	if len(urls) == 0 {
    28  		return nil
    29  	}
    30  	u, err := types.NewURLs(urls)
    31  	if err != nil {
    32  		t.Fatalf("error creating new URLs from %q: %v", urls, err)
    33  	}
    34  	return u
    35  }
    36  
    37  func TestConfigVerifyBootstrapWithoutClusterAndDiscoveryURLFail(t *testing.T) {
    38  	c := &ServerConfig{
    39  		Name:               "node1",
    40  		DiscoveryURL:       "",
    41  		InitialPeerURLsMap: types.URLsMap{},
    42  		Logger:             zap.NewExample(),
    43  	}
    44  	if err := c.VerifyBootstrap(); err == nil {
    45  		t.Errorf("err = nil, want not nil")
    46  	}
    47  }
    48  
    49  func TestConfigVerifyExistingWithDiscoveryURLFail(t *testing.T) {
    50  	cluster, err := types.NewURLsMap("node1=http://127.0.0.1:2380")
    51  	if err != nil {
    52  		t.Fatalf("NewCluster error: %v", err)
    53  	}
    54  	c := &ServerConfig{
    55  		Name:               "node1",
    56  		DiscoveryURL:       "http://127.0.0.1:2379/abcdefg",
    57  		PeerURLs:           mustNewURLs(t, []string{"http://127.0.0.1:2380"}),
    58  		InitialPeerURLsMap: cluster,
    59  		NewCluster:         false,
    60  		Logger:             zap.NewExample(),
    61  	}
    62  	if err := c.VerifyJoinExisting(); err == nil {
    63  		t.Errorf("err = nil, want not nil")
    64  	}
    65  }
    66  
    67  func TestConfigVerifyLocalMember(t *testing.T) {
    68  	tests := []struct {
    69  		clusterSetting string
    70  		apurls         []string
    71  		strict         bool
    72  		shouldError    bool
    73  	}{
    74  		{
    75  			// Node must exist in cluster
    76  			"",
    77  			nil,
    78  			true,
    79  
    80  			true,
    81  		},
    82  		{
    83  			// Initial cluster set
    84  			"node1=http://localhost:7001,node2=http://localhost:7002",
    85  			[]string{"http://localhost:7001"},
    86  			true,
    87  
    88  			false,
    89  		},
    90  		{
    91  			// Default initial cluster
    92  			"node1=http://localhost:2380,node1=http://localhost:7001",
    93  			[]string{"http://localhost:2380", "http://localhost:7001"},
    94  			true,
    95  
    96  			false,
    97  		},
    98  		{
    99  			// Advertised peer URLs must match those in cluster-state
   100  			"node1=http://localhost:7001",
   101  			[]string{"http://localhost:12345"},
   102  			true,
   103  
   104  			true,
   105  		},
   106  		{
   107  			// Advertised peer URLs must match those in cluster-state
   108  			"node1=http://localhost:2380,node1=http://localhost:12345",
   109  			[]string{"http://localhost:12345"},
   110  			true,
   111  
   112  			true,
   113  		},
   114  		{
   115  			// Advertised peer URLs must match those in cluster-state
   116  			"node1=http://localhost:12345",
   117  			[]string{"http://localhost:2380", "http://localhost:12345"},
   118  			true,
   119  
   120  			true,
   121  		},
   122  		{
   123  			// Advertised peer URLs must match those in cluster-state
   124  			"node1=http://localhost:2380",
   125  			[]string{},
   126  			true,
   127  
   128  			true,
   129  		},
   130  		{
   131  			// do not care about the urls if strict is not set
   132  			"node1=http://localhost:2380",
   133  			[]string{},
   134  			false,
   135  
   136  			false,
   137  		},
   138  	}
   139  
   140  	for i, tt := range tests {
   141  		cluster, err := types.NewURLsMap(tt.clusterSetting)
   142  		if err != nil {
   143  			t.Fatalf("#%d: Got unexpected error: %v", i, err)
   144  		}
   145  		cfg := ServerConfig{
   146  			Name:               "node1",
   147  			InitialPeerURLsMap: cluster,
   148  			Logger:             zap.NewExample(),
   149  		}
   150  		if tt.apurls != nil {
   151  			cfg.PeerURLs = mustNewURLs(t, tt.apurls)
   152  		}
   153  		if err = cfg.hasLocalMember(); err == nil && tt.strict {
   154  			err = cfg.advertiseMatchesCluster()
   155  		}
   156  		if (err == nil) && tt.shouldError {
   157  			t.Errorf("#%d: Got no error where one was expected", i)
   158  		}
   159  		if (err != nil) && !tt.shouldError {
   160  			t.Errorf("#%d: Got unexpected error: %v", i, err)
   161  		}
   162  	}
   163  }
   164  
   165  func TestSnapDir(t *testing.T) {
   166  	tests := map[string]string{
   167  		"/":            "/member/snap",
   168  		"/var/lib/etc": "/var/lib/etc/member/snap",
   169  	}
   170  	for dd, w := range tests {
   171  		cfg := ServerConfig{
   172  			DataDir: dd,
   173  			Logger:  zap.NewExample(),
   174  		}
   175  		if g := cfg.SnapDir(); g != w {
   176  			t.Errorf("DataDir=%q: SnapDir()=%q, want=%q", dd, g, w)
   177  		}
   178  	}
   179  }
   180  
   181  func TestWALDir(t *testing.T) {
   182  	tests := map[string]string{
   183  		"/":            "/member/wal",
   184  		"/var/lib/etc": "/var/lib/etc/member/wal",
   185  	}
   186  	for dd, w := range tests {
   187  		cfg := ServerConfig{
   188  			DataDir: dd,
   189  			Logger:  zap.NewExample(),
   190  		}
   191  		if g := cfg.WALDir(); g != w {
   192  			t.Errorf("DataDir=%q: WALDir()=%q, want=%q", dd, g, w)
   193  		}
   194  	}
   195  }
   196  
   197  func TestShouldDiscover(t *testing.T) {
   198  	tests := map[string]bool{
   199  		"":                              false,
   200  		"foo":                           true,
   201  		"http://discovery.etcd.io/asdf": true,
   202  	}
   203  	for durl, w := range tests {
   204  		cfg := ServerConfig{
   205  			DiscoveryURL: durl,
   206  			Logger:       zap.NewExample(),
   207  		}
   208  		if g := cfg.ShouldDiscover(); g != w {
   209  			t.Errorf("durl=%q: ShouldDiscover()=%t, want=%t", durl, g, w)
   210  		}
   211  	}
   212  }
   213  

View as plain text