Commit 5a3b1a0c authored by Rostislav M. Georgiev's avatar Rostislav M. Georgiev

kubeadm: Don't hardcode temp path in a test

Hardcoding a temp path of /tmp/... is not portable and can potentially cause other issues (such as flakyness) too. Use TempFile instead. Signed-off-by: 's avatarRostislav M. Georgiev <rostislavg@vmware.com>
parent 9c75270b
...@@ -18,11 +18,9 @@ package upgrade ...@@ -18,11 +18,9 @@ package upgrade
import ( import (
"bytes" "bytes"
"fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"testing" "testing"
"time"
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm" kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
) )
...@@ -77,17 +75,25 @@ func TestGetK8sVersionFromUserInput(t *testing.T) { ...@@ -77,17 +75,25 @@ func TestGetK8sVersionFromUserInput(t *testing.T) {
name: "Version is optional", name: "Version is optional",
}, },
} }
for tnum, tt := range tcases { for _, tt := range tcases {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
flags := &applyPlanFlags{} flags := &applyPlanFlags{}
if len(tt.clusterConfig) > 0 { if len(tt.clusterConfig) > 0 {
tmpfile := fmt.Sprintf("/tmp/kubeadm-upgrade-common-test-%d-%d.yaml", tnum, time.Now().Unix()) file, err := ioutil.TempFile("", "kubeadm-upgrade-common-test-*.yaml")
if err := ioutil.WriteFile(tmpfile, []byte(tt.clusterConfig), 0666); err != nil { if err != nil {
t.Fatalf("Failed to create test config file: %+v", err) t.Fatalf("Failed to create test config file: %+v", err)
} }
defer os.Remove(tmpfile)
flags.cfgPath = tmpfile tmpFileName := file.Name()
defer os.Remove(tmpFileName)
_, err = file.WriteString(tt.clusterConfig)
file.Close()
if err != nil {
t.Fatalf("Failed to write test config file contents: %+v", err)
}
flags.cfgPath = tmpFileName
} }
userVersion, err := getK8sVersionFromUserInput(flags, tt.args, tt.isVersionMandatory) userVersion, err := getK8sVersionFromUserInput(flags, tt.args, tt.isVersionMandatory)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment