| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | package test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2018-06-01 15:12:49 +01:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | 	"testing" | 
					
						
							|  |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-30 09:19:26 +00:00
										 |  |  | 	"github.com/coredns/coredns/pb" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | 	"github.com/miekg/dns" | 
					
						
							|  |  |  | 	"google.golang.org/grpc" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestGrpc(t *testing.T) { | 
					
						
							|  |  |  | 	corefile := `grpc://.:0 { | 
					
						
							|  |  |  | 		whoami | 
					
						
							| 
									
										
										
										
											2020-04-25 14:08:36 +08:00
										 |  |  | 	}` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-24 11:35:14 +01:00
										 |  |  | 	g, _, tcp, err := CoreDNSServerAndPorts(corefile) | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Could not get CoreDNS serving instance: %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	defer g.Stop() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-01 22:32:59 +02:00
										 |  |  | 	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | 
					
						
							|  |  |  | 	defer cancel() | 
					
						
							| 
									
										
										
										
											2019-10-01 10:12:10 +03:30
										 |  |  | 	conn, err := grpc.DialContext(ctx, tcp, grpc.WithInsecure(), grpc.WithBlock()) | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Expected no error but got: %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	defer conn.Close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	client := pb.NewDnsServiceClient(conn) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	m := new(dns.Msg) | 
					
						
							|  |  |  | 	m.SetQuestion("whoami.example.org.", dns.TypeA) | 
					
						
							|  |  |  | 	msg, _ := m.Pack() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	reply, err := client.Query(context.TODO(), &pb.DnsPacket{Msg: msg}) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Expected no error but got: %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	d := new(dns.Msg) | 
					
						
							|  |  |  | 	err = d.Unpack(reply.Msg) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Expected no error but got: %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if d.Rcode != dns.RcodeSuccess { | 
					
						
							| 
									
										
										
										
											2017-06-14 09:37:10 -07:00
										 |  |  | 		t.Errorf("Expected success but got %d", d.Rcode) | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if len(d.Extra) != 2 { | 
					
						
							| 
									
										
										
										
											2017-06-14 09:37:10 -07:00
										 |  |  | 		t.Errorf("Expected 2 RRs in additional section, but got %d", len(d.Extra)) | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | } |