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

     1  // Copyright 2016 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 embed
    16  
    17  import (
    18  	"fmt"
    19  	"io/ioutil"
    20  	"net/url"
    21  	"os"
    22  	"testing"
    23  
    24  	"github.com/coreos/etcd/pkg/transport"
    25  
    26  	"sigs.k8s.io/yaml"
    27  )
    28  
    29  func TestConfigFileOtherFields(t *testing.T) {
    30  	ctls := securityConfig{CAFile: "cca", CertFile: "ccert", KeyFile: "ckey"}
    31  	ptls := securityConfig{CAFile: "pca", CertFile: "pcert", KeyFile: "pkey"}
    32  	yc := struct {
    33  		ClientSecurityCfgFile securityConfig `json:"client-transport-security"`
    34  		PeerSecurityCfgFile   securityConfig `json:"peer-transport-security"`
    35  		ForceNewCluster       bool           `json:"force-new-cluster"`
    36  	}{
    37  		ctls,
    38  		ptls,
    39  		true,
    40  	}
    41  
    42  	b, err := yaml.Marshal(&yc)
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  
    47  	tmpfile := mustCreateCfgFile(t, b)
    48  	defer os.Remove(tmpfile.Name())
    49  
    50  	cfg, err := ConfigFromFile(tmpfile.Name())
    51  	if err != nil {
    52  		t.Fatal(err)
    53  	}
    54  
    55  	if !cfg.ForceNewCluster {
    56  		t.Errorf("ForceNewCluster = %v, want %v", cfg.ForceNewCluster, true)
    57  	}
    58  
    59  	if !ctls.equals(&cfg.ClientTLSInfo) {
    60  		t.Errorf("ClientTLS = %v, want %v", cfg.ClientTLSInfo, ctls)
    61  	}
    62  	if !ptls.equals(&cfg.PeerTLSInfo) {
    63  		t.Errorf("PeerTLS = %v, want %v", cfg.PeerTLSInfo, ptls)
    64  	}
    65  }
    66  
    67  // TestUpdateDefaultClusterFromName ensures that etcd can start with 'etcd --name=abc'.
    68  func TestUpdateDefaultClusterFromName(t *testing.T) {
    69  	cfg := NewConfig()
    70  	defaultInitialCluster := cfg.InitialCluster
    71  	oldscheme := cfg.APUrls[0].Scheme
    72  	origpeer := cfg.APUrls[0].String()
    73  	origadvc := cfg.ACUrls[0].String()
    74  
    75  	cfg.Name = "abc"
    76  	lpport := cfg.LPUrls[0].Port()
    77  
    78  	// in case of 'etcd --name=abc'
    79  	exp := fmt.Sprintf("%s=%s://localhost:%s", cfg.Name, oldscheme, lpport)
    80  	cfg.UpdateDefaultClusterFromName(defaultInitialCluster)
    81  	if exp != cfg.InitialCluster {
    82  		t.Fatalf("initial-cluster expected %q, got %q", exp, cfg.InitialCluster)
    83  	}
    84  	// advertise peer URL should not be affected
    85  	if origpeer != cfg.APUrls[0].String() {
    86  		t.Fatalf("advertise peer url expected %q, got %q", origadvc, cfg.APUrls[0].String())
    87  	}
    88  	// advertise client URL should not be affected
    89  	if origadvc != cfg.ACUrls[0].String() {
    90  		t.Fatalf("advertise client url expected %q, got %q", origadvc, cfg.ACUrls[0].String())
    91  	}
    92  }
    93  
    94  // TestUpdateDefaultClusterFromNameOverwrite ensures that machine's default host is only used
    95  // if advertise URLs are default values(localhost:2379,2380) AND if listen URL is 0.0.0.0.
    96  func TestUpdateDefaultClusterFromNameOverwrite(t *testing.T) {
    97  	if defaultHostname == "" {
    98  		t.Skip("machine's default host not found")
    99  	}
   100  
   101  	cfg := NewConfig()
   102  	defaultInitialCluster := cfg.InitialCluster
   103  	oldscheme := cfg.APUrls[0].Scheme
   104  	origadvc := cfg.ACUrls[0].String()
   105  
   106  	cfg.Name = "abc"
   107  	lpport := cfg.LPUrls[0].Port()
   108  	cfg.LPUrls[0] = url.URL{Scheme: cfg.LPUrls[0].Scheme, Host: fmt.Sprintf("0.0.0.0:%s", lpport)}
   109  	dhost, _ := cfg.UpdateDefaultClusterFromName(defaultInitialCluster)
   110  	if dhost != defaultHostname {
   111  		t.Fatalf("expected default host %q, got %q", defaultHostname, dhost)
   112  	}
   113  	aphost, apport := cfg.APUrls[0].Hostname(), cfg.APUrls[0].Port()
   114  	if apport != lpport {
   115  		t.Fatalf("advertise peer url got different port %s, expected %s", apport, lpport)
   116  	}
   117  	if aphost != defaultHostname {
   118  		t.Fatalf("advertise peer url expected machine default host %q, got %q", defaultHostname, aphost)
   119  	}
   120  	expected := fmt.Sprintf("%s=%s://%s:%s", cfg.Name, oldscheme, defaultHostname, lpport)
   121  	if expected != cfg.InitialCluster {
   122  		t.Fatalf("initial-cluster expected %q, got %q", expected, cfg.InitialCluster)
   123  	}
   124  
   125  	// advertise client URL should not be affected
   126  	if origadvc != cfg.ACUrls[0].String() {
   127  		t.Fatalf("advertise-client-url expected %q, got %q", origadvc, cfg.ACUrls[0].String())
   128  	}
   129  }
   130  
   131  func (s *securityConfig) equals(t *transport.TLSInfo) bool {
   132  	return s.CAFile == t.CAFile &&
   133  		s.CertFile == t.CertFile &&
   134  		s.CertAuth == t.ClientCertAuth &&
   135  		s.TrustedCAFile == t.TrustedCAFile
   136  }
   137  
   138  func mustCreateCfgFile(t *testing.T, b []byte) *os.File {
   139  	tmpfile, err := ioutil.TempFile("", "servercfg")
   140  	if err != nil {
   141  		t.Fatal(err)
   142  	}
   143  	if _, err = tmpfile.Write(b); err != nil {
   144  		t.Fatal(err)
   145  	}
   146  	if err = tmpfile.Close(); err != nil {
   147  		t.Fatal(err)
   148  	}
   149  	return tmpfile
   150  }
   151  
   152  func TestAutoCompactionModeInvalid(t *testing.T) {
   153  	cfg := NewConfig()
   154  	cfg.AutoCompactionMode = "period"
   155  	err := cfg.Validate()
   156  	if err == nil {
   157  		t.Errorf("expected non-nil error, got %v", err)
   158  	}
   159  }
   160  
   161  func TestAutoCompactionModeParse(t *testing.T) {
   162  	dur, err := parseCompactionRetention("revision", "1")
   163  	if err != nil {
   164  		t.Error(err)
   165  	}
   166  	if dur != 1 {
   167  		t.Fatalf("AutoCompactionRetention expected 1, got %d", dur)
   168  	}
   169  }