Skip to content

Commit

Permalink
cmd/dist: make GOROOT unwritable on builders
Browse files Browse the repository at this point in the history
Updates golang#30316

Change-Id: I57e489f6bbe4a3b39c907dabe5ac41fb9939cdb4
Reviewed-on: https://go-review.googlesource.com/c/go/+/163477
Run-TryBot: Brad Fitzpatrick <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Dmitri Shuralyov <[email protected]>
  • Loading branch information
bradfitz committed May 14, 2019
1 parent a75bfb0 commit 02d24fc
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/cmd/dist/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ func (t *tester) run() {
}
}

// On a few builders, make GOROOT unwritable to catch tests writing to it.
if strings.HasPrefix(os.Getenv("GO_BUILDER_NAME"), "linux-") {
t.makeGOROOTUnwritable()
}

for _, dt := range t.tests {
if !t.shouldRunTest(dt.name) {
t.partial = true
Expand Down Expand Up @@ -1388,6 +1393,36 @@ func (t *tester) packageHasBenchmarks(pkg string) bool {
return false
}

// makeGOROOTUnwritable makes all $GOROOT files & directories non-writable to
// check that no tests accidentally write to $GOROOT.
func (t *tester) makeGOROOTUnwritable() {
if os.Getenv("GO_BUILDER_NAME") == "" {
panic("not a builder")
}
if os.Getenv("GOROOT") == "" {
panic("GOROOT not set")
}
err := filepath.Walk(os.Getenv("GOROOT"), func(name string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
if !fi.Mode().IsRegular() && !fi.IsDir() {
return nil
}
mode := fi.Mode()
newMode := mode & ^os.FileMode(0222)
if newMode != mode {
if err := os.Chmod(name, newMode); err != nil {
return err
}
}
return nil
})
if err != nil {
log.Fatalf("making builder's files read-only: %v", err)
}
}

// raceDetectorSupported is a copy of the function
// cmd/internal/sys.RaceDetectorSupported, which can't be used here
// because cmd/dist has to be buildable by Go 1.4.
Expand Down

0 comments on commit 02d24fc

Please sign in to comment.