From fbdb065498baec98bf17e1f5705392e9855081a8 Mon Sep 17 00:00:00 2001 From: Hong Xu Date: Sat, 17 Apr 2021 15:39:23 -0700 Subject: [PATCH] TestGetPatchSetsForPathMustBeDirectory: Ensure the error type is os.PathError Ensure the tested error is an os.PathError instead of only non-nil. --- cmd/kubeadm/app/util/patches/patches.go | 6 +++++- cmd/kubeadm/app/util/patches/patches_test.go | 8 +++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/cmd/kubeadm/app/util/patches/patches.go b/cmd/kubeadm/app/util/patches/patches.go index 3b1ad23d0e3..fa0ca9fabd3 100644 --- a/cmd/kubeadm/app/util/patches/patches.go +++ b/cmd/kubeadm/app/util/patches/patches.go @@ -283,7 +283,11 @@ func getPatchSetsFromPath(targetPath string, knownTargets []string, output io.Wr goto return_path_error } if !info.IsDir() { - err = errors.New("not a directory") + err = &os.PathError{ + Op: "getPatchSetsFromPath", + Path: info.Name(), + Err: errors.New("not a directory"), + } goto return_path_error } diff --git a/cmd/kubeadm/app/util/patches/patches_test.go b/cmd/kubeadm/app/util/patches/patches_test.go index 7d2e8f18a67..eab7907d539 100644 --- a/cmd/kubeadm/app/util/patches/patches_test.go +++ b/cmd/kubeadm/app/util/patches/patches_test.go @@ -24,6 +24,7 @@ import ( "reflect" "testing" + "github.com/pkg/errors" "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/types" ) @@ -165,15 +166,16 @@ func TestCreatePatchSet(t *testing.T) { } func TestGetPatchSetsForPathMustBeDirectory(t *testing.T) { - tempFile, err := ioutil.TempFile("", "test-file") + tempFile, err := os.CreateTemp("", "test-file") if err != nil { t.Errorf("error creating temporary file: %v", err) } defer os.Remove(tempFile.Name()) _, _, _, err = getPatchSetsFromPath(tempFile.Name(), testKnownTargets, ioutil.Discard) - if err == nil { - t.Fatalf("expected error for non-directory path %q", tempFile.Name()) + var pathErr *os.PathError + if !errors.As(err, &pathErr) { + t.Fatalf("expected os.PathError for non-directory path %q, but got %v", tempFile.Name(), err) } }