|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "net" |
| 6 | + "github.com/stretchr/testify/require" |
| 7 | + "fmt" |
| 8 | + "math/rand" |
| 9 | + "os" |
| 10 | + "syscall" |
| 11 | + "github.com/testcontainers/testcontainers-go" |
| 12 | + "github.com/testcontainers/testcontainers-go/wait" |
| 13 | + "context" |
| 14 | + "google.golang.org/grpc" |
| 15 | + healthpb "google.golang.org/grpc/health/grpc_health_v1" |
| 16 | + "time" |
| 17 | + "google.golang.org/grpc/credentials/insecure" |
| 18 | + "strings" |
| 19 | + |
| 20 | + "github.com/Semior001/grpc-echo/echopb" |
| 21 | +) |
| 22 | + |
| 23 | +func TestMain_ReverseProxy(t *testing.T) { |
| 24 | + echoAddr := startEchoServer(t) |
| 25 | + _, conn := setup(t, fmt.Sprintf(` |
| 26 | +version: 1 |
| 27 | +upstreams: { benchmark: { address: "%s" } } |
| 28 | +rules: [{ match: { uri: "(.*)" }, forward: { upstream: "benchmark" } }]`, echoAddr)) |
| 29 | + waitForServerUp(t, conn) |
| 30 | + |
| 31 | + client := echopb.NewEchoServiceClient(conn) |
| 32 | + |
| 33 | + resp, err := client.Echo(context.Background(), &echopb.EchoRequest{Ping: "hello"}) |
| 34 | + require.NoError(t, err) |
| 35 | + |
| 36 | + t.Logf("%+v", resp) |
| 37 | +} |
| 38 | + |
| 39 | +func setup(tb testing.TB, config string, flags ...string) (port int, conn *grpc.ClientConn) { |
| 40 | + tb.Helper() |
| 41 | + |
| 42 | + f, err := os.CreateTemp("", "groxy-test") |
| 43 | + require.NoError(tb, err) |
| 44 | + tb.Cleanup(func() { require.NoError(tb, os.Remove(f.Name())) }) |
| 45 | + |
| 46 | + tb.Logf("writing config to %s", f.Name()) |
| 47 | + |
| 48 | + _, err = f.WriteString(config) |
| 49 | + require.NoError(tb, err) |
| 50 | + require.NoError(tb, f.Close()) |
| 51 | + |
| 52 | + port = 40000 + int(rand.Int31n(10000)) |
| 53 | + os.Args = append([]string{"test", "--file.name=" + f.Name(), "--addr=:" + fmt.Sprint(port)}, flags...) |
| 54 | + |
| 55 | + done := make(chan struct{}) |
| 56 | + go func() { |
| 57 | + <-done |
| 58 | + e := syscall.Kill(syscall.Getpid(), syscall.SIGTERM) |
| 59 | + require.NoError(tb, e) |
| 60 | + }() |
| 61 | + |
| 62 | + started, finished := make(chan struct{}), make(chan struct{}) |
| 63 | + go func() { |
| 64 | + tb.Logf("running server on port %d", port) |
| 65 | + close(started) |
| 66 | + main() |
| 67 | + close(finished) |
| 68 | + }() |
| 69 | + |
| 70 | + tb.Cleanup(func() { |
| 71 | + close(done) |
| 72 | + <-finished |
| 73 | + }) |
| 74 | + |
| 75 | + <-started |
| 76 | + time.Sleep(time.Millisecond * 50) // do not start right away |
| 77 | + conn, err = grpc.NewClient(fmt.Sprintf("localhost:%d", port), |
| 78 | + grpc.WithTransportCredentials(insecure.NewCredentials()), |
| 79 | + grpc.WithUserAgent("groxy-test-ua")) |
| 80 | + require.NoError(tb, err) |
| 81 | + |
| 82 | + tb.Cleanup(func() { |
| 83 | + if err := conn.Close(); err != nil && |
| 84 | + !strings.Contains(err.Error(), "grpc: the client connection is closing") { |
| 85 | + tb.Errorf("failed to close connection: %v", err) |
| 86 | + } |
| 87 | + }) |
| 88 | + |
| 89 | + tb.Logf("server started on port %d", port) |
| 90 | + return port, conn |
| 91 | +} |
| 92 | + |
| 93 | +func waitForServerUp(tb testing.TB, conn *grpc.ClientConn) { |
| 94 | + tb.Helper() |
| 95 | + |
| 96 | + healthClient := healthpb.NewHealthClient(conn) |
| 97 | + for i := 0; i < 100; i++ { |
| 98 | + time.Sleep(time.Millisecond * 100) |
| 99 | + st, err := healthClient.Check(context.Background(), &healthpb.HealthCheckRequest{}) |
| 100 | + if err == nil && st.Status == healthpb.HealthCheckResponse_SERVING { |
| 101 | + tb.Logf("server is up") |
| 102 | + return |
| 103 | + } |
| 104 | + } |
| 105 | + tb.Fatal("server is not up") |
| 106 | +} |
| 107 | + |
| 108 | +func startEchoServer(tb testing.TB) (addr string) { |
| 109 | + tb.Helper() |
| 110 | + ctx := context.Background() |
| 111 | + |
| 112 | + provider, err := testcontainers.ProviderDocker.GetProvider() |
| 113 | + if err != nil { |
| 114 | + tb.Skipf("docker not available, skipping test: %s", err) |
| 115 | + } |
| 116 | + if err = provider.Health(ctx); err != nil { |
| 117 | + tb.Skipf("docker not healthy, skipping test: %s", err) |
| 118 | + } |
| 119 | + |
| 120 | + echoReq := testcontainers.GenericContainerRequest{ |
| 121 | + ContainerRequest: testcontainers.ContainerRequest{ |
| 122 | + Image: "semior/grpc-echo:latest", |
| 123 | + ExposedPorts: []string{"8080/tcp"}, |
| 124 | + WaitingFor: wait.ForLog("listening gRPC"), |
| 125 | + }, |
| 126 | + Started: true, |
| 127 | + } |
| 128 | + echo, err := testcontainers.GenericContainer(ctx, echoReq) |
| 129 | + testcontainers.CleanupContainer(tb, echo) |
| 130 | + require.NoError(tb, err) |
| 131 | + |
| 132 | + echoIP, err := echo.Host(ctx) |
| 133 | + require.NoError(tb, err) |
| 134 | + |
| 135 | + echoPort, err := echo.MappedPort(ctx, "8080") |
| 136 | + require.NoError(tb, err) |
| 137 | + |
| 138 | + addr = net.JoinHostPort(echoIP, echoPort.Port()) |
| 139 | + tb.Logf("started echo server at %s", addr) |
| 140 | + return addr |
| 141 | +} |
0 commit comments