mirror of
https://github.com/psviderski/uncloud.git
synced 2026-10-08 14:14:52 +00:00
feat(machine): add validation for machine names to accept only DNS labels
This commit is contained in:
1 parent
11e1200fe8
commit
ce1c6bd05a
12 files changed
+286
-27
No files matched your search
@@ -1,8 +1,10 @@
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/psviderski/uncloud/pkg/api"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -18,9 +20,11 @@ func TestMachineNameFromHostname(t *testing.T) {
|
||||
{"simple", "web", "web"},
|
||||
{"fqdn uses first label", "web-1.example.com", "web-1"},
|
||||
{"uppercase lowercased", "Web-Server", "web-server"},
|
||||
{"invalid chars replaced", "host_name@1", "host_name-1"},
|
||||
{"trim surrounding hyphens", "-_host_-", "_host_"},
|
||||
{"invalid chars replaced", "host_name@1", "host-name-1"},
|
||||
{"trim surrounding hyphens", "-_host_-", "host"},
|
||||
{"whitespace trimmed", " myhost ", "myhost"},
|
||||
{"long hostname truncated", strings.Repeat("a", 64), strings.Repeat("a", 63)},
|
||||
{"truncation trims trailing hyphen", strings.Repeat("a", 62) + "-b", strings.Repeat("a", 62)},
|
||||
{"empty", "", ""},
|
||||
{"only invalid chars", "@#", ""},
|
||||
{"dot only", ".example.com", ""},
|
||||
@@ -37,10 +41,12 @@ func TestDefaultMachineName(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
t.Run("falls back to random", func(t *testing.T) {
|
||||
// An empty or fully invalid hostname falls back to a random "machine-xxxx" name.
|
||||
got, err := DefaultMachineName("***", nil)
|
||||
require.NoError(t, err)
|
||||
assert.Regexp(t, `^machine-[a-zA-Z0-9]{4}$`, got)
|
||||
for _, hostname := range []string{"***", "rr", "nearest", strings.Repeat("a", 32)} {
|
||||
got, err := DefaultMachineName(hostname, nil)
|
||||
require.NoError(t, err)
|
||||
assert.Regexp(t, `^machine-[a-z0-9]{4}$`, got)
|
||||
require.NoError(t, api.ValidateMachineName(got))
|
||||
}
|
||||
})
|
||||
|
||||
tests := []struct {
|
||||
@@ -49,10 +55,45 @@ func TestDefaultMachineName(t *testing.T) {
|
||||
existing []string
|
||||
want string
|
||||
}{
|
||||
{"from hostname", "web-1.example.com", nil, "web-1"},
|
||||
{"dedup against existing", "web", []string{"web"}, "web-1"},
|
||||
{"dedup multiple", "web", []string{"web", "web-1"}, "web-2"},
|
||||
{"sanitized", "My_Host", nil, "my_host"},
|
||||
{
|
||||
name: "from hostname",
|
||||
hostname: "web-1.example.com",
|
||||
want: "web-1",
|
||||
},
|
||||
{
|
||||
name: "dedup against existing",
|
||||
hostname: "web",
|
||||
existing: []string{"web"},
|
||||
want: "web-1",
|
||||
},
|
||||
{
|
||||
name: "dedup multiple",
|
||||
hostname: "web",
|
||||
existing: []string{"web", "web-1"},
|
||||
want: "web-2",
|
||||
},
|
||||
{
|
||||
name: "sanitized",
|
||||
hostname: "My_Host",
|
||||
want: "my-host",
|
||||
},
|
||||
{
|
||||
name: "long hostname",
|
||||
hostname: strings.Repeat("a", 64),
|
||||
want: strings.Repeat("a", 63),
|
||||
},
|
||||
{
|
||||
name: "dedup maximum length",
|
||||
hostname: strings.Repeat("a", 63),
|
||||
existing: []string{strings.Repeat("a", 63)},
|
||||
want: strings.Repeat("a", 61) + "-1",
|
||||
},
|
||||
{
|
||||
name: "dedup trims trailing hyphen",
|
||||
hostname: strings.Repeat("a", 60) + "-bb",
|
||||
existing: []string{strings.Repeat("a", 60) + "-bb"},
|
||||
want: strings.Repeat("a", 60) + "-1",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
@@ -60,6 +101,7 @@ func TestDefaultMachineName(t *testing.T) {
|
||||
got, err := DefaultMachineName(tt.hostname, tt.existing)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, tt.want, got)
|
||||
require.NoError(t, api.ValidateMachineName(got))
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user