Skip to content

Commit

Permalink
fix: Rewrite the tests using Ginkgo and Gomega, fix some bugs. (#147)
Browse files Browse the repository at this point in the history
* test: Convert tests to Ginkgo and Gomega RSpec style testing.
* fix: CredentialProvider returns InvalidArgument when the token cannot be parsed.
* test: Make sure to cleanup the client and caches between all tests.
* fix: Values are allowed to be blank.
* chore: Remove `go vet` from the `make test` target, it's already part of `make lint`.
* fix: DeleteCache() should succeed if the cache doesn't exist.
* chore: Allow us to see why the CI lint checks failed.
  • Loading branch information
schwern authored and cprice404 committed Mar 14, 2023
1 parent 7fe35b6 commit 4e5b6df
Show file tree
Hide file tree
Showing 18 changed files with 619 additions and 915 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,11 @@ jobs:
- name: Verify no changes from goimports and go mod tidy
run: |
if [ -n "$(git status --porcelain)" ]; then
git diff
exit 1
fi
- name: Run test
run: |
go vet ./...
go test -v ./momento
run: make test


3 changes: 3 additions & 0 deletions CONTRIBUTING.template.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Run `make install-devtools`. This will install...

* [goimports](https://pkg.go.dev/golang.org/x/tools/cmd/goimports)
* [staticcheck](https://staticcheck.io/)
* [gingko](https://onsi.github.io/ginkgo/) for testing

## Developing :computer:

Expand All @@ -36,6 +37,8 @@ Running `make precommit` will run all formatters, linters, and the tests. Run th

## Tests :zap:

We use [Ginkgo](https://onsi.github.io/ginkgo/) and [Gomega](https://onsi.github.io/gomega/) to write our tests.

Integration tests require an auth token for testing. Set the env var `TEST_AUTH_TOKEN` to
provide it, you can get this from your `~/.momento/credentials` file. The env `TEST_CACHE_NAME` is also required, but for now any string value works.

Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
install-devtools:
go install golang.org/x/tools/cmd/goimports@latest
go install honnef.co/go/tools/cmd/staticcheck@latest
go install github.com/onsi/ginkgo/v2/ginkgo

.PHONY: format
format:
Expand Down Expand Up @@ -42,7 +43,7 @@ precommit: lint test

.PHONY: test
test:
go test -v ./momento
ginkgo momento/ auth/

.PHONY: vendor
vendor:
Expand Down
13 changes: 13 additions & 0 deletions auth/auth_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package auth_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestAuth(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Auth Suite")
}
6 changes: 5 additions & 1 deletion auth/credential_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ func resolve(request *ResolveRequest) (*Endpoints, momentoerrors.MomentoSvcErr)
func getEndpointsFromToken(authToken string) (*Endpoints, momentoerrors.MomentoSvcErr) {
token, _, err := new(jwt.Parser).ParseUnverified(authToken, jwt.MapClaims{})
if err != nil {
return nil, momentoerrors.ConvertSvcErr(err)
return nil, momentoerrors.NewMomentoSvcErr(
momentoerrors.InvalidArgumentError,
"Could not parse auth token.",
err,
)
}
if claims, ok := token.Claims.(jwt.MapClaims); ok {
return &Endpoints{
Expand Down
24 changes: 24 additions & 0 deletions auth/credential_provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package auth_test

import (
"errors"

"github.com/momentohq/client-sdk-go/auth"
"github.com/momentohq/client-sdk-go/internal/momentoerrors"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("CredentialProvider", func() {
It(`errors on a invalid auth token`, func() {
badCredentialProvider, err := auth.NewStringMomentoTokenProvider("Invalid token")

Expect(badCredentialProvider).To(BeNil())
Expect(err).NotTo(BeNil())

var momentoErr momentoerrors.MomentoSvcErr
if errors.As(err, &momentoErr) {
Expect(momentoErr.Code()).To(Equal(momentoerrors.InvalidArgumentError))
}
})
})
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ go 1.19
require (
github.com/golang-jwt/jwt/v4 v4.3.0
github.com/google/uuid v1.3.0
github.com/onsi/ginkgo/v2 v2.8.1
github.com/onsi/gomega v1.26.0
google.golang.org/grpc v1.52.0
google.golang.org/protobuf v1.28.1
)

require (
github.com/go-logr/logr v1.2.3 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
google.golang.org/genproto v0.0.0-20221118155620-16455021b5e6 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/golang-jwt/jwt/v4 v4.3.0 h1:kHL1vqdqWNfATmA0FNMdmZNMyZI1U6O31X4rlIPoBog=
github.com/golang-jwt/jwt/v4 v4.3.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/onsi/ginkgo/v2 v2.8.1 h1:xFTEVwOFa1D/Ty24Ws1npBWkDYEV9BqZrsDxVrVkrrU=
github.com/onsi/ginkgo/v2 v2.8.1/go.mod h1:N1/NbDngAFcSLdyZ+/aYTYGSlq9qMCS/cNKGJjy+csc=
github.com/onsi/gomega v1.26.0 h1:03cDLK28U6hWvCAns6NeydX3zIm4SF3ci69ulidS32Q=
github.com/onsi/gomega v1.26.0/go.mod h1:r+zV744Re+DiYCIPRlYOTxn0YkOLcAnW8k1xXdMPGhM=
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
Expand All @@ -22,3 +29,7 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
40 changes: 8 additions & 32 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -228,39 +228,15 @@ github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1 h1:zH8ljVhhq7yC0MIeUL/
github.com/envoyproxy/go-control-plane v0.10.2-0.20220325020618-49ff273808a1 h1:xvqufLtNVwAhN8NMyWklVgxnWohi+wtMGQMhtxexlm0=
github.com/envoyproxy/go-control-plane v0.10.2-0.20220325020618-49ff273808a1/go.mod h1:KJwIaB5Mv44NWtYuAOFCVOjcI94vtpEz2JU/D2v6IjE=
github.com/envoyproxy/protoc-gen-validate v0.1.0 h1:EQciDnbrYxy13PgWoY8AqoxGiPrpgBZ1R8UNe3ddc+A=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/momentohq/client-sdk-go v0.6.0/go.mod h1:9llwnDsZ87XAACS5slQKqCcYhfLDSHxAMcIbQWDbdJw=
github.com/momentohq/client-sdk-go v0.8.0/go.mod h1:9llwnDsZ87XAACS5slQKqCcYhfLDSHxAMcIbQWDbdJw=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20221014081412-f15817d10f9b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783 h1:nt+Q6cXKz4MosCSpnbMtqiQ8Oz0pxTef2B4Vca2lvfk=
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783/go.mod h1:h4gKUeWbJ4rQPri7E0u6Gs4e9Ri2zaLxzw5DI5XGrYg=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.3.0 h1:qoo4akIqOcDME5bhc/NgxUdovd6BSS2uMsVjB56q1xI=
golang.org/x/term v0.3.0/go.mod h1:q750SLmJuPmVoN1blW3UFBPREJfb1KmY3vwxfr+nFDA=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
google.golang.org/grpc v1.50.1/go.mod h1:ZgQEeidpAuNRZ8iRrlBKXZQP1ghovWIVhdJRyCDK+GI=
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
111 changes: 111 additions & 0 deletions momento/control_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package momento_test

import (
"context"
"errors"
"time"

"github.com/google/uuid"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/momentohq/client-sdk-go/config"
. "github.com/momentohq/client-sdk-go/momento"
)

var _ = Describe("Control ops", func() {
var client SimpleCacheClient
var ctx context.Context

BeforeEach(func() {
ctx = context.Background()

client = getClient(&SimpleCacheClientProps{
Configuration: config.LatestLaptopConfig(),
DefaultTTL: 60 * time.Second,
})

DeferCleanup(func() { client.Close() })
})

Describe(`Happy Path`, func() {
It(`creates, lists, and deletes caches`, func() {
cacheNames := []string{uuid.NewString(), uuid.NewString()}
defer func() {
for _, cacheName := range cacheNames {
_, err := client.DeleteCache(ctx, &DeleteCacheRequest{CacheName: cacheName})
if err != nil {
panic(err)
}
}
}()

for _, cacheName := range cacheNames {
Expect(
client.CreateCache(ctx, &CreateCacheRequest{CacheName: cacheName}),
).To(BeAssignableToTypeOf(&CreateCacheSuccess{}))

Expect(
client.CreateCache(ctx, &CreateCacheRequest{CacheName: cacheName}),
).To(BeAssignableToTypeOf(&CreateCacheAlreadyExists{}))
}

resp, err := client.ListCaches(ctx, &ListCachesRequest{})
Expect(err).To(Succeed())

listedCaches := []string{}
switch r := resp.(type) {
case *ListCachesSuccess:
for _, info := range r.Caches() {
listedCaches = append(listedCaches, info.Name())
}
Expect(listedCaches).To(ContainElements(cacheNames))
default:
Fail("Unexpected repsonse type")
}

for _, cacheName := range cacheNames {
Expect(
client.DeleteCache(ctx, &DeleteCacheRequest{CacheName: cacheName}),
).To(BeAssignableToTypeOf(&DeleteCacheSuccess{}))
}
resp, err = client.ListCaches(ctx, &ListCachesRequest{})
Expect(err).To(Succeed())
Expect(resp).To(BeAssignableToTypeOf(&ListCachesSuccess{}))
switch r := resp.(type) {
case *ListCachesSuccess:
Expect(r.Caches()).To(Not(ContainElements(cacheNames)))
default:
Fail("Unexpected repsonse type")
}
})
})

Describe(`Validate cache name`, func() {
It(`CreateCache and DeleteCache errors on bad cache names`, func() {
badCacheNames := []string{``, ` `}
for _, badCacheName := range badCacheNames {
createResp, err := client.CreateCache(ctx, &CreateCacheRequest{CacheName: badCacheName})
Expect(createResp).To(BeNil())
var momentoErr MomentoError
if errors.As(err, &momentoErr) {
Expect(momentoErr.Code()).To(Equal(InvalidArgumentError))
}

deleteResp, err := client.DeleteCache(ctx, &DeleteCacheRequest{CacheName: badCacheName})
Expect(deleteResp).To(BeNil())
if errors.As(err, &momentoErr) {
Expect(momentoErr.Code()).To(Equal(InvalidArgumentError))
}
}
})
})

Describe(`DeleteCache`, func() {
It(`succeeds even if the cache does not exist`, func() {
Expect(
client.DeleteCache(ctx, &DeleteCacheRequest{CacheName: uuid.NewString()}),
).To(BeAssignableToTypeOf(&DeleteCacheSuccess{}))
})
})
})
4 changes: 4 additions & 0 deletions momento/create_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ type CreateCacheSuccess struct{}

func (CreateCacheSuccess) isCreateCacheResponse() {}

type CreateCacheAlreadyExists struct{}

func (CreateCacheAlreadyExists) isCreateCacheResponse() {}

///// Request

type CreateCacheRequest struct {
Expand Down
13 changes: 13 additions & 0 deletions momento/momento_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package momento_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestMomento(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Momento Suite")
}
Loading

0 comments on commit 4e5b6df

Please sign in to comment.