Browse Source

fix(plugins): Return error when plugin config directory is unreadable (#3491)

Signed-off-by: Tushar Verma <tusharmyself06@gmail.com>
Tushar-Verma 5 tháng trước cách đây
mục cha
commit
55c7f42a8b
2 tập tin đã thay đổi với 68 bổ sung0 xóa
  1. 1 0
      pkg/customcost/pipelineservice.go
  2. 67 0
      pkg/customcost/pipelineservice_test.go

+ 1 - 0
pkg/customcost/pipelineservice.go

@@ -37,6 +37,7 @@ func getRegisteredPlugins(configDir string, execDir string) (map[string]*plugin.
 	configFiles, err := os.ReadDir(configDir)
 	if err != nil {
 		log.Errorf("error reading files in directory %s: %v", configDir, err)
+		return nil, fmt.Errorf("failed to read plugin config directory: %w", err)
 	}
 
 	// list of plugins that we must run are the strings before _

+ 67 - 0
pkg/customcost/pipelineservice_test.go

@@ -6,6 +6,7 @@ import (
 	"net/http"
 	"os"
 	"runtime"
+	"strings"
 	"testing"
 	"time"
 
@@ -150,6 +151,72 @@ func downloadLatestPluginExec(dirName string, t *testing.T) {
 	}
 }
 
+func TestGetRegisteredPlugins_UnreadableConfigDir(t *testing.T) {
+	testCases := []struct {
+		name          string
+		setupFunc     func() (configDir string, execDir string, cleanup func())
+		expectError   bool
+		errorContains string
+	}{
+		{
+			name: "non-existent config directory",
+			setupFunc: func() (string, string, func()) {
+				execDir := t.TempDir()
+				return "/non/existent/path", execDir, func() {}
+			},
+			expectError:   true,
+			errorContains: "failed to read plugin config directory",
+		},
+		{
+			name: "config directory is a file not a directory",
+			setupFunc: func() (string, string, func()) {
+				tmpDir := t.TempDir()
+				execDir := t.TempDir()
+				// Create a file instead of a directory
+				configFile := tmpDir + "/not-a-directory"
+				err := os.WriteFile(configFile, []byte("test"), 0644)
+				if err != nil {
+					t.Fatalf("failed to create test file: %v", err)
+				}
+				return configFile, execDir, func() {}
+			},
+			expectError:   true,
+			errorContains: "failed to read plugin config directory",
+		},
+		{
+			name: "empty valid config directory",
+			setupFunc: func() (string, string, func()) {
+				configDir := t.TempDir()
+				execDir := t.TempDir()
+				return configDir, execDir, func() {}
+			},
+			expectError:   false,
+			errorContains: "",
+		},
+	}
+
+	for _, tc := range testCases {
+		t.Run(tc.name, func(t *testing.T) {
+			configDir, execDir, cleanup := tc.setupFunc()
+			defer cleanup()
+
+			_, err := getRegisteredPlugins(configDir, execDir)
+
+			if tc.expectError {
+				if err == nil {
+					t.Errorf("expected error but got nil")
+				} else if tc.errorContains != "" && !strings.Contains(err.Error(), tc.errorContains) {
+					t.Errorf("expected error to contain %q, but got: %v", tc.errorContains, err)
+				}
+			} else {
+				if err != nil {
+					t.Errorf("expected no error but got: %v", err)
+				}
+			}
+		})
+	}
+}
+
 func writeDDConfig(pluginConfigDir string, t *testing.T) {
 	// read necessary env vars. If any are missing, log warning and skip test
 	ddSite := os.Getenv("DD_SITE")