tests: use tailcfg.DERPMap instead of []byte

This commit is contained in:
ArcticLampyrid 2024-11-15 02:31:36 +08:00
parent c726224e53
commit b6adc84dc6
No known key found for this signature in database
GPG key ID: DC72A2519E77D6CF
3 changed files with 36 additions and 20 deletions

View file

@ -13,6 +13,7 @@ import (
"github.com/juanfont/headscale/integration/hsic" "github.com/juanfont/headscale/integration/hsic"
"github.com/juanfont/headscale/integration/integrationutil" "github.com/juanfont/headscale/integration/integrationutil"
"github.com/juanfont/headscale/integration/tsic" "github.com/juanfont/headscale/integration/tsic"
"tailscale.com/tailcfg"
) )
func TestDERPVerifyEndpoint(t *testing.T) { func TestDERPVerifyEndpoint(t *testing.T) {
@ -44,25 +45,32 @@ func TestDERPVerifyEndpoint(t *testing.T) {
) )
assertNoErr(t, err) assertNoErr(t, err)
derpConfig := "regions:\n" derpMap := tailcfg.DERPMap{
derpConfig += " 900:\n" Regions: map[int]*tailcfg.DERPRegion{
derpConfig += " regionid: 900\n" 900: {
derpConfig += " regioncode: test-derpverify\n" RegionID: 900,
derpConfig += " regionname: TestDerpVerify\n" RegionCode: "test-derpverify",
derpConfig += " nodes:\n" RegionName: "TestDerpVerify",
derpConfig += " - name: TestDerpVerify\n" Nodes: []*tailcfg.DERPNode{
derpConfig += " regionid: 900\n" {
derpConfig += " hostname: " + derper.GetHostname() + "\n" Name: "TestDerpVerify",
derpConfig += " stunport: " + derper.GetSTUNPort() + "\n" RegionID: 900,
derpConfig += " stunonly: false\n" HostName: derper.GetHostname(),
derpConfig += " derpport: " + derper.GetDERPPort() + "\n" STUNPort: derper.GetSTUNPort(),
STUNOnly: false,
DERPPort: derper.GetDERPPort(),
},
},
},
},
}
headscale, err := scenario.Headscale( headscale, err := scenario.Headscale(
hsic.WithHostname(hostname), hsic.WithHostname(hostname),
hsic.WithPort(headscalePort), hsic.WithPort(headscalePort),
hsic.WithCustomTLS(certHeadscale, keyHeadscale), hsic.WithCustomTLS(certHeadscale, keyHeadscale),
hsic.WithHostnameAsServerURL(), hsic.WithHostnameAsServerURL(),
hsic.WithCustomDERPServerOnly([]byte(derpConfig)), hsic.WithDERPConfig(derpMap),
) )
assertNoErrHeadscaleEnv(t, err) assertNoErrHeadscaleEnv(t, err)

View file

@ -267,18 +267,18 @@ func (t *DERPServerInContainer) GetHostname() string {
} }
// GetSTUNPort returns the STUN port of the DERPer instance. // GetSTUNPort returns the STUN port of the DERPer instance.
func (t *DERPServerInContainer) GetSTUNPort() string { func (t *DERPServerInContainer) GetSTUNPort() int {
return strconv.Itoa(t.stunPort) return t.stunPort
} }
// GetDERPPort returns the DERP port of the DERPer instance. // GetDERPPort returns the DERP port of the DERPer instance.
func (t *DERPServerInContainer) GetDERPPort() string { func (t *DERPServerInContainer) GetDERPPort() int {
return strconv.Itoa(t.derpPort) return t.derpPort
} }
// WaitForRunning blocks until the DERPer instance is ready to be used. // WaitForRunning blocks until the DERPer instance is ready to be used.
func (t *DERPServerInContainer) WaitForRunning() error { func (t *DERPServerInContainer) WaitForRunning() error {
url := "https://" + net.JoinHostPort(t.GetHostname(), t.GetDERPPort()) + "/" url := "https://" + net.JoinHostPort(t.GetHostname(), strconv.Itoa(t.GetDERPPort())) + "/"
log.Printf("waiting for DERPer to be ready at %s", url) log.Printf("waiting for DERPer to be ready at %s", url)
insecureTransport := http.DefaultTransport.(*http.Transport).Clone() //nolint insecureTransport := http.DefaultTransport.(*http.Transport).Clone() //nolint

View file

@ -25,6 +25,7 @@ import (
"github.com/juanfont/headscale/integration/integrationutil" "github.com/juanfont/headscale/integration/integrationutil"
"github.com/ory/dockertest/v3" "github.com/ory/dockertest/v3"
"github.com/ory/dockertest/v3/docker" "github.com/ory/dockertest/v3/docker"
"tailscale.com/tailcfg"
) )
const ( const (
@ -216,10 +217,17 @@ func WithEmbeddedDERPServerOnly() Option {
} }
} }
// WithCustomDERPServerOnly configures Headscale use a custom // WithDERPConfig configures Headscale use a custom
// DERP server only. // DERP server only.
func WithCustomDERPServerOnly(contents []byte) Option { func WithDERPConfig(derpMap tailcfg.DERPMap) Option {
return func(hsic *HeadscaleInContainer) { return func(hsic *HeadscaleInContainer) {
contents, err := json.Marshal(derpMap)
if err != nil {
log.Fatalf("failed to marshal DERP map: %s", err)
return
}
hsic.env["HEADSCALE_DERP_PATHS"] = "/etc/headscale/derp.yml" hsic.env["HEADSCALE_DERP_PATHS"] = "/etc/headscale/derp.yml"
hsic.filesInContainer = append(hsic.filesInContainer, hsic.filesInContainer = append(hsic.filesInContainer,
fileInContainer{ fileInContainer{