mirror of
https://github.com/juanfont/headscale.git
synced 2026-05-24 02:58:42 +09:00
Every Go-identifier reference in // and /* */ comments now uses
godoc's [Name] linking syntax so pkg.go.dev and `go doc` render
them as clickable cross-references. No behaviour change.
Pattern applied across the tree:
In-package [Foo], [Foo.Bar]
Cross-package [pkg.Foo], [pkg.Foo.Bar]
Stdlib [netip.Prefix], [errors.Is], [context.Context]
Tailscale [tailcfg.MapResponse], [tailcfg.Node.CapMap],
[tailcfg.NodeAttrSuggestExitNode]
Skip rules:
- File:line refs left as plain text
- HuJSON wire keys inside backtick raw strings untouched
- ACL/policy syntax tokens (tag:foo, autogroup:self, ...) not Go
symbols, left as plain text
- JSON/OIDC wire keys, gorm tags, RFC IPv6 placeholders, markdown
link tags, decorative dividers — all left as-is
74 lines
2.4 KiB
Go
74 lines
2.4 KiB
Go
package dockertestutil
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/juanfont/headscale/hscontrol/util"
|
|
"github.com/ory/dockertest/v3"
|
|
)
|
|
|
|
const (
|
|
// TimestampFormatRunID is used for generating unique run identifiers
|
|
// Format: "20060102-150405" provides compact date-time for file/directory names.
|
|
TimestampFormatRunID = "20060102-150405"
|
|
)
|
|
|
|
// 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 [dockertest.RunOptions].
|
|
// This allows the hi tool to identify containers belonging to specific test runs.
|
|
// This function should be called before passing [dockertest.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(TimestampFormatRunID)
|
|
|
|
// 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("unexpected container name format: " + 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 {
|
|
// 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
|
|
}
|