...

Source file src/go.etcd.io/etcd/client/v3/yaml/config_test.go

Documentation: go.etcd.io/etcd/client/v3/yaml

     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 yaml
    16  
    17  import (
    18  	"io/ioutil"
    19  	"log"
    20  	"os"
    21  	"reflect"
    22  	"testing"
    23  
    24  	"sigs.k8s.io/yaml"
    25  )
    26  
    27  var (
    28  	certPath       = "../../../tests/fixtures/server.crt"
    29  	privateKeyPath = "../../../tests/fixtures/server.key.insecure"
    30  	caPath         = "../../../tests/fixtures/ca.crt"
    31  )
    32  
    33  func TestConfigFromFile(t *testing.T) {
    34  	tests := []struct {
    35  		ym *yamlConfig
    36  
    37  		werr bool
    38  	}{
    39  		{
    40  			&yamlConfig{},
    41  			false,
    42  		},
    43  		{
    44  			&yamlConfig{
    45  				InsecureTransport: true,
    46  			},
    47  			false,
    48  		},
    49  		{
    50  			&yamlConfig{
    51  				Keyfile:               privateKeyPath,
    52  				Certfile:              certPath,
    53  				TrustedCAfile:         caPath,
    54  				InsecureSkipTLSVerify: true,
    55  			},
    56  			false,
    57  		},
    58  		{
    59  			&yamlConfig{
    60  				Keyfile:  "bad",
    61  				Certfile: "bad",
    62  			},
    63  			true,
    64  		},
    65  		{
    66  			&yamlConfig{
    67  				Keyfile:       privateKeyPath,
    68  				Certfile:      certPath,
    69  				TrustedCAfile: "bad",
    70  			},
    71  			true,
    72  		},
    73  	}
    74  
    75  	for i, tt := range tests {
    76  		tmpfile, err := ioutil.TempFile("", "clientcfg")
    77  		if err != nil {
    78  			log.Fatal(err)
    79  		}
    80  
    81  		b, err := yaml.Marshal(tt.ym)
    82  		if err != nil {
    83  			t.Fatal(err)
    84  		}
    85  
    86  		_, err = tmpfile.Write(b)
    87  		if err != nil {
    88  			t.Fatal(err)
    89  		}
    90  		err = tmpfile.Close()
    91  		if err != nil {
    92  			t.Fatal(err)
    93  		}
    94  
    95  		cfg, cerr := NewConfig(tmpfile.Name())
    96  		if cerr != nil && !tt.werr {
    97  			t.Errorf("#%d: err = %v, want %v", i, cerr, tt.werr)
    98  			continue
    99  		}
   100  		if cerr != nil {
   101  			os.Remove(tmpfile.Name())
   102  			continue
   103  		}
   104  
   105  		if !reflect.DeepEqual(cfg.Endpoints, tt.ym.Endpoints) {
   106  			t.Errorf("#%d: endpoint = %v, want %v", i, cfg.Endpoints, tt.ym.Endpoints)
   107  		}
   108  
   109  		if tt.ym.InsecureTransport != (cfg.TLS == nil) {
   110  			t.Errorf("#%d: insecureTransport = %v, want %v", i, cfg.TLS == nil, tt.ym.InsecureTransport)
   111  		}
   112  
   113  		if !tt.ym.InsecureTransport {
   114  			if tt.ym.Certfile != "" && len(cfg.TLS.Certificates) == 0 {
   115  				t.Errorf("#%d: failed to load in cert", i)
   116  			}
   117  			if tt.ym.TrustedCAfile != "" && cfg.TLS.RootCAs == nil {
   118  				t.Errorf("#%d: failed to load in ca cert", i)
   119  			}
   120  			if cfg.TLS.InsecureSkipVerify != tt.ym.InsecureSkipTLSVerify {
   121  				t.Errorf("#%d: skipTLSVeify = %v, want %v", i, cfg.TLS.InsecureSkipVerify, tt.ym.InsecureSkipTLSVerify)
   122  			}
   123  		}
   124  
   125  		os.Remove(tmpfile.Name())
   126  	}
   127  }
   128  

View as plain text