2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2021-05-31 16:35:01 +01:00
|
|
|
|
|
|
|
// Package integration contains Tailscale integration tests.
|
|
|
|
//
|
|
|
|
// This package is considered internal and the public API is subject
|
|
|
|
// to change without notice.
|
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
2021-07-08 19:39:45 +01:00
|
|
|
"bytes"
|
2021-06-25 20:59:45 +01:00
|
|
|
"crypto/tls"
|
2021-07-08 19:39:45 +01:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
2021-06-25 20:59:45 +01:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2021-05-31 16:35:01 +01:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-07-08 19:39:45 +01:00
|
|
|
"go4.org/mem"
|
2021-06-25 20:59:45 +01:00
|
|
|
"tailscale.com/derp"
|
|
|
|
"tailscale.com/derp/derphttp"
|
|
|
|
"tailscale.com/net/stun/stuntest"
|
|
|
|
"tailscale.com/tailcfg"
|
|
|
|
"tailscale.com/types/key"
|
|
|
|
"tailscale.com/types/logger"
|
2023-03-01 03:00:00 +00:00
|
|
|
"tailscale.com/types/logid"
|
2021-06-25 20:59:45 +01:00
|
|
|
"tailscale.com/types/nettype"
|
2024-03-21 19:20:38 +00:00
|
|
|
"tailscale.com/util/zstdframe"
|
2021-05-31 16:35:01 +01:00
|
|
|
"tailscale.com/version"
|
|
|
|
)
|
|
|
|
|
2021-07-20 21:55:09 +01:00
|
|
|
// CleanupBinaries cleans up any resources created by calls to BinaryDir, TailscaleBinary, or TailscaledBinary.
|
|
|
|
// It should be called from TestMain after all tests have completed.
|
|
|
|
func CleanupBinaries() {
|
|
|
|
buildOnce.Do(func() {})
|
|
|
|
if binDir != "" {
|
|
|
|
os.RemoveAll(binDir)
|
|
|
|
}
|
2021-05-31 16:35:01 +01:00
|
|
|
}
|
|
|
|
|
2021-07-20 21:55:09 +01:00
|
|
|
// BinaryDir returns a directory containing test tailscale and tailscaled binaries.
|
|
|
|
// If any test calls BinaryDir, there must be a TestMain function that calls
|
|
|
|
// CleanupBinaries after all tests are complete.
|
|
|
|
func BinaryDir(tb testing.TB) string {
|
|
|
|
buildOnce.Do(func() {
|
|
|
|
binDir, buildErr = buildTestBinaries()
|
|
|
|
})
|
|
|
|
if buildErr != nil {
|
|
|
|
tb.Fatal(buildErr)
|
2021-05-31 16:35:01 +01:00
|
|
|
}
|
2021-07-20 21:55:09 +01:00
|
|
|
return binDir
|
2021-05-31 16:35:01 +01:00
|
|
|
}
|
|
|
|
|
2021-07-20 21:55:09 +01:00
|
|
|
// TailscaleBinary returns the path to the test tailscale binary.
|
|
|
|
// If any test calls TailscaleBinary, there must be a TestMain function that calls
|
|
|
|
// CleanupBinaries after all tests are complete.
|
|
|
|
func TailscaleBinary(tb testing.TB) string {
|
|
|
|
return filepath.Join(BinaryDir(tb), "tailscale"+exe())
|
|
|
|
}
|
2021-05-31 16:35:01 +01:00
|
|
|
|
2021-07-20 21:55:09 +01:00
|
|
|
// TailscaledBinary returns the path to the test tailscaled binary.
|
|
|
|
// If any test calls TailscaleBinary, there must be a TestMain function that calls
|
|
|
|
// CleanupBinaries after all tests are complete.
|
|
|
|
func TailscaledBinary(tb testing.TB) string {
|
|
|
|
return filepath.Join(BinaryDir(tb), "tailscaled"+exe())
|
|
|
|
}
|
2021-05-31 16:35:01 +01:00
|
|
|
|
2021-07-20 21:55:09 +01:00
|
|
|
var (
|
|
|
|
buildOnce sync.Once
|
|
|
|
buildErr error
|
|
|
|
binDir string
|
|
|
|
)
|
2021-05-31 16:35:01 +01:00
|
|
|
|
2021-07-20 21:55:09 +01:00
|
|
|
// buildTestBinaries builds tailscale and tailscaled.
|
|
|
|
// It returns the dir containing the binaries.
|
|
|
|
func buildTestBinaries() (string, error) {
|
2022-09-15 13:06:59 +01:00
|
|
|
bindir, err := os.MkdirTemp("", "")
|
2021-07-20 21:55:09 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
err = build(bindir, "tailscale.com/cmd/tailscaled", "tailscale.com/cmd/tailscale")
|
|
|
|
if err != nil {
|
|
|
|
os.RemoveAll(bindir)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return bindir, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func build(outDir string, targets ...string) error {
|
|
|
|
goBin, err := findGo()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-31 16:35:01 +01:00
|
|
|
cmd := exec.Command(goBin, "install")
|
|
|
|
if version.IsRace() {
|
|
|
|
cmd.Args = append(cmd.Args, "-race")
|
|
|
|
}
|
|
|
|
cmd.Args = append(cmd.Args, targets...)
|
|
|
|
cmd.Env = append(os.Environ(), "GOARCH="+runtime.GOARCH, "GOBIN="+outDir)
|
|
|
|
errOut, err := cmd.CombinedOutput()
|
|
|
|
if err == nil {
|
2021-07-20 21:55:09 +01:00
|
|
|
return nil
|
2021-05-31 16:35:01 +01:00
|
|
|
}
|
|
|
|
if strings.Contains(string(errOut), "when GOBIN is set") {
|
|
|
|
// Fallback slow path for cross-compiled binaries.
|
|
|
|
for _, target := range targets {
|
|
|
|
outFile := filepath.Join(outDir, path.Base(target)+exe())
|
2023-10-14 03:22:36 +01:00
|
|
|
cmd := exec.Command(goBin, "build", "-o", outFile)
|
|
|
|
if version.IsRace() {
|
|
|
|
cmd.Args = append(cmd.Args, "-race")
|
|
|
|
}
|
|
|
|
cmd.Args = append(cmd.Args, target)
|
2021-05-31 16:35:01 +01:00
|
|
|
cmd.Env = append(os.Environ(), "GOARCH="+runtime.GOARCH)
|
|
|
|
if errOut, err := cmd.CombinedOutput(); err != nil {
|
2021-07-20 21:55:09 +01:00
|
|
|
return fmt.Errorf("failed to build %v with %v: %v, %s", target, goBin, err, errOut)
|
2021-05-31 16:35:01 +01:00
|
|
|
}
|
|
|
|
}
|
2021-07-20 21:55:09 +01:00
|
|
|
return nil
|
2021-05-31 16:35:01 +01:00
|
|
|
}
|
2021-07-20 21:55:09 +01:00
|
|
|
return fmt.Errorf("failed to build %v with %v: %v, %s", targets, goBin, err, errOut)
|
2021-05-31 16:35:01 +01:00
|
|
|
}
|
|
|
|
|
2021-07-20 21:55:09 +01:00
|
|
|
func findGo() (string, error) {
|
2023-02-14 22:56:31 +00:00
|
|
|
// Go 1.19 attempted to be helpful by prepending $PATH with GOROOT/bin based
|
|
|
|
// on the executed go binary when invoked using `go test` or `go generate`,
|
|
|
|
// however, this doesn't cover cases when run otherwise, such as via `go run`.
|
|
|
|
// runtime.GOROOT() may often be empty these days, so the safe thing to do
|
|
|
|
// here is, in order:
|
|
|
|
// 1. Look for a go binary in $PATH[0].
|
|
|
|
// 2. Look for a go binary in runtime.GOROOT()/bin if runtime.GOROOT() is non-empty.
|
|
|
|
// 3. Look for a go binary in $PATH.
|
|
|
|
|
2023-10-13 20:29:28 +01:00
|
|
|
// For tests we want to run as root on GitHub actions, we run with -exec=sudo,
|
|
|
|
// but that results in this test running with a different PATH and picking the
|
|
|
|
// wrong Go. So hard code the GitHub Actions case.
|
|
|
|
if os.Getuid() == 0 && os.Getenv("GITHUB_ACTIONS") == "true" {
|
|
|
|
const sudoGithubGo = "/home/runner/.cache/tailscale-go/bin/go"
|
|
|
|
if _, err := os.Stat(sudoGithubGo); err == nil {
|
|
|
|
return sudoGithubGo, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-14 22:56:31 +00:00
|
|
|
paths := strings.FieldsFunc(os.Getenv("PATH"), func(r rune) bool { return os.IsPathSeparator(uint8(r)) })
|
|
|
|
if len(paths) > 0 {
|
|
|
|
candidate := filepath.Join(paths[0], "go"+exe())
|
|
|
|
if path, err := exec.LookPath(candidate); err == nil {
|
|
|
|
return path, err
|
2021-05-31 16:35:01 +01:00
|
|
|
}
|
|
|
|
}
|
2023-02-14 22:56:31 +00:00
|
|
|
|
|
|
|
if runtime.GOROOT() != "" {
|
|
|
|
candidate := filepath.Join(runtime.GOROOT(), "bin", "go"+exe())
|
|
|
|
if path, err := exec.LookPath(candidate); err == nil {
|
|
|
|
return path, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return exec.LookPath("go")
|
2021-05-31 16:35:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func exe() string {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return ".exe"
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2021-06-25 20:59:45 +01:00
|
|
|
|
|
|
|
// RunDERPAndSTUN runs a local DERP and STUN server for tests, returning the derpMap
|
|
|
|
// that clients should use. This creates resources that must be cleaned up with the
|
|
|
|
// returned cleanup function.
|
|
|
|
func RunDERPAndSTUN(t testing.TB, logf logger.Logf, ipAddress string) (derpMap *tailcfg.DERPMap) {
|
|
|
|
t.Helper()
|
|
|
|
|
2021-10-28 23:42:50 +01:00
|
|
|
d := derp.NewServer(key.NewNode(), logf)
|
2021-06-25 20:59:45 +01:00
|
|
|
|
2021-08-30 14:38:53 +01:00
|
|
|
ln, err := net.Listen("tcp", net.JoinHostPort(ipAddress, "0"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-06-25 20:59:45 +01:00
|
|
|
httpsrv := httptest.NewUnstartedServer(derphttp.Handler(d))
|
2024-08-07 01:33:45 +01:00
|
|
|
httpsrv.Listener.Close()
|
2021-08-30 14:38:53 +01:00
|
|
|
httpsrv.Listener = ln
|
2021-06-25 20:59:45 +01:00
|
|
|
httpsrv.Config.ErrorLog = logger.StdLogger(logf)
|
|
|
|
httpsrv.Config.TLSNextProto = make(map[string]func(*http.Server, *tls.Conn, http.Handler))
|
|
|
|
httpsrv.StartTLS()
|
|
|
|
|
|
|
|
stunAddr, stunCleanup := stuntest.ServeWithPacketListener(t, nettype.Std{})
|
|
|
|
|
|
|
|
m := &tailcfg.DERPMap{
|
|
|
|
Regions: map[int]*tailcfg.DERPRegion{
|
|
|
|
1: {
|
|
|
|
RegionID: 1,
|
|
|
|
RegionCode: "test",
|
|
|
|
Nodes: []*tailcfg.DERPNode{
|
|
|
|
{
|
2021-07-09 19:16:43 +01:00
|
|
|
Name: "t1",
|
|
|
|
RegionID: 1,
|
|
|
|
HostName: ipAddress,
|
|
|
|
IPv4: ipAddress,
|
|
|
|
IPv6: "none",
|
|
|
|
STUNPort: stunAddr.Port,
|
|
|
|
DERPPort: httpsrv.Listener.Addr().(*net.TCPAddr).Port,
|
|
|
|
InsecureForTests: true,
|
2021-08-30 14:38:53 +01:00
|
|
|
STUNTestIP: ipAddress,
|
2021-06-25 20:59:45 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-08-30 14:38:53 +01:00
|
|
|
t.Logf("DERP httpsrv listener: %v", httpsrv.Listener.Addr())
|
|
|
|
|
2021-06-25 20:59:45 +01:00
|
|
|
t.Cleanup(func() {
|
|
|
|
httpsrv.CloseClientConnections()
|
|
|
|
httpsrv.Close()
|
|
|
|
d.Close()
|
|
|
|
stunCleanup()
|
2021-08-30 14:38:53 +01:00
|
|
|
ln.Close()
|
2021-06-25 20:59:45 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
2021-07-08 19:39:45 +01:00
|
|
|
|
|
|
|
// LogCatcher is a minimal logcatcher for the logtail upload client.
|
|
|
|
type LogCatcher struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
logf logger.Logf
|
|
|
|
buf bytes.Buffer
|
|
|
|
gotErr error
|
|
|
|
reqs int
|
2022-02-18 21:00:08 +00:00
|
|
|
raw bool // indicates whether to store the raw JSON logs uploaded, instead of just the text
|
2021-07-08 19:39:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// UseLogf makes the logcatcher implementation use a given logf function
|
|
|
|
// to dump all logs to.
|
|
|
|
func (lc *LogCatcher) UseLogf(fn logger.Logf) {
|
|
|
|
lc.mu.Lock()
|
|
|
|
defer lc.mu.Unlock()
|
|
|
|
lc.logf = fn
|
|
|
|
}
|
|
|
|
|
2022-02-18 21:00:08 +00:00
|
|
|
// StoreRawJSON instructs lc to save the raw JSON uploads, rather than just the text.
|
|
|
|
func (lc *LogCatcher) StoreRawJSON() {
|
|
|
|
lc.mu.Lock()
|
|
|
|
defer lc.mu.Unlock()
|
|
|
|
lc.raw = true
|
|
|
|
}
|
|
|
|
|
2021-07-08 19:39:45 +01:00
|
|
|
func (lc *LogCatcher) logsContains(sub mem.RO) bool {
|
|
|
|
lc.mu.Lock()
|
|
|
|
defer lc.mu.Unlock()
|
|
|
|
return mem.Contains(mem.B(lc.buf.Bytes()), sub)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lc *LogCatcher) numRequests() int {
|
|
|
|
lc.mu.Lock()
|
|
|
|
defer lc.mu.Unlock()
|
|
|
|
return lc.reqs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lc *LogCatcher) logsString() string {
|
|
|
|
lc.mu.Lock()
|
|
|
|
defer lc.mu.Unlock()
|
|
|
|
return lc.buf.String()
|
|
|
|
}
|
|
|
|
|
2021-07-16 23:21:00 +01:00
|
|
|
// Reset clears the buffered logs from memory.
|
|
|
|
func (lc *LogCatcher) Reset() {
|
|
|
|
lc.mu.Lock()
|
|
|
|
defer lc.mu.Unlock()
|
|
|
|
lc.buf.Reset()
|
|
|
|
}
|
|
|
|
|
2021-07-08 19:39:45 +01:00
|
|
|
func (lc *LogCatcher) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2021-08-30 14:46:59 +01:00
|
|
|
// POST /c/<collection-name>/<private-ID>
|
|
|
|
if r.Method != "POST" {
|
|
|
|
log.Printf("bad logcatcher method: %v", r.Method)
|
|
|
|
http.Error(w, "only POST is supported", 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pathParts := strings.Split(strings.TrimPrefix(r.URL.Path, "/c/"), "/")
|
|
|
|
if len(pathParts) != 2 {
|
|
|
|
log.Printf("bad logcatcher path: %q", r.URL.Path)
|
|
|
|
http.Error(w, "bad URL", 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// collectionName := pathPaths[0]
|
2023-03-01 03:00:00 +00:00
|
|
|
privID, err := logid.ParsePrivateID(pathParts[1])
|
2021-08-30 14:46:59 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("bad log ID: %q: %v", r.URL.Path, err)
|
|
|
|
}
|
|
|
|
|
2024-03-21 19:20:38 +00:00
|
|
|
bodyBytes, err := io.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("http.Request.Body.Read: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2021-07-08 19:39:45 +01:00
|
|
|
if r.Header.Get("Content-Encoding") == "zstd" {
|
2024-03-21 19:20:38 +00:00
|
|
|
bodyBytes, err = zstdframe.AppendDecode(nil, bodyBytes)
|
2021-07-08 19:39:45 +01:00
|
|
|
if err != nil {
|
2024-03-21 19:20:38 +00:00
|
|
|
log.Printf("zstdframe.AppendDecode: %v", err)
|
2021-07-08 19:39:45 +01:00
|
|
|
http.Error(w, err.Error(), 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Entry struct {
|
|
|
|
Logtail struct {
|
|
|
|
ClientTime time.Time `json:"client_time"`
|
|
|
|
ServerTime time.Time `json:"server_time"`
|
|
|
|
Error struct {
|
|
|
|
BadData string `json:"bad_data"`
|
|
|
|
} `json:"error"`
|
|
|
|
} `json:"logtail"`
|
|
|
|
Text string `json:"text"`
|
|
|
|
}
|
|
|
|
var jreq []Entry
|
|
|
|
if len(bodyBytes) > 0 && bodyBytes[0] == '[' {
|
|
|
|
err = json.Unmarshal(bodyBytes, &jreq)
|
|
|
|
} else {
|
|
|
|
var ent Entry
|
|
|
|
err = json.Unmarshal(bodyBytes, &ent)
|
|
|
|
jreq = append(jreq, ent)
|
|
|
|
}
|
|
|
|
|
|
|
|
lc.mu.Lock()
|
|
|
|
defer lc.mu.Unlock()
|
|
|
|
lc.reqs++
|
|
|
|
if lc.gotErr == nil && err != nil {
|
|
|
|
lc.gotErr = err
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(&lc.buf, "error from %s of %#q: %v\n", r.Method, bodyBytes, err)
|
2021-08-30 14:46:59 +01:00
|
|
|
if lc.logf != nil {
|
|
|
|
lc.logf("error from %s of %#q: %v\n", r.Method, bodyBytes, err)
|
|
|
|
}
|
2021-07-08 19:39:45 +01:00
|
|
|
} else {
|
2021-08-30 14:46:59 +01:00
|
|
|
id := privID.Public().String()[:3] // good enough for integration tests
|
2021-07-08 19:39:45 +01:00
|
|
|
for _, ent := range jreq {
|
2022-02-18 21:00:08 +00:00
|
|
|
if lc.raw {
|
|
|
|
lc.buf.Write(bodyBytes)
|
|
|
|
continue
|
|
|
|
}
|
2021-07-08 19:39:45 +01:00
|
|
|
fmt.Fprintf(&lc.buf, "%s\n", strings.TrimSpace(ent.Text))
|
|
|
|
if lc.logf != nil {
|
2021-08-30 14:46:59 +01:00
|
|
|
lc.logf("logcatch:%s: %s", id, strings.TrimSpace(ent.Text))
|
2021-07-08 19:39:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.WriteHeader(200) // must have no content, but not a 204
|
|
|
|
}
|