@@ -18,17 +18,25 @@ import (
1818 "crypto/tls"
1919 "fmt"
2020 "io/ioutil"
21+ "net"
2122 "net/url"
2223 "os"
2324 "testing"
2425 "time"
2526
2627 "github.com/stretchr/testify/assert"
28+ "go.etcd.io/etcd/pkg/srv"
2729 "go.etcd.io/etcd/pkg/transport"
30+ "go.etcd.io/etcd/pkg/types"
2831
2932 "sigs.k8s.io/yaml"
3033)
3134
35+ func notFoundErr (service , domain string ) error {
36+ name := fmt .Sprintf ("_%s._tcp.%s" , service , domain )
37+ return & net.DNSError {Err : "no such host" , Name : name , Server : "10.0.0.53:53" , IsTimeout : false , IsTemporary : false , IsNotFound : true }
38+ }
39+
3240func TestConfigFileOtherFields (t * testing.T ) {
3341 ctls := securityConfig {TrustedCAFile : "cca" , CertFile : "ccert" , KeyFile : "ckey" }
3442 ptls := securityConfig {TrustedCAFile : "pca" , CertFile : "pcert" , KeyFile : "pkey" }
@@ -86,7 +94,7 @@ func TestUpdateDefaultClusterFromName(t *testing.T) {
8694
8795 // in case of 'etcd --name=abc'
8896 exp := fmt .Sprintf ("%s=%s://localhost:%s" , cfg .Name , oldscheme , lpport )
89- cfg .UpdateDefaultClusterFromName (defaultInitialCluster )
97+ _ , _ = cfg .UpdateDefaultClusterFromName (defaultInitialCluster )
9098 if exp != cfg .InitialCluster {
9199 t .Fatalf ("initial-cluster expected %q, got %q" , exp , cfg .InitialCluster )
92100 }
@@ -281,3 +289,77 @@ func TestTLSVersionMinMax(t *testing.T) {
281289 })
282290 }
283291}
292+
293+ func TestPeerURLsMapAndTokenFromSRV (t * testing.T ) {
294+ defer func () { getCluster = srv .GetCluster }()
295+ tests := []struct {
296+ withSSL []string
297+ withoutSSL []string
298+ apurls []string
299+ wurls string
300+ werr bool
301+ }{
302+ {
303+ []string {},
304+ []string {},
305+ []string {"http://localhost:2380" },
306+ "" ,
307+ true ,
308+ },
309+ {
310+ []string {"1.example.com=https://1.example.com:2380" , "0=https://2.example.com:2380" , "1=https://3.example.com:2380" },
311+ []string {},
312+ []string {"https://1.example.com:2380" },
313+ "0=https://2.example.com:2380,1.example.com=https://1.example.com:2380,1=https://3.example.com:2380" ,
314+ false ,
315+ },
316+ {
317+ []string {"1.example.com=https://1.example.com:2380" },
318+ []string {"0=http://2.example.com:2380" , "1=http://3.example.com:2380" },
319+ []string {"https://1.example.com:2380" },
320+ "0=http://2.example.com:2380,1.example.com=https://1.example.com:2380,1=http://3.example.com:2380" ,
321+ false ,
322+ },
323+ {
324+ []string {},
325+ []string {"1.example.com=http://1.example.com:2380" , "0=http://2.example.com:2380" , "1=http://3.example.com:2380" },
326+ []string {"http://1.example.com:2380" },
327+ "0=http://2.example.com:2380,1.example.com=http://1.example.com:2380,1=http://3.example.com:2380" ,
328+ false ,
329+ },
330+ }
331+ hasErr := func (err error ) bool {
332+ return err != nil
333+ }
334+ for i , tt := range tests {
335+ getCluster = func (serviceScheme string , service string , name string , dns string , apurls types.URLs ) ([]string , error ) {
336+ var urls []string
337+ if serviceScheme == "https" && service == "etcd-server-ssl" {
338+ urls = tt .withSSL
339+ } else if serviceScheme == "http" && service == "etcd-server" {
340+ urls = tt .withoutSSL
341+ }
342+ if len (urls ) > 0 {
343+ return urls , nil
344+ }
345+ return urls , notFoundErr (service , dns )
346+ }
347+ cfg := NewConfig ()
348+ cfg .Name = "1.example.com"
349+ cfg .InitialCluster = ""
350+ cfg .InitialClusterToken = ""
351+ cfg .DNSCluster = "example.com"
352+ cfg .AdvertisePeerUrls = types .MustNewURLs (tt .apurls )
353+ if err := cfg .Validate (); err != nil {
354+ t .Errorf ("#%d: failed to validate test Config: %v" , i , err )
355+ continue
356+ }
357+ urlsmap , _ , err := cfg .PeerURLsMapAndToken ("etcd" )
358+ if urlsmap .String () != tt .wurls {
359+ t .Errorf ("#%d: urlsmap = %s, want = %s" , i , urlsmap .String (), tt .wurls )
360+ }
361+ if hasErr (err ) != tt .werr {
362+ t .Errorf ("#%d: err = %v, want = %v" , i , err , tt .werr )
363+ }
364+ }
365+ }
0 commit comments