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 <kristoffer@tailscale.com>
This commit is contained in:
Kristoffer Dalby 2023-01-05 12:44:28 +01:00 committed by Kristoffer Dalby
parent 81254cdf7a
commit 93aca81c1c
2 changed files with 55 additions and 13 deletions

View File

@ -60,6 +60,8 @@ package hsic
// } // }
// TODO: Reuse the actual configuration object above. // TODO: Reuse the actual configuration object above.
// Deprecated: use env function instead as it is easier to
// override.
func DefaultConfigYAML() string { func DefaultConfigYAML() string {
yaml := ` yaml := `
log: log:
@ -95,3 +97,35 @@ derp:
return yaml 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",
}
}

View File

@ -17,6 +17,7 @@ import (
"net/http" "net/http"
"time" "time"
"github.com/davecgh/go-spew/spew"
"github.com/juanfont/headscale" "github.com/juanfont/headscale"
v1 "github.com/juanfont/headscale/gen/go/headscale/v1" v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
"github.com/juanfont/headscale/integration/dockertestutil" "github.com/juanfont/headscale/integration/dockertestutil"
@ -45,7 +46,7 @@ type HeadscaleInContainer struct {
// optional config // optional config
port int port int
aclPolicy *headscale.ACLPolicy aclPolicy *headscale.ACLPolicy
env []string env map[string]string
tlsCert []byte tlsCert []byte
tlsKey []byte tlsKey []byte
} }
@ -55,7 +56,7 @@ type Option = func(c *HeadscaleInContainer)
func WithACLPolicy(acl *headscale.ACLPolicy) Option { func WithACLPolicy(acl *headscale.ACLPolicy) Option {
return func(hsic *HeadscaleInContainer) { return func(hsic *HeadscaleInContainer) {
// TODO(kradalby): Move somewhere appropriate // 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 hsic.aclPolicy = acl
} }
@ -69,8 +70,8 @@ func WithTLS() Option {
} }
// TODO(kradalby): Move somewhere appropriate // TODO(kradalby): Move somewhere appropriate
hsic.env = append(hsic.env, fmt.Sprintf("HEADSCALE_TLS_CERT_PATH=%s", tlsCertPath)) hsic.env["HEADSCALE_TLS_CERT_PATH"] = tlsCertPath
hsic.env = append(hsic.env, fmt.Sprintf("HEADSCALE_TLS_KEY_PATH=%s", tlsKeyPath)) hsic.env["HEADSCALE_TLS_KEY_PATH"] = tlsKeyPath
hsic.tlsCert = cert hsic.tlsCert = cert
hsic.tlsKey = key hsic.tlsKey = key
@ -80,7 +81,7 @@ func WithTLS() Option {
func WithConfigEnv(configEnv map[string]string) Option { func WithConfigEnv(configEnv map[string]string) Option {
return func(hsic *HeadscaleInContainer) { return func(hsic *HeadscaleInContainer) {
for key, value := range configEnv { 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 { func WithHostnameAsServerURL() Option {
return func(hsic *HeadscaleInContainer) { return func(hsic *HeadscaleInContainer) {
hsic.env = append( hsic.env["HEADSCALE_SERVER_URL"] = fmt.Sprintf("http://%s",
hsic.env, net.JoinHostPort(hsic.GetHostname(),
fmt.Sprintf("HEADSCALE_SERVER_URL=http://%s:%d", fmt.Sprintf("%d", hsic.port)),
hsic.GetHostname(), )
hsic.port,
))
} }
} }
@ -129,6 +128,8 @@ func New(
pool: pool, pool: pool,
network: network, network: network,
env: DefaultConfigEnv(),
} }
for _, opt := range opts { for _, opt := range opts {
@ -144,6 +145,13 @@ func New(
ContextDir: dockerContextPath, 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{ runOptions := &dockertest.RunOptions{
Name: hsic.hostname, Name: hsic.hostname,
ExposedPorts: []string{portProto}, ExposedPorts: []string{portProto},
@ -152,7 +160,7 @@ func New(
// TODO(kradalby): Get rid of this hack, we currently need to give us some // TODO(kradalby): Get rid of this hack, we currently need to give us some
// to inject the headscale configuration further down. // to inject the headscale configuration further down.
Entrypoint: []string{"/bin/bash", "-c", "/bin/sleep 3 ; headscale serve"}, 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 // dockertest isnt very good at handling containers that has already
@ -177,7 +185,7 @@ func New(
hsic.container = container hsic.container = container
err = hsic.WriteFile("/etc/headscale/config.yaml", []byte(DefaultConfigYAML())) err = hsic.WriteFile("/etc/headscale/config.yaml", []byte(MinimumConfigYAML()))
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to write headscale config to container: %w", err) return nil, fmt.Errorf("failed to write headscale config to container: %w", err)
} }