fix integration test

This commit is contained in:
crapStone 2023-11-19 22:54:42 +01:00
parent f79d7501a4
commit 2238a57801
No known key found for this signature in database
GPG key ID: D74B82E7CDD863FE
4 changed files with 18 additions and 6 deletions

View file

@ -1,6 +1,7 @@
package config package config
import ( import (
"context"
"os" "os"
"testing" "testing"
@ -15,10 +16,13 @@ func runApp(t *testing.T, fn func(*cli.Context) error, args []string) {
app := cmd.CreatePagesApp() app := cmd.CreatePagesApp()
app.Action = fn app.Action = fn
appCtx, appCancel := context.WithCancel(context.Background())
defer appCancel()
// os.Args always contains the binary name // os.Args always contains the binary name
args = append([]string{"testing"}, args...) args = append([]string{"testing"}, args...)
err := app.Run(args) err := app.RunContext(appCtx, args)
assert.NoError(t, err) assert.NoError(t, err)
} }

View file

@ -33,10 +33,7 @@ func TestMain(m *testing.M) {
} }
func startServer(ctx context.Context) error { func startServer(ctx context.Context) error {
args := []string{ args := []string{"integration"}
"--verbose",
"--acme-accept-terms", "true",
}
setEnvIfNotSet("ACME_API", "https://acme.mock.directory") setEnvIfNotSet("ACME_API", "https://acme.mock.directory")
setEnvIfNotSet("PAGES_DOMAIN", "localhost.mock.directory") setEnvIfNotSet("PAGES_DOMAIN", "localhost.mock.directory")
setEnvIfNotSet("RAW_DOMAIN", "raw.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("ENABLE_HTTP_SERVER", "true")
setEnvIfNotSet("DB_TYPE", "sqlite3") setEnvIfNotSet("DB_TYPE", "sqlite3")
setEnvIfNotSet("GITEA_ROOT", "https://codeberg.org") 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 := cli.NewApp()
app.Name = "pages-server" app.Name = "pages-server"

View file

@ -21,15 +21,21 @@ func setupAcmeConfig(cfg config.ACMEConfig) (*lego.Config, error) {
var myAcmeAccount AcmeAccount var myAcmeAccount AcmeAccount
var myAcmeConfig *lego.Config 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 { if account, err := os.ReadFile(cfg.AccountConfigFile); err == nil {
log.Info().Msgf("found existing acme account config file '%s'", cfg.AccountConfigFile) log.Info().Msgf("found existing acme account config file '%s'", cfg.AccountConfigFile)
if err := json.Unmarshal(account, &myAcmeAccount); err != nil { if err := json.Unmarshal(account, &myAcmeAccount); err != nil {
return nil, err return nil, err
} }
myAcmeAccount.Key, err = certcrypto.ParsePEMPrivateKey([]byte(myAcmeAccount.KeyPEM)) myAcmeAccount.Key, err = certcrypto.ParsePEMPrivateKey([]byte(myAcmeAccount.KeyPEM))
if err != nil { if err != nil {
return nil, err return nil, err
} }
myAcmeConfig = lego.NewConfig(&myAcmeAccount) myAcmeConfig = lego.NewConfig(&myAcmeAccount)
myAcmeConfig.CADirURL = cfg.APIEndpoint myAcmeConfig.CADirURL = cfg.APIEndpoint
myAcmeConfig.Certificate.KeyType = certcrypto.RSA2048 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") 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 nil, fmt.Errorf("acme config validation failed: %w", err)
} }
return myAcmeConfig, nil return myAcmeConfig, nil
} else if !os.IsNotExist(err) { } else if !os.IsNotExist(err) {
return nil, err return nil, err

View file

@ -122,7 +122,7 @@ func Serve(ctx *cli.Context) error {
log.Info().Msgf("Start HTTP server listening on %s", listeningHTTPAddress) log.Info().Msgf("Start HTTP server listening on %s", listeningHTTPAddress)
err := http.ListenAndServe(listeningHTTPAddress, httpHandler) err := http.ListenAndServe(listeningHTTPAddress, httpHandler)
if err != nil { if err != nil {
log.Panic().Err(err).Msg("Couldn't start HTTP server") log.Error().Err(err).Msg("Couldn't start HTTP server")
} }
}() }()
} }