chore: test server for direct resource access

This commit is contained in:
Tim Möhlmann
2023-04-24 20:40:31 +03:00
parent 2a79e77c7b
commit e167365548
7 changed files with 100 additions and 19 deletions

View File

@@ -5,7 +5,9 @@ import (
_ "embed"
"errors"
"io"
"os"
"strings"
"sync"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@@ -26,7 +28,7 @@ var (
defaultConfig []byte
)
func New(out io.Writer, in io.Reader, args []string) *cobra.Command {
func New(out io.Writer, in io.Reader, args []string, server chan<- *start.Server) *cobra.Command {
cmd := &cobra.Command{
Use: "zitadel",
Short: "The ZITADEL CLI lets you interact with ZITADEL",
@@ -51,9 +53,9 @@ func New(out io.Writer, in io.Reader, args []string) *cobra.Command {
admin.New(), //is now deprecated, remove later on
initialise.New(),
setup.New(),
start.New(),
start.NewStartFromInit(),
start.NewStartFromSetup(),
start.New(server),
start.NewStartFromInit(server),
start.NewStartFromSetup(server),
key.New(),
)
@@ -69,3 +71,30 @@ 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
}