From 93aca81c1cb20695d010766042a3abbe820364e8 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Thu, 5 Jan 2023 12:44:28 +0100 Subject: [PATCH] Read integration test config from Env This commit sets the Headscale config from env instead of file for integration tests, the main point is to make sure that when we add per test config, it properly replaces the config key and not append it or something similar. Signed-off-by: Kristoffer Dalby --- integration/hsic/config.go | 34 ++++++++++++++++++++++++++++++++++ integration/hsic/hsic.go | 34 +++++++++++++++++++++------------- 2 files changed, 55 insertions(+), 13 deletions(-) diff --git a/integration/hsic/config.go b/integration/hsic/config.go index 456ea478..00c1770c 100644 --- a/integration/hsic/config.go +++ b/integration/hsic/config.go @@ -60,6 +60,8 @@ package hsic // } // TODO: Reuse the actual configuration object above. +// Deprecated: use env function instead as it is easier to +// override. func DefaultConfigYAML() string { yaml := ` log: @@ -95,3 +97,35 @@ derp: return yaml } + +func MinimumConfigYAML() string { + return ` +private_key_path: /tmp/private.key +noise: + private_key_path: /tmp/noise_private.key +` +} + +func DefaultConfigEnv() map[string]string { + return map[string]string{ + "HEADSCALE_LOG_LEVEL": "trace", + "HEADSCALE_ACL_POLICY_PATH": "", + "HEADSCALE_DB_TYPE": "sqlite3", + "HEADSCALE_DB_PATH": "/tmp/integration_test_db.sqlite3", + "HEADSCALE_EPHEMERAL_NODE_INACTIVITY_TIMEOUT": "30m", + "HEADSCALE_NODE_UPDATE_CHECK_INTERVAL": "10s", + "HEADSCALE_IP_PREFIXES": "fd7a:115c:a1e0::/48 100.64.0.0/10", + "HEADSCALE_DNS_CONFIG_BASE_DOMAIN": "headscale.net", + "HEADSCALE_DNS_CONFIG_MAGIC_DNS": "true", + "HEADSCALE_DNS_CONFIG_DOMAINS": "", + "HEADSCALE_DNS_CONFIG_NAMESERVERS": "127.0.0.11 1.1.1.1", + "HEADSCALE_PRIVATE_KEY_PATH": "/tmp/private.key", + "HEADSCALE_NOISE_PRIVATE_KEY_PATH": "/tmp/noise_private.key", + "HEADSCALE_LISTEN_ADDR": "0.0.0.0:8080", + "HEADSCALE_METRICS_LISTEN_ADDR": "127.0.0.1:9090", + "HEADSCALE_SERVER_URL": "http://headscale:8080", + "HEADSCALE_DERP_URLS": "https://controlplane.tailscale.com/derpmap/default", + "HEADSCALE_DERP_AUTO_UPDATE_ENABLED": "false", + "HEADSCALE_DERP_UPDATE_FREQUENCY": "1m", + } +} diff --git a/integration/hsic/hsic.go b/integration/hsic/hsic.go index 9f74bb2f..b598c758 100644 --- a/integration/hsic/hsic.go +++ b/integration/hsic/hsic.go @@ -17,6 +17,7 @@ import ( "net/http" "time" + "github.com/davecgh/go-spew/spew" "github.com/juanfont/headscale" v1 "github.com/juanfont/headscale/gen/go/headscale/v1" "github.com/juanfont/headscale/integration/dockertestutil" @@ -45,7 +46,7 @@ type HeadscaleInContainer struct { // optional config port int aclPolicy *headscale.ACLPolicy - env []string + env map[string]string tlsCert []byte tlsKey []byte } @@ -55,7 +56,7 @@ type Option = func(c *HeadscaleInContainer) func WithACLPolicy(acl *headscale.ACLPolicy) Option { return func(hsic *HeadscaleInContainer) { // TODO(kradalby): Move somewhere appropriate - hsic.env = append(hsic.env, fmt.Sprintf("HEADSCALE_ACL_POLICY_PATH=%s", aclPolicyPath)) + hsic.env["HEADSCALE_ACL_POLICY_PATH"] = aclPolicyPath hsic.aclPolicy = acl } @@ -69,8 +70,8 @@ func WithTLS() Option { } // TODO(kradalby): Move somewhere appropriate - hsic.env = append(hsic.env, fmt.Sprintf("HEADSCALE_TLS_CERT_PATH=%s", tlsCertPath)) - hsic.env = append(hsic.env, fmt.Sprintf("HEADSCALE_TLS_KEY_PATH=%s", tlsKeyPath)) + hsic.env["HEADSCALE_TLS_CERT_PATH"] = tlsCertPath + hsic.env["HEADSCALE_TLS_KEY_PATH"] = tlsKeyPath hsic.tlsCert = cert hsic.tlsKey = key @@ -80,7 +81,7 @@ func WithTLS() Option { func WithConfigEnv(configEnv map[string]string) Option { return func(hsic *HeadscaleInContainer) { for key, value := range configEnv { - hsic.env = append(hsic.env, fmt.Sprintf("%s=%s", key, value)) + hsic.env[key] = value } } } @@ -102,12 +103,10 @@ func WithTestName(testName string) Option { func WithHostnameAsServerURL() Option { return func(hsic *HeadscaleInContainer) { - hsic.env = append( - hsic.env, - fmt.Sprintf("HEADSCALE_SERVER_URL=http://%s:%d", - hsic.GetHostname(), - hsic.port, - )) + hsic.env["HEADSCALE_SERVER_URL"] = fmt.Sprintf("http://%s", + net.JoinHostPort(hsic.GetHostname(), + fmt.Sprintf("%d", hsic.port)), + ) } } @@ -129,6 +128,8 @@ func New( pool: pool, network: network, + + env: DefaultConfigEnv(), } for _, opt := range opts { @@ -144,6 +145,13 @@ func New( ContextDir: dockerContextPath, } + env := []string{} + for key, value := range hsic.env { + env = append(env, fmt.Sprintf("%s=%s", key, value)) + } + + log.Printf("ENV: \n%s", spew.Sdump(hsic.env)) + runOptions := &dockertest.RunOptions{ Name: hsic.hostname, ExposedPorts: []string{portProto}, @@ -152,7 +160,7 @@ func New( // TODO(kradalby): Get rid of this hack, we currently need to give us some // to inject the headscale configuration further down. Entrypoint: []string{"/bin/bash", "-c", "/bin/sleep 3 ; headscale serve"}, - Env: hsic.env, + Env: env, } // dockertest isnt very good at handling containers that has already @@ -177,7 +185,7 @@ func New( hsic.container = container - err = hsic.WriteFile("/etc/headscale/config.yaml", []byte(DefaultConfigYAML())) + err = hsic.WriteFile("/etc/headscale/config.yaml", []byte(MinimumConfigYAML())) if err != nil { return nil, fmt.Errorf("failed to write headscale config to container: %w", err) }