test

package
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2025 License: Apache-2.0 Imports: 40 Imported by: 0

Documentation

Index

Constants

View Source
const (

	//nolint:revive // represents the default certificate's names.
	PrivateKey       = "private-key.pem"
	PublicKey        = "public-key.pem"
	CACertificateKey = "ca-certificate.pem"
)

Variables

View Source
var (
	// InsecureTLSConfig defines an empty tls config.
	InsecureTLSConfig connection.TLSConfig
)

ServerModes is a list of server-side TLS modes used for testing.

Functions

func CheckMetrics

func CheckMetrics(t *testing.T, url string, expectedMetrics ...string)

CheckMetrics checks the metrics endpoint for the expected metrics.

func CheckServerStopped

func CheckServerStopped(t *testing.T, addr string) bool

CheckServerStopped returns true if the grpc server listening on a given address has been stopped.

func CreateClientWithTLS added in v0.1.7

func CreateClientWithTLS[T any](
	t *testing.T,
	endpoint *connection.Endpoint,
	tlsCfg connection.TLSConfig,
	protoClient func(grpc.ClientConnInterface) T,
) T

CreateClientWithTLS creates and returns a typed gRPC client using the provided TLS configuration. It establishes a secure connection to the given endpoint and returns the generated client using the provided client creation proto function.

func CreateDockerNetwork added in v0.1.7

func CreateDockerNetwork(t *testing.T, name string) *docker.Network

CreateDockerNetwork creates a network if it doesn't exist.

func CreateServerAndClientTLSConfig added in v0.1.7

func CreateServerAndClientTLSConfig(t *testing.T, tlsMode string) (
	serverTLSConfig, clientTLSConfig connection.TLSConfig,
)

CreateServerAndClientTLSConfig creates server and client TLS configurations given a TLS mode.

func EnsurePersistedTxStatus

func EnsurePersistedTxStatus(
	ctx context.Context,
	t *testing.T,
	r StatusRetriever,
	txIDs []string,
	expected map[string]*protoblocktx.StatusWithHeight,
)

EnsurePersistedTxStatus fails the test if the given TX IDs does not match the expected status.

func EventuallyIntMetric

func EventuallyIntMetric(
	t *testing.T, expected int, m prometheus.Metric, waitFor, tick time.Duration, msgAndArgs ...any,
)

EventuallyIntMetric fail the test if the integer metric is not equal to the expected value after the given duration.

func FailHandler

func FailHandler(t *testing.T)

FailHandler registers a gomega fail handler.

func GetDockerClient added in v0.1.7

func GetDockerClient(t *testing.T) *docker.Client

GetDockerClient instantiate a new docker client.

func GetIntMetricValue

func GetIntMetricValue(t *testing.T, m prometheus.Metric) int

GetIntMetricValue returns the value of a prometheus metric, rounded to the nearest integer.

func GetMetricValue

func GetMetricValue(t *testing.T, m prometheus.Metric) float64

GetMetricValue returns the value of a prometheus metric.

func GetMetricValueFromURL

func GetMetricValueFromURL(t *testing.T, url, metricName string) int

GetMetricValueFromURL reads the metrics endpoint and fetch the value of a specific metric.

func LogStruct added in v0.1.6

func LogStruct(t *testing.T, name string, v any)

LogStruct logs a struct in a flat representation.

func MustCreateEndpoint added in v0.1.7

func MustCreateEndpoint(value string) *connection.Endpoint

MustCreateEndpoint parses an endpoint from an address string. It panics if it fails to parse.

func NewInsecureClientConfig added in v0.1.7

func NewInsecureClientConfig(ep *connection.Endpoint) *connection.ClientConfig

NewInsecureClientConfig creates a client configuration for test purposes given an endpoint.

func NewInsecureConnection added in v0.1.7

func NewInsecureConnection(t *testing.T, endpoint connection.WithAddress) *grpc.ClientConn

NewInsecureConnection creates the default connection with insecure credentials.

func NewInsecureConnectionWithRetry added in v0.1.7

func NewInsecureConnectionWithRetry(
	t *testing.T, endpoint connection.WithAddress, retry connection.RetryProfile,
) *grpc.ClientConn

NewInsecureConnectionWithRetry creates the default dial config with insecure credentials.

func NewInsecureLoadBalancedConnection added in v0.1.7

func NewInsecureLoadBalancedConnection(t *testing.T, endpoints []*connection.Endpoint) *grpc.ClientConn

NewInsecureLoadBalancedConnection creates the default connection with insecure credentials.

func NewSecuredConnection added in v0.1.7

func NewSecuredConnection(
	t *testing.T,
	endpoint connection.WithAddress,
	tlsConfig connection.TLSConfig,
) *grpc.ClientConn

NewSecuredConnection creates the default connection with given transport credentials.

func NewSecuredConnectionWithRetry added in v0.1.7

func NewSecuredConnectionWithRetry(
	t *testing.T,
	endpoint connection.WithAddress,
	tlsConfig connection.TLSConfig,
	retry connection.RetryProfile,
) *grpc.ClientConn

NewSecuredConnectionWithRetry creates the default connection with given transport credentials.

func NewTLSClientConfig added in v0.1.7

func NewTLSClientConfig(tlsConfig connection.TLSConfig, ep *connection.Endpoint) *connection.ClientConfig

NewTLSClientConfig creates a client configuration for test purposes given a single endpoint and creds.

func NewTLSMultiClientConfig added in v0.1.7

func NewTLSMultiClientConfig(
	tlsConfig connection.TLSConfig,
	ep ...*connection.Endpoint,
) *connection.MultiClientConfig

NewTLSMultiClientConfig creates a multi client configuration for test purposes given number of endpoints and a TLS configuration.

func RemoveDockerNetwork added in v0.1.7

func RemoveDockerNetwork(t *testing.T, name string)

RemoveDockerNetwork removes a Docker network by name.

func RequireIntMetricValue

func RequireIntMetricValue(t *testing.T, expected int, m prometheus.Metric)

RequireIntMetricValue fail the test if the integer metric is not equal to the expected value.

func RequireProtoElementsMatch

func RequireProtoElementsMatch[T proto.Message](
	t require.TestingT, expected, actual []T, msgAndArgs ...any,
)

RequireProtoElementsMatch verifies that two arrays of proto have the same elements.

func RequireProtoEqual

func RequireProtoEqual(t require.TestingT, expected, actual proto.Message)

RequireProtoEqual verifies that two proto are equal.

func RunGrpcServerForTest

func RunGrpcServerForTest(
	ctx context.Context, tb testing.TB, serverConfig *connection.ServerConfig, register func(server *grpc.Server),
) *grpc.Server

RunGrpcServerForTest starts a GRPC server using a register method. It handles the cleanup of the GRPC server at the end of a test, and ensure the test is ended only when the GRPC server is down. It also updates the server config endpoint port to the actual port if the configuration did not specify a port. The method asserts that the GRPC server did not end with failure.

func RunSecureConnectionTest added in v0.1.7

func RunSecureConnectionTest(
	t *testing.T,
	starter ServerStarter,
)

RunSecureConnectionTest starts a gRPC server with mTLS enabled and tests client connections using various TLS configurations to verify that the server correctly accepts or rejects connections based on the client's setup. It runs a server instance of the service and returns a function that starts a client with the required TLS mode, attempts an RPC call, and returns the resulting error. Server Mode | Client with mTLS | Client with server-side TLS | Client with no TLS ------------|------------------|-----------------------------|-------------------- mTLS | connect | can't connect | can't connect TLS | connect | connect | can't connect None | can't connect | can't connect | connect.

func RunServiceAndGrpcForTest

func RunServiceAndGrpcForTest(
	ctx context.Context,
	t *testing.T,
	service connection.Service,
	serverConfig ...*connection.ServerConfig,
) *channel.Ready

RunServiceAndGrpcForTest combines running a service and its GRPC server. It is intended for services that implements the Service API (i.e., command line services).

func RunServiceForTest

func RunServiceForTest(
	ctx context.Context,
	tb testing.TB,
	service func(ctx context.Context) error,
	waitFunc func(ctx context.Context) bool,
) *channel.Ready

RunServiceForTest runs a service using the given service method, and waits for it to be ready given the waitFunc method. It handles the cleanup of the service at the end of a test, and ensure the test is ended only when the service return. The method asserts that the service did not end with failure. Returns a ready flag that indicate that the service is done.

func ServerToMultiClientConfig added in v0.1.7

func ServerToMultiClientConfig(servers ...*connection.ServerConfig) *connection.MultiClientConfig

ServerToMultiClientConfig is used to create a multi client configuration from existing server(s).

func SetupDebugging

func SetupDebugging()

SetupDebugging can be added for development to tests that required additional debugging info.

func WaitUntilGrpcServerIsReady

func WaitUntilGrpcServerIsReady(
	ctx context.Context,
	t *testing.T,
	conn grpc.ClientConnInterface,
)

WaitUntilGrpcServerIsReady uses the health check API to check a service readiness.

Types

type BroadcastStream added in v0.1.6

type BroadcastStream struct {
	Broadcaster
	ConnectionManager *ordererconn.ConnectionManager
}

BroadcastStream implements broadcast, with the addition of automatically signing the envelope.

func NewBroadcastStream added in v0.1.6

func NewBroadcastStream(ctx context.Context, config *ordererconn.Config) (*BroadcastStream, error)

NewBroadcastStream starts a new broadcast stream (non-production).

func (*BroadcastStream) Close added in v0.1.6

func (s *BroadcastStream) Close() error

Close is included to implement the io.Closer interface.

func (*BroadcastStream) CloseConnections added in v0.1.7

func (s *BroadcastStream) CloseConnections()

CloseConnections closes all the connections for the stream.

func (*BroadcastStream) SendBatch added in v0.1.6

func (s *BroadcastStream) SendBatch(envelopes []*common.Envelope) error

SendBatch sends a batch one by one.

type Broadcaster added in v0.1.6

type Broadcaster interface {
	Send(*common.Envelope) error
}

Broadcaster can be either CFT or BFT broadcast.

type CredentialsFactory added in v0.1.7

type CredentialsFactory struct {
	CertificateAuthority tlsgen.CA
}

CredentialsFactory responsible for the creation of TLS certificates inside a TLSConfig for testing purposes by using the tls generation library of 'Hyperledger Fabric'.

func NewCredentialsFactory added in v0.1.7

func NewCredentialsFactory(t *testing.T) *CredentialsFactory

NewCredentialsFactory returns a CredentialsFactory with a new CA.

func (*CredentialsFactory) CreateClientCredentials added in v0.1.7

func (scm *CredentialsFactory) CreateClientCredentials(t *testing.T, tlsMode string) (connection.TLSConfig, string)

CreateClientCredentials creates a client key pair, Writing it to a temp testing folder and returns a connection.TLSConfig.

func (*CredentialsFactory) CreateServerCredentials added in v0.1.7

func (scm *CredentialsFactory) CreateServerCredentials(
	t *testing.T,
	tlsMode string,
	san ...string,
) (connection.TLSConfig, string)

CreateServerCredentials creates a server key pair given SAN(s) (Subject Alternative Name), Writing it to a temp testing folder and returns a connection.TLSConfig.

type GrpcServers

type GrpcServers struct {
	Servers []*grpc.Server
	Configs []*connection.ServerConfig
}

GrpcServers holds the server instances and their respective configurations.

func StartGrpcServersForTest

func StartGrpcServersForTest(
	ctx context.Context,
	t *testing.T,
	numService int,
	register func(*grpc.Server, int),
) *GrpcServers

StartGrpcServersForTest starts multiple GRPC servers with a default configuration.

func StartGrpcServersWithConfigForTest

func StartGrpcServersWithConfigForTest(
	ctx context.Context, t *testing.T, sc []*connection.ServerConfig, register func(*grpc.Server, int),
) *GrpcServers

StartGrpcServersWithConfigForTest starts multiple GRPC servers with given configurations.

type RPCAttempt added in v0.1.7

type RPCAttempt func(ctx context.Context, t *testing.T, cfg connection.TLSConfig) error

RPCAttempt is a function returned by ServerStarter that contains the information needed to start a client connection and attempt an RPC call.

type ServerStarter added in v0.1.7

type ServerStarter func(t *testing.T, serverTLS connection.TLSConfig) RPCAttempt

ServerStarter is a function that receives a TLS configuration, starts the server, and returns a RPCAttempt function for initiating a client connection and attempting an RPC call.

type StatusRetriever

type StatusRetriever interface {
	GetTransactionsStatus(context.Context, *protoblocktx.QueryStatus, ...grpc.CallOption) (
		*protoblocktx.TransactionsStatus, error,
	)
}

StatusRetriever provides implementation retrieve status of given transaction identifiers.

Jump to

Keyboard shortcuts

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