From a22b58f1c04a7ec7f58c41a5422ce117ea79c819 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20M=C3=B6hlmann?= Date: Tue, 25 Apr 2023 15:04:35 +0300 Subject: [PATCH] simple test of a health endpoint --- cmd/zitadel.go | 29 --------- cmd/zitadel_test.go | 13 ---- internal/api/grpc/admin/admin_test.go | 29 +++++++++ internal/api/grpc/admin/information_test.go | 16 +++++ internal/integration/integration.go | 68 +++++++++++++++++++++ internal/integration/integration_test.go | 18 ++++++ 6 files changed, 131 insertions(+), 42 deletions(-) delete mode 100644 cmd/zitadel_test.go create mode 100644 internal/api/grpc/admin/admin_test.go create mode 100644 internal/api/grpc/admin/information_test.go create mode 100644 internal/integration/integration.go create mode 100644 internal/integration/integration_test.go diff --git a/cmd/zitadel.go b/cmd/zitadel.go index 8de2aaf5b4..0c839f2678 100644 --- a/cmd/zitadel.go +++ b/cmd/zitadel.go @@ -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 -} diff --git a/cmd/zitadel_test.go b/cmd/zitadel_test.go deleted file mode 100644 index 7302469d3d..0000000000 --- a/cmd/zitadel_test.go +++ /dev/null @@ -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() -} diff --git a/internal/api/grpc/admin/admin_test.go b/internal/api/grpc/admin/admin_test.go new file mode 100644 index 0000000000..9ca65d819c --- /dev/null +++ b/internal/api/grpc/admin/admin_test.go @@ -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() + }()) +} diff --git a/internal/api/grpc/admin/information_test.go b/internal/api/grpc/admin/information_test.go new file mode 100644 index 0000000000..e2867805be --- /dev/null +++ b/internal/api/grpc/admin/information_test.go @@ -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) +} diff --git a/internal/integration/integration.go b/internal/integration/integration.go new file mode 100644 index 0000000000..e5a91f786b --- /dev/null +++ b/internal/integration/integration.go @@ -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 +} diff --git a/internal/integration/integration_test.go b/internal/integration/integration_test.go new file mode 100644 index 0000000000..4da09350fb --- /dev/null +++ b/internal/integration/integration_test.go @@ -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() +}