Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor tmp dir detection to improve testability #10381

Merged
merged 1 commit into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
medyagh marked this conversation as resolved.
Show resolved Hide resolved
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)
}
}
})
}