Skip to content

Commit

Permalink
intergration test nextdns
Browse files Browse the repository at this point in the history
  • Loading branch information
nielscil committed Jan 6, 2025
1 parent f870c78 commit f7a5ce5
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 2 deletions.
7 changes: 5 additions & 2 deletions hscontrol/mapper/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,11 @@ func addNextDNSMetadata(resolvers []*dnstype.Resolver, node *types.Node, nodeAtt
}

attrs := url.Values{
"device_name": []string{node.Hostname},
"device_model": []string{node.Hostinfo.OS},
"device_name": []string{node.Hostname},
}

if node.Hostinfo != nil {
attrs.Add("device_model", node.Hostinfo.OS)
}

if len(node.IPs()) > 0 {
Expand Down
148 changes: 148 additions & 0 deletions integration/nodeAttrs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package integration

import (
"regexp"
"testing"

"github.com/juanfont/headscale/hscontrol/policy"
"github.com/juanfont/headscale/integration/hsic"
"github.com/juanfont/headscale/integration/tsic"
"github.com/stretchr/testify/require"
)

func TestNodeAttrsNextDNS(t *testing.T) {
IntegrationSkip(t)

tests := []struct {
name string
policy policy.ACLPolicy
wantedResolverRegex map[string]string
}{
{
name: "NextDNS attribute for all",
policy: policy.ACLPolicy{
ACLs: []policy.ACL{
{
Action: "accept",
Sources: []string{"*"},
Destinations: []string{"*:*"},
},
},
NodeAttributes: []policy.NodeAttributes{
{
Targets: []string{"*"},
Attributes: []string{"nextdns:fedcba"},
},
},
},
wantedResolverRegex: map[string]string{
"user1": "https://dns\\.nextdns\\.io/fedcba\\?device_ip=.*?\\&device_model=.*?&device_name=.*",
"user2": "https://dns\\.nextdns\\.io/fedcba\\?device_ip=.*?\\&device_model=.*?&device_name=.*",
},
},
{
name: "NextDNS attribute for user 1",
policy: policy.ACLPolicy{
ACLs: []policy.ACL{
{
Action: "accept",
Sources: []string{"*"},
Destinations: []string{"*:*"},
},
},
NodeAttributes: []policy.NodeAttributes{
{
Targets: []string{"user1"},
Attributes: []string{"nextdns:fedcba"},
},
},
},
wantedResolverRegex: map[string]string{
"user1": "https://dns\\.nextdns\\.io/fedcba\\?device_ip=.*?\\&device_model=.*?&device_name=.*",
"user2": "https://dns\\.nextdns\\.io/abcdef\\?device_ip=.*?\\&device_model=.*?&device_name=.*",
},
},
{
name: "NextDNS attribute for no deviceInfo",
policy: policy.ACLPolicy{
ACLs: []policy.ACL{
{
Action: "accept",
Sources: []string{"*"},
Destinations: []string{"*:*"},
},
},
NodeAttributes: []policy.NodeAttributes{
{
Targets: []string{"*"},
Attributes: []string{"nextdns:no-device-info"},
},
},
},
wantedResolverRegex: map[string]string{
"user1": "https://dns\\.nextdns\\.io/abcdef",
"user2": "https://dns\\.nextdns\\.io/abcdef",
},
},
}

spec := map[string]int{
"user1": 2,
"user2": 2,
}

for _, testcase := range tests {
t.Run(testcase.name, func(t *testing.T) {
scenario, err := NewScenario(dockertestMaxWait())
require.NoError(t, err)

scenario.CreateHeadscaleEnv(spec,
[]tsic.Option{
tsic.WithSSH(),

// Alpine containers dont have ip6tables set up, which causes
// tailscaled to stop configuring the wgengine, causing it
// to not configure DNS.
tsic.WithNetfilter("off"),
tsic.WithDockerEntrypoint([]string{
"/bin/sh",
"-c",
"/bin/sleep 3 ; apk add openssh ; adduser ssh-it-user ; update-ca-certificates ; tailscaled --tun=tsdev",
}),
tsic.WithDockerWorkdir("/"),
},
hsic.WithACLPolicy(&testcase.policy),
hsic.WithConfigEnv(map[string]string{
"HEADSCALE_DNS_NAMESERVERS_GLOBAL": "https://dns.nextdns.io/abcdef",
}),
)

require.NoError(t, err)
defer scenario.ShutdownAssertNoPanics(t)

for user, expectedResolver := range testcase.wantedResolverRegex {

expr, err := regexp.Compile(expectedResolver)
require.NoError(t, err)

clients, err := scenario.ListTailscaleClients(user)
require.NoError(t, err)

for _, client := range clients {

output, _, err := client.Execute([]string{
"tailscale",
"dns",
"status",
})

require.NoError(t, err)

if !expr.MatchString(output) {
t.Logf("unexpected resolver expected: '%s', actual: '%s'", expectedResolver, output)
}
}
}
})
}
}

0 comments on commit f7a5ce5

Please sign in to comment.