Skip to content

Commit 682fd72

Browse files
Maisem Alimaisem
Maisem Ali
authored andcommitted
util/testenv: add new package to hold InTest
Removes duplicated code. Updates #cleanup Signed-off-by: Maisem Ali <maisem@tailscale.com>
1 parent 3e255d7 commit 682fd72

File tree

10 files changed

+53
-25
lines changed

10 files changed

+53
-25
lines changed

cmd/tailscale/cli/risks.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"strings"
1313
"syscall"
1414
"time"
15+
16+
"tailscale.com/util/testenv"
1517
)
1618

1719
var (
@@ -56,7 +58,7 @@ func presentRiskToUser(riskType, riskMessage, acceptedRisks string) error {
5658
if isRiskAccepted(riskType, acceptedRisks) {
5759
return nil
5860
}
59-
if inTest() {
61+
if testenv.InTest() {
6062
return errAborted
6163
}
6264
outln(riskMessage)

cmd/tailscale/cli/up.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,6 @@ func acceptRouteDefault(goos string) bool {
9191

9292
var upFlagSet = newUpFlagSet(effectiveGOOS(), &upArgsGlobal, "up")
9393

94-
func inTest() bool { return flag.Lookup("test.v") != nil }
95-
9694
// newUpFlagSet returns a new flag set for the "up" and "login" commands.
9795
func newUpFlagSet(goos string, upArgs *upArgsT, cmd string) *flag.FlagSet {
9896
if cmd != "up" && cmd != "login" {

cmd/tailscale/depaware.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ tailscale.com/cmd/tailscale dependencies: (generated by github.com/tailscale/dep
145145
tailscale.com/util/set from tailscale.com/health+
146146
tailscale.com/util/singleflight from tailscale.com/net/dnscache
147147
tailscale.com/util/slicesx from tailscale.com/net/dnscache+
148+
tailscale.com/util/testenv from tailscale.com/cmd/tailscale/cli
148149
💣 tailscale.com/util/winutil from tailscale.com/hostinfo+
149150
W 💣 tailscale.com/util/winutil/authenticode from tailscale.com/cmd/tailscale/cli
150151
tailscale.com/version from tailscale.com/cmd/tailscale/cli+

cmd/tailscaled/depaware.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ tailscale.com/cmd/tailscaled dependencies: (generated by github.com/tailscale/de
343343
tailscale.com/util/slicesx from tailscale.com/net/dnscache+
344344
tailscale.com/util/sysresources from tailscale.com/wgengine/magicsock
345345
tailscale.com/util/systemd from tailscale.com/control/controlclient+
346+
tailscale.com/util/testenv from tailscale.com/ipn/ipnlocal+
346347
tailscale.com/util/uniq from tailscale.com/wgengine/magicsock+
347348
💣 tailscale.com/util/winutil from tailscale.com/control/controlclient+
348349
W 💣 tailscale.com/util/winutil/authenticode from tailscale.com/util/osdiag

ipn/ipnlocal/local.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"encoding/base64"
99
"encoding/json"
1010
"errors"
11-
"flag"
1211
"fmt"
1312
"io"
1413
"log"
@@ -65,7 +64,6 @@ import (
6564
"tailscale.com/types/dnstype"
6665
"tailscale.com/types/empty"
6766
"tailscale.com/types/key"
68-
"tailscale.com/types/lazy"
6967
"tailscale.com/types/logger"
7068
"tailscale.com/types/logid"
7169
"tailscale.com/types/netmap"
@@ -81,6 +79,7 @@ import (
8179
"tailscale.com/util/osshare"
8280
"tailscale.com/util/set"
8381
"tailscale.com/util/systemd"
82+
"tailscale.com/util/testenv"
8483
"tailscale.com/util/uniq"
8584
"tailscale.com/version"
8685
"tailscale.com/version/distro"
@@ -93,14 +92,6 @@ import (
9392
"tailscale.com/wgengine/wgcfg/nmcfg"
9493
)
9594

96-
var lazyInTest lazy.SyncValue[bool]
97-
98-
func inTest() bool {
99-
return lazyInTest.Get(func() bool {
100-
return flag.Lookup("test.v") != nil
101-
})
102-
}
103-
10495
var controlDebugFlags = getControlDebugFlags()
10596

10697
func getControlDebugFlags() []string {
@@ -506,7 +497,7 @@ func (b *LocalBackend) maybePauseControlClientLocked() {
506497
return
507498
}
508499
networkUp := b.prevIfState.AnyInterfaceUp()
509-
b.cc.SetPaused((b.state == ipn.Stopped && b.netMap != nil) || (!networkUp && !inTest()))
500+
b.cc.SetPaused((b.state == ipn.Stopped && b.netMap != nil) || (!networkUp && !testenv.InTest()))
510501
}
511502

512503
// linkChange is our network monitor callback, called whenever the network changes.

logpolicy/logpolicy.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"crypto/tls"
1414
"encoding/json"
1515
"errors"
16-
"flag"
1716
"fmt"
1817
"io"
1918
"log"
@@ -49,13 +48,12 @@ import (
4948
"tailscale.com/util/clientmetric"
5049
"tailscale.com/util/must"
5150
"tailscale.com/util/racebuild"
51+
"tailscale.com/util/testenv"
5252
"tailscale.com/util/winutil"
5353
"tailscale.com/version"
5454
"tailscale.com/version/distro"
5555
)
5656

57-
func inTest() bool { return flag.Lookup("test.v") != nil }
58-
5957
var getLogTargetOnce struct {
6058
sync.Once
6159
v string // URL of logs server, or empty for default
@@ -576,7 +574,7 @@ func NewWithConfigPath(collection, dir, cmdName string, netMon *netmon.Monitor,
576574
conf.IncludeProcSequence = true
577575
}
578576

579-
if envknob.NoLogsNoSupport() || inTest() {
577+
if envknob.NoLogsNoSupport() || testenv.InTest() {
580578
logf("You have disabled logging. Tailscale will not be able to provide support.")
581579
conf.HTTPC = &http.Client{Transport: noopPretendSuccessTransport{}}
582580
} else if val := getLogTarget(); val != "" {
@@ -756,7 +754,7 @@ func dialContext(ctx context.Context, netw, addr string, netMon *netmon.Monitor,
756754
// The logf parameter is optional; if non-nil, logs are printed using the
757755
// provided function; if nil, log.Printf will be used instead.
758756
func NewLogtailTransport(host string, netMon *netmon.Monitor, logf logger.Logf) http.RoundTripper {
759-
if inTest() {
757+
if testenv.InTest() {
760758
return noopPretendSuccessTransport{}
761759
}
762760
// Start with a copy of http.DefaultTransport and tweak it a bit.

tsnet/tsnet.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"crypto/tls"
1313
"encoding/hex"
1414
"errors"
15-
"flag"
1615
"fmt"
1716
"io"
1817
"log"
@@ -53,12 +52,11 @@ import (
5352
"tailscale.com/types/logid"
5453
"tailscale.com/types/nettype"
5554
"tailscale.com/util/mak"
55+
"tailscale.com/util/testenv"
5656
"tailscale.com/wgengine"
5757
"tailscale.com/wgengine/netstack"
5858
)
5959

60-
func inTest() bool { return flag.Lookup("test.v") != nil }
61-
6260
// Server is an embedded Tailscale server.
6361
//
6462
// Its exported fields may be changed until the first method call.
@@ -605,7 +603,7 @@ func (s *Server) start() (reterr error) {
605603
}
606604

607605
func (s *Server) startLogger(closePool *closeOnErrorPool) error {
608-
if inTest() {
606+
if testenv.InTest() {
609607
return nil
610608
}
611609
cfgPath := filepath.Join(s.rootPath, "tailscaled.log.conf")

tstest/deptest/deptest.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import (
1515
)
1616

1717
type DepChecker struct {
18-
GOOS string
19-
GOARCH string
18+
GOOS string // optional
19+
GOARCH string // optional
2020
BadDeps map[string]string // package => why
2121
}
2222

util/testenv/testenv.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) Tailscale Inc & AUTHORS
2+
// SPDX-License-Identifier: BSD-3-Clause
3+
4+
// Package testenv provides utility functions for tests. It does not depend on
5+
// the `testing` package to allow usage in non-test code.
6+
package testenv
7+
8+
import (
9+
"flag"
10+
11+
"tailscale.com/types/lazy"
12+
)
13+
14+
var lazyInTest lazy.SyncValue[bool]
15+
16+
// InTest reports whether the current binary is a test binary.
17+
func InTest() bool {
18+
return lazyInTest.Get(func() bool {
19+
return flag.Lookup("test.v") != nil
20+
})
21+
}

util/testenv/testenv_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) Tailscale Inc & AUTHORS
2+
// SPDX-License-Identifier: BSD-3-Clause
3+
4+
package testenv
5+
6+
import (
7+
"testing"
8+
9+
"tailscale.com/tstest/deptest"
10+
)
11+
12+
func TestDeps(t *testing.T) {
13+
deptest.DepChecker{
14+
BadDeps: map[string]string{
15+
"testing": "see pkg docs",
16+
},
17+
}.Check(t)
18+
}

0 commit comments

Comments
 (0)