Skip to content

Commit

Permalink
chore: test server for direct resource access
Browse files Browse the repository at this point in the history
  • Loading branch information
muhlemmer committed Apr 24, 2023
1 parent 2a79e77 commit e167365
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 19 deletions.
4 changes: 2 additions & 2 deletions cmd/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func New() *cobra.Command {
adminCMD.AddCommand(
initialise.New(),
setup.New(),
start.New(),
start.NewStartFromInit(),
start.New(nil),
start.NewStartFromInit(nil),
key.New(),
)

Expand Down
55 changes: 47 additions & 8 deletions cmd/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
"github.com/zitadel/zitadel/internal/authz"
authz_repo "github.com/zitadel/zitadel/internal/authz/repository"
"github.com/zitadel/zitadel/internal/command"
"github.com/zitadel/zitadel/internal/crypto"
cryptoDB "github.com/zitadel/zitadel/internal/crypto/database"
"github.com/zitadel/zitadel/internal/database"
"github.com/zitadel/zitadel/internal/eventstore"
Expand All @@ -60,7 +61,7 @@ import (
"github.com/zitadel/zitadel/openapi"
)

func New() *cobra.Command {
func New(server chan<- *Server) *cobra.Command {
start := &cobra.Command{
Use: "start",
Short: "starts ZITADEL instance",
Expand All @@ -78,7 +79,7 @@ Requirements:
return err
}

return startZitadel(config, masterKey)
return startZitadel(config, masterKey, server)
},
}

Expand All @@ -87,7 +88,24 @@ Requirements:
return start
}

func startZitadel(config *Config, masterKey string) error {
type Server struct {
background context.Context
Config *Config
DB *database.DB
KeyStorage crypto.KeyStorage
Keys *encryptionKeys
Eventstore *eventstore.Eventstore
Queries *query.Queries
AuthzRepo authz_repo.Repository
Storage static.Storage
Commands *command.Commands
LogStore *logstore.Service
Router *mux.Router
TLSConfig *tls.Config
Shutdown chan<- os.Signal
}

func startZitadel(config *Config, masterKey string, server chan<- *Server) error {
ctx := context.Background()

dbClient, err := database.Connect(config.Database, false)
Expand Down Expand Up @@ -179,7 +197,31 @@ func startZitadel(config *Config, masterKey string) error {
if err != nil {
return err
}
return listen(ctx, router, config.Port, tlsConfig)

shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, os.Interrupt, syscall.SIGTERM)

if server != nil {
server <- &Server{
background: ctx,
Config: config,
DB: dbClient,
KeyStorage: keyStorage,
Keys: keys,
Eventstore: eventstoreClient,
Queries: queries,
AuthzRepo: authZRepo,
Storage: storage,
Commands: commands,
LogStore: actionsLogstoreSvc,
Router: router,
TLSConfig: tlsConfig,
Shutdown: shutdown,
}
close(server)
}

return listen(ctx, router, config.Port, tlsConfig, shutdown)
}

func startAPIs(
Expand Down Expand Up @@ -300,7 +342,7 @@ func startAPIs(
return nil
}

func listen(ctx context.Context, router *mux.Router, port uint16, tlsConfig *tls.Config) error {
func listen(ctx context.Context, router *mux.Router, port uint16, tlsConfig *tls.Config, shutdown <-chan os.Signal) error {
http2Server := &http2.Server{}
http1Server := &http.Server{Handler: h2c.NewHandler(router, http2Server), TLSConfig: tlsConfig}
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
Expand All @@ -320,9 +362,6 @@ func listen(ctx context.Context, router *mux.Router, port uint16, tlsConfig *tls
}
}()

shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, os.Interrupt, syscall.SIGTERM)

select {
case err := <-errCh:
return fmt.Errorf("error starting server: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions cmd/start/start_from_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/zitadel/zitadel/cmd/tls"
)

func NewStartFromInit() *cobra.Command {
func NewStartFromInit(server chan<- *Server) *cobra.Command {
cmd := &cobra.Command{
Use: "start-from-init",
Short: "cold starts zitadel",
Expand All @@ -37,7 +37,7 @@ Requirements:

startConfig := MustNewConfig(viper.GetViper())

err = startZitadel(startConfig, masterKey)
err = startZitadel(startConfig, masterKey, server)
logging.OnError(err).Fatal("unable to start zitadel")
},
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/start/start_from_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/zitadel/zitadel/cmd/tls"
)

func NewStartFromSetup() *cobra.Command {
func NewStartFromSetup(server chan<- *Server) *cobra.Command {
cmd := &cobra.Command{
Use: "start-from-setup",
Short: "cold starts zitadel",
Expand All @@ -35,7 +35,7 @@ Requirements:

startConfig := MustNewConfig(viper.GetViper())

err = startZitadel(startConfig, masterKey)
err = startZitadel(startConfig, masterKey, server)
logging.OnError(err).Fatal("unable to start zitadel")
},
}
Expand Down
37 changes: 33 additions & 4 deletions cmd/zitadel.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
_ "embed"
"errors"
"io"
"os"
"strings"
"sync"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -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",
Expand All @@ -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(),
)

Expand All @@ -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
}
13 changes: 13 additions & 0 deletions cmd/zitadel_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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()
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ import (

func main() {
args := os.Args[1:]
rootCmd := cmd.New(os.Stdout, os.Stdin /*, int(os.Stdin.Fd())*/, args)
rootCmd := cmd.New(os.Stdout, os.Stdin, args, nil)
cobra.CheckErr(rootCmd.Execute())
}

0 comments on commit e167365

Please sign in to comment.