Files
dokku/plugins/scheduler-k3s/node_profiles_test.go
T
Jose Diaz-Gonzalez a21145fc57 fix: validate k3s profile names against derived release name
`scheduler-k3s:profiles:add` accepted profile names it could not later derive a valid helm release name from, so a profile was created successfully and only failed on commands with no obvious relationship to it: `profiles:remove` could not delete the profile it created, and a single bad name broke every subsequent node sysctls change, including the global scope and unrelated profiles. Names are now refused at add time against the release name they derive rather than against the profile name alone, and the paths that have to cope with a profile stored before this check keep working instead of erroring.
2026-08-29 19:13:46 -04:00

100 lines
4.0 KiB
Go

package scheduler_k3s
import (
"strings"
"testing"
"helm.sh/helm/v3/pkg/chartutil"
)
func TestValidateNodeProfileName(t *testing.T) {
tests := []struct {
name string
profileName string
wantErr bool
}{
{name: "typical name", profileName: "edge-workers"},
{name: "single character", profileName: "a"},
{name: "digits", profileName: "pool0"},
{name: "maximum length", profileName: strings.Repeat("a", maxNodeProfileNameLength)},
{name: "empty", profileName: "", wantErr: true},
{name: "uppercase", profileName: "EdgePool", wantErr: true},
{name: "one over the maximum length", profileName: strings.Repeat("a", maxNodeProfileNameLength+1), wantErr: true},
{name: "twenty seven characters", profileName: "a-twenty-seven-char-profile", wantErr: true},
{name: "leading dash", profileName: "-edge", wantErr: true},
{name: "trailing dash", profileName: "edge-", wantErr: true},
{name: "dot", profileName: "edge.workers", wantErr: true},
{name: "underscore", profileName: "edge_workers", wantErr: true},
{name: "space", profileName: "edge workers", wantErr: true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := validateNodeProfileName(test.profileName)
if test.wantErr && err == nil {
t.Errorf("validateNodeProfileName(%q) = nil, want an error", test.profileName)
}
if !test.wantErr && err != nil {
t.Errorf("validateNodeProfileName(%q) = %v, want nil", test.profileName, err)
}
})
}
}
// TestMaxNodeProfileNameLengthMatchesHelm asserts the limit validateNodeProfileName
// reports is the same one helm enforces. Without it the local arithmetic could drift
// from chartutil.ValidateReleaseName, which is the failure this limit exists to prevent.
func TestMaxNodeProfileNameLengthMatchesHelm(t *testing.T) {
longest := getNodeSysctlsReleaseName(strings.Repeat("a", maxNodeProfileNameLength))
if err := chartutil.ValidateReleaseName(longest); err != nil {
t.Errorf("chartutil.ValidateReleaseName(%q) = %v, want nil", longest, err)
}
tooLong := getNodeSysctlsReleaseName(strings.Repeat("a", maxNodeProfileNameLength+1))
if err := chartutil.ValidateReleaseName(tooLong); err == nil {
t.Errorf("chartutil.ValidateReleaseName(%q) = nil, want an error", tooLong)
}
}
// TestValidateStoredNodeProfileNameAcceptsLegacyNames asserts profiles:remove can still
// address a profile stored before names were validated against their helm release name
func TestValidateStoredNodeProfileNameAcceptsLegacyNames(t *testing.T) {
tests := []struct {
name string
profileName string
wantErr bool
}{
{name: "uppercase", profileName: "EdgePool"},
{name: "twenty seven characters", profileName: "a-twenty-seven-char-profile"},
{name: "legacy maximum length", profileName: strings.Repeat("a", legacyMaxNodeProfileNameLength)},
{name: "empty", profileName: "", wantErr: true},
{name: "one over the legacy maximum length", profileName: strings.Repeat("a", legacyMaxNodeProfileNameLength+1), wantErr: true},
{name: "underscore", profileName: "edge_workers", wantErr: true},
{name: "path traversal", profileName: "../config", wantErr: true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := validateStoredNodeProfileName(test.profileName)
if test.wantErr && err == nil {
t.Errorf("validateStoredNodeProfileName(%q) = nil, want an error", test.profileName)
}
if !test.wantErr && err != nil {
t.Errorf("validateStoredNodeProfileName(%q) = %v, want nil", test.profileName, err)
}
})
}
}
// TestVerifyNodeProfileSysctlsSupported asserts node-sysctls:set refuses a legacy profile
// name rather than storing sysctls the reconcile loop would have to skip
func TestVerifyNodeProfileSysctlsSupported(t *testing.T) {
if err := verifyNodeProfileSysctlsSupported("edge-workers"); err != nil {
t.Errorf("verifyNodeProfileSysctlsSupported(\"edge-workers\") = %v, want nil", err)
}
if err := verifyNodeProfileSysctlsSupported("EdgePool"); err == nil {
t.Error("verifyNodeProfileSysctlsSupported(\"EdgePool\") = nil, want an error")
}
}