Documentation
¶
Overview ¶
Package testutils contains code that is useful in tests.
Index ¶
- Constants
- Variables
- func ConvertTuplesKeysToTuples(input []*openfgav1.TupleKey) []*openfgav1.Tuple
- func ConvertTuplesToTupleKeys(input []*openfgav1.Tuple) []*openfgav1.TupleKey
- func CreateGrpcConnection(t *testing.T, grpcAddress string, opts ...grpc.DialOption) *grpc.ClientConn
- func CreateRandomString(n int) string
- func EnsureServiceHealthy(t testing.TB, grpcAddr, httpAddr string, ...)
- func MakeSliceWithGenerator[T any](n uint64, generator func(n uint64) any) []T
- func MakeStringWithRuneset(n uint64, runeSet []rune) string
- func MustDefaultConfig() *serverconfig.Config
- func MustDefaultConfigForParallelTests() *serverconfig.Config
- func MustDefaultConfigWithRandomPorts() *serverconfig.Config
- func MustNewStruct(t require.TestingT, v map[string]interface{}) *structpb.Struct
- func MustTransformDSLToProtoWithID(s string) *openfgav1.AuthorizationModel
- func NumericalStringGenerator(n uint64) any
- func Shuffle(arr []*openfgav1.TupleKey) []*openfgav1.TupleKey
- func TCPRandomPort() (int, func())
- type DockerClient
- func (d *DockerClient) Close() error
- func (d *DockerClient) ExecCommand(ctx context.Context, containerID string, execConfig client.ExecCreateOptions) error
- func (d *DockerClient) FindRunningContainer(ctx context.Context, containerName, imageName string) (*container.InspectResponse, bool, error)
- func (d *DockerClient) GetHostPort(inspect *container.InspectResponse, containerPort network.Port) (string, error)
- func (d *DockerClient) PullImage(ctx context.Context, imageName string) error
- func (d *DockerClient) RemoveContainer(ctx context.Context, containerID string) error
- func (d *DockerClient) RunContainer(ctx context.Context, containerCfg *container.Config, ...) (*container.InspectResponse, error)
Constants ¶
const (
AllChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
)
Variables ¶
var ( TupleCmpTransformer = cmp.Transformer("Sort", func(in []*openfgav1.Tuple) []*openfgav1.Tuple { out := append([]*openfgav1.Tuple(nil), in...) sort.SliceStable(out, func(i, j int) bool { if out[i].GetKey().GetObject() != out[j].GetKey().GetObject() { return out[i].GetKey().GetObject() < out[j].GetKey().GetObject() } if out[i].GetKey().GetRelation() != out[j].GetKey().GetRelation() { return out[i].GetKey().GetRelation() < out[j].GetKey().GetRelation() } if out[i].GetKey().GetUser() != out[j].GetKey().GetUser() { return out[i].GetKey().GetUser() < out[j].GetKey().GetUser() } return true }) return out }) TupleKeyCmpTransformer = cmp.Transformer("Sort", func(in []*openfgav1.TupleKey) []*openfgav1.TupleKey { out := append([]*openfgav1.TupleKey(nil), in...) sort.SliceStable(out, func(i, j int) bool { if out[i].GetObject() != out[j].GetObject() { return out[i].GetObject() < out[j].GetObject() } if out[i].GetRelation() != out[j].GetRelation() { return out[i].GetRelation() < out[j].GetRelation() } if out[i].GetUser() != out[j].GetUser() { return out[i].GetUser() < out[j].GetUser() } return true }) return out }) )
Functions ¶
func ConvertTuplesKeysToTuples ¶ added in v1.8.5
func ConvertTuplesToTupleKeys ¶ added in v1.5.5
func CreateGrpcConnection ¶ added in v1.5.0
func CreateGrpcConnection(t *testing.T, grpcAddress string, opts ...grpc.DialOption) *grpc.ClientConn
CreateGrpcConnection creates a grpc connection to an address and closes it when the test ends.
func CreateRandomString ¶
func EnsureServiceHealthy ¶ added in v1.4.1
func EnsureServiceHealthy(t testing.TB, grpcAddr, httpAddr string, transportCredentials credentials.TransportCredentials)
EnsureServiceHealthy is a test helper that ensures that a service's grpc and http health endpoints are responding OK. If the http address is empty, it doesn't check the http health endpoint. If the service doesn't respond healthy in 30 seconds it fails the test.
func MakeSliceWithGenerator ¶ added in v1.3.8
MakeSliceWithGenerator generates a slice of length 'n' and populates the contents with values based on the generator provided.
func MakeStringWithRuneset ¶ added in v1.3.8
func MustDefaultConfig ¶ added in v1.14.1
func MustDefaultConfig() *serverconfig.Config
MustDefaultConfig returns the default server configuration for tests. Playground, tracing, and metrics are disabled. The shutdown timeout is increased.
func MustDefaultConfigForParallelTests ¶ added in v1.11.4
func MustDefaultConfigForParallelTests() *serverconfig.Config
MustDefaultConfigForParallelTests returns default server config suitable for parallel tests. It limits the connection idle time and lifetime to prevent bad connections and extends the request timeout to accommodate slower test environments.
func MustDefaultConfigWithRandomPorts ¶ added in v1.5.3
func MustDefaultConfigWithRandomPorts() *serverconfig.Config
MustDefaultConfigWithRandomPorts returns default server config but with random ports for the grpc and http addresses and with the playground, tracing and metrics turned off. This function may panic if somehow a random port cannot be chosen.
func MustNewStruct ¶ added in v1.3.8
func MustTransformDSLToProtoWithID ¶ added in v1.4.1
func MustTransformDSLToProtoWithID(s string) *openfgav1.AuthorizationModel
MustTransformDSLToProtoWithID interprets the provided string s as an FGA model and attempts to parse it using the official OpenFGA language parser. The model returned includes an auto-generated model id which assists with producing models for testing purposes.
func NumericalStringGenerator ¶ added in v1.3.8
NumericalStringGenerator generates a string representation of the provided uint value.
func TCPRandomPort ¶ added in v1.5.3
func TCPRandomPort() (int, func())
TCPRandomPort tries to find a random TCP Port. If it can't find one, it panics. Else, it returns the port and a function that releases the port. It is the responsibility of the caller to call the release function right before trying to listen on the given port.
Types ¶
type DockerClient ¶ added in v1.15.0
type DockerClient struct {
// contains filtered or unexported fields
}
DockerClient is a simple wrapper around the Docker client to provide utility methods for managing containers and images in tests.
func NewDockerClient ¶ added in v1.15.0
func NewDockerClient() (*DockerClient, error)
NewDockerClient creates a new instance of DockerClient.
func (*DockerClient) Close ¶ added in v1.15.0
func (d *DockerClient) Close() error
Close calls the underlying Docker client's Close method, releasing its transport resources.
func (*DockerClient) ExecCommand ¶ added in v1.15.0
func (d *DockerClient) ExecCommand(ctx context.Context, containerID string, execConfig client.ExecCreateOptions) error
ExecCommand executes a command in the specified container and waits for it to complete.
func (*DockerClient) FindRunningContainer ¶ added in v1.15.0
func (d *DockerClient) FindRunningContainer( ctx context.Context, containerName, imageName string, ) (*container.InspectResponse, bool, error)
FindRunningContainer returns the inspection data of a running container matching the given name and image, or nil/false if not found.
func (*DockerClient) GetHostPort ¶ added in v1.15.0
func (d *DockerClient) GetHostPort(inspect *container.InspectResponse, containerPort network.Port) (string, error)
GetHostPort returns the published host port for the given container port.
func (*DockerClient) PullImage ¶ added in v1.15.0
func (d *DockerClient) PullImage(ctx context.Context, imageName string) error
PullImage checks if the specified image is already present locally, and if not, it pulls it from the registry.
func (*DockerClient) RemoveContainer ¶ added in v1.15.0
func (d *DockerClient) RemoveContainer(ctx context.Context, containerID string) error
RemoveContainer kills and removes a container.
func (*DockerClient) RunContainer ¶ added in v1.15.0
func (d *DockerClient) RunContainer( ctx context.Context, containerCfg *container.Config, hostCfg *container.HostConfig, containerName string, ) (*container.InspectResponse, error)
RunContainer starts a container, then returns its inspection data. If Docker reports a conflict because a container with the same name already exists, the existing container is reused and started instead.