|
| 1 | +// Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package controller |
| 16 | + |
| 17 | +import ( |
| 18 | + "regexp" |
| 19 | + "strings" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | + "k8s.io/apimachinery/pkg/util/validation" |
| 25 | +) |
| 26 | + |
| 27 | +func TestClaimLeaseName_FitsAndStaysReadable(t *testing.T) { |
| 28 | + t.Parallel() |
| 29 | + |
| 30 | + // "mr-claim." plus the kept prefix fills 244 characters before "-<8 hex>", |
| 31 | + // so node[234] is the last character kept from a node name that is too long. |
| 32 | + const lastKept = 234 |
| 33 | + |
| 34 | + hashSuffix := regexp.MustCompile(`-[0-9a-f]{8}$`) |
| 35 | + |
| 36 | + tests := []struct { |
| 37 | + name string |
| 38 | + nodeName string |
| 39 | + want string |
| 40 | + wantHashed bool |
| 41 | + }{ |
| 42 | + { |
| 43 | + name: "typical node name is prefixed unchanged", |
| 44 | + nodeName: "gpu-node-7", |
| 45 | + want: "mr-claim.gpu-node-7", |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "name that exactly fits 253 characters is unchanged", |
| 49 | + nodeName: strings.Repeat("a", validation.DNS1123SubdomainMaxLength-len(claimLeasePrefix)), |
| 50 | + want: claimLeasePrefix + strings.Repeat("a", validation.DNS1123SubdomainMaxLength-len(claimLeasePrefix)), |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "name one character too long keeps its start and gains a hash", |
| 54 | + nodeName: strings.Repeat("a", validation.DNS1123SubdomainMaxLength-len(claimLeasePrefix)+1), |
| 55 | + wantHashed: true, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "cut landing on a dot drops the dot", |
| 59 | + nodeName: strings.Repeat("a", lastKept) + "." + strings.Repeat("b", 20), |
| 60 | + wantHashed: true, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "cut landing on a dash drops the dash", |
| 64 | + nodeName: strings.Repeat("a", lastKept) + "-" + strings.Repeat("b", 20), |
| 65 | + wantHashed: true, |
| 66 | + }, |
| 67 | + } |
| 68 | + |
| 69 | + for _, tt := range tests { |
| 70 | + t.Run(tt.name, func(t *testing.T) { |
| 71 | + t.Parallel() |
| 72 | + |
| 73 | + got := ClaimLeaseName(tt.nodeName) |
| 74 | + |
| 75 | + assert.Empty(t, validation.IsDNS1123Subdomain(got), "claim name %q must be a valid lease name", got) |
| 76 | + assert.NotEqual(t, tt.nodeName, got, "claim must not collide with the janitor lock for the same node") |
| 77 | + |
| 78 | + if !tt.wantHashed { |
| 79 | + assert.Equal(t, tt.want, got) |
| 80 | + |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + assert.LessOrEqual(t, len(got), validation.DNS1123SubdomainMaxLength) |
| 85 | + assert.Regexp(t, hashSuffix, got) |
| 86 | + assert.True(t, strings.HasPrefix(got, claimLeasePrefix+strings.Repeat("a", lastKept)), |
| 87 | + "the readable start of the node name must be kept") |
| 88 | + assert.NotContains(t, got, ".-") |
| 89 | + assert.NotContains(t, got, "--") |
| 90 | + }) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func TestClaimLeaseName_LongNamesWithSharedStartDoNotCollide(t *testing.T) { |
| 95 | + t.Parallel() |
| 96 | + |
| 97 | + shared := strings.Repeat("a", 250) |
| 98 | + |
| 99 | + first := ClaimLeaseName(shared + "-1") |
| 100 | + second := ClaimLeaseName(shared + "-2") |
| 101 | + |
| 102 | + require.NotEqual(t, first, second) |
| 103 | +} |
0 commit comments