diff --git a/internal/controller/pod_utils.go b/internal/controller/pod_utils.go index 6ebbbbbf5acce4c2f391b2c3cd207573df6885d4..cd3f13d1a07b3f651a18f30d71b1cf2f7eb87016 100644 --- a/internal/controller/pod_utils.go +++ b/internal/controller/pod_utils.go @@ -159,7 +159,7 @@ func GenerateAnnotations(overlayName string, ammount int) string { annotationsString := []string{} var newAnnotation string for i := 1; i <= ammount; i++ { - newAnnotation = fmt.Sprintf(`{"name": "%s-veth%d", "ips": ["fe80::58d0:b8ff:fe%s:%s/64"]}`, overlayName, i, fmt.Sprintf("%02d", i), Generate4byteChunk) + newAnnotation = fmt.Sprintf(`{"name": "%s-veth%d", "ips": ["fe80::58d0:b8ff:fe%s:%s/64"]}`, overlayName, i, fmt.Sprintf("%02d", i), Generate4byteChunk()) annotationsString = append(annotationsString, newAnnotation) } @@ -169,9 +169,9 @@ func GenerateAnnotations(overlayName string, ammount int) string { func Generate4byteChunk() string { // Generating the interface ID (64 bits) - interfaceID := rand.Uint64() + interfaceID := rand.Uint64() & 0xffff - // Formatting to a 16 character hexadecimal string + // Formatting to a 4 character hexadecimal string interfaceIDHex := fmt.Sprintf("%04x", interfaceID) return interfaceIDHex diff --git a/internal/controller/pod_utils_test.go b/internal/controller/pod_utils_test.go new file mode 100644 index 0000000000000000000000000000000000000000..82251f65be4e333ccf764154153a8c60ab89b498 --- /dev/null +++ b/internal/controller/pod_utils_test.go @@ -0,0 +1,37 @@ +// Copyright 2024 Universidad Carlos III de Madrid +// +// 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. + +// generate_test.go +package controller + +import ( + "regexp" + "testing" +) + +// TestGenerate4byteChunk verifies that Generate4byteChunk produces a 4-character hexadecimal string. +func TestGenerate4byteChunk(t *testing.T) { + // Regular expression to match exactly 4 hexadecimal characters + re := regexp.MustCompile(`^[0-9a-fA-F]{4}$`) + + // Call Generate4byteChunk multiple times to check if output is always 4 characters + for i := 0; i < 100; i++ { + output := Generate4byteChunk() + + // Check if the output matches the 4-character hex pattern + if !re.MatchString(output) { + t.Errorf("Expected a 4-character hexadecimal string, but got: %s", output) + } + } +}