Skip to content

Commit

Permalink
don't depend on pubsub emulator in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
christeredvartsen committed Feb 7, 2024
1 parent c58f63c commit aac1e4b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 28 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ jobs:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- name: Find Go version
id: go_version
run: echo "GO_VERSION=$(grep golang .tool-versions | awk '{print $2}')" >> $GITHUB_OUTPUT
- uses: asdf-vm/actions/install@v3
with:
before_install: |
Expand All @@ -25,7 +22,9 @@ jobs:
- run: go test ./...
# - run: helm lint --strict ./charts
- run: make check
- run: make staticcheck
- run: make vulncheck
- run: make deadcode
- run: |
make fmt
make generate
Expand Down
13 changes: 9 additions & 4 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ jobs:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: ${{ needs.go_version.outputs.go_version }}
- uses: asdf-vm/actions/install@v3
with:
before_install: |
asdf install golang latest
asdf global golang latest
- run: go test ./...
- run: helm lint --strict ./charts
- run: make check
- run: make staticcheck
- run: make vulncheck
- run: make deadcode
- run: |
make fmt
make generate
git diff --exit-code --name-only
50 changes: 30 additions & 20 deletions internal/graph/teams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package graph_test

import (
"context"
"net"
"os"
"testing"
"time"

"cloud.google.com/go/pubsub"
"cloud.google.com/go/pubsub/pstest"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
"github.com/nais/api/internal/auditlogger"
Expand All @@ -26,6 +24,9 @@ import (
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"google.golang.org/api/option"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

func TestMutationResolver_CreateTeam(t *testing.T) {
Expand Down Expand Up @@ -108,14 +109,14 @@ func TestMutationResolver_CreateTeam(t *testing.T) {
Return(nil).
Once()

pubsubClient, err := getPubsubClient(ctx, t)
if err != nil {
t.Fatal("unexpected error when creating pubsub client")
}
_, psClient, closer := getPubsubServerAndClient(ctx, "some-id", "topic-id")
defer closer()

// TODO: check message sent to the pubsub topic

auditLogger := auditlogger.NewAuditLoggerForTesting()
returnedTeam, err := graph.
NewResolver(nil, nil, nil, nil, db, tenantDomain, userSync, auditLogger, nil, userSyncRuns, pubsubClient.Topic("topic-id"), log).
NewResolver(nil, nil, nil, nil, db, tenantDomain, userSync, auditLogger, nil, userSyncRuns, psClient.Topic("topic-id"), log).
Mutation().
CreateTeam(ctx, model.CreateTeamInput{
Slug: teamSlug,
Expand Down Expand Up @@ -159,14 +160,14 @@ func TestMutationResolver_CreateTeam(t *testing.T) {
Return(nil).
Once()

pubsubClient, err := getPubsubClient(ctx, t)
if err != nil {
t.Fatal("unexpected error when creating pubsub client")
}
_, psClient, closer := getPubsubServerAndClient(ctx, "some-id", "topic-id")
defer closer()

// TODO: check message sent to the pubsub topic

auditLogger := auditlogger.NewAuditLoggerForTesting()
returnedTeam, err := graph.
NewResolver(nil, nil, nil, nil, db, tenantDomain, userSync, auditLogger, nil, userSyncRuns, pubsubClient.Topic("topic-id"), log).
NewResolver(nil, nil, nil, nil, db, tenantDomain, userSync, auditLogger, nil, userSyncRuns, psClient.Topic("topic-id"), log).
Mutation().CreateTeam(saCtx, model.CreateTeamInput{
Slug: teamSlug,
Purpose: " some purpose ",
Expand Down Expand Up @@ -350,13 +351,22 @@ func TestMutationResolver_RequestTeamDeletion(t *testing.T) {
})
}

func getPubsubClient(ctx context.Context, t *testing.T) (*pubsub.Client, error) {
host, port := "localhost", "3004"
if err := os.Setenv("PUBSUB_EMULATOR_HOST", host+":"+port); err != nil {
t.Fatal("unable to set env var for pubsub emulator")
} else if _, err := net.DialTimeout("tcp", net.JoinHostPort(host, port), time.Second); err != nil {
t.Fatal("unable to connect to pubsub emulator, start it with docker compose up -d")
func getPubsubServerAndClient(ctx context.Context, projectID string, topics ...string) (*pstest.Server, *pubsub.Client, func()) {
srv := pstest.NewServer()
client, _ := pubsub.NewClient(
ctx,
projectID,
option.WithEndpoint(srv.Addr),
option.WithoutAuthentication(),
option.WithGRPCDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
)

for _, topic := range topics {
_, _ = client.CreateTopic(ctx, topic)
}

return pubsub.NewClient(ctx, "some-id")
return srv, client, func() {
_ = srv.Close()
_ = client.Close()
}
}

0 comments on commit aac1e4b

Please sign in to comment.