Skip to content

Commit

Permalink
poll on test start
Browse files Browse the repository at this point in the history
  • Loading branch information
muhlemmer committed Apr 26, 2023
1 parent 234186c commit 90ba3a8
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 16 deletions.
11 changes: 7 additions & 4 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ jobs:
runs-on: ubuntu-20.04
env:
DOCKER_BUILDKIT: 1
INTEGRATION_DB_FLAVOR: ${{ matrix.db }}
steps:
- name: Set up Go
uses: actions/setup-go@v3
Expand All @@ -28,8 +29,10 @@ jobs:
- name: Download Go modules
run: go mod download
- name: Start ${{ matrix.db }} database
run: docker compose -f e2e/config/integration/docker-compose.yaml up --wait ${{ matrix.db }}
- name: Run integration test
env:
INTEGRATION_DB_FLAVOR: ${{ matrix.db }}
run: docker compose -f e2e/config/integration/docker-compose.yaml up --wait ${INTEGRATION_DB_FLAVOR}
- name: Run zitadel init and setup
run: |
go run main.go init --config internal/integration/config/zitadel.yaml --config internal/integration/config/${INTEGRATION_DB_FLAVOR}.yaml
go run main.go setup --masterkey MasterkeyNeedsToHave32Characters --config internal/integration/config/zitadel.yaml --config internal/integration/config/${INTEGRATION_DB_FLAVOR}.yaml
- name: Run integration tests
run: go test -tags=integration -parallel 1 -v ./internal/integration ./internal/api/grpc/...
2 changes: 0 additions & 2 deletions cmd/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ Requirements:
}

type Server struct {
background context.Context
Config *Config
DB *database.DB
KeyStorage crypto.KeyStorage
Expand Down Expand Up @@ -203,7 +202,6 @@ func startZitadel(config *Config, masterKey string, server chan<- *Server) error

if server != nil {
server <- &Server{
background: ctx,
Config: config,
DB: dbClient,
KeyStorage: keyStorage,
Expand Down
3 changes: 1 addition & 2 deletions internal/api/grpc/admin/information_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ func TestMain(m *testing.M) {
os.Exit(func() int {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

Tester = integration.NewTester(ctx)
defer Tester.Done()

Expand All @@ -31,7 +30,7 @@ func TestMain(m *testing.M) {
}

func TestServer_Healthz(t *testing.T) {
client := admin.NewAdminServiceClient(Tester.ClientConn)
client := admin.NewAdminServiceClient(Tester.GRPCClientConn)
_, err := client.Healthz(context.TODO(), &admin.HealthzRequest{})
require.NoError(t, err)
}
45 changes: 38 additions & 7 deletions internal/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"strings"
"sync"
"time"

"github.com/spf13/viper"
"github.com/zitadel/logging"
Expand All @@ -17,6 +18,7 @@ import (

"github.com/zitadel/zitadel/cmd"
"github.com/zitadel/zitadel/cmd/start"
"github.com/zitadel/zitadel/pkg/grpc/admin"
)

var (
Expand All @@ -30,12 +32,11 @@ var (

type Tester struct {
*start.Server
ClientConn *grpc.ClientConn

wg sync.WaitGroup // used for shutdown
GRPCClientConn *grpc.ClientConn
wg sync.WaitGroup // used for shutdown
}

const commandLine = `start-from-init --masterkey MasterkeyNeedsToHave32Characters --tlsMode disabled`
const commandLine = `start --masterkey MasterkeyNeedsToHave32Characters`

func (s *Tester) createClientConn(ctx context.Context) {
target := fmt.Sprintf("localhost:%d", s.Config.Port)
Expand All @@ -49,11 +50,41 @@ func (s *Tester) createClientConn(ctx context.Context) {
logging.OnError(err).Fatal("integration tester client dial")
logging.New().WithField("target", target).Info("finished dialing grpc client conn")

s.ClientConn = cc
s.GRPCClientConn = cc
err = s.pollHealth(ctx)
logging.OnError(err).Fatal("integration tester health")
}

// pollHealth waits until a healthy status is reported.
// TODO: remove when we make the setup blocking on all
// projections completed.
func (s *Tester) pollHealth(ctx context.Context) (err error) {
client := admin.NewAdminServiceClient(s.GRPCClientConn)

for {
err = func(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

_, err := client.Healthz(ctx, &admin.HealthzRequest{})
return err
}(ctx)
if err == nil {
return nil
}
logging.WithError(err).Info("poll healthz")

select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(time.Second):
continue
}
}
}

func (s *Tester) Done() {
err := s.ClientConn.Close()
err := s.GRPCClientConn.Close()
logging.OnError(err).Error("integration tester client close")

s.Shutdown <- os.Interrupt
Expand All @@ -71,7 +102,7 @@ func NewTester(ctx context.Context) *Tester {

flavor := os.Getenv("INTEGRATION_DB_FLAVOR")
switch flavor {
case "cockroach":
case "cockroach", "":
err = viper.MergeConfig(bytes.NewBuffer(cockroachYAML))
case "postgres":
err = viper.MergeConfig(bytes.NewBuffer(postgresYAML))
Expand Down
2 changes: 1 addition & 1 deletion internal/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestNewTester(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

s := NewTester(ctx)
Expand Down

0 comments on commit 90ba3a8

Please sign in to comment.