Skip to content

Commit

Permalink
enhancement: Test utilities
Browse files Browse the repository at this point in the history
Signed-off-by: Charith Ellawala <charith@cerbos.dev>
  • Loading branch information
charithe committed Sep 15, 2023
1 parent 4f621bb commit 1f8050b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/cerbos/cerbos-sdk-go?color=green&logo=github&sort=semver) [![Go Reference](https://pkg.go.dev/badge/github.com/cerbos/cerbos-sdk-go/client.svg)](https://pkg.go.dev/github.com/cerbos/cerbos-sdk-go)
[![Go Reference](https://pkg.go.dev/badge/github.com/cerbos/cerbos-sdk-go/client.svg)](https://pkg.go.dev/github.com/cerbos/cerbos-sdk-go)

# Cerbos Client SDK for Go

Expand Down
2 changes: 1 addition & 1 deletion cerbos/grpc_admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func TestAdminClient(t *testing.T) {
confFile := filepath.Join(tests.PathToTestDataDir(t, "configs"), "tcp_with_tls.yaml")
policyDir := tests.PathToTestDataDir(t, "policies")

s, err := launcher.Launch(&testutil.LaunchConf{
s, err := launcher.Launch(testutil.LaunchConf{
ConfFilePath: confFile,
AdditionalMounts: []string{
fmt.Sprintf("%s:/certs", certsDir),
Expand Down
4 changes: 2 additions & 2 deletions cerbos/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestGRPCClient(t *testing.T) {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Run("tcp", func(t *testing.T) {
s, err := launcher.Launch(&testutil.LaunchConf{
s, err := launcher.Launch(testutil.LaunchConf{
ConfFilePath: tc.confFilePath,
PolicyDir: policyDir,
AdditionalMounts: []string{
Expand Down Expand Up @@ -94,7 +94,7 @@ func TestGRPCClient(t *testing.T) {

t.Run("uds", func(t *testing.T) {
tempDir := t.TempDir()
s, err := launcher.Launch(&testutil.LaunchConf{
s, err := launcher.Launch(testutil.LaunchConf{
ConfFilePath: tc.confFilePath,
PolicyDir: policyDir,
AdditionalMounts: []string{
Expand Down
17 changes: 16 additions & 1 deletion testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func NewCerbosServerLauncherFromImage(repo, tag string) (*CerbosServerLauncher,
return &CerbosServerLauncher{pool: pool, repo: repo, tag: tag}, nil
}

func (csl *CerbosServerLauncher) Launch(conf *LaunchConf) (*CerbosServerInstance, error) {
func (csl *CerbosServerLauncher) Launch(conf LaunchConf) (*CerbosServerInstance, error) {
options := &dockertest.RunOptions{
Repository: csl.repo,
Tag: csl.tag,
Expand Down Expand Up @@ -210,3 +210,18 @@ func envOrDefault(envVarName, defaultVal string) string {

return val
}

// LaunchCerbosServer is a utility method to start a Cerbos server and wait for it be ready.
func LaunchCerbosServer(ctx context.Context, launchConf LaunchConf) (*CerbosServerInstance, error) {
launcher, err := NewCerbosServerLauncher()
if err != nil {
return nil, fmt.Errorf("failed to create launcher: %w", err)
}

server, err := launcher.Launch(launchConf)
if err != nil {
return nil, fmt.Errorf("failed to launch server: %w", err)
}

return server, server.WaitForReady(ctx)
}

0 comments on commit 1f8050b

Please sign in to comment.