Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gRPC e2e test #646

Merged
merged 4 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions command/forwarder/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ package forwarder

import (
"github.com/saucelabs/forwarder/bind"
"github.com/saucelabs/forwarder/command/httpbin"
"github.com/saucelabs/forwarder/command/pac"
"github.com/saucelabs/forwarder/command/ready"
"github.com/saucelabs/forwarder/command/run"
"github.com/saucelabs/forwarder/command/test/grpc"
"github.com/saucelabs/forwarder/command/test/httpbin"
"github.com/saucelabs/forwarder/command/version"
"github.com/saucelabs/forwarder/utils/cobrautil"
"github.com/saucelabs/forwarder/utils/cobrautil/templates"
Expand Down Expand Up @@ -105,11 +106,19 @@ func Command() *cobra.Command {

templates.ActsAsRootCommand(cmd, nil, cg, FlagGroups(), EnvPrefix)

// Add other commands.
cmd.AddCommand(
httpbin.Command(), // hidden
version.Command(),
// Add test commands.
test := &cobra.Command{
Use: "test",
Short: "Run test servers for various protocols",
}
test.AddCommand(
grpc.Command(),
httpbin.Command(),
)
cmd.AddCommand(test)
Choraden marked this conversation as resolved.
Show resolved Hide resolved

// Add version command.
cmd.AddCommand(version.Command())

// Add config-file command to all commands.
cobrautil.AddConfigFileForEachCommand(cmd, FlagGroups(), ConfigFileFlagName)
Expand Down
124 changes: 124 additions & 0 deletions command/test/grpc/grpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright 2023 Sauce Labs Inc., all rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

package grpc

import (
"context"
"crypto/tls"
"net"

"github.com/saucelabs/forwarder"
"github.com/saucelabs/forwarder/bind"
ts "github.com/saucelabs/forwarder/internal/martian/h2/testing"
tspb "github.com/saucelabs/forwarder/internal/martian/h2/testservice"
"github.com/saucelabs/forwarder/log"
"github.com/saucelabs/forwarder/log/stdlog"
"github.com/saucelabs/forwarder/runctx"
"github.com/saucelabs/forwarder/utils/cobrautil"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
_ "google.golang.org/grpc/encoding/gzip" // register gzip encoding
)

type command struct {
addr string
tlsServerConfig *forwarder.TLSServerConfig
logConfig *log.Config
}

func (c *command) runE(cmd *cobra.Command, _ []string) (cmdErr error) {
if f := c.logConfig.File; f != nil {
defer f.Close()
}
logger := stdlog.New(c.logConfig)

defer func() {
if cmdErr != nil {
logger.Errorf("fatal error exiting: %s", cmdErr)
cmd.SilenceErrors = true
}
}()

{
var (
cfg []byte
err error
)

d := cobrautil.FlagsDescriber{
Format: cobrautil.Plain,
}
cfg, err = d.DescribeFlags(cmd.Flags())
if err != nil {
return err
}
logger.Infof("configuration\n%s", cfg)

d.ShowNotChanged = true
cfg, err = d.DescribeFlags(cmd.Flags())
if err != nil {
return err
}
logger.Debugf("all configuration\n%s\n\n", cfg)
}

g := runctx.NewGroup()

{
tlsCfg := new(tls.Config)
if err := c.tlsServerConfig.ConfigureTLSConfig(tlsCfg); err != nil {
return err
}
gs := grpc.NewServer(
grpc.Creds(credentials.NewServerTLSFromCert(&tlsCfg.Certificates[0])),
)
tspb.RegisterTestServiceServer(gs, &ts.Server{})
defer gs.Stop()

l, err := net.Listen("tcp", c.addr)
if err != nil {
return err
}
defer l.Close()

g.Add(func(ctx context.Context) error {
logger.Named("grpc").Infof("server listen address=%s", l.Addr())
go func() {
<-ctx.Done()
gs.GracefulStop()
}()
return gs.Serve(l)
})
}

return g.Run()
}

func Command() *cobra.Command {
c := command{
addr: "localhost:1443",
tlsServerConfig: new(forwarder.TLSServerConfig),
logConfig: log.DefaultConfig(),
}

cmd := &cobra.Command{
Use: "grpc [--address <host:port>] [flags]",
Short: "Start HTTP/2 gRPC server for testing",
RunE: c.runE,
}

fs := cmd.Flags()
fs.StringVar(&c.addr, "address", c.addr, "<host:port>"+
"Address to listen on. "+
"If the host is empty, the server will listen on all available interfaces. ")
bind.TLSServerConfig(fs, c.tlsServerConfig, "")
bind.LogConfig(fs, c.logConfig)
bind.AutoMarkFlagFilename(cmd)

return cmd
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,9 @@ func Command() *cobra.Command {
c.apiServerConfig.Addr = "localhost:10000"

cmd := &cobra.Command{
Use: "httpbin [--protocol <http|https|h2>] [--address <host:port>] [flags]",
Short: "Start HTTP(S) server that serves httpbin.org API",
RunE: c.runE,
Hidden: true,
Use: "httpbin [--protocol <http|https|h2>] [--address <host:port>] [flags]",
Short: "Start HTTP(S) server that serves httpbin.org API",
RunE: c.runE,
}

fs := cmd.Flags()
Expand Down
2 changes: 1 addition & 1 deletion e2e/forwarder/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func HttpbinService() *Service {
return &Service{
Name: HttpbinServiceName,
Image: Image,
Command: "httpbin",
Command: "test httpbin",
Environment: map[string]string{
"FORWARDER_API_ADDRESS": ":10000",
},
Expand Down