Skip to content
This repository has been archived by the owner on Apr 16, 2023. It is now read-only.

Commit

Permalink
Merge pull request #65 from GoogleCloudPlatform/release
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
codrienne authored May 2, 2018
2 parents 7b17d1f + db869c1 commit 1c9ef7b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
31 changes: 30 additions & 1 deletion build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"os"
"path"
"regexp"
"runtime"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -925,6 +926,34 @@ func (b *Build) timeAndRunStep(ctx context.Context, idx int, waitChans []chan st
close(done)
}

// runtimeGOOS is the operating system detected at runtime and is stubbable in testing.
var runtimeGOOS = runtime.GOOS

// osTempDir returns the default temporary directory for the OS.
func osTempDir() string {
if runtimeGOOS == "darwin" {
// The default temporary directory in MacOS lives in the /var path. Docker reserves the /var
// path and will deny the build from mounting or using resources in that path. See b/78897068.
// Use /tmp instead.
return "/tmp"
}
return os.TempDir()
}

// getTempDir returns the full tempdir path. If the subpath is empty, the OS temporary directory is returned.
// Note that this does not create the temporary directory.
func getTempDir(subpath string) string {
if subpath == "" {
return osTempDir()
}

fullpath := path.Join(osTempDir(), subpath)
if !strings.HasSuffix(fullpath, "/") {
fullpath = fullpath + "/"
}
return fullpath
}

func (b *Build) runStep(ctx context.Context, idx int) error {
step := b.Request.Steps[idx]

Expand Down Expand Up @@ -1312,7 +1341,7 @@ func (b *Build) pushArtifacts(ctx context.Context) error {
flags := gsutil.DockerFlags{
Workvol: b.hostWorkspaceDir + ":" + containerWorkspaceDir,
Workdir: workdir,
Tmpdir: os.TempDir(),
Tmpdir: osTempDir(),
}

b.mu.Lock()
Expand Down
48 changes: 48 additions & 0 deletions build/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"io/ioutil"
"log"
"net/http"
"os"
"runtime"
"reflect"
"regexp"
"strings"
Expand Down Expand Up @@ -2573,6 +2575,52 @@ func TestWorkdir(t *testing.T) {
}


func TestOsTempDir(t *testing.T) {
defer func() { runtimeGOOS = runtime.GOOS }()

testCases := []struct {
goos string
wantTempDir string
}{{
goos: "notarealosbutnotdarwin",
wantTempDir: os.TempDir(),
}, {
goos: "darwin",
wantTempDir: "/tmp",
}}
for _, tc := range testCases {
runtimeGOOS = tc.goos
gotTempDir := osTempDir()
if gotTempDir != tc.wantTempDir {
t.Errorf("%s: got osTempDir() = %q, want %q", tc.goos, gotTempDir, tc.wantTempDir)
}
}
}

func TestGetTempDir(t *testing.T) {
tmpdir := osTempDir()

testCases := []struct {
name string
subpath string
wantTempDir string
}{{
name: "NoSubpath",
wantTempDir: tmpdir,
}, {
name: "Subpath",
subpath: "iamsubpath",
wantTempDir: fmt.Sprintf("%s/iamsubpath/", tmpdir),
}}
for _, tc := range testCases {
gotTempDir := getTempDir(tc.subpath)
if gotTempDir != tc.wantTempDir {
t.Errorf("%s: got getTempDir() = %q, want %q", tc.name, gotTempDir, tc.wantTempDir)
}
}

}

type nopBuildLogger struct{}

func (nopBuildLogger) WriteMainEntry(string) { return }
Expand Down

0 comments on commit 1c9ef7b

Please sign in to comment.