tailscale/tstest/resource_test.go
Will Norris 3ec5be3f51 all: remove AUTHORS file and references to it
This file was never truly necessary and has never actually been used in
the history of Tailscale's open source releases.

A Brief History of AUTHORS files
---

The AUTHORS file was a pattern developed at Google, originally for
Chromium, then adopted by Go and a bunch of other projects. The problem
was that Chromium originally had a copyright line only recognizing
Google as the copyright holder. Because Google (and most open source
projects) do not require copyright assignemnt for contributions, each
contributor maintains their copyright. Some large corporate contributors
then tried to add their own name to the copyright line in the LICENSE
file or in file headers. This quickly becomes unwieldy, and puts a
tremendous burden on anyone building on top of Chromium, since the
license requires that they keep all copyright lines intact.

The compromise was to create an AUTHORS file that would list all of the
copyright holders. The LICENSE file and source file headers would then
include that list by reference, listing the copyright holder as "The
Chromium Authors".

This also become cumbersome to simply keep the file up to date with a
high rate of new contributors. Plus it's not always obvious who the
copyright holder is. Sometimes it is the individual making the
contribution, but many times it may be their employer. There is no way
for the proejct maintainer to know.

Eventually, Google changed their policy to no longer recommend trying to
keep the AUTHORS file up to date proactively, and instead to only add to
it when requested: https://opensource.google/docs/releasing/authors.
They are also clear that:

> Adding contributors to the AUTHORS file is entirely within the
> project's discretion and has no implications for copyright ownership.

It was primarily added to appease a small number of large contributors
that insisted that they be recognized as copyright holders (which was
entirely their right to do). But it's not truly necessary, and not even
the most accurate way of identifying contributors and/or copyright
holders.

In practice, we've never added anyone to our AUTHORS file. It only lists
Tailscale, so it's not really serving any purpose. It also causes
confusion because Tailscalars put the "Tailscale Inc & AUTHORS" header
in other open source repos which don't actually have an AUTHORS file, so
it's ambiguous what that means.

Instead, we just acknowledge that the contributors to Tailscale (whoever
they are) are copyright holders for their individual contributions. We
also have the benefit of using the DCO (developercertificate.org) which
provides some additional certification of their right to make the
contribution.

The source file changes were purely mechanical with:

    git ls-files | xargs sed -i -e 's/\(Tailscale Inc &\) AUTHORS/\1 contributors/g'

Updates #cleanup

Change-Id: Ia101a4a3005adb9118051b3416f5a64a4a45987d
Signed-off-by: Will Norris <will@tailscale.com>
2026-01-23 15:49:45 -08:00

257 lines
7.3 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
package tstest
import (
"strings"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestPrintGoroutines(t *testing.T) {
tests := []struct {
name string
in string
want string
}{
{
name: "empty",
in: "goroutine profile: total 0\n",
want: "goroutine profile: total 0",
},
{
name: "single goroutine",
in: `goroutine profile: total 1
1 @ 0x47bc0e 0x458e57 0x847587 0x483da1
# 0x847586 database/sql.(*DB).connectionOpener+0x86 database/sql/sql.go:1261
`,
want: `goroutine profile: total 1
1 @ 0x47bc0e 0x458e57 0x847587 0x483da1
# 0x847586 database/sql.(*DB).connectionOpener+0x86 database/sql/sql.go:1261
`,
},
{
name: "multiple goroutines sorted",
in: `goroutine profile: total 14
7 @ 0x47bc0e 0x413705 0x4132b2 0x10fda4d 0x483da1
# 0x10fda4c github.com/user/pkg.RoutineA+0x16c pkg/a.go:443
7 @ 0x47bc0e 0x458e57 0x754927 0x483da1
# 0x754926 net/http.(*persistConn).writeLoop+0xe6 net/http/transport.go:2596
`,
want: `goroutine profile: total 14
7 @ 0x47bc0e 0x413705 0x4132b2 0x10fda4d 0x483da1
# 0x10fda4c github.com/user/pkg.RoutineA+0x16c pkg/a.go:443
7 @ 0x47bc0e 0x458e57 0x754927 0x483da1
# 0x754926 net/http.(*persistConn).writeLoop+0xe6 net/http/transport.go:2596
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := string(printGoroutines(parseGoroutines([]byte(tt.in))))
if got != tt.want {
t.Errorf("printGoroutines() = %q, want %q, diff:\n%s", got, tt.want, cmp.Diff(tt.want, got))
}
})
}
}
func TestDiffPprofGoroutines(t *testing.T) {
tests := []struct {
name string
x, y string
want string
}{
{
name: "no difference",
x: `goroutine profile: total 1
1 @ 0x47bc0e 0x458e57 0x847587 0x483da1
# 0x847586 database/sql.(*DB).connectionOpener+0x86 database/sql/sql.go:1261`,
y: `goroutine profile: total 1
1 @ 0x47bc0e 0x458e57 0x847587 0x483da1
# 0x847586 database/sql.(*DB).connectionOpener+0x86 database/sql/sql.go:1261
`,
want: "",
},
{
name: "different counts",
x: `goroutine profile: total 1
1 @ 0x47bc0e 0x458e57 0x847587 0x483da1
# 0x847586 database/sql.(*DB).connectionOpener+0x86 database/sql/sql.go:1261
`,
y: `goroutine profile: total 2
2 @ 0x47bc0e 0x458e57 0x847587 0x483da1
# 0x847586 database/sql.(*DB).connectionOpener+0x86 database/sql/sql.go:1261
`,
want: `- goroutine profile: total 1
+ goroutine profile: total 2
- 1 @ 0x47bc0e 0x458e57 0x847587 0x483da1
+ 2 @ 0x47bc0e 0x458e57 0x847587 0x483da1
# 0x847586 database/sql.(*DB).connectionOpener+0x86 database/sql/sql.go:1261
`,
},
{
name: "new goroutine",
x: `goroutine profile: total 1
1 @ 0x47bc0e 0x458e57 0x847587 0x483da1
# 0x847586 database/sql.(*DB).connectionOpener+0x86 database/sql/sql.go:1261
`,
y: `goroutine profile: total 2
1 @ 0x47bc0e 0x458e57 0x847587 0x483da1
# 0x847586 database/sql.(*DB).connectionOpener+0x86 database/sql/sql.go:1261
1 @ 0x47bc0e 0x458e57 0x754927 0x483da1
# 0x754926 net/http.(*persistConn).writeLoop+0xe6 net/http/transport.go:2596
`,
want: `- goroutine profile: total 1
+ goroutine profile: total 2
+ 1 @ 0x47bc0e 0x458e57 0x754927 0x483da1
+ # 0x754926 net/http.(*persistConn).writeLoop+0xe6 net/http/transport.go:2596
`,
},
{
name: "removed goroutine",
x: `goroutine profile: total 2
1 @ 0x47bc0e 0x458e57 0x847587 0x483da1
# 0x847586 database/sql.(*DB).connectionOpener+0x86 database/sql/sql.go:1261
1 @ 0x47bc0e 0x458e57 0x754927 0x483da1
# 0x754926 net/http.(*persistConn).writeLoop+0xe6 net/http/transport.go:2596
`,
y: `goroutine profile: total 1
1 @ 0x47bc0e 0x458e57 0x847587 0x483da1
# 0x847586 database/sql.(*DB).connectionOpener+0x86 database/sql/sql.go:1261
`,
want: `- goroutine profile: total 2
+ goroutine profile: total 1
- 1 @ 0x47bc0e 0x458e57 0x754927 0x483da1
- # 0x754926 net/http.(*persistConn).writeLoop+0xe6 net/http/transport.go:2596
`,
},
{
name: "removed many goroutine",
x: `goroutine profile: total 2
1 @ 0x47bc0e 0x458e57 0x847587 0x483da1
# 0x847586 database/sql.(*DB).connectionOpener+0x86 database/sql/sql.go:1261
1 @ 0x47bc0e 0x458e57 0x754927 0x483da1
# 0x754926 net/http.(*persistConn).writeLoop+0xe6 net/http/transport.go:2596
`,
y: `goroutine profile: total 0`,
want: `- goroutine profile: total 2
+ goroutine profile: total 0
- 1 @ 0x47bc0e 0x458e57 0x754927 0x483da1
- # 0x754926 net/http.(*persistConn).writeLoop+0xe6 net/http/transport.go:2596
- 1 @ 0x47bc0e 0x458e57 0x847587 0x483da1
- # 0x847586 database/sql.(*DB).connectionOpener+0x86 database/sql/sql.go:1261
`,
},
{
name: "invalid input x",
x: "invalid",
y: "goroutine profile: total 0\n",
want: "- invalid\n+ goroutine profile: total 0\n",
},
{
name: "invalid input y",
x: "goroutine profile: total 0\n",
y: "invalid",
want: "- goroutine profile: total 0\n+ invalid\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := diffGoroutines(
parseGoroutines([]byte(tt.x)),
parseGoroutines([]byte(tt.y)),
)
if got != tt.want {
t.Errorf("diffPprofGoroutines() diff:\ngot:\n%s\nwant:\n%s\ndiff (-want +got):\n%s", got, tt.want, cmp.Diff(tt.want, got))
}
})
}
}
func TestParseGoroutines(t *testing.T) {
tests := []struct {
name string
in string
wantHeader string
wantCount int
}{
{
name: "empty profile",
in: "goroutine profile: total 0\n",
wantHeader: "goroutine profile: total 0",
wantCount: 0,
},
{
name: "single goroutine",
in: `goroutine profile: total 1
1 @ 0x47bc0e 0x458e57 0x847587 0x483da1
# 0x847586 database/sql.(*DB).connectionOpener+0x86 database/sql/sql.go:1261
`,
wantHeader: "goroutine profile: total 1",
wantCount: 1,
},
{
name: "multiple goroutines",
in: `goroutine profile: total 14
7 @ 0x47bc0e 0x413705 0x4132b2 0x10fda4d 0x483da1
# 0x10fda4c github.com/user/pkg.RoutineA+0x16c pkg/a.go:443
7 @ 0x47bc0e 0x458e57 0x754927 0x483da1
# 0x754926 net/http.(*persistConn).writeLoop+0xe6 net/http/transport.go:2596
`,
wantHeader: "goroutine profile: total 14",
wantCount: 2,
},
{
name: "invalid format",
in: "invalid",
wantHeader: "invalid",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := parseGoroutines([]byte(tt.in))
if got := string(g.head); got != tt.wantHeader {
t.Errorf("parseGoroutines() header = %q, want %q", got, tt.wantHeader)
}
if got := len(g.goroutines); got != tt.wantCount {
t.Errorf("parseGoroutines() goroutine count = %d, want %d", got, tt.wantCount)
}
// Verify that the sort field is correctly reversed
for _, g := range g.goroutines {
original := strings.Fields(string(g.header))
sorted := strings.Fields(string(g.sort))
if len(original) != len(sorted) {
t.Errorf("sort field has different number of words: got %d, want %d", len(sorted), len(original))
continue
}
for i := 0; i < len(original); i++ {
if original[i] != sorted[len(sorted)-1-i] {
t.Errorf("sort field word mismatch at position %d: got %q, want %q", i, sorted[len(sorted)-1-i], original[i])
}
}
}
})
}
}