Hi I'm struggling to add unit tests for my applic...
# spicedb
h
Hi I'm struggling to add unit tests for my application which uses spicedb. Say I have a
mutation func()
in go. after executing the mutation it will write a
(resource, subject, permission)
tuple to authzed. I'd like to test after i executed func(), does
subject
have
permission
to
resource
. I am aware of the serve-testing. But I'm wondering how I can put it into my go unittest code. I'm trying this example https://github.com/authzed/spicedb/blob/main/cmd/spicedb/servetesting_integration_test.go But it requires building a docker image. And when I removed first two lines
Copy code
//go:build docker
// +build docker
and run
go test
, it pops up error saying
Copy code
go/pkg/mod/github.com/authzed/spicedb@v1.8.0/pkg/proto/core/v1/util.go:17:47: undefined: v0.RelationTupleTreeNode
v
Hey @HOPE ! I think that's right, if you want to run integration tests in your application with SpiceDB, I think a good strategy would be to spin the SpiceDB container as part of the test. I failed to reproducing what you are seeing 🤔 Have you tried running
go get ./...
to resolve all module dependencies? Then you can run the test with
Copy code
go test --tags=docker ./cmd/spicedb/servetesting_integration_test.go

ok      command-line-arguments  0.638s
You'd need to remove the
tags: ci
part of the test, as this is part of our CI pipeline, where each branch commit is pushed to a container with tag
ci
and the integration test is run against it.
h
Thank you so much. Let me try
Copy code
➜  api git:(@peter/test) go test --tags=docker internal/authz/authz_test.go
# github.com/authzed/spicedb/pkg/proto/core/v1
../../go/pkg/mod/github.com/authzed/spicedb@v1.8.0/pkg/proto/core/v1/util.go:17:47: undefined: v0.RelationTupleTreeNode
../../go/pkg/mod/github.com/authzed/spicedb@v1.8.0/pkg/proto/core/v1/util.go:50:43: undefined: v0.RelationTupleUpdate
../../go/pkg/mod/github.com/authzed/spicedb@v1.8.0/pkg/proto/core/v1/util.go:74:30: undefined: v0.Zookie
../../go/pkg/mod/github.com/authzed/spicedb@v1.8.0/pkg/proto/core/v1/util.go:101:40: undefined: v0.NamespaceDefinition
../../go/pkg/mod/github.com/authzed/spicedb@v1.8.0/pkg/proto/core/v1/util.go:113:44: undefined: v0.NamespaceDefinition
../../go/pkg/mod/github.com/authzed/spicedb@v1.8.0/pkg/proto/core/v1/util.go:122:69: undefined: v0.RelationTupleTreeNode
../../go/pkg/mod/github.com/authzed/spicedb@v1.8.0/pkg/proto/core/v1/util.go:167:60: undefined: v0.NamespaceDefinition
../../go/pkg/mod/github.com/authzed/spicedb@v1.8.0/pkg/proto/core/v1/util.go:179:66: undefined: v0.NamespaceDefinition
../../go/pkg/mod/github.com/authzed/spicedb@v1.8.0/pkg/proto/core/v1/util.go:188:63: undefined: v0.RelationTupleUpdate
../../go/pkg/mod/github.com/authzed/spicedb@v1.8.0/pkg/proto/core/v1/util.go:200:69: undefined: v0.RelationTupleUpdate
../../go/pkg/mod/github.com/authzed/spicedb@v1.8.0/pkg/proto/core/v1/util.go:200:69: too many errors
FAIL    command-line-arguments [build failed]
FAIL
it is still showing me this error after I run go get. I think I have all dependencies
v
which go version are you running?
h
go version go1.18 darwin/amd64
v
weird 🤔 I just downloaded the same version and couldn't reproduce the same problem
I see from the path in your snippet above you are running this in a different repo
I wonder if you are missing deps in your project
let me try to create a new repo with an example test
h
yes, go root is at my user root path. and I am running go test in the repo.
v
I see to have reproduced your problem in a brand new repo:
Copy code
`
# github.com/authzed/spicedb/pkg/proto/core/v1
../../go/pkg/mod/github.com/authzed/spicedb@v1.8.0/pkg/proto/core/v1/util.go:17:47: undefined: v0.RelationTupleTreeNode
../../go/pkg/mod/github.com/authzed/spicedb@v1.8.0/pkg/proto/core/v1/util.go:50:43: undefined: v0.RelationTupleUpdate
../../go/pkg/mod/github.com/authzed/spicedb@v1.8.0/pkg/proto/core/v1/util.go:74:30: undefined: v0.Zookie
../../go/pkg/mod/github.com/authzed/spicedb@v1.8.0/pkg/proto/core/v1/util.go:101:40: undefined: v0.NamespaceDefinition
../../go/pkg/mod/github.com/authzed/spicedb@v1.8.0/pkg/proto/core/v1/util.go:113:44: undefined: v0.NamespaceDefinition
../../go/pkg/mod/github.com/authzed/spicedb@v1.8.0/pkg/proto/core/v1/util.go:122:69: undefined: v0.RelationTupleTreeNode
../../go/pkg/mod/github.com/authzed/spicedb@v1.8.0/pkg/proto/core/v1/util.go:167:60: undefined: v0.NamespaceDefinition
../../go/pkg/mod/github.com/authzed/spicedb@v1.8.0/pkg/proto/core/v1/util.go:179:66: undefined: v0.NamespaceDefinition
../../go/pkg/mod/github.com/authzed/spicedb@v1.8.0/pkg/proto/core/v1/util.go:188:63: undefined: v0.RelationTupleUpdate
../../go/pkg/mod/github.com/authzed/spicedb@v1.8.0/pkg/proto/core/v1/util.go:200:69: undefined: v0.RelationTupleUpdate
../../go/pkg/mod/github.com/authzed/spicedb@v1.8.0/pkg/proto/core/v1/util.go:200:69: too many errors

Compilation finished with exit code 2
h
🧐
v
I suspect we need a new spicedb tag
h
cool. coz I saw that you just deprecated v0 a few days ago. might be related to this change?
v
correct!
and yes, just validated that's the problem. You'd need to run against an unreleased version until we do a new release
1.8.1
. Do this:
Copy code
go get github.com/authzed/spicedb@5fbb49830d60325a7fbea47b8155e0d2a9242c2b
then run
go mod tidy
and you should be set
h
👍 thank you so much
v
@HOPE doing further release today, I'm not sure what went wrong but spicedb 1.8.0 is pinned with the right version of
authzed-go
, so you shouldn't need to use an unreleased version of spicedb
j
I would also recommend against compiling SpiceDB in, unless you are in an environment where you cannot run an external binary or container
because while the SpiceDB gRPC/Rest API is guaranteed to maintain compatibility across versions, the go-level interface can change
v
yeah I think what happened here is that @HOPE used the integration test as example, which uses SpiceDB Go-level API
what we need is to provide examples on how to run integration tests by just relying on
authzed-go
client library and the docker container
h
thank you for helping out! Is it possible that https://docs.authzed.com/guides/validation-and-testing could provide examples with different languages?
Or add testing examples in Protecting your first app page https://docs.authzed.com/guides/first-app..
j
sure, we can extend those guides
it should be fairly straightforward though: just connect your client to the local address
and (optionally) send an auth token per test
that uses serve-testing
h
Thank you so much!