mock

package
v1.18.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 27, 2025 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateCertWithCommonName

func GenerateCertWithCommonName(i FakeCSQLInstance, cn string) []byte

GenerateCertWithCommonName produces a certificate signed by the Fake Cloud SQL instance's CA with the specified common name cn.

func NewSQLAdminService

func NewSQLAdminService(ctx context.Context, reqs ...*Request) (*sqladmin.Service, func() error, error)

NewSQLAdminService creates a SQL Admin API service backed by a mock HTTP backend. Callers should use the cleanup function to close down the server. If the cleanup function returns an error, a caller has not exercised all the registered requests.

func RotateCA added in v1.15.0

func RotateCA(inst FakeCSQLInstance)

RotateCA rotates all CA certificates and keys.

func RotateClientCA added in v1.15.0

func RotateClientCA(inst FakeCSQLInstance)

RotateClientCA rotates only client CA certificates and keys.

func SelfSign

func SelfSign(c *x509.Certificate, k *rsa.PrivateKey) ([]byte, error)

SelfSign produces a PEM encoded certificate that is self-signed.

func StartServerProxy

func StartServerProxy(t *testing.T, i FakeCSQLInstance) func()

StartServerProxy starts a fake server proxy and listens on the provided port on all interfaces, configured with TLS as specified by the FakeCSQLInstance. Callers should invoke the returned function to clean up all resources.

Types

type ClientSignFunc

type ClientSignFunc = func(*x509.Certificate, *rsa.PrivateKey, *rsa.PublicKey) ([]byte, error)

ClientSignFunc is a function that produces a certificate signed using the provided certificate, using the server's private key and the client's public key. The result should be PEM-encoded.

type DbClient added in v1.18.1

type DbClient struct {

	// This channel is open until
	C chan struct{}
	// contains filtered or unexported fields
}

DbClient represents an open connection to the FailoverTestServer. it sends a message every 2 seconds and reads the response until Close() is called.

func NewDbClient added in v1.18.1

func NewDbClient(t *testing.T, conn net.Conn, id string) *DbClient

NewDbClient creates a new client that sends and receives data from the conn.

func (*DbClient) Close added in v1.18.1

func (c *DbClient) Close()

Close stops the send-receive loop and closes the socket.

func (*DbClient) Closed added in v1.18.1

func (c *DbClient) Closed() bool

Closed reports whether the client has been closed.

func (*DbClient) Execute added in v1.18.1

func (c *DbClient) Execute(ctx context.Context)

Execute runs the loop to send and receive data from the client. This will stop when ctx Context is canceled.

func (*DbClient) Recv added in v1.18.1

func (c *DbClient) Recv() []string

Recv returns a copy of the messages received by the client.

type EmptyTokenSource

type EmptyTokenSource struct{}

EmptyTokenSource is an Oauth2.TokenSource that returns empty tokens.

func (EmptyTokenSource) Token

func (EmptyTokenSource) Token() (*oauth2.Token, error)

Token provides an empty oauth2.Token.

type FailoverTestServer added in v1.18.1

type FailoverTestServer struct {
	// contains filtered or unexported fields
}

FailoverTestServer creates a mock server listening on port 3307 using TLS certificate validation like a real CloudSQL instance.

func NewFailoverTestServer added in v1.18.1

func NewFailoverTestServer(t *testing.T) *FailoverTestServer

NewFailoverTestServer creates a new test server.

func (*FailoverTestServer) Close added in v1.18.1

func (s *FailoverTestServer) Close()

Close closes the server socket and client sockets.

func (*FailoverTestServer) Start added in v1.18.1

func (s *FailoverTestServer) Start(i *FakeCSQLInstance)

Start starts the test server up, to make sure that it is ready to go

func (*FailoverTestServer) Stop added in v1.18.1

func (s *FailoverTestServer) Stop()

Stop closes the server socket, but leaves existing client sockets open.

type FakeCSQLInstance

type FakeCSQLInstance struct {

	// DNSName is the legacy field
	// DNSNames supersedes DNSName.
	DNSName    string
	MissingSAN string
	DNSNames   []*sqladmin.DnsNameMapping

	// Key is the server's private key
	Key *rsa.PrivateKey
	// Cert is the server's certificate
	Cert *x509.Certificate
	// contains filtered or unexported fields
}

FakeCSQLInstance represents settings for a specific Cloud SQL instance.

Use NewFakeCSQLInstance to instantiate.

func NewFakeCSQLInstance

func NewFakeCSQLInstance(project, region, name string, opts ...FakeCSQLInstanceOption) FakeCSQLInstance

NewFakeCSQLInstance returns a CloudSQLInst object for configuring mocks.

func (FakeCSQLInstance) ClientCert added in v1.9.0

func (f FakeCSQLInstance) ClientCert(pubKey *rsa.PublicKey) ([]byte, error)

ClientCert creates an ephemeral client certificate signed with the Cloud SQL instance's private key. The return value is PEM encoded.

func (FakeCSQLInstance) String added in v1.9.0

func (f FakeCSQLInstance) String() string

String returns the instance connection name for the instance.

type FakeCSQLInstanceOption

type FakeCSQLInstanceOption func(f *FakeCSQLInstance)

FakeCSQLInstanceOption is a function that configures a FakeCSQLInstance.

func WithCertExpiry

func WithCertExpiry(t time.Time) FakeCSQLInstanceOption

WithCertExpiry sets the server certificate's expiration to t.

func WithCertSigner

func WithCertSigner(s SignFunc) FakeCSQLInstanceOption

WithCertSigner configures the signing function used to generate a signed certificate.

func WithClientCertSigner

func WithClientCertSigner(s ClientSignFunc) FakeCSQLInstanceOption

WithClientCertSigner configures the signing function used to generate a certificate signed with the client's public key.

func WithDNS added in v1.12.0

func WithDNS(dns string) FakeCSQLInstanceOption

WithDNS sets the DnsName to addr.

func WithDNSMapping added in v1.16.0

func WithDNSMapping(name, dnsScope, connectionType string) FakeCSQLInstanceOption

WithDNSMapping adds the DnsNames records

func WithEngineVersion

func WithEngineVersion(s string) FakeCSQLInstanceOption

WithEngineVersion sets the "DB Version"

func WithFirstGenBackend

func WithFirstGenBackend() FakeCSQLInstanceOption

WithFirstGenBackend sets the server backend type to FIRST_GEN.

func WithMissingSAN added in v1.17.0

func WithMissingSAN(dns string) FakeCSQLInstanceOption

WithMissingSAN will cause the omit this dns name from the server cert, even though it is in the metadata.

func WithNoIPAddrs added in v1.0.0

func WithNoIPAddrs() FakeCSQLInstanceOption

WithNoIPAddrs configures a Fake Cloud SQL instance to have no IP addresses.

func WithPSC added in v1.4.0

func WithPSC(enabled bool) FakeCSQLInstanceOption

WithPSC sets the PSC enabled.

func WithPrivateIP

func WithPrivateIP(addr string) FakeCSQLInstanceOption

WithPrivateIP sets the private IP address to addr.

func WithPublicIP

func WithPublicIP(addr string) FakeCSQLInstanceOption

WithPublicIP sets the public IP address to addr.

func WithRegion

func WithRegion(region string) FakeCSQLInstanceOption

WithRegion sets the server's region to the provided value.

func WithServerCAMode added in v1.12.0

func WithServerCAMode(serverCAMode string) FakeCSQLInstanceOption

WithServerCAMode sets the ServerCaMode of the instance.

type Request

type Request struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Request represents a HTTP request for a test Server to mock responses for.

Use NewRequest to initialize new Requests.

func CreateEphemeral500 added in v1.10.0

func CreateEphemeral500(i FakeCSQLInstance, count int) *Request

CreateEphemeral500 returns a 500 HTTP response.

func CreateEphemeralSuccess

func CreateEphemeralSuccess(i FakeCSQLInstance, ct int) *Request

CreateEphemeralSuccess returns a Request that responds to the `connect.generateEphemeralCert` SQL Admin endpoint. It responds with a "StatusOK" and a SslCerts object.

https://cloud.google.com/sql/docs/mysql/admin-api/rest/v1beta4/connect/generateEphemeralCert

func InstanceGet500 added in v1.10.0

func InstanceGet500(i FakeCSQLInstance, count int) *Request

InstanceGet500 returns a 500 HTTP response

func InstanceGetSuccess

func InstanceGetSuccess(i FakeCSQLInstance, ct int) *Request

InstanceGetSuccess returns a Request that responds to the `instance.get` SQL Admin endpoint. It responds with a "StatusOK" and a DatabaseInstance object.

https://cloud.google.com/sql/docs/mysql/admin-api/rest/v1beta4/instances/get

type SignFunc

type SignFunc = func(*x509.Certificate, *rsa.PrivateKey) ([]byte, error)

SignFunc is a function that signs the certificate using the provided key. The result should be PEM-encoded.

type TLSCertificates added in v1.15.0

type TLSCertificates struct {
	// contains filtered or unexported fields
}

TLSCertificates generates an accurate reproduction of the TLS certificates used by Cloud SQL. This was translated to Go from the Java connector.

From the cloud-sql-jdbc-socket-factory project: core/src/test/java/com/google/cloud/sql/core/TestCertificateGenerator.java

func NewTLSCertificates added in v1.17.0

func NewTLSCertificates(projectName, instanceName string, sans []string, clientCertExpires time.Time) *TLSCertificates

NewTLSCertificates creates a new instance of the TLSCertificates.

func (*TLSCertificates) CreateCASServerChain added in v1.17.0

func (ct *TLSCertificates) CreateCASServerChain(cn string, sans []string) []*x509.Certificate

CreateCASServerChain creates a certificate chain containing the CN and SAN fields.

func (*TLSCertificates) CreateServerChain added in v1.17.0

func (ct *TLSCertificates) CreateServerChain(cn string, sans []string) []*x509.Certificate

CreateServerChain creates a legacy server certificate chain containing the CN and SAN fields.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL