k8s.io/apiserver@v0.29.3/pkg/storage/etcd3/preflight/checks_test.go (about)

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package preflight
    18  
    19  import (
    20  	"net/url"
    21  	"testing"
    22  	"time"
    23  
    24  	utilwait "k8s.io/apimachinery/pkg/util/wait"
    25  )
    26  
    27  func TestParseServerURIGood(t *testing.T) {
    28  	connURL, err := parseServerURI("https://127.0.0.1:2379")
    29  	if err != nil {
    30  		t.Fatalf("unexpected error: %v", err)
    31  	}
    32  
    33  	reference := "127.0.0.1:2379"
    34  	if connURL.Host != reference {
    35  		t.Fatalf("server uri was not parsed correctly, host %s was invalid", connURL.Host)
    36  	}
    37  }
    38  
    39  func TestParseServerURIGoodUnix(t *testing.T) {
    40  	connURL, err := parseServerURI("unix://127.0.0.1:21002112605")
    41  	if err != nil {
    42  		t.Fatalf("unexpected error: %v", err)
    43  	}
    44  
    45  	reference := "127.0.0.1:21002112605"
    46  	if connURL.Host != reference {
    47  		t.Fatalf("server uri was not parsed correctly, host %s was invalid", connURL.Host)
    48  	}
    49  }
    50  
    51  func TestParseServerURIBad(t *testing.T) {
    52  	_, err := parseServerURI("-invalid uri$@#%")
    53  	if err == nil {
    54  		t.Fatal("expected bad uri to raise parse error")
    55  	}
    56  }
    57  
    58  func TestEtcdConnection(t *testing.T) {
    59  	etcd := new(EtcdConnection)
    60  
    61  	result := etcd.serverReachable(&url.URL{Host: "-not a real network address-", Scheme: "tcp"})
    62  	if result {
    63  		t.Fatal("checkConnection should not have succeeded")
    64  	}
    65  }
    66  
    67  func TestCheckEtcdServersEmpty(t *testing.T) {
    68  	etcd := new(EtcdConnection)
    69  	result, err := etcd.CheckEtcdServers()
    70  	if err != nil {
    71  		t.Fatalf("unexpected error: %v", err)
    72  	}
    73  	if result {
    74  		t.Fatal("CheckEtcdServers should not have succeeded")
    75  	}
    76  }
    77  
    78  func TestCheckEtcdServersUri(t *testing.T) {
    79  	etcd := new(EtcdConnection)
    80  	etcd.ServerList = []string{"-invalid uri$@#%"}
    81  	result, err := etcd.CheckEtcdServers()
    82  	if err == nil {
    83  		t.Fatalf("expected bad uri to raise parse error")
    84  	}
    85  	if result {
    86  		t.Fatal("CheckEtcdServers should not have succeeded")
    87  	}
    88  }
    89  
    90  func TestCheckEtcdServers(t *testing.T) {
    91  	etcd := new(EtcdConnection)
    92  	etcd.ServerList = []string{""}
    93  	result, err := etcd.CheckEtcdServers()
    94  	if err != nil {
    95  		t.Fatalf("unexpected error: %v", err)
    96  	}
    97  	if result {
    98  		t.Fatal("CheckEtcdServers should not have succeeded")
    99  	}
   100  }
   101  
   102  func TestPollCheckServer(t *testing.T) {
   103  	err := utilwait.PollImmediate(1*time.Microsecond,
   104  		2*time.Microsecond,
   105  		EtcdConnection{ServerList: []string{""}}.CheckEtcdServers)
   106  	if err == nil {
   107  		t.Fatal("expected check to time out")
   108  	}
   109  }