|
| 1 | +//go:build !envtest |
| 2 | + |
| 3 | +/* |
| 4 | +Copyright 2023. |
| 5 | +
|
| 6 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +you may not use this file except in compliance with the License. |
| 8 | +You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | +Unless required by applicable law or agreed to in writing, software |
| 13 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +See the License for the specific language governing permissions and |
| 16 | +limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +package k0smotronio |
| 20 | + |
| 21 | +import ( |
| 22 | + "strings" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/stretchr/testify/assert" |
| 26 | +) |
| 27 | + |
| 28 | +func Test_pathToVolumeName(t *testing.T) { |
| 29 | + tests := []struct { |
| 30 | + path string |
| 31 | + want string // exact expected name for simple/clean paths (no hash) |
| 32 | + wantPrefix string // expected prefix for sanitized paths (ends with "-") |
| 33 | + }{ |
| 34 | + { |
| 35 | + // Simple path: already valid, no hash added (backward-compatible) |
| 36 | + path: "/etc/kubernetes/pki", |
| 37 | + want: "etc-kubernetes-pki", |
| 38 | + }, |
| 39 | + { |
| 40 | + // Simple path used in e2e upgrade test - must stay stable across upgrades |
| 41 | + path: "/tmp/test", |
| 42 | + want: "tmp-test", |
| 43 | + }, |
| 44 | + { |
| 45 | + // Underscore is invalid -> sanitized with hash |
| 46 | + path: "/my_config/file.conf", |
| 47 | + wantPrefix: "my-config-file-conf-", |
| 48 | + }, |
| 49 | + { |
| 50 | + // Dot is invalid -> sanitized with hash |
| 51 | + path: "/etc/ssl/certs/ca-certificates.crt", |
| 52 | + wantPrefix: "etc-ssl-certs-ca-certificates-crt-", |
| 53 | + }, |
| 54 | + { |
| 55 | + // Uppercase is invalid -> sanitized with hash |
| 56 | + path: "/VAR/lib/K0s", |
| 57 | + wantPrefix: "var-lib-k0s-", |
| 58 | + }, |
| 59 | + { |
| 60 | + // Single alphanumeric: valid, no hash |
| 61 | + path: "/a", |
| 62 | + want: "a", |
| 63 | + }, |
| 64 | + { |
| 65 | + // Dot in path segment -> sanitized with hash |
| 66 | + path: "/root/.aws/credentials", |
| 67 | + wantPrefix: "root-aws-credentials-", |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + for _, tt := range tests { |
| 72 | + t.Run(tt.path, func(t *testing.T) { |
| 73 | + got := pathToVolumeName(tt.path) |
| 74 | + |
| 75 | + // Must be at most 63 chars (DNS label limit) |
| 76 | + assert.LessOrEqual(t, len(got), 63, "volume name exceeds DNS label limit") |
| 77 | + |
| 78 | + // Must be a valid DNS label |
| 79 | + assert.Regexp(t, `^[a-z0-9]([a-z0-9-]*[a-z0-9])?$`, got, "volume name must be a valid DNS label") |
| 80 | + |
| 81 | + if tt.want != "" { |
| 82 | + assert.Equal(t, tt.want, got) |
| 83 | + } else { |
| 84 | + assert.True(t, strings.HasPrefix(got, tt.wantPrefix), "expected prefix %q, got %q", tt.wantPrefix, got) |
| 85 | + } |
| 86 | + }) |
| 87 | + } |
| 88 | + |
| 89 | + t.Run("unique names for paths that sanitize to the same string", func(t *testing.T) { |
| 90 | + paths := []string{ |
| 91 | + "/my_path", |
| 92 | + "/my.path", |
| 93 | + } |
| 94 | + names := make(map[string]string) |
| 95 | + for _, p := range paths { |
| 96 | + name := pathToVolumeName(p) |
| 97 | + for prev, prevName := range names { |
| 98 | + assert.NotEqual(t, prevName, name, "paths %q and %q produced the same volume name %q", prev, p, name) |
| 99 | + } |
| 100 | + names[p] = name |
| 101 | + } |
| 102 | + }) |
| 103 | + |
| 104 | + t.Run("long path is truncated to 63 chars", func(t *testing.T) { |
| 105 | + longPath := "/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/aa/bb/cc/dd/ee/ff" |
| 106 | + got := pathToVolumeName(longPath) |
| 107 | + assert.LessOrEqual(t, len(got), 63) |
| 108 | + assert.Regexp(t, `^[a-z0-9]([a-z0-9-]*[a-z0-9])?$`, got) |
| 109 | + }) |
| 110 | +} |
0 commit comments