Skip to content

Commit

Permalink
Refactor tmp dir detection to improve testability
Browse files Browse the repository at this point in the history
  • Loading branch information
spowelljr committed Feb 6, 2021
1 parent 3f4ff5b commit f13ae5c
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 9 deletions.
32 changes: 23 additions & 9 deletions pkg/minikube/command/kic_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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 {
Expand Down
54 changes: 54 additions & 0 deletions pkg/minikube/command/kic_runner_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
})
}

0 comments on commit f13ae5c

Please sign in to comment.