Skip to content

Commit

Permalink
mockgcp: serve multiple hosts, including variables
Browse files Browse the repository at this point in the history
  • Loading branch information
justinsb committed Aug 8, 2024
1 parent 395b43f commit 7a2507f
Show file tree
Hide file tree
Showing 39 changed files with 128 additions and 90 deletions.
64 changes: 52 additions & 12 deletions mockgcp/mock_http_roundtrip.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"log"
"net"
"net/http"
"regexp"
"strings"
"testing"

Expand Down Expand Up @@ -78,11 +79,24 @@ type mockRoundTripper struct {
grpcConnection *grpc.ClientConn
grpcListener net.Listener

hosts map[string]http.Handler

iamPolicies *mockIAMPolicies

services []MockService
services []registeredService
}

type registeredService struct {
hostRegexes []*regexp.Regexp
handler http.Handler
impl MockService
}

func (h *registeredService) MatchesHost(host string) (http.Handler, bool) {
for _, hostRegex := range h.hostRegexes {
if hostRegex.MatchString(host) {
return h.handler, true
}
}
return nil, false
}

// MockService is the interface implemented by all services
Expand All @@ -93,8 +107,9 @@ type MockService interface {
// NewHTTPMux creates an HTTP mux for serving http traffic
NewHTTPMux(ctx context.Context, conn *grpc.ClientConn) (http.Handler, error)

// ExpectedHost is the hostname we serve on e.g. foo.googleapis.com
ExpectedHost() string
// ExpectedHosts is the hostname(s) we serve on e.g. foo.googleapis.com
// We also support patterns like `{region}-foo.googleapis.com`
ExpectedHosts() []string
}

type Interface interface {
Expand Down Expand Up @@ -137,8 +152,6 @@ func NewMockRoundTripper(t *testing.T, k8sClient client.Client, storage storage.
var serverOpts []grpc.ServerOption
server := grpc.NewServer(serverOpts...)

mockRoundTripper.hosts = make(map[string]http.Handler)

var services []MockService

services = append(services, resourcemanagerService)
Expand Down Expand Up @@ -179,7 +192,6 @@ func NewMockRoundTripper(t *testing.T, k8sClient client.Client, storage storage.
services = append(services, mockcontaineranalysis.New(env, storage))
services = append(services, mockdataform.New(env, storage))
services = append(services, mockbigqueryconnection.New(env, storage))
mockRoundTripper.services = services

for _, service := range services {
service.Register(server)
Expand Down Expand Up @@ -216,7 +228,15 @@ func NewMockRoundTripper(t *testing.T, k8sClient client.Client, storage storage.
if err != nil {
t.Fatalf("error building mux: %v", err)
}
mockRoundTripper.hosts[service.ExpectedHost()] = mux
var hostRegexes []*regexp.Regexp
for _, host := range service.ExpectedHosts() {
hostRegexes = append(hostRegexes, toHostRegex(host))
}
mockRoundTripper.services = append(mockRoundTripper.services, registeredService{
impl: service,
hostRegexes: hostRegexes,
handler: mux,
})
}

mockRoundTripper.iamPolicies = newMockIAMPolicies()
Expand All @@ -226,11 +246,11 @@ func NewMockRoundTripper(t *testing.T, k8sClient client.Client, storage storage.

func (m *mockRoundTripper) RunTestCommand(ctx context.Context, serviceName string, command string) error {
for _, service := range m.services {
if service.ExpectedHost() != serviceName {
if _, match := service.MatchesHost(serviceName); !match {
continue
}

supportsTestCommands, ok := service.(SupportsTestCommands)
supportsTestCommands, ok := service.impl.(SupportsTestCommands)
if !ok {
return fmt.Errorf("service %T does not support test commands", service)
}
Expand All @@ -251,6 +271,19 @@ func (m *mockRoundTripper) NewGRPCConnection(ctx context.Context) *grpc.ClientCo
return conn
}

func toHostRegex(host string) *regexp.Regexp {
r := regexp.MustCompile(`{[^}]+}`)

tokens := strings.Split(host, ".")
for i, token := range tokens {
token = r.ReplaceAllStringFunc(token, func(match string) string {
return "[^.]*"
})
tokens[i] = token
}
return regexp.MustCompile("^" + strings.Join(tokens, `\.`) + "$")
}

func (m *mockRoundTripper) prefilterRequest(req *http.Request) error {
if req.Body != nil {
var requestBody bytes.Buffer
Expand Down Expand Up @@ -389,7 +422,14 @@ func (m *mockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error)
return m.roundTripIAMPolicy(req)
}

mux := m.hosts[req.Host]
var mux http.Handler
for _, service := range m.services {
m, found := service.MatchesHost(req.Host)
if found {
mux = m
break
}
}
if mux != nil {
if err := m.prefilterRequest(req); err != nil {
return nil, err
Expand Down
5 changes: 2 additions & 3 deletions mockgcp/mockaiplatform/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ func New(env *common.MockEnvironment, storage storage.Storage) *MockService {
return s
}

func (s *MockService) ExpectedHost() string {
// TODO: Support more endpoints
return "us-central1-aiplatform.googleapis.com"
func (s *MockService) ExpectedHosts() []string {
return []string{"{region}-aiplatform.googleapis.com"}
}

func (s *MockService) Register(grpcServer *grpc.Server) {
Expand Down
4 changes: 2 additions & 2 deletions mockgcp/mockalloydb/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func New(env *common.MockEnvironment, storage storage.Storage) *MockService {
return s
}

func (s *MockService) ExpectedHost() string {
return "alloydb.googleapis.com"
func (s *MockService) ExpectedHosts() []string {
return []string{"alloydb.googleapis.com"}
}

func (s *MockService) Register(grpcServer *grpc.Server) {
Expand Down
4 changes: 2 additions & 2 deletions mockgcp/mockapikeys/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func New(env *common.MockEnvironment, storage storage.Storage) *MockService {
return s
}

func (s *MockService) ExpectedHost() string {
return "apikeys.googleapis.com"
func (s *MockService) ExpectedHosts() []string {
return []string{"apikeys.googleapis.com"}
}

func (s *MockService) Register(grpcServer *grpc.Server) {
Expand Down
4 changes: 2 additions & 2 deletions mockgcp/mockartifactregistry/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func New(env *common.MockEnvironment, storage storage.Storage) *MockService {
return s
}

func (s *MockService) ExpectedHost() string {
return "artifactregistry.googleapis.com"
func (s *MockService) ExpectedHosts() []string {
return []string{"artifactregistry.googleapis.com"}
}

func (s *MockService) Register(grpcServer *grpc.Server) {
Expand Down
4 changes: 2 additions & 2 deletions mockgcp/mockbigquery/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func New(env *common.MockEnvironment, storage storage.Storage) *MockService {
return s
}

func (s *MockService) ExpectedHost() string {
return "bigquery.googleapis.com"
func (s *MockService) ExpectedHosts() []string {
return []string{"bigquery.googleapis.com"}
}

func (s *MockService) Register(grpcServer *grpc.Server) {
Expand Down
4 changes: 2 additions & 2 deletions mockgcp/mockbigqueryconnection/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ func New(env *common.MockEnvironment, storage storage.Storage) *MockService {
return s
}

func (s *MockService) ExpectedHost() string {
return "bigqueryconnection.googleapis.com"
func (s *MockService) ExpectedHosts() []string {
return []string{"bigqueryconnection.googleapis.com"}
}

func (s *MockService) Register(grpcServer *grpc.Server) {
Expand Down
4 changes: 2 additions & 2 deletions mockgcp/mockbigtable/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ func New(env *common.MockEnvironment, storage storage.Storage) *MockService {
return s
}

func (s *MockService) ExpectedHost() string {
return "bigtableadmin.googleapis.com"
func (s *MockService) ExpectedHosts() []string {
return []string{"bigtableadmin.googleapis.com"}
}

func (s *MockService) Register(grpcServer *grpc.Server) {
Expand Down
4 changes: 2 additions & 2 deletions mockgcp/mockbilling/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func New(env *common.MockEnvironment, storage storage.Storage) *MockService {
return s
}

func (s *MockService) ExpectedHost() string {
return "cloudbilling.googleapis.com"
func (s *MockService) ExpectedHosts() []string {
return []string{"cloudbilling.googleapis.com"}
}

func (s *MockService) Register(grpcServer *grpc.Server) {
Expand Down
4 changes: 2 additions & 2 deletions mockgcp/mockcertificatemanager/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func New(env *common.MockEnvironment, storage storage.Storage) *MockService {
return s
}

func (s *MockService) ExpectedHost() string {
return "certificatemanager.googleapis.com"
func (s *MockService) ExpectedHosts() []string {
return []string{"certificatemanager.googleapis.com"}
}

func (s *MockService) Register(grpcServer *grpc.Server) {
Expand Down
4 changes: 2 additions & 2 deletions mockgcp/mockcloudbuild/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func New(env *common.MockEnvironment, storage storage.Storage) *MockService {
return s
}

func (s *MockService) ExpectedHost() string {
return "cloudbuild.googleapis.com"
func (s *MockService) ExpectedHosts() []string {
return []string{"cloudbuild.googleapis.com"}
}

func (s *MockService) Register(grpcServer *grpc.Server) {
Expand Down
4 changes: 2 additions & 2 deletions mockgcp/mockcloudfunctions/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func New(env *common.MockEnvironment, storage storage.Storage) *MockService {
return s
}

func (s *MockService) ExpectedHost() string {
return "cloudfunctions.googleapis.com"
func (s *MockService) ExpectedHosts() []string {
return []string{"cloudfunctions.googleapis.com"}
}

func (s *MockService) Register(grpcServer *grpc.Server) {
Expand Down
4 changes: 2 additions & 2 deletions mockgcp/mockcloudids/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func New(env *common.MockEnvironment, storage storage.Storage) *MockService {
return s
}

func (s *MockService) ExpectedHost() string {
return "ids.googleapis.com"
func (s *MockService) ExpectedHosts() []string {
return []string{"ids.googleapis.com"}
}

func (s *MockService) Register(grpcServer *grpc.Server) {
Expand Down
4 changes: 2 additions & 2 deletions mockgcp/mockcompute/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ func New(env *common.MockEnvironment, storage storage.Storage) *MockService {
return s
}

func (s *MockService) ExpectedHost() string {
return "compute.googleapis.com"
func (s *MockService) ExpectedHosts() []string {
return []string{"compute.googleapis.com"}
}

func (s *MockService) Register(grpcServer *grpc.Server) {
Expand Down
4 changes: 2 additions & 2 deletions mockgcp/mockcontainer/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func New(env *common.MockEnvironment, storage storage.Storage) *MockService {
return s
}

func (s *MockService) ExpectedHost() string {
return "container.googleapis.com"
func (s *MockService) ExpectedHosts() []string {
return []string{"container.googleapis.com"}
}

func (s *MockService) Register(grpcServer *grpc.Server) {
Expand Down
4 changes: 2 additions & 2 deletions mockgcp/mockcontaineranalysis/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func New(env *common.MockEnvironment, storage storage.Storage) *MockService {
return s
}

func (s *MockService) ExpectedHost() string {
return "containeranalysis.googleapis.com"
func (s *MockService) ExpectedHosts() []string {
return []string{"containeranalysis.googleapis.com"}
}

func (s *MockService) Register(grpcServer *grpc.Server) {
Expand Down
4 changes: 2 additions & 2 deletions mockgcp/mockdataform/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func New(env *common.MockEnvironment, storage storage.Storage) *MockService {
return s
}

func (s *MockService) ExpectedHost() string {
return "dataform.googleapis.com"
func (s *MockService) ExpectedHosts() []string {
return []string{"dataform.googleapis.com"}
}

func (s *MockService) Register(grpcServer *grpc.Server) {
Expand Down
4 changes: 2 additions & 2 deletions mockgcp/mockedgecontainer/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func New(env *common.MockEnvironment, storage storage.Storage) *MockService {
return s
}

func (s *MockService) ExpectedHost() string {
return "edgecontainer.googleapis.com"
func (s *MockService) ExpectedHosts() []string {
return []string{"edgecontainer.googleapis.com"}
}

func (s *MockService) Register(grpcServer *grpc.Server) {
Expand Down
4 changes: 2 additions & 2 deletions mockgcp/mockedgenetwork/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func New(env *common.MockEnvironment, storage storage.Storage) *MockService {
return s
}

func (s *MockService) ExpectedHost() string {
return "edgenetwork.googleapis.com"
func (s *MockService) ExpectedHosts() []string {
return []string{"edgenetwork.googleapis.com"}
}

func (s *MockService) Register(grpcServer *grpc.Server) {
Expand Down
4 changes: 2 additions & 2 deletions mockgcp/mockgkehub/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ func New(env *common.MockEnvironment, storage storage.Storage) *MockService {
return s
}

func (s *MockService) ExpectedHost() string {
return "gkehub.googleapis.com"
func (s *MockService) ExpectedHosts() []string {
return []string{"gkehub.googleapis.com"}
}

func (s *MockService) Register(grpcServer *grpc.Server) {
Expand Down
4 changes: 2 additions & 2 deletions mockgcp/mockgkemulticloud/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func New(env *common.MockEnvironment, storage storage.Storage) *MockService {
return s
}

func (s *MockService) ExpectedHost() string {
return "us-west1-gkemulticloud.googleapis.com"
func (s *MockService) ExpectedHosts() []string {
return []string{"{region}-gkemulticloud.googleapis.com"}
}

func (s *MockService) Register(grpcServer *grpc.Server) {
Expand Down
4 changes: 2 additions & 2 deletions mockgcp/mockiam/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ func New(env *common.MockEnvironment, storage storage.Storage) *MockService {
return s
}

func (s *MockService) ExpectedHost() string {
return "iam.googleapis.com"
func (s *MockService) ExpectedHosts() []string {
return []string{"iam.googleapis.com"}
}

func (s *MockService) Register(grpcServer *grpc.Server) {
Expand Down
4 changes: 2 additions & 2 deletions mockgcp/mockkms/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func New(env *common.MockEnvironment, storage storage.Storage) *MockService {
return s
}

func (s *MockService) ExpectedHost() string {
return "cloudkms.googleapis.com"
func (s *MockService) ExpectedHosts() []string {
return []string{"cloudkms.googleapis.com"}
}

func (s *MockService) Register(grpcServer *grpc.Server) {
Expand Down
4 changes: 2 additions & 2 deletions mockgcp/mocklogging/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func New(env *common.MockEnvironment, storage storage.Storage) *MockService {
return s
}

func (s *MockService) ExpectedHost() string {
return "logging.googleapis.com"
func (s *MockService) ExpectedHosts() []string {
return []string{"logging.googleapis.com"}
}

func (s *MockService) Register(grpcServer *grpc.Server) {
Expand Down
4 changes: 2 additions & 2 deletions mockgcp/mockmonitoring/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ func New(env *common.MockEnvironment, storage storage.Storage) *MockService {
return s
}

func (s *MockService) ExpectedHost() string {
return "monitoring.googleapis.com"
func (s *MockService) ExpectedHosts() []string {
return []string{"monitoring.googleapis.com"}
}

func (s *MockService) Register(grpcServer *grpc.Server) {
Expand Down
Loading

0 comments on commit 7a2507f

Please sign in to comment.