-
Notifications
You must be signed in to change notification settings - Fork 32
fix:104 unsafe integer conversion in instance_utils.go #106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gzb2025
wants to merge
8
commits into
sgl-project:main
Choose a base branch
from
gzb2025:fix-104-integer-conversion
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c9884a6
fix:104 unsafe integer conversion in instance_utils.go
gzb2025 ed239e9
Apply suggestions from code review
gzb2025 83add91
Update pkg/reconciler/instance/utils/instance_utils_test.go
gzb2025 b67f034
Update pkg/reconciler/instance/utils/instance_utils_test.go
gzb2025 1b17a9e
Update pkg/reconciler/instance/utils/instance_utils_test.go
gzb2025 19d90aa
Update comment for label exceeding int32 max
gzb2025 b2c5b57
Follow DRY principle
gzb2025 c128b37
Add test case for int32 min value in pod name
gzb2025 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| package utils | ||
|
|
||
| import ( | ||
| "math" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| corev1 "k8s.io/api/core/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "sigs.k8s.io/rbgs/api/workloads/v1alpha1" | ||
| ) | ||
|
|
||
| // TestGetPodComponentID tests the GetPodComponentID function after the fix | ||
| func TestGetPodComponentID(t *testing.T) { | ||
| // Test cases: covers scenarios with labels present/absent, valid values, boundary values, out-of-range, and non-numeric cases | ||
| tests := []struct { | ||
| name string // Test case name | ||
| pod *corev1.Pod // Input Pod object | ||
| expected int32 // Expected returned ComponentID | ||
| }{ | ||
| // Scenario 1: Get ID from label (normal case) | ||
| { | ||
| name: "valid id from label (positive)", | ||
| pod: &corev1.Pod{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Labels: map[string]string{ | ||
| v1alpha1.InstanceComponentIDKey: "123", | ||
| }, | ||
| }, | ||
| }, | ||
| expected: 123, | ||
| }, | ||
| { | ||
| name: "valid id from label (negative)", | ||
| pod: &corev1.Pod{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Labels: map[string]string{ | ||
| v1alpha1.InstanceComponentIDKey: "-456", | ||
| }, | ||
| }, | ||
| }, | ||
| expected: -456, | ||
| }, | ||
|
|
||
| // Scenario 2: Label exists but value is invalid (non-numeric, out of range) | ||
| { | ||
| name: "invalid non-numeric id in label", | ||
| pod: &corev1.Pod{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Labels: map[string]string{ | ||
| v1alpha1.InstanceComponentIDKey: "abc", | ||
| }, | ||
| }, | ||
| }, | ||
| expected: 0, // Return default value 0 on error | ||
| }, | ||
| { | ||
| name: "id in label exceeds int32 max", | ||
| pod: &corev1.Pod{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Labels: map[string]string{ | ||
| v1alpha1.InstanceComponentIDKey: "2147483648", // 1 greater than MaxInt32 | ||
| }, | ||
| }, | ||
| }, | ||
| expected: 0, | ||
| }, | ||
| { | ||
| name: "id in label less than int32 min", | ||
| pod: &corev1.Pod{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Labels: map[string]string{ | ||
| v1alpha1.InstanceComponentIDKey: "-2147483649", // 1 less than MinInt32 | ||
| }, | ||
| }, | ||
| }, | ||
| expected: 0, | ||
| }, | ||
|
|
||
| // Scenario 3: Label doesn't exist, parse ID from Pod name (name format: prefix-componentName-ID) | ||
| { | ||
| name: "valid id from pod name (positive)", | ||
| pod: &corev1.Pod{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "instance-web-789", // last segment is the ID | ||
| Labels: map[string]string{}, // no ComponentID label | ||
| }, | ||
| }, | ||
| expected: 789, | ||
| }, | ||
| { | ||
| name: "valid id from pod name (negative)", | ||
| pod: &corev1.Pod{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "instance-db--987", // Last segment is negative ID | ||
| Labels: map[string]string{}, | ||
| }, | ||
| }, | ||
| expected: -987, | ||
| }, | ||
|
|
||
| // Scenario 4: Label doesn't exist, name parsing is invalid | ||
| { | ||
| name: "invalid non-numeric id in pod name", | ||
| pod: &corev1.Pod{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "instance-cache-xyz", // Last segment is non-numeric | ||
| Labels: map[string]string{}, | ||
| }, | ||
| }, | ||
| expected: 0, | ||
| }, | ||
| { | ||
| name: "id in pod name exceeds int32 max", | ||
| pod: &corev1.Pod{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "instance-store-2147483648", | ||
| Labels: map[string]string{}, | ||
| }, | ||
| }, | ||
| expected: 0, | ||
| }, | ||
| { | ||
| name: "id in pod name less than int32 min", | ||
| pod: &corev1.Pod{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "instance-queue--2147483649", | ||
| Labels: map[string]string{}, | ||
| }, | ||
| }, | ||
| expected: 0, | ||
| }, | ||
|
|
||
| // Scenario 5: Boundary value tests (int32 max/min values) | ||
| { | ||
| name: "int32 max value in label", | ||
| pod: &corev1.Pod{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Labels: map[string]string{ | ||
| v1alpha1.InstanceComponentIDKey: "2147483647", // math.MaxInt32 | ||
| }, | ||
| }, | ||
| }, | ||
| expected: math.MaxInt32, | ||
| }, | ||
| { | ||
| name: "int32 min value in label", | ||
| pod: &corev1.Pod{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Labels: map[string]string{ | ||
| v1alpha1.InstanceComponentIDKey: "-2147483648", // math.MinInt32 | ||
| }, | ||
| }, | ||
| }, | ||
| expected: math.MinInt32, | ||
| }, | ||
| { | ||
| name: "int32 max value in pod name", | ||
| pod: &corev1.Pod{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "instance-max-2147483647", | ||
| Labels: map[string]string{}, | ||
| }, | ||
| }, | ||
| expected: math.MaxInt32, | ||
| }, | ||
| } | ||
gzb2025 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Execute test cases | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| result := GetPodComponentID(tt.pod) | ||
| assert.Equal(t, tt.expected, result, "Test case [%s] failed", tt.name) | ||
| }) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.