-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdriver_test.go
91 lines (81 loc) · 2.33 KB
/
driver_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package lang
import (
"context"
"net"
"os"
"testing"
"time"
"github.com/freeconf/lang/pb"
"github.com/freeconf/yang/fc"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func TestDriverServer(t *testing.T) {
addr := "/tmp/foo"
s, err := NewDriver(addr, "")
go func() {
if err = s.Serve(); err != nil {
panic(err)
}
}()
fc.AssertEqual(t, nil, err)
c := createGrpcClient(t, addr)
defer c.Close()
client := pb.NewParserClient(c)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
srcResp, err := client.Source(ctx, &pb.SourceRequest{Path: "./test/testdata/yang"})
fc.RequireEqual(t, nil, err)
target := &pb.LoadModuleRequest_Name{Name: "basic"}
resp, err := client.LoadModule(ctx, &pb.LoadModuleRequest{SourceHnd: srcResp.SourceHnd, Module: target})
fc.AssertEqual(t, nil, err)
fc.AssertEqual(t, "basic", resp.Module.Ident)
}
func TestDriverClient(t *testing.T) {
addr := "/tmp/foo"
s, l := createGrpcServer(t, addr)
pb.RegisterXNodeServer(s, &dummyXNode{})
defer l.Close()
go s.Serve(l)
<-time.After(10 * time.Millisecond)
c := createGrpcClient(t, addr)
defer c.Close()
xc := pb.NewXNodeClient(c)
ctx := context.Background()
req := &pb.XChildRequest{}
resp, err := xc.XChild(ctx, req)
fc.AssertEqual(t, nil, err)
fc.AssertEqual(t, uint64(1000), resp.NodeHnd)
}
type dummyXNode struct {
pb.UnimplementedXNodeServer
}
func (*dummyXNode) XChild(context.Context, *pb.XChildRequest) (*pb.XChildResponse, error) {
return &pb.XChildResponse{NodeHnd: 1000}, nil
}
func createGrpcClient(t *testing.T, addr string) *grpc.ClientConn {
credentials := insecure.NewCredentials()
dialer := func(ctx context.Context, addr string) (net.Conn, error) {
var d net.Dialer
return d.DialContext(ctx, "unix", addr)
}
options := []grpc.DialOption{
grpc.WithTransportCredentials(credentials),
grpc.WithBlock(),
grpc.WithContextDialer(dialer),
}
c, err := grpc.Dial(addr, options...)
fc.AssertEqual(t, nil, err)
return c
}
func createGrpcServer(t *testing.T, addr string) (*grpc.Server, net.Listener) {
if _, ferr := os.Stat(addr); ferr == nil {
if err := os.Remove(addr); err != nil {
t.Fatalf("could not remove old socket file. %s", err)
}
}
l, err := net.Listen("unix", addr)
fc.AssertEqual(t, nil, err)
<-time.After(100 * time.Millisecond)
return grpc.NewServer(), l
}