Skip to content

Commit

Permalink
feat: allow openfga test container to be reused (#172)
Browse files Browse the repository at this point in the history
* feat: allow openfga test container to be reused

Signed-off-by: Sarah Funkhouser <[email protected]>

* allow setting cpu and memory a well

Signed-off-by: Sarah Funkhouser <[email protected]>

---------

Signed-off-by: Sarah Funkhouser <[email protected]>
  • Loading branch information
golanglemonade authored Feb 25, 2025
1 parent b42133c commit b0ac80f
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 3 deletions.
47 changes: 44 additions & 3 deletions fgax/testutils/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ type OpenFGATestFixture struct {
storeName string
// tf is the openFGA test fixture
tf *openfga.OpenFGAContainer
// reuse is a flag to reuse the container
reuse bool
// containerName is the name of the container, defaults to tc-openfga when reusing containers
containerName string
// memory is the memory for the container
memory int64
// cpu is the CPU for the container
cpu int64
}

// WithModelFile sets the model file path for the openFGA client
Expand All @@ -48,12 +56,41 @@ func WithVersion(version string) Option {
}
}

// WithReuse allows the container to be reused
func WithReuse(reuse bool) Option {
return func(c *OpenFGATestFixture) {
c.reuse = reuse
}
}

// WithContainerName allows the container name to be set when using reusable containers
func WithContainerName(name string) Option {
return func(c *OpenFGATestFixture) {
c.containerName = name
}
}

// WithMemory sets the memory for the openFGA container
func WithMemory(memory int64) Option {
return func(c *OpenFGATestFixture) {
c.memory = memory
}
}

// WithCPU sets the CPU for the openFGA container
func WithCPU(cpu int64) Option {
return func(c *OpenFGATestFixture) {
c.cpu = cpu
}
}

// NewFGATestcontainer creates a new test container with the provided context and options
func NewFGATestcontainer(ctx context.Context, opts ...Option) *OpenFGATestFixture {
// setup the default config
c := &OpenFGATestFixture{
openFGAVersion: "latest", // default to latest
storeName: gofakeit.Name(), // add a random store name used if not provided
openFGAVersion: "latest", // default to latest
storeName: gofakeit.Name(), // add a random store name used if not provided
containerName: testcontainerName, // default to tc-openfga
}

// apply the options
Expand All @@ -64,7 +101,11 @@ func NewFGATestcontainer(ctx context.Context, opts ...Option) *OpenFGATestFixtur
// run the openfga container
container := fmt.Sprintf("openfga/openfga:%s", c.openFGAVersion)

openfgaContainer, err := openfga.Run(ctx, container)
openfgaContainer, err := openfga.Run(
ctx,
container,
WithCustomizer(c.reuse, c.containerName, c.memory, c.cpu),
)
if err != nil {
log.Fatal().Err(err).Msg("failed to run openfga container")
}
Expand Down
46 changes: 46 additions & 0 deletions fgax/testutils/customizer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package testutils

import (
"github.com/docker/docker/api/types/container"
"github.com/testcontainers/testcontainers-go"
)

const (
testcontainerName = "tc-openfga"
)

// Customizer type represents a container customizer for transferring state from the options to the container
type Customizer struct {
reuse bool
name string
memory int64
cpu int64
}

// Customize satisfies the ContainerCustomizer interface
func (c Customizer) Customize(req *testcontainers.GenericContainerRequest) error {
req.Reuse = c.reuse
req.Name = c.name

req.HostConfigModifier = func(config *container.HostConfig) {
if c.memory > 0 {
config.Resources.Memory = c.memory
}

if c.cpu > 0 {
config.Resources.CPUQuota = c.cpu
}
}

return nil
}

// WithCustomizer function option to use the customizer
func WithCustomizer(reuse bool, name string, memory, cpu int64) testcontainers.ContainerCustomizer {
return Customizer{
reuse: reuse,
name: name,
memory: memory,
cpu: cpu,
}
}

0 comments on commit b0ac80f

Please sign in to comment.