Files
tailscale-custom/cmd/tailscale/cli/dns_test.go
T
Kristoffer Dalby d82e478dbc cli: --json for tailscale dns status|query
This commit adds `--json` output mode to dns debug commands.

It defines structs for the data that is returned from:
`tailscale dns status` and `tailscale dns query <DOMAIN>` and
populates that as it runs the diagnostics.

When all the information is collected, it is serialised to JSON
or string built into an output and returned to the user.

The structs are defined and exported to golang consumers of this command
can use them for unmarshalling.

Updates #13326

Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
2026-03-05 05:31:41 -08:00

66 lines
1.6 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
package cli
import (
"context"
"strings"
"testing"
)
func TestRunDNSQueryArgs(t *testing.T) {
tests := []struct {
name string
args []string
wantErr string
}{
{
name: "no_args",
args: []string{},
wantErr: "missing required argument: name",
},
{
name: "flag_after_name",
args: []string{"example.com", "--json"},
wantErr: "unexpected flags after query name: --json",
},
{
name: "flag_after_name_and_type",
args: []string{"example.com", "AAAA", "--json"},
wantErr: "unexpected flags after query name: --json",
},
{
name: "extra_args_after_type",
args: []string{"example.com", "AAAA", "extra"},
wantErr: "unexpected extra arguments: extra",
},
{
name: "multiple_extra_args",
args: []string{"example.com", "AAAA", "extra1", "extra2"},
wantErr: "unexpected extra arguments: extra1 extra2",
},
{
name: "non_flag_then_flag",
args: []string{"example.com", "AAAA", "foo", "--json"},
wantErr: "unexpected flags after query name: --json",
},
{
name: "multiple_misplaced_flags",
args: []string{"example.com", "--json", "--verbose"},
wantErr: "unexpected flags after query name: --json, --verbose",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := runDNSQuery(context.Background(), tt.args)
if err == nil {
t.Fatal("expected error, got nil")
}
if !strings.Contains(err.Error(), tt.wantErr) {
t.Errorf("error = %q, want it to contain %q", err.Error(), tt.wantErr)
}
})
}
}