Skip to content

Commit b414901

Browse files
authored
Merge pull request #10381 from spowelljr/addTmpDirTests
Refactor tmp dir detection to improve testability
2 parents de03a37 + f13ae5c commit b414901

File tree

2 files changed

+77
-9
lines changed

2 files changed

+77
-9
lines changed

pkg/minikube/command/kic_runner.go

+23-9
Original file line numberDiff line numberDiff line change
@@ -186,17 +186,13 @@ func (k *kicRunner) Copy(f assets.CopyableFile) error {
186186
}
187187
}
188188
klog.Infof("%s (temp): %s --> %s (%d bytes)", k.ociBin, src, dst, f.GetLength())
189-
tmpFolder := ""
190189

191-
// Snap only allows an application to see its own files in /tmp, making Docker unable to copy memory assets
192-
// https://github.com/kubernetes/minikube/issues/10020
193-
if isSnapBinary() {
194-
home, err := os.UserHomeDir()
195-
if err != nil {
196-
return errors.Wrap(err, "detecting home dir")
197-
}
198-
tmpFolder = home
190+
isSnap := isSnapBinary()
191+
tmpFolder, err := tempDirectory(isSnap)
192+
if err != nil {
193+
return errors.Wrap(err, "determining temp directory")
199194
}
195+
200196
tf, err := ioutil.TempFile(tmpFolder, "tmpf-memory-asset")
201197
if err != nil {
202198
return errors.Wrap(err, "creating temporary file")
@@ -209,6 +205,24 @@ func (k *kicRunner) Copy(f assets.CopyableFile) error {
209205
return k.copy(tf.Name(), dst)
210206
}
211207

208+
// tempDirectory returns the directory to use as the temp directory
209+
// or an empty string if it should use the os default temp directory.
210+
func tempDirectory(isSnap bool) (string, error) {
211+
if !isSnap {
212+
return "", nil
213+
}
214+
215+
// Snap only allows an application to see its own files in /tmp, making Docker unable to copy memory assets
216+
// https://github.com/kubernetes/minikube/issues/10020
217+
218+
home, err := os.UserHomeDir()
219+
if err != nil {
220+
return "", errors.Wrap(err, "detecting home dir")
221+
}
222+
return home, nil
223+
}
224+
225+
// isSnapBinary returns true if the binary path includes "snap".
212226
func isSnapBinary() bool {
213227
ex, err := os.Executable()
214228
if err != nil {
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package command
18+
19+
import (
20+
"os"
21+
"testing"
22+
)
23+
24+
func TestKICRunner(t *testing.T) {
25+
t.Parallel()
26+
27+
t.Run("TestTempDirectory", func(t *testing.T) {
28+
t.Parallel()
29+
30+
home, err := os.UserHomeDir()
31+
if err != nil {
32+
t.Fatalf("failed to get user home directory: %v", err)
33+
}
34+
35+
tests := []struct {
36+
in bool
37+
want string
38+
}{
39+
{false, ""},
40+
{true, home},
41+
}
42+
43+
for _, tt := range tests {
44+
got, err := tempDirectory(tt.in)
45+
if err != nil {
46+
t.Fatalf("failed to get temp directory: %v", err)
47+
}
48+
49+
if got != tt.want {
50+
t.Errorf("tempDirectory(%t) = %s; want %s", tt.in, got, tt.want)
51+
}
52+
}
53+
})
54+
}

0 commit comments

Comments
 (0)