Skip to content

Commit

Permalink
cmd/tailscale: pull out, parameterize up FlagSet creation for tests
Browse files Browse the repository at this point in the history
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
  • Loading branch information
bradfitz committed May 3, 2021
1 parent 98d7c28 commit fb67d83
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 21 deletions.
43 changes: 30 additions & 13 deletions cmd/tailscale/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func TestCheckForAccidentalSettingReverts(t *testing.T) {
flagSet: f("authkey"),
curPrefs: ipn.NewPrefs(),
mp: &ipn.MaskedPrefs{
Prefs: *defaultPrefsFromUpArgs(t),
Prefs: *defaultPrefsFromUpArgs(t, "linux"),
WantRunningSet: true,
},
want: "",
Expand Down Expand Up @@ -382,13 +382,8 @@ func TestCheckForAccidentalSettingReverts(t *testing.T) {
}
}

func defaultPrefsFromUpArgs(t testing.TB) *ipn.Prefs {
upFlagSet.Parse(nil) // populates upArgs
if upFlagSet.Lookup("netfilter-mode") == nil && upArgs.netfilterMode == "" {
// This flag is not compiled on on-Linux platforms,
// but prefsFromUpArgs requires it be populated.
upArgs.netfilterMode = defaultNetfilterMode()
}
func defaultPrefsFromUpArgs(t testing.TB, goos string) *ipn.Prefs {
upArgs := defaultUpArgsByGOOS(goos)
prefs, err := prefsFromUpArgs(upArgs, logger.Discard, new(ipnstate.Status), "linux")
if err != nil {
t.Fatalf("defaultPrefsFromUpArgs: %v", err)
Expand All @@ -397,6 +392,12 @@ func defaultPrefsFromUpArgs(t testing.TB) *ipn.Prefs {
return prefs
}

func defaultUpArgsByGOOS(goos string) (args upArgsT) {
fs := newUpFlagSet(goos, &args)
fs.Parse(nil) // populates args
return
}

func TestPrefsFromUpArgs(t *testing.T) {
tests := []struct {
name string
Expand All @@ -408,13 +409,29 @@ func TestPrefsFromUpArgs(t *testing.T) {
wantWarn string
}{
{
name: "zero",
name: "default_linux",
goos: "linux",
args: defaultUpArgsByGOOS("linux"),
want: &ipn.Prefs{
ControlURL: ipn.DefaultControlURL,
WantRunning: true,
NoSNAT: false,
NetfilterMode: preftype.NetfilterOn,
CorpDNS: true,
AllowSingleHosts: true,
},
},
{
name: "default_windows",
goos: "windows",
args: upArgsT{},
args: defaultUpArgsByGOOS("windows"),
want: &ipn.Prefs{
WantRunning: true,
NoSNAT: true,
NetfilterMode: preftype.NetfilterOn, // silly, but default from ipn.NewPref currently
ControlURL: ipn.DefaultControlURL,
WantRunning: true,
CorpDNS: true,
AllowSingleHosts: true,
NetfilterMode: preftype.NetfilterOn,
NoSNAT: true,
},
},
{
Expand Down
14 changes: 8 additions & 6 deletions cmd/tailscale/cli/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ flag is also used.
Exec: runUp,
}

var upFlagSet = (func() *flag.FlagSet {
var upFlagSet = newUpFlagSet(runtime.GOOS, &upArgs)

func newUpFlagSet(goos string, upArgs *upArgsT) *flag.FlagSet {
upf := flag.NewFlagSet("up", flag.ExitOnError)

upf.BoolVar(&upArgs.forceReauth, "force-reauth", false, "force reauthentication")
Expand All @@ -70,18 +72,18 @@ var upFlagSet = (func() *flag.FlagSet {
upf.StringVar(&upArgs.hostname, "hostname", "", "hostname to use instead of the one provided by the OS")
upf.StringVar(&upArgs.advertiseRoutes, "advertise-routes", "", "routes to advertise to other nodes (comma-separated, e.g. \"10.0.0.0/8,192.168.0.0/24\")")
upf.BoolVar(&upArgs.advertiseDefaultRoute, "advertise-exit-node", false, "offer to be an exit node for internet traffic for the tailnet")
if safesocket.PlatformUsesPeerCreds() {
if safesocket.GOOSUsesPeerCreds(goos) {
upf.StringVar(&upArgs.opUser, "operator", "", "Unix username to allow to operate on tailscaled without sudo")
}
if runtime.GOOS == "linux" {
switch goos {
case "linux":
upf.BoolVar(&upArgs.snat, "snat-subnet-routes", true, "source NAT traffic to local routes advertised with --advertise-routes")
upf.StringVar(&upArgs.netfilterMode, "netfilter-mode", defaultNetfilterMode(), "netfilter mode (one of on, nodivert, off)")
}
if runtime.GOOS == "windows" {
case "windows":
upf.BoolVar(&upArgs.forceDaemon, "unattended", false, "run in \"Unattended Mode\" where Tailscale keeps running even after the current GUI user logs out (Windows-only)")
}
return upf
})()
}

func defaultNetfilterMode() string {
if distro.Get() == distro.Synology {
Expand Down
8 changes: 6 additions & 2 deletions safesocket/safesocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,12 @@ func LocalTCPPortAndToken() (port int, token string, err error) {

// PlatformUsesPeerCreds reports whether the current platform uses peer credentials
// to authenticate connections.
func PlatformUsesPeerCreds() bool {
switch runtime.GOOS {
func PlatformUsesPeerCreds() bool { return GOOSUsesPeerCreds(runtime.GOOS) }

// GOOSUsesPeerCreds is like PlatformUsesPeerCreds but takes a
// runtime.GOOS value instead of using the current one.
func GOOSUsesPeerCreds(goos string) bool {
switch goos {
case "linux", "darwin", "freebsd":
return true
}
Expand Down

0 comments on commit fb67d83

Please sign in to comment.