mirror of https://github.com/helm/helm
Signed-off-by: Scott Rigby <scott@r6by.com>pull/10364/merge
parent
d19130f69e
commit
5c663db853
@ -0,0 +1,80 @@
|
||||
/*
|
||||
Copyright The Helm Authors.
|
||||
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 installer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"helm.sh/helm/v4/internal/plugin"
|
||||
)
|
||||
|
||||
// detectPluginRoot searches for plugin.yaml in the extracted directory
|
||||
// and returns the path to the directory containing it.
|
||||
// This handles cases where the tarball contains the plugin in a subdirectory.
|
||||
func detectPluginRoot(extractDir string) (string, error) {
|
||||
// First check if plugin.yaml is at the root
|
||||
if _, err := os.Stat(filepath.Join(extractDir, plugin.PluginFileName)); err == nil {
|
||||
return extractDir, nil
|
||||
}
|
||||
|
||||
// Otherwise, look for plugin.yaml in subdirectories (only one level deep)
|
||||
entries, err := os.ReadDir(extractDir)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() {
|
||||
subdir := filepath.Join(extractDir, entry.Name())
|
||||
if _, err := os.Stat(filepath.Join(subdir, plugin.PluginFileName)); err == nil {
|
||||
return subdir, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("plugin.yaml not found in %s or its immediate subdirectories", extractDir)
|
||||
}
|
||||
|
||||
// validatePluginName checks if the plugin directory name matches the plugin name
|
||||
// from plugin.yaml when the plugin is in a subdirectory.
|
||||
func validatePluginName(pluginRoot string, expectedName string) error {
|
||||
// Only validate if plugin is in a subdirectory
|
||||
dirName := filepath.Base(pluginRoot)
|
||||
if dirName == expectedName {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Load plugin.yaml to get the actual name
|
||||
p, err := plugin.LoadDir(pluginRoot)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to load plugin from %s: %w", pluginRoot, err)
|
||||
}
|
||||
|
||||
m := p.Metadata()
|
||||
actualName := m.Name
|
||||
|
||||
// For now, just log a warning if names don't match
|
||||
// In the future, we might want to enforce this more strictly
|
||||
if actualName != dirName && actualName != strings.TrimSuffix(expectedName, filepath.Ext(expectedName)) {
|
||||
// This is just informational - not an error
|
||||
return nil
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -0,0 +1,165 @@
|
||||
/*
|
||||
Copyright The Helm Authors.
|
||||
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 installer
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDetectPluginRoot(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
setup func(dir string) error
|
||||
expectRoot string
|
||||
expectError bool
|
||||
}{
|
||||
{
|
||||
name: "plugin.yaml at root",
|
||||
setup: func(dir string) error {
|
||||
return os.WriteFile(filepath.Join(dir, "plugin.yaml"), []byte("name: test"), 0644)
|
||||
},
|
||||
expectRoot: ".",
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "plugin.yaml in subdirectory",
|
||||
setup: func(dir string) error {
|
||||
subdir := filepath.Join(dir, "my-plugin")
|
||||
if err := os.MkdirAll(subdir, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(filepath.Join(subdir, "plugin.yaml"), []byte("name: test"), 0644)
|
||||
},
|
||||
expectRoot: "my-plugin",
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "no plugin.yaml",
|
||||
setup: func(dir string) error {
|
||||
return os.WriteFile(filepath.Join(dir, "README.md"), []byte("test"), 0644)
|
||||
},
|
||||
expectRoot: "",
|
||||
expectError: true,
|
||||
},
|
||||
{
|
||||
name: "plugin.yaml in nested subdirectory (should not find)",
|
||||
setup: func(dir string) error {
|
||||
subdir := filepath.Join(dir, "outer", "inner")
|
||||
if err := os.MkdirAll(subdir, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(filepath.Join(subdir, "plugin.yaml"), []byte("name: test"), 0644)
|
||||
},
|
||||
expectRoot: "",
|
||||
expectError: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
if err := tt.setup(dir); err != nil {
|
||||
t.Fatalf("Setup failed: %v", err)
|
||||
}
|
||||
|
||||
root, err := detectPluginRoot(dir)
|
||||
if tt.expectError {
|
||||
if err == nil {
|
||||
t.Error("Expected error but got none")
|
||||
}
|
||||
} else {
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
}
|
||||
expectedPath := dir
|
||||
if tt.expectRoot != "." {
|
||||
expectedPath = filepath.Join(dir, tt.expectRoot)
|
||||
}
|
||||
if root != expectedPath {
|
||||
t.Errorf("Expected root %s but got %s", expectedPath, root)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidatePluginName(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
setup func(dir string) error
|
||||
pluginRoot string
|
||||
expectedName string
|
||||
expectError bool
|
||||
}{
|
||||
{
|
||||
name: "matching directory and plugin name",
|
||||
setup: func(dir string) error {
|
||||
subdir := filepath.Join(dir, "my-plugin")
|
||||
if err := os.MkdirAll(subdir, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
yaml := `name: my-plugin
|
||||
version: 1.0.0
|
||||
usage: test
|
||||
description: test`
|
||||
return os.WriteFile(filepath.Join(subdir, "plugin.yaml"), []byte(yaml), 0644)
|
||||
},
|
||||
pluginRoot: "my-plugin",
|
||||
expectedName: "my-plugin",
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "different directory and plugin name",
|
||||
setup: func(dir string) error {
|
||||
subdir := filepath.Join(dir, "wrong-name")
|
||||
if err := os.MkdirAll(subdir, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
yaml := `name: my-plugin
|
||||
version: 1.0.0
|
||||
usage: test
|
||||
description: test`
|
||||
return os.WriteFile(filepath.Join(subdir, "plugin.yaml"), []byte(yaml), 0644)
|
||||
},
|
||||
pluginRoot: "wrong-name",
|
||||
expectedName: "wrong-name",
|
||||
expectError: false, // Currently we don't error on mismatch
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
if err := tt.setup(dir); err != nil {
|
||||
t.Fatalf("Setup failed: %v", err)
|
||||
}
|
||||
|
||||
pluginRoot := filepath.Join(dir, tt.pluginRoot)
|
||||
err := validatePluginName(pluginRoot, tt.expectedName)
|
||||
if tt.expectError {
|
||||
if err == nil {
|
||||
t.Error("Expected error but got none")
|
||||
}
|
||||
} else {
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in new issue