diff --git a/pkg/minikube/command/kic_runner.go b/pkg/minikube/command/kic_runner.go index 8f4a6826187d..3f679d762f74 100644 --- a/pkg/minikube/command/kic_runner.go +++ b/pkg/minikube/command/kic_runner.go @@ -186,17 +186,13 @@ func (k *kicRunner) Copy(f assets.CopyableFile) error { } } klog.Infof("%s (temp): %s --> %s (%d bytes)", k.ociBin, src, dst, f.GetLength()) - tmpFolder := "" - // Snap only allows an application to see its own files in /tmp, making Docker unable to copy memory assets - // https://github.com/kubernetes/minikube/issues/10020 - if isSnapBinary() { - home, err := os.UserHomeDir() - if err != nil { - return errors.Wrap(err, "detecting home dir") - } - tmpFolder = home + isSnap := isSnapBinary() + tmpFolder, err := tempDirectory(isSnap) + if err != nil { + return errors.Wrap(err, "determining temp directory") } + tf, err := ioutil.TempFile(tmpFolder, "tmpf-memory-asset") if err != nil { return errors.Wrap(err, "creating temporary file") @@ -209,6 +205,24 @@ func (k *kicRunner) Copy(f assets.CopyableFile) error { return k.copy(tf.Name(), dst) } +// tempDirectory returns the directory to use as the temp directory +// or an empty string if it should use the os default temp directory. +func tempDirectory(isSnap bool) (string, error) { + if !isSnap { + return "", nil + } + + // Snap only allows an application to see its own files in /tmp, making Docker unable to copy memory assets + // https://github.com/kubernetes/minikube/issues/10020 + + home, err := os.UserHomeDir() + if err != nil { + return "", errors.Wrap(err, "detecting home dir") + } + return home, nil +} + +// isSnapBinary returns true if the binary path includes "snap". func isSnapBinary() bool { ex, err := os.Executable() if err != nil { diff --git a/pkg/minikube/command/kic_runner_test.go b/pkg/minikube/command/kic_runner_test.go new file mode 100644 index 000000000000..63fc63a5b8cd --- /dev/null +++ b/pkg/minikube/command/kic_runner_test.go @@ -0,0 +1,54 @@ +/* +Copyright 2019 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package command + +import ( + "os" + "testing" +) + +func TestKICRunner(t *testing.T) { + t.Parallel() + + t.Run("TestTempDirectory", func(t *testing.T) { + t.Parallel() + + home, err := os.UserHomeDir() + if err != nil { + t.Fatalf("failed to get user home directory: %v", err) + } + + tests := []struct { + in bool + want string + }{ + {false, ""}, + {true, home}, + } + + for _, tt := range tests { + got, err := tempDirectory(tt.in) + if err != nil { + t.Fatalf("failed to get temp directory: %v", err) + } + + if got != tt.want { + t.Errorf("tempDirectory(%t) = %s; want %s", tt.in, got, tt.want) + } + } + }) +}