Skip to content
Draft
14 changes: 0 additions & 14 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,14 @@ permissions:
contents: read
id-token: write

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

on:
pull_request:

jobs:
lint:
uses: ./.github/workflows/lint.yml
generated-code-check:
uses: ./.github/workflows/pr-no-generated-changes.yml
out-of-order-migrations:
uses: ./.github/workflows/out-of-order-migrations.yml
unit-tests:
uses: ./.github/workflows/pr-tests.yml
integration-tests:
needs: [ lint, out-of-order-migrations ]
uses: ./.github/workflows/integration_tests.yml
publish-test-results:
needs:
- unit-tests
- integration-tests
runs-on: ubuntu-latest
permissions:
Expand Down
16 changes: 13 additions & 3 deletions packages/api/internal/analytics_collector/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/protobuf/types/known/emptypb"

"github.com/e2b-dev/infra/packages/api/internal/testhacks"
)

var host = strings.TrimSpace(os.Getenv("ANALYTICS_COLLECTOR_HOST"))
Expand Down Expand Up @@ -40,12 +42,20 @@ func NewAnalytics() (*Analytics, error) {
MinVersion: tls.VersionTLS13,
})

conn, err := grpc.NewClient(
fmt.Sprintf("%s:443", host),
grpcOptions := []grpc.DialOption{
grpc.WithPerRPCCredentials(&gRPCApiKey{}),
grpc.WithAuthority(host),
grpc.WithTransportCredentials(cred),
)
}

if testhacks.IsTesting() {
grpcOptions = append(grpcOptions,
grpc.WithUnaryInterceptor(testhacks.GRPCUnaryInterceptor),
grpc.WithStreamInterceptor(testhacks.GRPCStreamInterceptor),
)
}

conn, err := grpc.NewClient(fmt.Sprintf("%s:443", host), grpcOptions...)
if err != nil {
return nil, fmt.Errorf("failed to create GRPC client: %w", err)
}
Expand Down
8 changes: 8 additions & 0 deletions packages/api/internal/edge/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"google.golang.org/grpc/keepalive"

grpclient "github.com/e2b-dev/infra/packages/api/internal/grpc"
"github.com/e2b-dev/infra/packages/api/internal/testhacks"
"github.com/e2b-dev/infra/packages/shared/pkg/consts"
orchestratorgrpc "github.com/e2b-dev/infra/packages/shared/pkg/grpc/orchestrator"
infogrpc "github.com/e2b-dev/infra/packages/shared/pkg/grpc/orchestrator-info"
Expand Down Expand Up @@ -59,6 +60,13 @@ func createClusterClient(tel *telemetry.Client, auth clientAuthorization, endpoi
grpcOptions = append(grpcOptions, grpc.WithTransportCredentials(insecure.NewCredentials()))
}

if testhacks.IsTesting() {
grpcOptions = append(grpcOptions,
grpc.WithUnaryInterceptor(testhacks.GRPCUnaryInterceptor),
grpc.WithStreamInterceptor(testhacks.GRPCStreamInterceptor),
)
}

conn, err := grpc.NewClient(endpoint, grpcOptions...)
if err != nil {
return nil, fmt.Errorf("failed to create grpc client: %w", err)
Expand Down
14 changes: 12 additions & 2 deletions packages/api/internal/orchestrator/nodemanager/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/e2b-dev/infra/packages/api/internal/api"
grpclient "github.com/e2b-dev/infra/packages/api/internal/grpc"
"github.com/e2b-dev/infra/packages/api/internal/testhacks"
"github.com/e2b-dev/infra/packages/shared/pkg/grpc/orchestrator"
orchestratorinfo "github.com/e2b-dev/infra/packages/shared/pkg/grpc/orchestrator-info"
)
Expand All @@ -22,15 +23,24 @@ var OrchestratorToApiNodeStateMapper = map[orchestratorinfo.ServiceInfoStatus]ap
}

func NewClient(tracerProvider trace.TracerProvider, meterProvider metric.MeterProvider, host string) (*grpclient.GRPCClient, error) {
conn, err := grpc.NewClient(host,
grpcOptions := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithStatsHandler(
otelgrpc.NewClientHandler(
otelgrpc.WithTracerProvider(tracerProvider),
otelgrpc.WithMeterProvider(meterProvider),
),
),
)
}

if testhacks.IsTesting() {
grpcOptions = append(grpcOptions,
grpc.WithUnaryInterceptor(testhacks.GRPCUnaryInterceptor),
grpc.WithStreamInterceptor(testhacks.GRPCStreamInterceptor),
)
}

conn, err := grpc.NewClient(host, grpcOptions...)
if err != nil {
return nil, fmt.Errorf("failed to establish GRPC connection: %w", err)
}
Expand Down
14 changes: 12 additions & 2 deletions packages/api/internal/template-manager/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import (
"google.golang.org/grpc/keepalive"

grpclient "github.com/e2b-dev/infra/packages/api/internal/grpc"
"github.com/e2b-dev/infra/packages/api/internal/testhacks"
infogrpc "github.com/e2b-dev/infra/packages/shared/pkg/grpc/orchestrator-info"
templatemanagergrpc "github.com/e2b-dev/infra/packages/shared/pkg/grpc/template-manager"
)

var templateManagerHost = os.Getenv("TEMPLATE_MANAGER_HOST")

func createClient(tracerProvider trace.TracerProvider, meterProvider metric.MeterProvider) (*grpclient.GRPCClient, error) {
conn, err := grpc.NewClient(templateManagerHost,
grpcOptions := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithStatsHandler(
otelgrpc.NewClientHandler(
Expand All @@ -32,7 +33,16 @@ func createClient(tracerProvider trace.TracerProvider, meterProvider metric.Mete
Timeout: 2 * time.Second, // Wait 2s for response
PermitWithoutStream: true,
}),
)
}

if testhacks.IsTesting() {
grpcOptions = append(grpcOptions,
grpc.WithUnaryInterceptor(testhacks.GRPCUnaryInterceptor),
grpc.WithStreamInterceptor(testhacks.GRPCStreamInterceptor),
)
}

conn, err := grpc.NewClient(templateManagerHost, grpcOptions...)
if err != nil {
return nil, err
}
Expand Down
14 changes: 14 additions & 0 deletions packages/api/internal/testhacks/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package testhacks

import "context"

type testNameContextKey struct{}

func addTestName(ctx context.Context, testName string) context.Context {
return context.WithValue(ctx, testNameContextKey{}, testName)
}

func GetTestName(ctx context.Context) string {
val, _ := ctx.Value(testNameContextKey{}).(string)
return val
}
22 changes: 22 additions & 0 deletions packages/api/internal/testhacks/gin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package testhacks

import (
"fmt"

"github.com/gin-gonic/gin"
)

func PrintTestName(c *gin.Context) {
testName := c.GetHeader("x-test-name")
if testName != "" {
ctx := addTestName(c.Request.Context(), testName)
c.Request = c.Request.WithContext(ctx)
fmt.Printf("====================== START api request for %s ========================\n", testName)
}

c.Next()

if testName != "" {
fmt.Printf("====================== FINISH api request for %s ========================\n", testName)
}
}
24 changes: 24 additions & 0 deletions packages/api/internal/testhacks/grpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package testhacks

import (
"context"

"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)

func GRPCUnaryInterceptor(ctx context.Context, method string, req any, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
if testName := GetTestName(ctx); testName != "" {
ctx = metadata.AppendToOutgoingContext(ctx, "x-test-name", testName)
}

return invoker(ctx, method, req, reply, cc, opts...)
}

func GRPCStreamInterceptor(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
if testName := GetTestName(ctx); testName != "" {
ctx = metadata.AppendToOutgoingContext(ctx, "x-test-name", testName)
}

return streamer(ctx, desc, cc, method, opts...)
}
7 changes: 7 additions & 0 deletions packages/api/internal/testhacks/switch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package testhacks

import "os"

func IsTesting() bool {
return os.Getenv("CI") == "true"
}
5 changes: 5 additions & 0 deletions packages/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
customMiddleware "github.com/e2b-dev/infra/packages/api/internal/middleware"
metricsMiddleware "github.com/e2b-dev/infra/packages/api/internal/middleware/otel/metrics"
tracingMiddleware "github.com/e2b-dev/infra/packages/api/internal/middleware/otel/tracing"
"github.com/e2b-dev/infra/packages/api/internal/testhacks"
"github.com/e2b-dev/infra/packages/api/internal/utils"
"github.com/e2b-dev/infra/packages/shared/pkg/env"
l "github.com/e2b-dev/infra/packages/shared/pkg/logger"
Expand Down Expand Up @@ -67,6 +68,10 @@ func NewGinServer(ctx context.Context, tel *telemetry.Client, logger *zap.Logger

r := gin.New()

if testhacks.IsTesting() {
r.Use(testhacks.PrintTestName)
}

r.Use(
// We use custom otel gin middleware because we want to log 4xx errors in the otel
customMiddleware.ExcludeRoutes(
Expand Down
14 changes: 12 additions & 2 deletions packages/client-proxy/internal/edge-pass-through/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
e2binfo "github.com/e2b-dev/infra/packages/proxy/internal/edge/info"
e2borchestrators "github.com/e2b-dev/infra/packages/proxy/internal/edge/pool"
"github.com/e2b-dev/infra/packages/proxy/internal/edge/sandboxes"
"github.com/e2b-dev/infra/packages/proxy/internal/testhacks"
"github.com/e2b-dev/infra/packages/shared/pkg/consts"
api "github.com/e2b-dev/infra/packages/shared/pkg/http/edge"
)
Expand Down Expand Up @@ -48,10 +49,19 @@ func NewNodePassThroughServer(
info: info,
}

return grpc.NewServer(
serverOpts := []grpc.ServerOption{
grpc.UnknownServiceHandler(nodePassThrough.handler),
grpc.MaxRecvMsgSize(grpcMaxMsgSize),
)
}

if testhacks.IsTesting() {
serverOpts = append(serverOpts,
grpc.ChainUnaryInterceptor(testhacks.UnaryTestNamePrinter),
grpc.ChainStreamInterceptor(testhacks.StreamingTestNamePrinter),
)
}

return grpc.NewServer(serverOpts...)
}

func (s *NodePassThroughServer) director(ctx context.Context) (*grpc.ClientConn, metadata.MD, error) {
Expand Down
5 changes: 5 additions & 0 deletions packages/client-proxy/internal/edge/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/e2b-dev/infra/packages/proxy/internal/edge/authorization"
"github.com/e2b-dev/infra/packages/proxy/internal/edge/handlers"
"github.com/e2b-dev/infra/packages/proxy/internal/testhacks"
"github.com/e2b-dev/infra/packages/shared/pkg/consts"
"github.com/e2b-dev/infra/packages/shared/pkg/http/edge"
"github.com/e2b-dev/infra/packages/shared/pkg/telemetry"
Expand Down Expand Up @@ -51,6 +52,10 @@ func NewGinServer(logger *zap.Logger, store *handlers.APIStore, swagger *openapi
handler := gin.New()
handler.MaxMultipartMemory = maxMultipartMemory

if testhacks.IsTesting() {
handler.Use(testhacks.ProcessGinRequest)
}

// Use our validation middleware to check all requests against the
// OpenAPI schema.
handler.Use(
Expand Down
14 changes: 12 additions & 2 deletions packages/client-proxy/internal/edge/pool/orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/protobuf/types/known/emptypb"

"github.com/e2b-dev/infra/packages/proxy/internal/testhacks"
e2bgrpcorchestratorinfo "github.com/e2b-dev/infra/packages/shared/pkg/grpc/orchestrator-info"
l "github.com/e2b-dev/infra/packages/shared/pkg/logger"
)
Expand Down Expand Up @@ -161,15 +162,24 @@ func getMappedStatus(s e2bgrpcorchestratorinfo.ServiceInfoStatus) OrchestratorSt
}

func newClient(tracerProvider trace.TracerProvider, meterProvider metric.MeterProvider, host string) (*OrchestratorGRPCClient, error) {
conn, err := grpc.NewClient(host,
grpcOptions := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithStatsHandler(
otelgrpc.NewClientHandler(
otelgrpc.WithTracerProvider(tracerProvider),
otelgrpc.WithMeterProvider(meterProvider),
),
),
)
}

if testhacks.IsTesting() {
grpcOptions = append(grpcOptions,
grpc.WithUnaryInterceptor(testhacks.GRPCUnaryInterceptor),
grpc.WithStreamInterceptor(testhacks.GRPCStreamInterceptor),
)
}

conn, err := grpc.NewClient(host, grpcOptions...)
if err != nil {
return nil, fmt.Errorf("failed to create GRPC client: %w", err)
}
Expand Down
3 changes: 3 additions & 0 deletions packages/client-proxy/internal/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

orchestratorspool "github.com/e2b-dev/infra/packages/proxy/internal/edge/pool"
"github.com/e2b-dev/infra/packages/proxy/internal/edge/sandboxes"
"github.com/e2b-dev/infra/packages/proxy/internal/testhacks"
l "github.com/e2b-dev/infra/packages/shared/pkg/logger"
reverseproxy "github.com/e2b-dev/infra/packages/shared/pkg/proxy"
"github.com/e2b-dev/infra/packages/shared/pkg/proxy/pool"
Expand Down Expand Up @@ -109,6 +110,8 @@ func NewClientProxy(meterProvider metric.MeterProvider, serviceName string, port
port,
idleTimeout,
func(r *http.Request) (*pool.Destination, error) {
r = testhacks.ProcessHTTPRequest(r)

sandboxId, port, err := reverseproxy.ParseHost(r.Host)
if err != nil {
return nil, err
Expand Down
14 changes: 14 additions & 0 deletions packages/client-proxy/internal/testhacks/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package testhacks

import "context"

type testNameContextKey struct{}

func addTestName(ctx context.Context, testName string) context.Context {
return context.WithValue(ctx, testNameContextKey{}, testName)
}

func GetTestName(ctx context.Context) string {
val, _ := ctx.Value(testNameContextKey{}).(string)
return val
}
23 changes: 23 additions & 0 deletions packages/client-proxy/internal/testhacks/gin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package testhacks

import (
"fmt"

"github.com/gin-gonic/gin"
)

func ProcessGinRequest(c *gin.Context) {
testName := c.GetHeader("x-test-name")
if testName == "" {
c.Next()
return
}

ctx := addTestName(c.Request.Context(), testName)
c.Request = c.Request.WithContext(ctx)
fmt.Printf("====================== START client-proxy request for %s ========================\n", testName)

c.Next()

fmt.Printf("====================== FINISH client-proxy call for %s ========================\n", testName)
}
Loading
Loading