3 * Copyright 2017 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
30 "google.golang.org/grpc/resolver"
31 "google.golang.org/grpc/test/leakcheck"
34 func TestMain(m *testing.M) {
35 cleanup := replaceNetFunc()
45 type testClientConn struct {
48 addrs []resolver.Address
55 func (t *testClientConn) NewAddress(addresses []resolver.Address) {
62 func (t *testClientConn) getAddress() ([]resolver.Address, int) {
68 func (t *testClientConn) NewServiceConfig(serviceConfig string) {
75 func (t *testClientConn) getSc() (string, int) {
81 var hostLookupTbl = struct {
83 tbl map[string][]string
85 tbl: map[string][]string{
86 "foo.bar.com": {"1.2.3.4", "5.6.7.8"},
87 "ipv4.single.fake": {"1.2.3.4"},
88 "srv.ipv4.single.fake": {"2.4.6.8"},
89 "ipv4.multi.fake": {"1.2.3.4", "5.6.7.8", "9.10.11.12"},
90 "ipv6.single.fake": {"2607:f8b0:400a:801::1001"},
91 "ipv6.multi.fake": {"2607:f8b0:400a:801::1001", "2607:f8b0:400a:801::1002", "2607:f8b0:400a:801::1003"},
95 func hostLookup(host string) ([]string, error) {
97 defer hostLookupTbl.Unlock()
98 if addrs, cnt := hostLookupTbl.tbl[host]; cnt {
101 return nil, fmt.Errorf("failed to lookup host:%s resolution in hostLookupTbl", host)
104 var srvLookupTbl = struct {
106 tbl map[string][]*net.SRV
108 tbl: map[string][]*net.SRV{
109 "_grpclb._tcp.srv.ipv4.single.fake": {&net.SRV{Target: "ipv4.single.fake", Port: 1234}},
110 "_grpclb._tcp.srv.ipv4.multi.fake": {&net.SRV{Target: "ipv4.multi.fake", Port: 1234}},
111 "_grpclb._tcp.srv.ipv6.single.fake": {&net.SRV{Target: "ipv6.single.fake", Port: 1234}},
112 "_grpclb._tcp.srv.ipv6.multi.fake": {&net.SRV{Target: "ipv6.multi.fake", Port: 1234}},
116 func srvLookup(service, proto, name string) (string, []*net.SRV, error) {
117 cname := "_" + service + "._" + proto + "." + name
119 defer srvLookupTbl.Unlock()
120 if srvs, cnt := srvLookupTbl.tbl[cname]; cnt {
121 return cname, srvs, nil
123 return "", nil, fmt.Errorf("failed to lookup srv record for %s in srvLookupTbl", cname)
126 // div divides a byte slice into a slice of strings, each of which is of maximum
127 // 255 bytes length, which is the length limit per TXT record in DNS.
128 func div(b []byte) []string {
130 for i := 0; i < len(b); i += txtBytesLimit {
131 if i+txtBytesLimit > len(b) {
132 r = append(r, string(b[i:]))
134 r = append(r, string(b[i:i+txtBytesLimit]))
140 // scfs contains an array of service config file string in JSON format.
141 // Notes about the scfs contents and usage:
142 // scfs contains 4 service config file JSON strings for testing. Inside each
143 // service config file, there are multiple choices. scfs[0:3] each contains 5
144 // choices, and first 3 choices are nonmatching choices based on canarying rule,
145 // while the last two are matched choices. scfs[3] only contains 3 choices, and
146 // all of them are nonmatching based on canarying rule. For each of scfs[0:3],
147 // the eventually returned service config, which is from the first of the two
148 // matched choices, is stored in the corresponding scs element (e.g.
149 // scfs[0]->scs[0]). scfs and scs elements are used in pair to test the dns
150 // resolver functionality, with scfs as the input and scs used for validation of
151 // the output. For scfs[3], it corresponds to empty service config, since there
152 // isn't a matched choice.
161 "loadBalancingPolicy": "grpclb",
177 "loadBalancingPolicy": "grpclb",
195 "loadBalancingPolicy": "grpclb",
221 "maxRequestMessageBytes": 1024,
222 "maxResponseMessageBytes": 1024
229 "loadBalancingPolicy": "round_robin",
251 "loadBalancingPolicy": "grpclb",
267 "loadBalancingPolicy": "grpclb",
285 "loadBalancingPolicy": "grpclb",
312 "waitForReady": true,
314 "maxRequestMessageBytes": 1024,
315 "maxResponseMessageBytes": 1024
322 "loadBalancingPolicy": "round_robin",
344 "loadBalancingPolicy": "grpclb",
360 "loadBalancingPolicy": "grpclb",
378 "loadBalancingPolicy": "grpclb",
397 "loadBalancingPolicy": "round_robin",
405 "waitForReady": true,
414 "waitForReady": false
421 "loadBalancingPolicy": "round_robin",
443 "loadBalancingPolicy": "grpclb",
459 "loadBalancingPolicy": "grpclb",
477 "loadBalancingPolicy": "grpclb",
493 // scs contains an array of service config string in JSON format.
503 "maxRequestMessageBytes": 1024,
504 "maxResponseMessageBytes": 1024
517 "waitForReady": true,
519 "maxRequestMessageBytes": 1024,
520 "maxResponseMessageBytes": 1024
525 "loadBalancingPolicy": "round_robin",
533 "waitForReady": true,
542 "waitForReady": false
548 // scLookupTbl is a set, which contains targets that have service config. Target
549 // not in this set should not have service config.
550 var scLookupTbl = map[string]bool{
552 "srv.ipv4.single.fake": true,
553 "srv.ipv4.multi.fake": true,
554 "no.attribute": true,
557 // generateSCF generates a slice of strings (aggregately representing a single
558 // service config file) for the input name, which mocks the result from a real
559 // DNS TXT record lookup.
560 func generateSCF(name string) []string {
565 case "srv.ipv4.single.fake":
567 case "srv.ipv4.multi.fake":
572 if name == "no.attribute" {
575 return div(append([]byte(txtAttribute), b...))
578 // generateSC returns a service config string in JSON format for the input name.
579 func generateSC(name string) string {
580 _, cnt := scLookupTbl[name]
581 if !cnt || name == "no.attribute" {
587 case "srv.ipv4.single.fake":
589 case "srv.ipv4.multi.fake":
596 var txtLookupTbl = struct {
598 tbl map[string][]string
600 tbl: map[string][]string{
601 "foo.bar.com": generateSCF("foo.bar.com"),
602 "srv.ipv4.single.fake": generateSCF("srv.ipv4.single.fake"),
603 "srv.ipv4.multi.fake": generateSCF("srv.ipv4.multi.fake"),
604 "srv.ipv6.single.fake": generateSCF("srv.ipv6.single.fake"),
605 "srv.ipv6.multi.fake": generateSCF("srv.ipv6.multi.fake"),
606 "no.attribute": generateSCF("no.attribute"),
610 func txtLookup(host string) ([]string, error) {
612 defer txtLookupTbl.Unlock()
613 if scs, cnt := txtLookupTbl.tbl[host]; cnt {
616 return nil, fmt.Errorf("failed to lookup TXT:%s resolution in txtLookupTbl", host)
619 func TestResolve(t *testing.T) {
625 func testDNSResolver(t *testing.T) {
626 defer leakcheck.Check(t)
629 addrWant []resolver.Address
634 []resolver.Address{{Addr: "1.2.3.4" + colonDefaultPort}, {Addr: "5.6.7.8" + colonDefaultPort}},
635 generateSC("foo.bar.com"),
639 []resolver.Address{{Addr: "1.2.3.4:1234"}, {Addr: "5.6.7.8:1234"}},
640 generateSC("foo.bar.com"),
643 "srv.ipv4.single.fake",
644 []resolver.Address{{Addr: "1.2.3.4:1234", Type: resolver.GRPCLB, ServerName: "ipv4.single.fake"}, {Addr: "2.4.6.8" + colonDefaultPort}},
645 generateSC("srv.ipv4.single.fake"),
648 "srv.ipv4.multi.fake",
650 {Addr: "1.2.3.4:1234", Type: resolver.GRPCLB, ServerName: "ipv4.multi.fake"},
651 {Addr: "5.6.7.8:1234", Type: resolver.GRPCLB, ServerName: "ipv4.multi.fake"},
652 {Addr: "9.10.11.12:1234", Type: resolver.GRPCLB, ServerName: "ipv4.multi.fake"},
654 generateSC("srv.ipv4.multi.fake"),
657 "srv.ipv6.single.fake",
658 []resolver.Address{{Addr: "[2607:f8b0:400a:801::1001]:1234", Type: resolver.GRPCLB, ServerName: "ipv6.single.fake"}},
659 generateSC("srv.ipv6.single.fake"),
662 "srv.ipv6.multi.fake",
664 {Addr: "[2607:f8b0:400a:801::1001]:1234", Type: resolver.GRPCLB, ServerName: "ipv6.multi.fake"},
665 {Addr: "[2607:f8b0:400a:801::1002]:1234", Type: resolver.GRPCLB, ServerName: "ipv6.multi.fake"},
666 {Addr: "[2607:f8b0:400a:801::1003]:1234", Type: resolver.GRPCLB, ServerName: "ipv6.multi.fake"},
668 generateSC("srv.ipv6.multi.fake"),
673 generateSC("no.attribute"),
677 for _, a := range tests {
679 cc := &testClientConn{target: a.target}
680 r, err := b.Build(resolver.Target{Endpoint: a.target}, cc, resolver.BuildOption{})
682 t.Fatalf("%v\n", err)
684 var addrs []resolver.Address
687 addrs, cnt = cc.getAddress()
691 time.Sleep(time.Millisecond)
699 time.Sleep(time.Millisecond)
701 if !reflect.DeepEqual(a.addrWant, addrs) {
702 t.Errorf("Resolved addresses of target: %q = %+v, want %+v\n", a.target, addrs, a.addrWant)
704 if !reflect.DeepEqual(a.scWant, sc) {
705 t.Errorf("Resolved service config of target: %q = %+v, want %+v\n", a.target, sc, a.scWant)
711 func mutateTbl(target string) func() {
713 oldHostTblEntry := hostLookupTbl.tbl[target]
714 hostLookupTbl.tbl[target] = hostLookupTbl.tbl[target][:len(oldHostTblEntry)-1]
715 hostLookupTbl.Unlock()
717 oldTxtTblEntry := txtLookupTbl.tbl[target]
718 txtLookupTbl.tbl[target] = []string{""}
719 txtLookupTbl.Unlock()
723 hostLookupTbl.tbl[target] = oldHostTblEntry
724 hostLookupTbl.Unlock()
726 txtLookupTbl.tbl[target] = oldTxtTblEntry
727 txtLookupTbl.Unlock()
731 func testDNSResolveNow(t *testing.T) {
732 defer leakcheck.Check(t)
735 addrWant []resolver.Address
736 addrNext []resolver.Address
742 []resolver.Address{{Addr: "1.2.3.4" + colonDefaultPort}, {Addr: "5.6.7.8" + colonDefaultPort}},
743 []resolver.Address{{Addr: "1.2.3.4" + colonDefaultPort}},
744 generateSC("foo.bar.com"),
749 for _, a := range tests {
751 cc := &testClientConn{target: a.target}
752 r, err := b.Build(resolver.Target{Endpoint: a.target}, cc, resolver.BuildOption{})
754 t.Fatalf("%v\n", err)
756 var addrs []resolver.Address
759 addrs, cnt = cc.getAddress()
763 time.Sleep(time.Millisecond)
771 time.Sleep(time.Millisecond)
773 if !reflect.DeepEqual(a.addrWant, addrs) {
774 t.Errorf("Resolved addresses of target: %q = %+v, want %+v\n", a.target, addrs, a.addrWant)
776 if !reflect.DeepEqual(a.scWant, sc) {
777 t.Errorf("Resolved service config of target: %q = %+v, want %+v\n", a.target, sc, a.scWant)
779 revertTbl := mutateTbl(a.target)
780 r.ResolveNow(resolver.ResolveNowOption{})
782 addrs, cnt = cc.getAddress()
786 time.Sleep(time.Millisecond)
793 time.Sleep(time.Millisecond)
795 if !reflect.DeepEqual(a.addrNext, addrs) {
796 t.Errorf("Resolved addresses of target: %q = %+v, want %+v\n", a.target, addrs, a.addrNext)
798 if !reflect.DeepEqual(a.scNext, sc) {
799 t.Errorf("Resolved service config of target: %q = %+v, want %+v\n", a.target, sc, a.scNext)
806 const colonDefaultPort = ":" + defaultPort
808 func testIPResolver(t *testing.T) {
809 defer leakcheck.Check(t)
812 want []resolver.Address
814 {"127.0.0.1", []resolver.Address{{Addr: "127.0.0.1" + colonDefaultPort}}},
815 {"127.0.0.1:12345", []resolver.Address{{Addr: "127.0.0.1:12345"}}},
816 {"::1", []resolver.Address{{Addr: "[::1]" + colonDefaultPort}}},
817 {"[::1]:12345", []resolver.Address{{Addr: "[::1]:12345"}}},
818 {"[::1]:", []resolver.Address{{Addr: "[::1]:443"}}},
819 {"2001:db8:85a3::8a2e:370:7334", []resolver.Address{{Addr: "[2001:db8:85a3::8a2e:370:7334]" + colonDefaultPort}}},
820 {"[2001:db8:85a3::8a2e:370:7334]", []resolver.Address{{Addr: "[2001:db8:85a3::8a2e:370:7334]" + colonDefaultPort}}},
821 {"[2001:db8:85a3::8a2e:370:7334]:12345", []resolver.Address{{Addr: "[2001:db8:85a3::8a2e:370:7334]:12345"}}},
822 {"[2001:db8::1]:http", []resolver.Address{{Addr: "[2001:db8::1]:http"}}},
823 // TODO(yuxuanli): zone support?
826 for _, v := range tests {
828 cc := &testClientConn{target: v.target}
829 r, err := b.Build(resolver.Target{Endpoint: v.target}, cc, resolver.BuildOption{})
831 t.Fatalf("%v\n", err)
833 var addrs []resolver.Address
836 addrs, cnt = cc.getAddress()
840 time.Sleep(time.Millisecond)
842 if !reflect.DeepEqual(v.want, addrs) {
843 t.Errorf("Resolved addresses of target: %q = %+v, want %+v\n", v.target, addrs, v.want)
845 r.ResolveNow(resolver.ResolveNowOption{})
847 addrs, cnt = cc.getAddress()
851 time.Sleep(time.Millisecond)
853 if !reflect.DeepEqual(v.want, addrs) {
854 t.Errorf("Resolved addresses of target: %q = %+v, want %+v\n", v.target, addrs, v.want)
860 func TestResolveFunc(t *testing.T) {
861 defer leakcheck.Check(t)
866 // TODO(yuxuanli): More false cases?
867 {"www.google.com", nil},
868 {"foo.bar:12345", nil},
870 {"127.0.0.1:12345", nil},
872 {"[2001:db8:a0b:12f0::1]:21", nil},
874 {"127.0.0...1:12345", nil},
875 {"[fe80::1%lo0]:80", nil},
876 {"golang.org:http", nil},
877 {"[2001:db8::1]:http", nil},
879 {"", errMissingAddr},
880 {"[2001:db8:a0b:12f0::1", errForInvalidTarget},
884 for _, v := range tests {
885 cc := &testClientConn{target: v.addr}
886 r, err := b.Build(resolver.Target{Endpoint: v.addr}, cc, resolver.BuildOption{})
890 if !reflect.DeepEqual(err, v.want) {
891 t.Errorf("Build(%q, cc, resolver.BuildOption{}) = %v, want %v", v.addr, err, v.want)