Skip to content

Commit

Permalink
Switch tests to invoke fga CLI directly rather than via exec
Browse files Browse the repository at this point in the history
  • Loading branch information
evankanderson committed Dec 18, 2023
1 parent c260c67 commit ec5932d
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 125 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ jobs:
with:
go-version-file: 'go.mod'

- name: Bootstrap
run: make bootstrap

- name: Set up helm (test dependency)
uses: azure/setup-helm@v3

Expand Down
40 changes: 27 additions & 13 deletions authz/tests/fga_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
package tests

import (
"errors"
"os/exec"
"bytes"
"path/filepath"
"testing"

// We need the init() function here, but we can't copy rootCmd because it is private
_ "github.com/openfga/cli/cmd"
"github.com/openfga/cli/cmd/model"
)

func TestFGA(t *testing.T) {

Check failure on line 30 in authz/tests/fga_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint / Go Lint

TestFGA's subtests should call t.Parallel (tparallel)
Expand All @@ -36,19 +39,30 @@ func TestFGA(t *testing.T) {
for _, f := range files {

Check failure on line 39 in authz/tests/fga_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint / Go Lint

Range statement for test TestFGA missing the call to method parallel in test Run
file := f
t.Run(file, func(t *testing.T) {
t.Parallel()
// We invoke cobra commands directly, which reference some global state in FGA.

output, err := exec.Command("fga", "model", "test", "--tests", file).CombinedOutput()
if err == nil {
t.Logf("%s succeeded, output:\n%s", file, string(output))
return
}
exit := &exec.ExitError{}
if errors.As(err, &exit) {
t.Errorf("%s failed with %s, output:%s\n", file, exit, string(output))
output, err := runFgaModelTest(file)
if err != nil {
t.Logf("%s failed: %s, output:\n%s", file, err, string(output))
} else {
t.Errorf("failed to exec `fga`: %v", err)
t.Logf("%s succeeded, output:\n%s", file, string(output))
}
})
}
}
}

// This is a little slimy, but we pull in the FGA CLI which implements "model test"
// directly here, so we don't need to bootstrap the external command.
//
// This means we're using the CLI's exported `ModelCmd` as an API, which is icky.
func runFgaModelTest(filename string) ([]byte, error) {
cmd := *model.ModelCmd
cmd.SetArgs([]string{"test", "--tests", filename})
buffer := new(bytes.Buffer)
cmd.SetOutput(buffer)

if err := cmd.Execute(); err != nil {
return buffer.Bytes(), err
}
return buffer.Bytes(), nil
}
28 changes: 0 additions & 28 deletions cmd/fga/main.go

This file was deleted.

24 changes: 3 additions & 21 deletions tools/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ require (
github.com/norwoodj/helm-docs v1.11.3
github.com/pseudomuto/protoc-gen-doc v1.5.1
github.com/sqlc-dev/sqlc v1.24.0
github.com/stacklok/minder v0.0.0-00010101000000-000000000000
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616
golang.org/x/tools v0.16.1
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.3.0
Expand Down Expand Up @@ -65,7 +64,6 @@ require (
github.com/bytecodealliance/wasmtime-go/v14 v14.0.0 // indirect
github.com/catenacyber/perfsprint v0.2.0 // indirect
github.com/ccojocar/zxcvbn-go v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/charithe/durationcheck v0.0.10 // indirect
github.com/chavacava/garif v0.1.0 // indirect
Expand Down Expand Up @@ -111,7 +109,6 @@ require (
github.com/gofrs/flock v0.8.1 // indirect
github.com/gofrs/uuid/v5 v5.0.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golangci/check v0.0.0-20180506172741-cfe4005ccda2 // indirect
github.com/golangci/dupl v0.0.0-20180902072040-3e9179ac440a // indirect
Expand All @@ -132,8 +129,6 @@ require (
github.com/gostaticanalysis/comment v1.4.2 // indirect
github.com/gostaticanalysis/forcetypeassert v0.1.0 // indirect
github.com/gostaticanalysis/nilerr v0.1.1 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.0.1 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
Expand All @@ -152,7 +147,6 @@ require (
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jirfag/go-printf-func-name v0.0.0-20200119135958-7558a9eaa5af // indirect
github.com/julz/importas v0.1.0 // indirect
github.com/karlseguin/ccache/v3 v3.0.5 // indirect
github.com/kisielk/errcheck v1.6.3 // indirect
github.com/kisielk/gotool v1.0.0 // indirect
github.com/kkHAIKE/contextcheck v1.1.4 // indirect
Expand Down Expand Up @@ -184,26 +178,14 @@ require (
github.com/moby/term v0.5.0 // indirect
github.com/moricho/tparallel v0.3.1 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/muesli/mango v0.2.0 // indirect
github.com/muesli/mango-cobra v1.2.0 // indirect
github.com/muesli/mango-pflag v0.1.0 // indirect
github.com/muesli/roff v0.1.0 // indirect
github.com/mwitkow/go-proto-validators v0.0.0-20180403085117-0950a7990007 // indirect
github.com/nakabonne/nestif v0.3.1 // indirect
github.com/natefinch/wrap v0.2.0 // indirect
github.com/nishanths/exhaustive v0.11.0 // indirect
github.com/nishanths/predeclared v0.2.2 // indirect
github.com/nunnatsa/ginkgolinter v0.14.1 // indirect
github.com/nwidger/jsoncolor v0.3.2 // indirect
github.com/oklog/ulid/v2 v2.1.0 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc5 // indirect
github.com/openfga/api/proto v0.0.0-20231212010208-061d8d8d395f // indirect
github.com/openfga/cli v0.2.1 // indirect
github.com/openfga/go-sdk v0.3.0 // indirect
github.com/openfga/language/pkg/go v0.0.0-20231214002416-cca4c4312106 // indirect
github.com/openfga/openfga v1.4.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/pganalyze/pg_query_go/v4 v4.2.3 // indirect
github.com/pingcap/errors v0.11.5-0.20210425183316-da1aaba5fb63 // indirect
Expand Down Expand Up @@ -276,14 +258,13 @@ require (
gitlab.com/bosi/decorder v0.4.1 // indirect
go-simpler.org/sloglint v0.1.2 // indirect
go.opentelemetry.io/otel v1.21.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.21.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.21.0 // indirect
go.opentelemetry.io/otel/metric v1.21.0 // indirect
go.opentelemetry.io/otel/sdk v1.21.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.21.0 // indirect
go.opentelemetry.io/otel/trace v1.21.0 // indirect
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
go.tmz.dev/musttag v0.7.2 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/goleak v1.3.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/crypto v0.16.0 // indirect
Expand All @@ -303,6 +284,7 @@ require (
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gotest.tools/v3 v3.4.0 // indirect
honnef.co/go/tools v0.4.6 // indirect
k8s.io/helm v2.14.3+incompatible // indirect
mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed // indirect
Expand Down
Loading

0 comments on commit ec5932d

Please sign in to comment.