simple test of a health endpoint

This commit is contained in:
Tim Möhlmann 2023-04-25 15:04:35 +03:00
parent e167365548
commit a22b58f1c0
6 changed files with 131 additions and 42 deletions

View File

@ -5,9 +5,7 @@ import (
_ "embed"
"errors"
"io"
"os"
"strings"
"sync"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@ -71,30 +69,3 @@ func initConfig() {
logging.WithFields("file", file).OnError(err).Warn("unable to read config file")
}
}
type TestServer struct {
*start.Server
wg sync.WaitGroup
}
func (s *TestServer) Done() {
s.Shutdown <- os.Interrupt
s.wg.Wait()
}
func NewTestServer(args []string) *TestServer {
testServer := new(TestServer)
server := make(chan *start.Server, 1)
testServer.wg.Add(1)
go func(wg *sync.WaitGroup) {
defer wg.Done()
cmd := New(os.Stdout, os.Stdin, args, server)
cmd.SetArgs(args)
logging.OnError(cmd.Execute()).Fatal()
}(&testServer.wg)
testServer.Server = <-server
return testServer
}

View File

@ -1,13 +0,0 @@
package cmd
import (
"strings"
"testing"
)
const commandLine = `start-from-init --masterkey MasterkeyNeedsToHave32Characters --tlsMode disabled --config ../e2e/config/localhost/zitadel.yaml --steps ../e2e/config/localhost/zitadel.yaml`
func TestNewTestServer(t *testing.T) {
s := NewTestServer(strings.Split(commandLine, " "))
defer s.Done()
}

View File

@ -0,0 +1,29 @@
package admin_test
import (
"context"
"os"
"strings"
"testing"
"time"
"github.com/zitadel/zitadel/internal/integration"
)
const commandLine = `start-from-init --masterkey MasterkeyNeedsToHave32Characters --tlsMode disabled --config ../../e2e/config/localhost/zitadel.yaml --steps ../../e2e/config/localhost/zitadel.yaml`
var (
Tester *integration.Tester
)
func TestMain(m *testing.M) {
os.Exit(func() int {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
Tester = integration.NewTester(ctx, strings.Split(commandLine, " "))
defer Tester.Done()
return m.Run()
}())
}

View File

@ -0,0 +1,16 @@
package admin_test
import (
"context"
"testing"
"github.com/stretchr/testify/require"
"github.com/zitadel/zitadel/pkg/grpc/admin"
)
func TestServer_Healthz(t *testing.T) {
client := admin.NewAdminServiceClient(Tester.ClientConn)
_, err := client.Healthz(context.TODO(), &admin.HealthzRequest{})
require.NoError(t, err)
}

View File

@ -0,0 +1,68 @@
// Package integration provides helpers for integration testing.
package integration
import (
"context"
"fmt"
"os"
"sync"
"github.com/zitadel/logging"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"github.com/zitadel/zitadel/cmd"
"github.com/zitadel/zitadel/cmd/start"
)
type Tester struct {
*start.Server
ClientConn *grpc.ClientConn
wg sync.WaitGroup // used for shutdown
}
func (s *Tester) createClientConn(ctx context.Context) {
target := fmt.Sprintf("localhost:%d", s.Config.Port)
cc, err := grpc.DialContext(ctx, target,
grpc.WithBlock(), grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
s.Shutdown <- os.Interrupt
s.wg.Wait()
}
logging.OnError(err).Fatal("integration tester client dial")
logging.New().WithField("target", target).Info("finished dialing grpc client conn")
s.ClientConn = cc
}
func (s *Tester) Done() {
err := s.ClientConn.Close()
logging.OnError(err).Error("integration tester client close")
s.Shutdown <- os.Interrupt
s.wg.Wait()
}
func NewTester(ctx context.Context, args []string) *Tester {
tester := new(Tester)
sc := make(chan *start.Server)
tester.wg.Add(1)
go func(wg *sync.WaitGroup) {
defer wg.Done()
cmd := cmd.New(os.Stdout, os.Stdin, args, sc)
cmd.SetArgs(args)
logging.OnError(cmd.Execute()).Fatal()
}(&tester.wg)
select {
case tester.Server = <-sc:
case <-ctx.Done():
logging.OnError(ctx.Err()).Fatal("waiting for integration tester server")
}
tester.createClientConn(ctx)
return tester
}

View File

@ -0,0 +1,18 @@
package integration
import (
"context"
"strings"
"testing"
"time"
)
const commandLine = `start-from-init --masterkey MasterkeyNeedsToHave32Characters --tlsMode disabled --config ../../e2e/config/localhost/zitadel.yaml --steps ../../e2e/config/localhost/zitadel.yaml`
func TestNewTester(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
s := NewTester(ctx, strings.Split(commandLine, " "))
defer s.Done()
}