mirror of
https://github.com/stefanprodan/podinfo.git
synced 2026-04-19 17:26:39 +00:00
65 lines
1.2 KiB
Go
65 lines
1.2 KiB
Go
package grpc
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"net"
|
|
"regexp"
|
|
"testing"
|
|
|
|
"github.com/stefanprodan/podinfo/pkg/api/grpc/env"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/status"
|
|
"google.golang.org/grpc/test/bufconn"
|
|
)
|
|
|
|
func TestGrpcEnv(t *testing.T) {
|
|
|
|
lis := bufconn.Listen(1024 * 1024)
|
|
t.Cleanup(func() {
|
|
lis.Close()
|
|
})
|
|
|
|
srv := grpc.NewServer()
|
|
t.Cleanup(func() {
|
|
srv.Stop()
|
|
})
|
|
|
|
env.RegisterEnvServiceServer(srv, &EnvServer{})
|
|
|
|
go func() {
|
|
if err := srv.Serve(lis); err != nil {
|
|
log.Fatalf("srv.Serve %v", err)
|
|
}
|
|
}()
|
|
|
|
dialer := func(context.Context, string) (net.Conn, error) {
|
|
return lis.Dial()
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
conn, err := grpc.DialContext(ctx, "", grpc.WithContextDialer(dialer), grpc.WithInsecure())
|
|
t.Cleanup(func() {
|
|
conn.Close()
|
|
})
|
|
|
|
if err != nil {
|
|
t.Fatalf("grpc.DialContext %v", err)
|
|
}
|
|
|
|
client := env.NewEnvServiceClient(conn)
|
|
res, err := client.Env(context.Background(), &env.EnvRequest{})
|
|
|
|
if _, ok := status.FromError(err); !ok {
|
|
t.Errorf("Env returned type %T, want %T", err, status.Error)
|
|
}
|
|
|
|
expected := ".*PATH.*"
|
|
r := regexp.MustCompile(expected)
|
|
if !r.MatchString(res.String()) {
|
|
t.Fatalf("Returned unexpected body:\ngot \n%v \nwant \n%s",
|
|
res, expected)
|
|
}
|
|
}
|