Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Utility function to launch and wait for server #3

Merged
merged 2 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,7 @@ issues:
- goconst
- gomnd
- govet
- path: example_test\.go
linters:
- gocritic
- errcheck
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
65 changes: 65 additions & 0 deletions testutil/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2021-2023 Zenauth Ltd.
// SPDX-License-Identifier: Apache-2.0

package testutil_test

import (
"context"
"fmt"
"log"
"time"

"github.com/cerbos/cerbos-sdk-go/cerbos"
"github.com/cerbos/cerbos-sdk-go/testutil"
)

func ExampleLaunchCerbosServer() {
// Configure Cerbos with the SQLite storage driver
conf := testutil.LaunchConf{
Cmd: []string{
"server",
"--set=storage.driver=sqlite3",
"--set=storage.sqlite3.dsn=:mem:?_fk=true",
},
Env: []string{
"CERBOS_LOG_LEVEL=error",
},
}

// Set timeout for launching the server
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

s, err := testutil.LaunchCerbosServer(ctx, conf)
if err != nil {
log.Fatalf("Failed to launch Cerbos server: %v", err)
}

defer s.Stop()

c, err := cerbos.New(s.GRPCAddr(), cerbos.WithPlaintext())
if err != nil {
log.Fatalf("Failed to create Cerbos client: %v", err)
}

allowed, err := c.IsAllowed(context.TODO(),
cerbos.NewPrincipal("john").
WithRoles("employee", "manager").
WithAttr("department", "marketing").
WithAttr("geography", "GB"),
cerbos.NewResource("leave_request", "XX125").
WithAttributes(map[string]any{
"department": "marketing",
"geography": "GB",
"owner": "harry",
"status": "DRAFT",
}),
"view",
)
if err != nil {
log.Fatalf("API request failed: %v", err)
}

fmt.Println(allowed)
// Output: false
}
20 changes: 18 additions & 2 deletions testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ type LaunchConf struct {
PolicyDirMountPoint string
AdditionalMounts []string
Cmd []string
Env []string
}

// NewCerbosServerLauncher creates a launcher for Cerbos containers.
Expand All @@ -125,12 +126,12 @@ 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,
Cmd: conf.Cmd,
Env: []string{"CERBOS_NO_TELEMETRY=1"},
Env: append([]string{"CERBOS_NO_TELEMETRY=1"}, conf.Env...),
}

if conf.ConfFilePath != "" {
Expand Down Expand Up @@ -210,3 +211,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)
}