Files
tailscale/version/version_test.go
Brad Fitzpatrick c3736ae41a Dockerfile: use our own Go toolchain, not the golang Docker image
The golang:N-alpine Docker image lags behind Go minor releases by a
day or two. When we bump go.mod to a new minor version before the
image catches up, the required "Build Docker image" CI check fails
with "go.mod requires go >= 1.27.1 (running go 1.27.0;
GOTOOLCHAIN=local)" and blocks the toolchain bump from merging.

Instead, base the build stage on plain alpine and download the
Tailscale Go toolchain release for the revision in go.toolchain.rev,
matching how everything else in this repo is built.

Fixes #21072

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
Change-Id: I1ccb77fa3c7a49532ea87dbdbf9e3340880ec94e
2026-09-01 15:47:33 -07:00

74 lines
1.7 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
package version_test
import (
"bytes"
"os"
"path"
"runtime/debug"
"testing"
ts "tailscale.com"
"tailscale.com/version"
)
func TestAlpineTag(t *testing.T) {
if tag := readAlpineTag(t, "../Dockerfile.base"); tag == "" {
t.Fatal(`"FROM alpine:" not found in Dockerfile.base`)
} else if tag != ts.AlpineDockerTag {
t.Errorf("alpine version mismatch: Dockerfile.base has %q; ALPINE.txt has %q", tag, ts.AlpineDockerTag)
}
if tag := readAlpineTag(t, "../Dockerfile"); tag == "" {
t.Fatal(`"FROM alpine:" not found in Dockerfile`)
} else if tag != ts.AlpineDockerTag {
t.Errorf("alpine version mismatch: Dockerfile has %q; ALPINE.txt has %q", tag, ts.AlpineDockerTag)
}
}
func readAlpineTag(t *testing.T, file string) string {
f, err := os.ReadFile(file)
if err != nil {
t.Fatal(err)
}
for line := range bytes.SplitSeq(f, []byte{'\n'}) {
line = bytes.TrimSpace(line)
_, suf, ok := bytes.Cut(line, []byte("FROM alpine:"))
if !ok {
continue
}
// Drop anything after the tag, such as an "AS stage-name" suffix.
tag, _, _ := bytes.Cut(suf, []byte(" "))
return string(tag)
}
return ""
}
func TestShortAllocs(t *testing.T) {
allocs := int(testing.AllocsPerRun(10000, func() {
_ = version.Short()
}))
if allocs > 0 {
t.Errorf("allocs = %v; want 0", allocs)
}
}
func BenchmarkCmdName(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_ = version.CmdName()
}
}
func BenchmarkReadBuildInfo(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
info, ok := debug.ReadBuildInfo()
if !ok {
b.Fatal("ReadBuildInfo failed")
}
_ = path.Base(info.Path)
}
}