mirror of
https://github.com/juanfont/headscale.git
synced 2025-08-12 01:37:34 +00:00
cmd/hi: fixes and qol (#2649)
This commit is contained in:
@@ -1,44 +1,65 @@
|
||||
package dockertestutil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/ory/dockertest/v3/docker"
|
||||
"github.com/juanfont/headscale/hscontrol/util"
|
||||
"github.com/ory/dockertest/v3"
|
||||
)
|
||||
|
||||
// GetIntegrationRunID returns the run ID for the current integration test session.
|
||||
// This is set by the hi tool and passed through environment variables.
|
||||
func GetIntegrationRunID() string {
|
||||
return os.Getenv("HEADSCALE_INTEGRATION_RUN_ID")
|
||||
}
|
||||
|
||||
// DockerAddIntegrationLabels adds integration test labels to Docker RunOptions.
|
||||
// This allows the hi tool to identify containers belonging to specific test runs.
|
||||
// This function should be called before passing RunOptions to dockertest functions.
|
||||
func DockerAddIntegrationLabels(opts *dockertest.RunOptions, testType string) {
|
||||
runID := GetIntegrationRunID()
|
||||
if runID == "" {
|
||||
panic("HEADSCALE_INTEGRATION_RUN_ID environment variable is required")
|
||||
}
|
||||
|
||||
if opts.Labels == nil {
|
||||
opts.Labels = make(map[string]string)
|
||||
}
|
||||
opts.Labels["hi.run-id"] = runID
|
||||
opts.Labels["hi.test-type"] = testType
|
||||
}
|
||||
|
||||
// GenerateRunID creates a unique run identifier with timestamp and random hash.
|
||||
// Format: YYYYMMDD-HHMMSS-HASH (e.g., 20250619-143052-a1b2c3)
|
||||
func GenerateRunID() string {
|
||||
now := time.Now()
|
||||
timestamp := now.Format("20060102-150405")
|
||||
|
||||
// Add a short random hash to ensure uniqueness
|
||||
randomHash := util.MustGenerateRandomStringDNSSafe(6)
|
||||
return fmt.Sprintf("%s-%s", timestamp, randomHash)
|
||||
}
|
||||
|
||||
// ExtractRunIDFromContainerName extracts the run ID from container name.
|
||||
// Expects format: "prefix-YYYYMMDD-HHMMSS-HASH"
|
||||
func ExtractRunIDFromContainerName(containerName string) string {
|
||||
parts := strings.Split(containerName, "-")
|
||||
if len(parts) >= 3 {
|
||||
// Return the last three parts as the run ID (YYYYMMDD-HHMMSS-HASH)
|
||||
return strings.Join(parts[len(parts)-3:], "-")
|
||||
}
|
||||
|
||||
panic(fmt.Sprintf("unexpected container name format: %s", containerName))
|
||||
}
|
||||
|
||||
// IsRunningInContainer checks if the current process is running inside a Docker container.
|
||||
// This is used by tests to determine if they should run integration tests.
|
||||
func IsRunningInContainer() bool {
|
||||
if _, err := os.Stat("/.dockerenv"); err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func DockerRestartPolicy(config *docker.HostConfig) {
|
||||
// set AutoRemove to true so that stopped container goes away by itself on error *immediately*.
|
||||
// when set to false, containers remain until the end of the integration test.
|
||||
config.AutoRemove = false
|
||||
config.RestartPolicy = docker.RestartPolicy{
|
||||
Name: "no",
|
||||
}
|
||||
}
|
||||
|
||||
func DockerAllowLocalIPv6(config *docker.HostConfig) {
|
||||
if config.Sysctls == nil {
|
||||
config.Sysctls = make(map[string]string, 1)
|
||||
}
|
||||
config.Sysctls["net.ipv6.conf.all.disable_ipv6"] = "0"
|
||||
}
|
||||
|
||||
func DockerAllowNetworkAdministration(config *docker.HostConfig) {
|
||||
// Needed since containerd (1.7.24)
|
||||
// https://github.com/tailscale/tailscale/issues/14256
|
||||
// https://github.com/opencontainers/runc/commit/2ce40b6ad72b4bd4391380cafc5ef1bad1fa0b31
|
||||
config.CapAdd = append(config.CapAdd, "NET_ADMIN")
|
||||
config.CapAdd = append(config.CapAdd, "NET_RAW")
|
||||
config.Devices = append(config.Devices, docker.Device{
|
||||
PathOnHost: "/dev/net/tun",
|
||||
PathInContainer: "/dev/net/tun",
|
||||
CgroupPermissions: "rwm",
|
||||
})
|
||||
}
|
||||
// Check for the common indicator that we're in a container
|
||||
// This could be improved with more robust detection if needed
|
||||
_, err := os.Stat("/.dockerenv")
|
||||
return err == nil
|
||||
}
|
@@ -126,3 +126,24 @@ func CleanImagesInCI(pool *dockertest.Pool) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DockerRestartPolicy sets the restart policy for containers.
|
||||
func DockerRestartPolicy(config *docker.HostConfig) {
|
||||
config.RestartPolicy = docker.RestartPolicy{
|
||||
Name: "unless-stopped",
|
||||
}
|
||||
}
|
||||
|
||||
// DockerAllowLocalIPv6 allows IPv6 traffic within the container.
|
||||
func DockerAllowLocalIPv6(config *docker.HostConfig) {
|
||||
config.NetworkMode = "default"
|
||||
config.Sysctls = map[string]string{
|
||||
"net.ipv6.conf.all.disable_ipv6": "0",
|
||||
}
|
||||
}
|
||||
|
||||
// DockerAllowNetworkAdministration gives the container network administration capabilities.
|
||||
func DockerAllowNetworkAdministration(config *docker.HostConfig) {
|
||||
config.CapAdd = append(config.CapAdd, "NET_ADMIN")
|
||||
config.Privileged = true
|
||||
}
|
||||
|
Reference in New Issue
Block a user