1
0
mirror of https://github.com/golang/go synced 2024-11-07 04:16:10 -07:00
go/misc/reboot/reboot_test.go
Daniel Martí ef06a5f44a misc/reboot: don't use symlinks when copying GOROOT/src
go:embed disallows using symlinked files by design.
crypto/elliptic is the first std package to use it as of CL 380475,
and unfortunately that broke the TestRepeatBootstrap long test.

The reason it uses symlinks is for speed; it wants to copy GOROOT/src,
but regular files aren't going to be modified in any way,
so a symlink, if supported, means not needing to copy the contents.

Replace the symlink attempt with hard links,
which will mean regular files remain as such, fixing go:embed.
It's worth noting that on many systems hard links won't work,
as the temporary filesystem tends to be separate,
but it doesn't hurt to try.

In my system, where /tmp is tmpfs, the test now copies more bytes.
With the added Logf, I can see overlayDir goes from ~30ms to ~100ms.
This makes sense, as GOROOT/src currently weighs around 100MiB.
To alleviate that slow-down, stop copying testdata directories,
as they currently weigh around 20MiB and aren't needed for the test.
This gets overlayDir on my system down to an acceptable ~70ms.

I briefly considered teaching overlayDir what files can be symlinks,
but that seemed fairly complex long-term, as any file could be embedded.

While here, start using testing.T.TempDir and fs.WalkDir.

For #50995.

Change-Id: I17947e6bdee96237e1ca0606ad0b95e7c5987bc1
Reviewed-on: https://go-review.googlesource.com/c/go/+/383995
Trust: Daniel Martí <mvdan@mvdan.cc>
Trust: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2022-02-08 15:30:12 +00:00

55 lines
1.3 KiB
Go

// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package reboot_test verifies that the current GOROOT can be used to bootstrap
// itself.
package reboot_test
import (
"os"
"os/exec"
"path/filepath"
"runtime"
"testing"
"time"
)
func TestRepeatBootstrap(t *testing.T) {
if testing.Short() {
t.Skipf("skipping test that rebuilds the entire toolchain")
}
goroot := t.TempDir()
gorootSrc := filepath.Join(goroot, "src")
overlayStart := time.Now()
if err := overlayDir(gorootSrc, filepath.Join(runtime.GOROOT(), "src")); err != nil {
t.Fatal(err)
}
t.Logf("GOROOT/src overlay set up in %s", time.Since(overlayStart))
if err := os.WriteFile(filepath.Join(goroot, "VERSION"), []byte(runtime.Version()), 0666); err != nil {
t.Fatal(err)
}
var makeScript string
switch runtime.GOOS {
case "windows":
makeScript = "make.bat"
case "plan9":
makeScript = "make.rc"
default:
makeScript = "make.bash"
}
cmd := exec.Command(filepath.Join(runtime.GOROOT(), "src", makeScript))
cmd.Dir = gorootSrc
cmd.Env = append(os.Environ(), "GOROOT=", "GOROOT_BOOTSTRAP="+runtime.GOROOT())
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
t.Fatal(err)
}
}