Skip to content
Snippets Groups Projects
Commit e9fef587 authored by Alex de Cock Buning's avatar Alex de Cock Buning
Browse files

operator: bug fix for switch ip addresses

switch ipv6 addresses where buggy, and where having assigned a non compliant ipv6. now implemented some unit tests plus fixed this error
parent ea6d06bf
No related branches found
No related tags found
No related merge requests found
...@@ -159,7 +159,7 @@ func GenerateAnnotations(overlayName string, ammount int) string { ...@@ -159,7 +159,7 @@ func GenerateAnnotations(overlayName string, ammount int) string {
annotationsString := []string{} annotationsString := []string{}
var newAnnotation string var newAnnotation string
for i := 1; i <= ammount; i++ { 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) annotationsString = append(annotationsString, newAnnotation)
} }
...@@ -169,9 +169,9 @@ func GenerateAnnotations(overlayName string, ammount int) string { ...@@ -169,9 +169,9 @@ func GenerateAnnotations(overlayName string, ammount int) string {
func Generate4byteChunk() string { func Generate4byteChunk() string {
// Generating the interface ID (64 bits) // 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) interfaceIDHex := fmt.Sprintf("%04x", interfaceID)
return interfaceIDHex return interfaceIDHex
......
// 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)
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment