From 2238a578010c6630d90a2c442d33491efa6b41e0 Mon Sep 17 00:00:00 2001 From: crapStone Date: Sun, 19 Nov 2023 22:54:42 +0100 Subject: [PATCH] fix integration test --- config/setup_test.go | 6 +++++- integration/main_test.go | 9 +++++---- server/certificates/acme_config.go | 7 +++++++ server/startup.go | 2 +- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/config/setup_test.go b/config/setup_test.go index b04598f..60df384 100644 --- a/config/setup_test.go +++ b/config/setup_test.go @@ -1,6 +1,7 @@ package config import ( + "context" "os" "testing" @@ -15,10 +16,13 @@ func runApp(t *testing.T, fn func(*cli.Context) error, args []string) { app := cmd.CreatePagesApp() app.Action = fn + appCtx, appCancel := context.WithCancel(context.Background()) + defer appCancel() + // os.Args always contains the binary name args = append([]string{"testing"}, args...) - err := app.Run(args) + err := app.RunContext(appCtx, args) assert.NoError(t, err) } diff --git a/integration/main_test.go b/integration/main_test.go index 6d2aaf4..86fd9d3 100644 --- a/integration/main_test.go +++ b/integration/main_test.go @@ -33,10 +33,7 @@ func TestMain(m *testing.M) { } func startServer(ctx context.Context) error { - args := []string{ - "--verbose", - "--acme-accept-terms", "true", - } + args := []string{"integration"} setEnvIfNotSet("ACME_API", "https://acme.mock.directory") setEnvIfNotSet("PAGES_DOMAIN", "localhost.mock.directory") setEnvIfNotSet("RAW_DOMAIN", "raw.localhost.mock.directory") @@ -46,6 +43,10 @@ func startServer(ctx context.Context) error { setEnvIfNotSet("ENABLE_HTTP_SERVER", "true") setEnvIfNotSet("DB_TYPE", "sqlite3") setEnvIfNotSet("GITEA_ROOT", "https://codeberg.org") + setEnvIfNotSet("LOG_LEVEL", "trace") + setEnvIfNotSet("ENABLE_LFS_SUPPORT", "true") + setEnvIfNotSet("ENABLE_SYMLINK_SUPPORT", "true") + setEnvIfNotSet("ACME_ACCOUNT_CONFIG", "integration/acme-account.json") app := cli.NewApp() app.Name = "pages-server" diff --git a/server/certificates/acme_config.go b/server/certificates/acme_config.go index b96dca3..2b5151d 100644 --- a/server/certificates/acme_config.go +++ b/server/certificates/acme_config.go @@ -21,15 +21,21 @@ func setupAcmeConfig(cfg config.ACMEConfig) (*lego.Config, error) { var myAcmeAccount AcmeAccount var myAcmeConfig *lego.Config + if cfg.AccountConfigFile == "" { + return nil, fmt.Errorf("invalid acme config file: '%s'", cfg.AccountConfigFile) + } + if account, err := os.ReadFile(cfg.AccountConfigFile); err == nil { log.Info().Msgf("found existing acme account config file '%s'", cfg.AccountConfigFile) if err := json.Unmarshal(account, &myAcmeAccount); err != nil { return nil, err } + myAcmeAccount.Key, err = certcrypto.ParsePEMPrivateKey([]byte(myAcmeAccount.KeyPEM)) if err != nil { return nil, err } + myAcmeConfig = lego.NewConfig(&myAcmeAccount) myAcmeConfig.CADirURL = cfg.APIEndpoint myAcmeConfig.Certificate.KeyType = certcrypto.RSA2048 @@ -40,6 +46,7 @@ func setupAcmeConfig(cfg config.ACMEConfig) (*lego.Config, error) { log.Info().Err(err).Msg("config validation failed, you might just delete the config file and let it recreate") return nil, fmt.Errorf("acme config validation failed: %w", err) } + return myAcmeConfig, nil } else if !os.IsNotExist(err) { return nil, err diff --git a/server/startup.go b/server/startup.go index 9036535..d6c4a55 100644 --- a/server/startup.go +++ b/server/startup.go @@ -122,7 +122,7 @@ func Serve(ctx *cli.Context) error { log.Info().Msgf("Start HTTP server listening on %s", listeningHTTPAddress) err := http.ListenAndServe(listeningHTTPAddress, httpHandler) if err != nil { - log.Panic().Err(err).Msg("Couldn't start HTTP server") + log.Error().Err(err).Msg("Couldn't start HTTP server") } }() }