2
0

integration.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Package integration contains Tailscale integration tests.
  4. //
  5. // This package is considered internal and the public API is subject
  6. // to change without notice.
  7. package integration
  8. import (
  9. "bytes"
  10. "context"
  11. "crypto/tls"
  12. "encoding/json"
  13. "flag"
  14. "fmt"
  15. "io"
  16. "log"
  17. "net"
  18. "net/http"
  19. "net/http/httptest"
  20. "net/netip"
  21. "os"
  22. "os/exec"
  23. "path"
  24. "path/filepath"
  25. "regexp"
  26. "runtime"
  27. "strconv"
  28. "strings"
  29. "sync"
  30. "testing"
  31. "time"
  32. "go4.org/mem"
  33. "tailscale.com/client/local"
  34. "tailscale.com/derp/derpserver"
  35. "tailscale.com/ipn"
  36. "tailscale.com/ipn/ipnlocal"
  37. "tailscale.com/ipn/ipnstate"
  38. "tailscale.com/ipn/store"
  39. "tailscale.com/net/stun/stuntest"
  40. "tailscale.com/safesocket"
  41. "tailscale.com/syncs"
  42. "tailscale.com/tailcfg"
  43. "tailscale.com/tstest"
  44. "tailscale.com/tstest/integration/testcontrol"
  45. "tailscale.com/types/key"
  46. "tailscale.com/types/logger"
  47. "tailscale.com/types/logid"
  48. "tailscale.com/types/nettype"
  49. "tailscale.com/util/rands"
  50. "tailscale.com/util/zstdframe"
  51. "tailscale.com/version"
  52. )
  53. var (
  54. verboseTailscaled = flag.Bool("verbose-tailscaled", false, "verbose tailscaled logging")
  55. verboseTailscale = flag.Bool("verbose-tailscale", false, "verbose tailscale CLI logging")
  56. )
  57. // MainError is an error that's set if an error conditions happens outside of a
  58. // context where a testing.TB is available. The caller can check it in its TestMain
  59. // as a last ditch place to report errors.
  60. var MainError syncs.AtomicValue[error]
  61. // Binaries contains the paths to the tailscale and tailscaled binaries.
  62. type Binaries struct {
  63. Dir string
  64. Tailscale BinaryInfo
  65. Tailscaled BinaryInfo
  66. }
  67. // BinaryInfo describes a tailscale or tailscaled binary.
  68. type BinaryInfo struct {
  69. Path string // abs path to tailscale or tailscaled binary
  70. Size int64
  71. // FD and FDmu are set on Unix to efficiently copy the binary to a new
  72. // test's automatically-cleaned-up temp directory.
  73. FD *os.File // for Unix (macOS, Linux, ...)
  74. FDMu sync.Locker
  75. // Contents is used on Windows instead of FD to copy the binary between
  76. // test directories. (On Windows you can't keep an FD open while an earlier
  77. // test's temp directories are deleted.)
  78. // This burns some memory and costs more in I/O, but oh well.
  79. Contents []byte
  80. }
  81. func (b BinaryInfo) CopyTo(dir string) (BinaryInfo, error) {
  82. ret := b
  83. ret.Path = filepath.Join(dir, path.Base(b.Path))
  84. switch runtime.GOOS {
  85. case "linux":
  86. // TODO(bradfitz): be fancy and use linkat with AT_EMPTY_PATH to avoid
  87. // copying? I couldn't get it to work, though.
  88. // For now, just do the same thing as every other Unix and copy
  89. // the binary.
  90. fallthrough
  91. case "darwin", "freebsd", "openbsd", "netbsd":
  92. f, err := os.OpenFile(ret.Path, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0o755)
  93. if err != nil {
  94. return BinaryInfo{}, err
  95. }
  96. b.FDMu.Lock()
  97. b.FD.Seek(0, 0)
  98. size, err := io.Copy(f, b.FD)
  99. b.FDMu.Unlock()
  100. if err != nil {
  101. f.Close()
  102. return BinaryInfo{}, fmt.Errorf("copying %q: %w", b.Path, err)
  103. }
  104. if size != b.Size {
  105. f.Close()
  106. return BinaryInfo{}, fmt.Errorf("copy %q: size mismatch: %d != %d", b.Path, size, b.Size)
  107. }
  108. if err := f.Close(); err != nil {
  109. return BinaryInfo{}, err
  110. }
  111. return ret, nil
  112. case "windows":
  113. return ret, os.WriteFile(ret.Path, b.Contents, 0o755)
  114. default:
  115. return BinaryInfo{}, fmt.Errorf("unsupported OS %q", runtime.GOOS)
  116. }
  117. }
  118. // GetBinaries create a temp directory using tb and builds (or copies previously
  119. // built) cmd/tailscale and cmd/tailscaled binaries into that directory.
  120. //
  121. // It fails tb if the build or binary copies fail.
  122. func GetBinaries(tb testing.TB) *Binaries {
  123. dir := tb.TempDir()
  124. buildOnce.Do(func() {
  125. buildErr = buildTestBinaries(dir)
  126. })
  127. if buildErr != nil {
  128. tb.Fatal(buildErr)
  129. }
  130. if binariesCache.Dir == dir {
  131. return binariesCache
  132. }
  133. ts, err := binariesCache.Tailscale.CopyTo(dir)
  134. if err != nil {
  135. tb.Fatalf("copying tailscale binary: %v", err)
  136. }
  137. tsd, err := binariesCache.Tailscaled.CopyTo(dir)
  138. if err != nil {
  139. tb.Fatalf("copying tailscaled binary: %v", err)
  140. }
  141. return &Binaries{
  142. Dir: dir,
  143. Tailscale: ts,
  144. Tailscaled: tsd,
  145. }
  146. }
  147. var (
  148. buildOnce sync.Once
  149. buildErr error
  150. binariesCache *Binaries
  151. )
  152. // buildTestBinaries builds tailscale and tailscaled.
  153. // On success, it initializes [binariesCache].
  154. func buildTestBinaries(dir string) error {
  155. getBinaryInfo := func(name string) (BinaryInfo, error) {
  156. bi := BinaryInfo{Path: filepath.Join(dir, name+exe())}
  157. fi, err := os.Stat(bi.Path)
  158. if err != nil {
  159. return BinaryInfo{}, fmt.Errorf("stat %q: %v", bi.Path, err)
  160. }
  161. bi.Size = fi.Size()
  162. switch runtime.GOOS {
  163. case "windows":
  164. bi.Contents, err = os.ReadFile(bi.Path)
  165. if err != nil {
  166. return BinaryInfo{}, fmt.Errorf("read %q: %v", bi.Path, err)
  167. }
  168. default:
  169. bi.FD, err = os.OpenFile(bi.Path, os.O_RDONLY, 0)
  170. if err != nil {
  171. return BinaryInfo{}, fmt.Errorf("open %q: %v", bi.Path, err)
  172. }
  173. bi.FDMu = new(sync.Mutex)
  174. // Note: bi.FD is copied around between tests but never closed, by
  175. // design. It will be closed when the process exits, and that will
  176. // close the inode that we're copying the bytes from for each test.
  177. }
  178. return bi, nil
  179. }
  180. err := build(dir, "tailscale.com/cmd/tailscaled", "tailscale.com/cmd/tailscale")
  181. if err != nil {
  182. return err
  183. }
  184. b := &Binaries{
  185. Dir: dir,
  186. }
  187. b.Tailscale, err = getBinaryInfo("tailscale")
  188. if err != nil {
  189. return err
  190. }
  191. b.Tailscaled, err = getBinaryInfo("tailscaled")
  192. if err != nil {
  193. return err
  194. }
  195. binariesCache = b
  196. return nil
  197. }
  198. func build(outDir string, targets ...string) error {
  199. goBin, err := findGo()
  200. if err != nil {
  201. return err
  202. }
  203. cmd := exec.Command(goBin, "install")
  204. if version.IsRace() {
  205. cmd.Args = append(cmd.Args, "-race")
  206. }
  207. cmd.Args = append(cmd.Args, targets...)
  208. cmd.Env = append(os.Environ(), "GOARCH="+runtime.GOARCH, "GOBIN="+outDir)
  209. errOut, err := cmd.CombinedOutput()
  210. if err == nil {
  211. return nil
  212. }
  213. if strings.Contains(string(errOut), "when GOBIN is set") {
  214. // Fallback slow path for cross-compiled binaries.
  215. for _, target := range targets {
  216. outFile := filepath.Join(outDir, path.Base(target)+exe())
  217. cmd := exec.Command(goBin, "build", "-o", outFile)
  218. if version.IsRace() {
  219. cmd.Args = append(cmd.Args, "-race")
  220. }
  221. cmd.Args = append(cmd.Args, target)
  222. cmd.Env = append(os.Environ(), "GOARCH="+runtime.GOARCH)
  223. if errOut, err := cmd.CombinedOutput(); err != nil {
  224. return fmt.Errorf("failed to build %v with %v: %v, %s", target, goBin, err, errOut)
  225. }
  226. }
  227. return nil
  228. }
  229. return fmt.Errorf("failed to build %v with %v: %v, %s", targets, goBin, err, errOut)
  230. }
  231. func findGo() (string, error) {
  232. // Go 1.19 attempted to be helpful by prepending $PATH with GOROOT/bin based
  233. // on the executed go binary when invoked using `go test` or `go generate`,
  234. // however, this doesn't cover cases when run otherwise, such as via `go run`.
  235. // runtime.GOROOT() may often be empty these days, so the safe thing to do
  236. // here is, in order:
  237. // 1. Look for a go binary in $PATH[0].
  238. // 2. Look for a go binary in runtime.GOROOT()/bin if runtime.GOROOT() is non-empty.
  239. // 3. Look for a go binary in $PATH.
  240. // For tests we want to run as root on GitHub actions, we run with -exec=sudo,
  241. // but that results in this test running with a different PATH and picking the
  242. // wrong Go. So hard code the GitHub Actions case.
  243. if os.Getuid() == 0 && os.Getenv("GITHUB_ACTIONS") == "true" {
  244. const sudoGithubGo = "/home/runner/.cache/tailscale-go/bin/go"
  245. if _, err := os.Stat(sudoGithubGo); err == nil {
  246. return sudoGithubGo, nil
  247. }
  248. }
  249. paths := strings.FieldsFunc(os.Getenv("PATH"), func(r rune) bool { return os.IsPathSeparator(uint8(r)) })
  250. if len(paths) > 0 {
  251. candidate := filepath.Join(paths[0], "go"+exe())
  252. if path, err := exec.LookPath(candidate); err == nil {
  253. return path, err
  254. }
  255. }
  256. if runtime.GOROOT() != "" {
  257. candidate := filepath.Join(runtime.GOROOT(), "bin", "go"+exe())
  258. if path, err := exec.LookPath(candidate); err == nil {
  259. return path, err
  260. }
  261. }
  262. return exec.LookPath("go")
  263. }
  264. func exe() string {
  265. if runtime.GOOS == "windows" {
  266. return ".exe"
  267. }
  268. return ""
  269. }
  270. // RunDERPAndSTUN runs a local DERP and STUN server for tests, returning the derpMap
  271. // that clients should use. This creates resources that must be cleaned up with the
  272. // returned cleanup function.
  273. func RunDERPAndSTUN(t testing.TB, logf logger.Logf, ipAddress string) (derpMap *tailcfg.DERPMap) {
  274. t.Helper()
  275. d := derpserver.New(key.NewNode(), logf)
  276. ln, err := net.Listen("tcp", net.JoinHostPort(ipAddress, "0"))
  277. if err != nil {
  278. t.Fatal(err)
  279. }
  280. httpsrv := httptest.NewUnstartedServer(derpserver.Handler(d))
  281. httpsrv.Listener.Close()
  282. httpsrv.Listener = ln
  283. httpsrv.Config.ErrorLog = logger.StdLogger(logf)
  284. httpsrv.Config.TLSNextProto = make(map[string]func(*http.Server, *tls.Conn, http.Handler))
  285. httpsrv.StartTLS()
  286. stunAddr, stunCleanup := stuntest.ServeWithPacketListener(t, nettype.Std{})
  287. m := &tailcfg.DERPMap{
  288. Regions: map[int]*tailcfg.DERPRegion{
  289. 1: {
  290. RegionID: 1,
  291. RegionCode: "test",
  292. Nodes: []*tailcfg.DERPNode{
  293. {
  294. Name: "t1",
  295. RegionID: 1,
  296. HostName: ipAddress,
  297. IPv4: ipAddress,
  298. IPv6: "none",
  299. STUNPort: stunAddr.Port,
  300. DERPPort: httpsrv.Listener.Addr().(*net.TCPAddr).Port,
  301. InsecureForTests: true,
  302. STUNTestIP: ipAddress,
  303. },
  304. },
  305. },
  306. },
  307. }
  308. t.Logf("DERP httpsrv listener: %v", httpsrv.Listener.Addr())
  309. t.Cleanup(func() {
  310. httpsrv.CloseClientConnections()
  311. httpsrv.Close()
  312. d.Close()
  313. stunCleanup()
  314. ln.Close()
  315. })
  316. return m
  317. }
  318. // LogCatcher is a minimal logcatcher for the logtail upload client.
  319. type LogCatcher struct {
  320. mu sync.Mutex
  321. logf logger.Logf
  322. buf bytes.Buffer
  323. gotErr error
  324. reqs int
  325. raw bool // indicates whether to store the raw JSON logs uploaded, instead of just the text
  326. }
  327. // UseLogf makes the logcatcher implementation use a given logf function
  328. // to dump all logs to.
  329. func (lc *LogCatcher) UseLogf(fn logger.Logf) {
  330. lc.mu.Lock()
  331. defer lc.mu.Unlock()
  332. lc.logf = fn
  333. }
  334. // StoreRawJSON instructs lc to save the raw JSON uploads, rather than just the text.
  335. func (lc *LogCatcher) StoreRawJSON() {
  336. lc.mu.Lock()
  337. defer lc.mu.Unlock()
  338. lc.raw = true
  339. }
  340. func (lc *LogCatcher) logsContains(sub mem.RO) bool {
  341. lc.mu.Lock()
  342. defer lc.mu.Unlock()
  343. return mem.Contains(mem.B(lc.buf.Bytes()), sub)
  344. }
  345. func (lc *LogCatcher) numRequests() int {
  346. lc.mu.Lock()
  347. defer lc.mu.Unlock()
  348. return lc.reqs
  349. }
  350. func (lc *LogCatcher) logsString() string {
  351. lc.mu.Lock()
  352. defer lc.mu.Unlock()
  353. return lc.buf.String()
  354. }
  355. // Reset clears the buffered logs from memory.
  356. func (lc *LogCatcher) Reset() {
  357. lc.mu.Lock()
  358. defer lc.mu.Unlock()
  359. lc.buf.Reset()
  360. }
  361. func (lc *LogCatcher) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  362. // POST /c/<collection-name>/<private-ID>
  363. if r.Method != "POST" {
  364. log.Printf("bad logcatcher method: %v", r.Method)
  365. http.Error(w, "only POST is supported", 400)
  366. return
  367. }
  368. pathParts := strings.Split(strings.TrimPrefix(r.URL.Path, "/c/"), "/")
  369. if len(pathParts) != 2 {
  370. log.Printf("bad logcatcher path: %q", r.URL.Path)
  371. http.Error(w, "bad URL", 400)
  372. return
  373. }
  374. // collectionName := pathPaths[0]
  375. privID, err := logid.ParsePrivateID(pathParts[1])
  376. if err != nil {
  377. log.Printf("bad log ID: %q: %v", r.URL.Path, err)
  378. }
  379. bodyBytes, err := io.ReadAll(r.Body)
  380. if err != nil {
  381. log.Printf("http.Request.Body.Read: %v", err)
  382. return
  383. }
  384. if r.Header.Get("Content-Encoding") == "zstd" {
  385. bodyBytes, err = zstdframe.AppendDecode(nil, bodyBytes)
  386. if err != nil {
  387. log.Printf("zstdframe.AppendDecode: %v", err)
  388. http.Error(w, err.Error(), 400)
  389. return
  390. }
  391. }
  392. type Entry struct {
  393. Logtail struct {
  394. ClientTime time.Time `json:"client_time"`
  395. ServerTime time.Time `json:"server_time"`
  396. Error struct {
  397. BadData string `json:"bad_data"`
  398. } `json:"error"`
  399. } `json:"logtail"`
  400. Text string `json:"text"`
  401. }
  402. var jreq []Entry
  403. if len(bodyBytes) > 0 && bodyBytes[0] == '[' {
  404. err = json.Unmarshal(bodyBytes, &jreq)
  405. } else {
  406. var ent Entry
  407. err = json.Unmarshal(bodyBytes, &ent)
  408. jreq = append(jreq, ent)
  409. }
  410. lc.mu.Lock()
  411. defer lc.mu.Unlock()
  412. lc.reqs++
  413. if lc.gotErr == nil && err != nil {
  414. lc.gotErr = err
  415. }
  416. if err != nil {
  417. fmt.Fprintf(&lc.buf, "error from %s of %#q: %v\n", r.Method, bodyBytes, err)
  418. if lc.logf != nil {
  419. lc.logf("error from %s of %#q: %v\n", r.Method, bodyBytes, err)
  420. }
  421. } else {
  422. id := privID.Public().String()[:3] // good enough for integration tests
  423. for _, ent := range jreq {
  424. if lc.raw {
  425. lc.buf.Write(bodyBytes)
  426. continue
  427. }
  428. fmt.Fprintf(&lc.buf, "%s\n", strings.TrimSpace(ent.Text))
  429. if lc.logf != nil {
  430. lc.logf("logcatch:%s: %s", id, strings.TrimSpace(ent.Text))
  431. }
  432. }
  433. }
  434. w.WriteHeader(200) // must have no content, but not a 204
  435. }
  436. // TestEnv contains the test environment (set of servers) used by one
  437. // or more nodes.
  438. type TestEnv struct {
  439. t testing.TB
  440. tunMode bool
  441. cli string
  442. daemon string
  443. loopbackPort *int
  444. neverDirectUDP bool
  445. relayServerUseLoopback bool
  446. LogCatcher *LogCatcher
  447. LogCatcherServer *httptest.Server
  448. Control *testcontrol.Server
  449. ControlServer *httptest.Server
  450. TrafficTrap *trafficTrap
  451. TrafficTrapServer *httptest.Server
  452. }
  453. // ControlURL returns e.ControlServer.URL, panicking if it's the empty string,
  454. // which it should never be in tests.
  455. func (e *TestEnv) ControlURL() string {
  456. s := e.ControlServer.URL
  457. if s == "" {
  458. panic("control server not set")
  459. }
  460. return s
  461. }
  462. // TestEnvOpt represents an option that can be passed to NewTestEnv.
  463. type TestEnvOpt interface {
  464. ModifyTestEnv(*TestEnv)
  465. }
  466. // ConfigureControl is a test option that configures the test control server.
  467. type ConfigureControl func(*testcontrol.Server)
  468. func (f ConfigureControl) ModifyTestEnv(te *TestEnv) {
  469. f(te.Control)
  470. }
  471. // NewTestEnv starts a bunch of services and returns a new test environment.
  472. // NewTestEnv arranges for the environment's resources to be cleaned up on exit.
  473. func NewTestEnv(t testing.TB, opts ...TestEnvOpt) *TestEnv {
  474. if runtime.GOOS == "windows" {
  475. t.Skip("not tested/working on Windows yet")
  476. }
  477. derpMap := RunDERPAndSTUN(t, logger.Discard, "127.0.0.1")
  478. logc := new(LogCatcher)
  479. control := &testcontrol.Server{
  480. Logf: logger.WithPrefix(t.Logf, "testcontrol: "),
  481. DERPMap: derpMap,
  482. }
  483. control.HTTPTestServer = httptest.NewUnstartedServer(control)
  484. trafficTrap := new(trafficTrap)
  485. binaries := GetBinaries(t)
  486. e := &TestEnv{
  487. t: t,
  488. cli: binaries.Tailscale.Path,
  489. daemon: binaries.Tailscaled.Path,
  490. LogCatcher: logc,
  491. LogCatcherServer: httptest.NewServer(logc),
  492. Control: control,
  493. ControlServer: control.HTTPTestServer,
  494. TrafficTrap: trafficTrap,
  495. TrafficTrapServer: httptest.NewServer(trafficTrap),
  496. }
  497. for _, o := range opts {
  498. o.ModifyTestEnv(e)
  499. }
  500. control.HTTPTestServer.Start()
  501. t.Cleanup(func() {
  502. // Shut down e.
  503. if err := e.TrafficTrap.Err(); err != nil {
  504. e.t.Errorf("traffic trap: %v", err)
  505. e.t.Logf("logs: %s", e.LogCatcher.logsString())
  506. }
  507. e.LogCatcherServer.Close()
  508. e.TrafficTrapServer.Close()
  509. e.ControlServer.Close()
  510. })
  511. t.Logf("control URL: %v", e.ControlURL())
  512. return e
  513. }
  514. // TestNode is a machine with a tailscale & tailscaled.
  515. // Currently, the test is simplistic and user==node==machine.
  516. // That may grow complexity later to test more.
  517. type TestNode struct {
  518. env *TestEnv
  519. tailscaledParser *nodeOutputParser
  520. dir string // temp dir for sock & state
  521. configFile string // or empty for none
  522. sockFile string
  523. stateFile string
  524. upFlagGOOS string // if non-empty, sets TS_DEBUG_UP_FLAG_GOOS for cmd/tailscale CLI
  525. encryptState bool
  526. allowUpdates bool
  527. mu sync.Mutex
  528. onLogLine []func([]byte)
  529. lc *local.Client
  530. }
  531. // NewTestNode allocates a temp directory for a new test node.
  532. // The node is not started automatically.
  533. func NewTestNode(t *testing.T, env *TestEnv) *TestNode {
  534. dir := t.TempDir()
  535. sockFile := filepath.Join(dir, "tailscale.sock")
  536. if len(sockFile) >= 104 {
  537. // Maximum length for a unix socket on darwin. Try something else.
  538. sockFile = filepath.Join(os.TempDir(), rands.HexString(8)+".sock")
  539. t.Cleanup(func() { os.Remove(sockFile) })
  540. }
  541. n := &TestNode{
  542. env: env,
  543. dir: dir,
  544. sockFile: sockFile,
  545. stateFile: filepath.Join(dir, "tailscaled.state"), // matches what cmd/tailscaled uses
  546. }
  547. // Look for a data race or panic.
  548. // Once we see the start marker, start logging the rest.
  549. var sawRace bool
  550. var sawPanic bool
  551. n.addLogLineHook(func(line []byte) {
  552. lineB := mem.B(line)
  553. if mem.Contains(lineB, mem.S("DEBUG-ADDR=")) {
  554. t.Log(strings.TrimSpace(string(line)))
  555. }
  556. if mem.Contains(lineB, mem.S("WARNING: DATA RACE")) {
  557. sawRace = true
  558. }
  559. if mem.HasPrefix(lineB, mem.S("panic: ")) {
  560. sawPanic = true
  561. }
  562. if sawRace || sawPanic {
  563. t.Logf("%s", line)
  564. }
  565. })
  566. return n
  567. }
  568. func (n *TestNode) LocalClient() *local.Client {
  569. n.mu.Lock()
  570. defer n.mu.Unlock()
  571. if n.lc == nil {
  572. tr := &http.Transport{}
  573. n.lc = &local.Client{
  574. Socket: n.sockFile,
  575. UseSocketOnly: true,
  576. }
  577. n.env.t.Cleanup(tr.CloseIdleConnections)
  578. }
  579. return n.lc
  580. }
  581. func (n *TestNode) diskPrefs() *ipn.Prefs {
  582. t := n.env.t
  583. t.Helper()
  584. if _, err := os.ReadFile(n.stateFile); err != nil {
  585. t.Fatalf("reading prefs: %v", err)
  586. }
  587. fs, err := store.New(nil, n.stateFile)
  588. if err != nil {
  589. t.Fatalf("reading prefs, NewFileStore: %v", err)
  590. }
  591. p, err := ipnlocal.ReadStartupPrefsForTest(t.Logf, fs)
  592. if err != nil {
  593. t.Fatalf("reading prefs, ReadDiskPrefsForTest: %v", err)
  594. }
  595. return p.AsStruct()
  596. }
  597. // AwaitResponding waits for n's tailscaled to be up enough to be
  598. // responding, but doesn't wait for any particular state.
  599. func (n *TestNode) AwaitResponding() {
  600. t := n.env.t
  601. t.Helper()
  602. n.AwaitListening()
  603. st := n.MustStatus()
  604. t.Logf("Status: %s", st.BackendState)
  605. if err := tstest.WaitFor(20*time.Second, func() error {
  606. const sub = `Program starting: `
  607. if !n.env.LogCatcher.logsContains(mem.S(sub)) {
  608. return fmt.Errorf("log catcher didn't see %#q; got %s", sub, n.env.LogCatcher.logsString())
  609. }
  610. return nil
  611. }); err != nil {
  612. t.Fatal(err)
  613. }
  614. }
  615. // addLogLineHook registers a hook f to be called on each tailscaled
  616. // log line output.
  617. func (n *TestNode) addLogLineHook(f func([]byte)) {
  618. n.mu.Lock()
  619. defer n.mu.Unlock()
  620. n.onLogLine = append(n.onLogLine, f)
  621. }
  622. // socks5AddrChan returns a channel that receives the address (e.g. "localhost:23874")
  623. // of the node's SOCKS5 listener, once started.
  624. func (n *TestNode) socks5AddrChan() <-chan string {
  625. ch := make(chan string, 1)
  626. n.addLogLineHook(func(line []byte) {
  627. const sub = "SOCKS5 listening on "
  628. i := mem.Index(mem.B(line), mem.S(sub))
  629. if i == -1 {
  630. return
  631. }
  632. addr := strings.TrimSpace(string(line)[i+len(sub):])
  633. select {
  634. case ch <- addr:
  635. default:
  636. }
  637. })
  638. return ch
  639. }
  640. func (n *TestNode) AwaitSocksAddr(ch <-chan string) string {
  641. t := n.env.t
  642. t.Helper()
  643. timer := time.NewTimer(10 * time.Second)
  644. defer timer.Stop()
  645. select {
  646. case v := <-ch:
  647. return v
  648. case <-timer.C:
  649. t.Fatal("timeout waiting for node to log its SOCK5 listening address")
  650. panic("unreachable")
  651. }
  652. }
  653. // nodeOutputParser parses stderr of tailscaled processes, calling the
  654. // per-line callbacks previously registered via
  655. // testNode.addLogLineHook.
  656. type nodeOutputParser struct {
  657. allBuf bytes.Buffer
  658. pendLineBuf bytes.Buffer
  659. n *TestNode
  660. }
  661. func (op *nodeOutputParser) Write(p []byte) (n int, err error) {
  662. tn := op.n
  663. tn.mu.Lock()
  664. defer tn.mu.Unlock()
  665. op.allBuf.Write(p)
  666. n, err = op.pendLineBuf.Write(p)
  667. op.parseLinesLocked()
  668. return
  669. }
  670. func (op *nodeOutputParser) parseLinesLocked() {
  671. n := op.n
  672. buf := op.pendLineBuf.Bytes()
  673. for len(buf) > 0 {
  674. nl := bytes.IndexByte(buf, '\n')
  675. if nl == -1 {
  676. break
  677. }
  678. line := buf[:nl+1]
  679. buf = buf[nl+1:]
  680. for _, f := range n.onLogLine {
  681. f(line)
  682. }
  683. }
  684. if len(buf) == 0 {
  685. op.pendLineBuf.Reset()
  686. } else {
  687. io.CopyN(io.Discard, &op.pendLineBuf, int64(op.pendLineBuf.Len()-len(buf)))
  688. }
  689. }
  690. type Daemon struct {
  691. Process *os.Process
  692. }
  693. func (d *Daemon) MustCleanShutdown(t testing.TB) {
  694. d.Process.Signal(os.Interrupt)
  695. ps, err := d.Process.Wait()
  696. if err != nil {
  697. t.Fatalf("tailscaled Wait: %v", err)
  698. }
  699. if ps.ExitCode() != 0 {
  700. t.Errorf("tailscaled ExitCode = %d; want 0", ps.ExitCode())
  701. }
  702. }
  703. // awaitTailscaledRunnable tries to run `tailscaled --version` until it
  704. // works. This is an unsatisfying workaround for ETXTBSY we were seeing
  705. // on GitHub Actions that aren't understood. It's not clear what's holding
  706. // a writable fd to tailscaled after `go install` completes.
  707. // See https://github.com/tailscale/tailscale/issues/15868.
  708. func (n *TestNode) awaitTailscaledRunnable() error {
  709. t := n.env.t
  710. t.Helper()
  711. if err := tstest.WaitFor(10*time.Second, func() error {
  712. out, err := exec.Command(n.env.daemon, "--version").CombinedOutput()
  713. if err == nil {
  714. return nil
  715. }
  716. t.Logf("error running tailscaled --version: %v, %s", err, out)
  717. return err
  718. }); err != nil {
  719. return fmt.Errorf("gave up trying to run tailscaled: %v", err)
  720. }
  721. return nil
  722. }
  723. // StartDaemon starts the node's tailscaled, failing if it fails to start.
  724. // StartDaemon ensures that the process will exit when the test completes.
  725. func (n *TestNode) StartDaemon() *Daemon {
  726. return n.StartDaemonAsIPNGOOS(runtime.GOOS)
  727. }
  728. func (n *TestNode) StartDaemonAsIPNGOOS(ipnGOOS string) *Daemon {
  729. t := n.env.t
  730. if err := n.awaitTailscaledRunnable(); err != nil {
  731. t.Fatalf("awaitTailscaledRunnable: %v", err)
  732. }
  733. cmd := exec.Command(n.env.daemon)
  734. cmd.Args = append(cmd.Args,
  735. "--statedir="+n.dir,
  736. "--socket="+n.sockFile,
  737. "--socks5-server=localhost:0",
  738. "--debug=localhost:0",
  739. )
  740. if *verboseTailscaled {
  741. cmd.Args = append(cmd.Args, "-verbose=2")
  742. }
  743. if !n.env.tunMode {
  744. cmd.Args = append(cmd.Args,
  745. "--tun=userspace-networking",
  746. )
  747. }
  748. if n.configFile != "" {
  749. cmd.Args = append(cmd.Args, "--config="+n.configFile)
  750. }
  751. if n.encryptState {
  752. cmd.Args = append(cmd.Args, "--encrypt-state")
  753. }
  754. cmd.Env = append(os.Environ(),
  755. "TS_DEBUG_PERMIT_HTTP_C2N=1",
  756. "TS_LOG_TARGET="+n.env.LogCatcherServer.URL,
  757. "HTTP_PROXY="+n.env.TrafficTrapServer.URL,
  758. "HTTPS_PROXY="+n.env.TrafficTrapServer.URL,
  759. "TS_DEBUG_FAKE_GOOS="+ipnGOOS,
  760. "TS_LOGS_DIR="+t.TempDir(),
  761. "TS_NETCHECK_GENERATE_204_URL="+n.env.ControlServer.URL+"/generate_204",
  762. "TS_ASSUME_NETWORK_UP_FOR_TEST=1", // don't pause control client in airplane mode (no wifi, etc)
  763. "TS_PANIC_IF_HIT_MAIN_CONTROL=1",
  764. "TS_DISABLE_PORTMAPPER=1", // shouldn't be needed; test is all localhost
  765. "TS_DEBUG_LOG_RATE=all",
  766. )
  767. if n.allowUpdates {
  768. cmd.Env = append(cmd.Env, "TS_TEST_ALLOW_AUTO_UPDATE=1")
  769. }
  770. if n.env.loopbackPort != nil {
  771. cmd.Env = append(cmd.Env, "TS_DEBUG_NETSTACK_LOOPBACK_PORT="+strconv.Itoa(*n.env.loopbackPort))
  772. }
  773. if n.env.neverDirectUDP {
  774. cmd.Env = append(cmd.Env, "TS_DEBUG_NEVER_DIRECT_UDP=1")
  775. }
  776. if n.env.relayServerUseLoopback {
  777. cmd.Env = append(cmd.Env, "TS_DEBUG_RELAY_SERVER_ADDRS=::1,127.0.0.1")
  778. }
  779. if version.IsRace() {
  780. cmd.Env = append(cmd.Env, "GORACE=halt_on_error=1")
  781. }
  782. n.tailscaledParser = &nodeOutputParser{n: n}
  783. cmd.Stderr = n.tailscaledParser
  784. if *verboseTailscaled {
  785. cmd.Stdout = os.Stdout
  786. cmd.Stderr = io.MultiWriter(cmd.Stderr, os.Stderr)
  787. }
  788. if runtime.GOOS != "windows" {
  789. pr, pw, err := os.Pipe()
  790. if err != nil {
  791. t.Fatal(err)
  792. }
  793. t.Cleanup(func() { pw.Close() })
  794. cmd.ExtraFiles = append(cmd.ExtraFiles, pr)
  795. cmd.Env = append(cmd.Env, "TS_PARENT_DEATH_FD=3")
  796. }
  797. if err := cmd.Start(); err != nil {
  798. t.Fatalf("starting tailscaled: %v", err)
  799. }
  800. t.Cleanup(func() { cmd.Process.Kill() })
  801. return &Daemon{
  802. Process: cmd.Process,
  803. }
  804. }
  805. func (n *TestNode) MustUp(extraArgs ...string) {
  806. t := n.env.t
  807. t.Helper()
  808. args := []string{
  809. "up",
  810. "--login-server=" + n.env.ControlURL(),
  811. "--reset",
  812. }
  813. args = append(args, extraArgs...)
  814. cmd := n.Tailscale(args...)
  815. t.Logf("Running %v ...", cmd)
  816. cmd.Stdout = nil // in case --verbose-tailscale was set
  817. cmd.Stderr = nil // in case --verbose-tailscale was set
  818. if b, err := cmd.CombinedOutput(); err != nil {
  819. t.Fatalf("up: %v, %v", string(b), err)
  820. }
  821. }
  822. func (n *TestNode) MustDown() {
  823. t := n.env.t
  824. t.Logf("Running down ...")
  825. if err := n.Tailscale("down", "--accept-risk=all").Run(); err != nil {
  826. t.Fatalf("down: %v", err)
  827. }
  828. }
  829. func (n *TestNode) MustLogOut() {
  830. t := n.env.t
  831. t.Logf("Running logout ...")
  832. if err := n.Tailscale("logout").Run(); err != nil {
  833. t.Fatalf("logout: %v", err)
  834. }
  835. }
  836. func (n *TestNode) Ping(otherNode *TestNode) error {
  837. t := n.env.t
  838. ip := otherNode.AwaitIP4().String()
  839. t.Logf("Running ping %v (from %v)...", ip, n.AwaitIP4())
  840. return n.Tailscale("ping", "--timeout=1s", ip).Run()
  841. }
  842. // AwaitListening waits for the tailscaled to be serving local clients
  843. // over its localhost IPC mechanism. (Unix socket, etc)
  844. func (n *TestNode) AwaitListening() {
  845. t := n.env.t
  846. if err := tstest.WaitFor(20*time.Second, func() (err error) {
  847. c, err := safesocket.ConnectContext(context.Background(), n.sockFile)
  848. if err == nil {
  849. c.Close()
  850. }
  851. return err
  852. }); err != nil {
  853. t.Fatal(err)
  854. }
  855. }
  856. func (n *TestNode) AwaitIPs() []netip.Addr {
  857. t := n.env.t
  858. t.Helper()
  859. var addrs []netip.Addr
  860. if err := tstest.WaitFor(20*time.Second, func() error {
  861. cmd := n.Tailscale("ip")
  862. cmd.Stdout = nil // in case --verbose-tailscale was set
  863. cmd.Stderr = nil // in case --verbose-tailscale was set
  864. out, err := cmd.Output()
  865. if err != nil {
  866. return err
  867. }
  868. ips := string(out)
  869. ipslice := strings.Fields(ips)
  870. addrs = make([]netip.Addr, len(ipslice))
  871. for i, ip := range ipslice {
  872. netIP, err := netip.ParseAddr(ip)
  873. if err != nil {
  874. t.Fatal(err)
  875. }
  876. addrs[i] = netIP
  877. }
  878. return nil
  879. }); err != nil {
  880. t.Fatalf("awaiting an IP address: %v", err)
  881. }
  882. if len(addrs) == 0 {
  883. t.Fatalf("returned IP address was blank")
  884. }
  885. return addrs
  886. }
  887. // AwaitIP4 returns the IPv4 address of n.
  888. func (n *TestNode) AwaitIP4() netip.Addr {
  889. t := n.env.t
  890. t.Helper()
  891. ips := n.AwaitIPs()
  892. return ips[0]
  893. }
  894. // AwaitIP6 returns the IPv6 address of n.
  895. func (n *TestNode) AwaitIP6() netip.Addr {
  896. t := n.env.t
  897. t.Helper()
  898. ips := n.AwaitIPs()
  899. return ips[1]
  900. }
  901. // AwaitRunning waits for n to reach the IPN state "Running".
  902. func (n *TestNode) AwaitRunning() {
  903. t := n.env.t
  904. t.Helper()
  905. n.AwaitBackendState("Running")
  906. }
  907. func (n *TestNode) AwaitBackendState(state string) {
  908. t := n.env.t
  909. t.Helper()
  910. if err := tstest.WaitFor(20*time.Second, func() error {
  911. st, err := n.Status()
  912. if err != nil {
  913. return err
  914. }
  915. if st.BackendState != state {
  916. return fmt.Errorf("in state %q; want %q", st.BackendState, state)
  917. }
  918. return nil
  919. }); err != nil {
  920. t.Fatalf("failure/timeout waiting for transition to Running status: %v", err)
  921. }
  922. }
  923. // AwaitNeedsLogin waits for n to reach the IPN state "NeedsLogin".
  924. func (n *TestNode) AwaitNeedsLogin() {
  925. t := n.env.t
  926. t.Helper()
  927. if err := tstest.WaitFor(20*time.Second, func() error {
  928. st, err := n.Status()
  929. if err != nil {
  930. return err
  931. }
  932. if st.BackendState != "NeedsLogin" {
  933. return fmt.Errorf("in state %q", st.BackendState)
  934. }
  935. return nil
  936. }); err != nil {
  937. t.Fatalf("failure/timeout waiting for transition to NeedsLogin status: %v", err)
  938. }
  939. }
  940. func (n *TestNode) TailscaleForOutput(arg ...string) *exec.Cmd {
  941. cmd := n.Tailscale(arg...)
  942. cmd.Stdout = nil
  943. cmd.Stderr = nil
  944. return cmd
  945. }
  946. // Tailscale returns a command that runs the tailscale CLI with the provided arguments.
  947. // It does not start the process.
  948. func (n *TestNode) Tailscale(arg ...string) *exec.Cmd {
  949. cmd := exec.Command(n.env.cli)
  950. cmd.Args = append(cmd.Args, "--socket="+n.sockFile)
  951. cmd.Args = append(cmd.Args, arg...)
  952. cmd.Dir = n.dir
  953. cmd.Env = append(os.Environ(),
  954. "TS_DEBUG_UP_FLAG_GOOS="+n.upFlagGOOS,
  955. "TS_LOGS_DIR="+n.env.t.TempDir(),
  956. )
  957. if *verboseTailscale {
  958. cmd.Stdout = os.Stdout
  959. cmd.Stderr = os.Stderr
  960. }
  961. return cmd
  962. }
  963. func (n *TestNode) Status() (*ipnstate.Status, error) {
  964. cmd := n.Tailscale("status", "--json")
  965. cmd.Stdout = nil // in case --verbose-tailscale was set
  966. cmd.Stderr = nil // in case --verbose-tailscale was set
  967. out, err := cmd.CombinedOutput()
  968. if err != nil {
  969. return nil, fmt.Errorf("running tailscale status: %v, %s", err, out)
  970. }
  971. st := new(ipnstate.Status)
  972. if err := json.Unmarshal(out, st); err != nil {
  973. return nil, fmt.Errorf("decoding tailscale status JSON: %w\njson:\n%s", err, out)
  974. }
  975. return st, nil
  976. }
  977. func (n *TestNode) MustStatus() *ipnstate.Status {
  978. tb := n.env.t
  979. tb.Helper()
  980. st, err := n.Status()
  981. if err != nil {
  982. tb.Fatal(err)
  983. }
  984. return st
  985. }
  986. // PublicKey returns the hex-encoded public key of this node,
  987. // e.g. `nodekey:123456abc`
  988. func (n *TestNode) PublicKey() string {
  989. tb := n.env.t
  990. tb.Helper()
  991. cmd := n.Tailscale("status", "--json")
  992. out, err := cmd.CombinedOutput()
  993. if err != nil {
  994. tb.Fatalf("running `tailscale status`: %v, %s", err, out)
  995. }
  996. type Self struct{ PublicKey string }
  997. type StatusOutput struct{ Self Self }
  998. var st StatusOutput
  999. if err := json.Unmarshal(out, &st); err != nil {
  1000. tb.Fatalf("decoding `tailscale status` JSON: %v\njson:\n%s", err, out)
  1001. }
  1002. return st.Self.PublicKey
  1003. }
  1004. // NLPublicKey returns the hex-encoded network lock public key of
  1005. // this node, e.g. `tlpub:123456abc`
  1006. func (n *TestNode) NLPublicKey() string {
  1007. tb := n.env.t
  1008. tb.Helper()
  1009. cmd := n.Tailscale("lock", "status", "--json")
  1010. out, err := cmd.CombinedOutput()
  1011. if err != nil {
  1012. tb.Fatalf("running `tailscale lock status`: %v, %s", err, out)
  1013. }
  1014. st := struct {
  1015. PublicKey string `json:"PublicKey"`
  1016. }{}
  1017. if err := json.Unmarshal(out, &st); err != nil {
  1018. tb.Fatalf("decoding `tailscale lock status` JSON: %v\njson:\n%s", err, out)
  1019. }
  1020. return st.PublicKey
  1021. }
  1022. // trafficTrap is an HTTP proxy handler to note whether any
  1023. // HTTP traffic tries to leave localhost from tailscaled. We don't
  1024. // expect any, so any request triggers a failure.
  1025. type trafficTrap struct {
  1026. atomicErr syncs.AtomicValue[error]
  1027. }
  1028. func (tt *trafficTrap) Err() error {
  1029. return tt.atomicErr.Load()
  1030. }
  1031. func (tt *trafficTrap) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  1032. var got bytes.Buffer
  1033. r.Write(&got)
  1034. err := fmt.Errorf("unexpected HTTP request via proxy: %s", got.Bytes())
  1035. MainError.Store(err)
  1036. if tt.Err() == nil {
  1037. // Best effort at remembering the first request.
  1038. tt.atomicErr.Store(err)
  1039. }
  1040. log.Printf("Error: %v", err)
  1041. w.WriteHeader(403)
  1042. }
  1043. type authURLParserWriter struct {
  1044. t *testing.T
  1045. buf bytes.Buffer
  1046. // Handle login URLs, and count how many times they were seen
  1047. authURLFn func(urlStr string) error
  1048. // Handle machine approval URLs, and count how many times they were seen.
  1049. deviceApprovalURLFn func(urlStr string) error
  1050. }
  1051. // Note: auth URLs from testcontrol look slightly different to real auth URLs,
  1052. // e.g. http://127.0.0.1:60456/auth/96af2ff7e04ae1499a9a
  1053. var authURLRx = regexp.MustCompile(`(https?://\S+/auth/\S+)`)
  1054. // Looks for any device approval URL, which is any URL ending with `/admin`
  1055. // e.g. http://127.0.0.1:60456/admin
  1056. var deviceApprovalURLRx = regexp.MustCompile(`(https?://\S+/admin)[^\S]`)
  1057. func (w *authURLParserWriter) Write(p []byte) (n int, err error) {
  1058. w.t.Helper()
  1059. w.t.Logf("received bytes: %s", string(p))
  1060. n, err = w.buf.Write(p)
  1061. defer w.buf.Reset() // so it's not matched again
  1062. m := authURLRx.FindSubmatch(w.buf.Bytes())
  1063. if m != nil {
  1064. urlStr := string(m[1])
  1065. if err := w.authURLFn(urlStr); err != nil {
  1066. return 0, err
  1067. }
  1068. }
  1069. m = deviceApprovalURLRx.FindSubmatch(w.buf.Bytes())
  1070. if m != nil && w.deviceApprovalURLFn != nil {
  1071. urlStr := string(m[1])
  1072. if err := w.deviceApprovalURLFn(urlStr); err != nil {
  1073. return 0, err
  1074. }
  1075. }
  1076. return n, err
  1077. }