generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from nojnhuh/unit-tests
add unit tests
- Loading branch information
Showing
6 changed files
with
402 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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,99 @@ | ||
/* | ||
* Copyright 2025 The Kubernetes Authors. | ||
* | ||
* 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. | ||
*/ | ||
|
||
package v1alpha1 | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGpuConfigNormalize(t *testing.T) { | ||
tests := map[string]struct { | ||
gpuConfig *GpuConfig | ||
expected *GpuConfig | ||
expectedErr error | ||
}{ | ||
"nil GpuConfig": { | ||
gpuConfig: nil, | ||
expectedErr: errors.New("config is 'nil'"), | ||
}, | ||
"empty GpuConfig": { | ||
gpuConfig: &GpuConfig{}, | ||
expected: &GpuConfig{ | ||
Sharing: &GpuSharing{ | ||
Strategy: TimeSlicingStrategy, | ||
TimeSlicingConfig: &TimeSlicingConfig{ | ||
Interval: DefaultTimeSlice, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"empty GpuConfig with SpacePartitioning": { | ||
gpuConfig: &GpuConfig{ | ||
Sharing: &GpuSharing{ | ||
Strategy: SpacePartitioningStrategy, | ||
}, | ||
}, | ||
expected: &GpuConfig{ | ||
Sharing: &GpuSharing{ | ||
Strategy: SpacePartitioningStrategy, | ||
SpacePartitioningConfig: &SpacePartitioningConfig{ | ||
PartitionCount: 1, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"full GpuConfig": { | ||
gpuConfig: &GpuConfig{ | ||
Sharing: &GpuSharing{ | ||
Strategy: SpacePartitioningStrategy, | ||
TimeSlicingConfig: &TimeSlicingConfig{ | ||
Interval: ShortTimeSlice, | ||
}, | ||
SpacePartitioningConfig: &SpacePartitioningConfig{ | ||
PartitionCount: 5, | ||
}, | ||
}, | ||
}, | ||
expected: &GpuConfig{ | ||
Sharing: &GpuSharing{ | ||
Strategy: SpacePartitioningStrategy, | ||
TimeSlicingConfig: &TimeSlicingConfig{ | ||
Interval: ShortTimeSlice, | ||
}, | ||
SpacePartitioningConfig: &SpacePartitioningConfig{ | ||
PartitionCount: 5, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"default GpuConfig is already normalized": { | ||
gpuConfig: DefaultGpuConfig(), | ||
expected: DefaultGpuConfig(), | ||
}, | ||
} | ||
|
||
for name, test := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
err := test.gpuConfig.Normalize() | ||
assert.Equal(t, test.expected, test.gpuConfig) | ||
assert.Equal(t, test.expectedErr, err) | ||
}) | ||
} | ||
} |
This file contains 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,114 @@ | ||
/* | ||
* Copyright 2025 The Kubernetes Authors. | ||
* | ||
* 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. | ||
*/ | ||
|
||
package v1alpha1 | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGpuSharingGetTimeSlicingConfig(t *testing.T) { | ||
tests := map[string]struct { | ||
gpuSharing *GpuSharing | ||
expected *TimeSlicingConfig | ||
expectedErr error | ||
}{ | ||
"nil GpuSharing": { | ||
gpuSharing: nil, | ||
expectedErr: errors.New("no sharing set to get config from"), | ||
}, | ||
"strategy is not TimeSlicing": { | ||
gpuSharing: &GpuSharing{ | ||
Strategy: SpacePartitioningStrategy, | ||
}, | ||
expectedErr: errors.New("strategy is not set to 'TimeSlicing'"), | ||
}, | ||
"non-nil SpacePartitioningConfig": { | ||
gpuSharing: &GpuSharing{ | ||
Strategy: TimeSlicingStrategy, | ||
SpacePartitioningConfig: &SpacePartitioningConfig{}, | ||
}, | ||
expectedErr: errors.New("cannot use SpacePartitioningConfig with the 'TimeSlicing' strategy"), | ||
}, | ||
"valid TimeSlicingConfig": { | ||
gpuSharing: &GpuSharing{ | ||
Strategy: TimeSlicingStrategy, | ||
TimeSlicingConfig: &TimeSlicingConfig{ | ||
Interval: LongTimeSlice, | ||
}, | ||
}, | ||
expected: &TimeSlicingConfig{ | ||
Interval: LongTimeSlice, | ||
}, | ||
}, | ||
} | ||
|
||
for name, test := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
timeSlicing, err := test.gpuSharing.GetTimeSlicingConfig() | ||
assert.Equal(t, test.expected, timeSlicing) | ||
assert.Equal(t, test.expectedErr, err) | ||
}) | ||
} | ||
|
||
} | ||
func TestGpuSharingGetSpacePartitioningConfig(t *testing.T) { | ||
tests := map[string]struct { | ||
gpuSharing *GpuSharing | ||
expected *SpacePartitioningConfig | ||
expectedErr error | ||
}{ | ||
"nil GpuSharing": { | ||
gpuSharing: nil, | ||
expectedErr: errors.New("no sharing set to get config from"), | ||
}, | ||
"strategy is not SpacePartitioning": { | ||
gpuSharing: &GpuSharing{ | ||
Strategy: TimeSlicingStrategy, | ||
}, | ||
expectedErr: errors.New("strategy is not set to 'SpacePartitioning'"), | ||
}, | ||
"non-nil TimeSlicingConfig": { | ||
gpuSharing: &GpuSharing{ | ||
Strategy: SpacePartitioningStrategy, | ||
TimeSlicingConfig: &TimeSlicingConfig{}, | ||
}, | ||
expectedErr: errors.New("cannot use TimeSlicingConfig with the 'SpacePartitioning' strategy"), | ||
}, | ||
"valid SpacePartitioningConfig": { | ||
gpuSharing: &GpuSharing{ | ||
Strategy: SpacePartitioningStrategy, | ||
SpacePartitioningConfig: &SpacePartitioningConfig{ | ||
PartitionCount: 5, | ||
}, | ||
}, | ||
expected: &SpacePartitioningConfig{ | ||
PartitionCount: 5, | ||
}, | ||
}, | ||
} | ||
|
||
for name, test := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
spacePartitioning, err := test.gpuSharing.GetSpacePartitioningConfig() | ||
assert.Equal(t, test.expected, spacePartitioning) | ||
assert.Equal(t, test.expectedErr, err) | ||
}) | ||
} | ||
} |
This file contains 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,129 @@ | ||
/* | ||
* Copyright 2025 The Kubernetes Authors. | ||
* | ||
* 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. | ||
*/ | ||
|
||
package v1alpha1 | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGpuConfigValidate(t *testing.T) { | ||
tests := map[string]struct { | ||
gpuConfig *GpuConfig | ||
expected error | ||
}{ | ||
"empty GpuConfig": { | ||
gpuConfig: &GpuConfig{}, | ||
expected: errors.New("no sharing strategy set"), | ||
}, | ||
"empty GpuConfig.Sharing": { | ||
gpuConfig: &GpuConfig{ | ||
Sharing: &GpuSharing{}, | ||
}, | ||
expected: errors.New("unknown GPU sharing strategy: "), | ||
}, | ||
"unknown GPU sharing strategy": { | ||
gpuConfig: &GpuConfig{ | ||
Sharing: &GpuSharing{ | ||
Strategy: "unknown", | ||
}, | ||
}, | ||
expected: errors.New("unknown GPU sharing strategy: unknown"), | ||
}, | ||
"empty GpuConfig.Sharing.TimeSlicingConfig": { | ||
gpuConfig: &GpuConfig{ | ||
Sharing: &GpuSharing{ | ||
Strategy: TimeSlicingStrategy, | ||
TimeSlicingConfig: &TimeSlicingConfig{}, | ||
}, | ||
}, | ||
expected: errors.New("unknown time-slice interval: "), | ||
}, | ||
"valid GpuConfig with TimeSlicing": { | ||
gpuConfig: &GpuConfig{ | ||
Sharing: &GpuSharing{ | ||
Strategy: TimeSlicingStrategy, | ||
TimeSlicingConfig: &TimeSlicingConfig{ | ||
Interval: MediumTimeSlice, | ||
}, | ||
}, | ||
}, | ||
expected: nil, | ||
}, | ||
"negative GpuConfig.Sharing.SpacePartitioningConfig.PartitionCount": { | ||
gpuConfig: &GpuConfig{ | ||
Sharing: &GpuSharing{ | ||
Strategy: SpacePartitioningStrategy, | ||
SpacePartitioningConfig: &SpacePartitioningConfig{ | ||
PartitionCount: -1, | ||
}, | ||
}, | ||
}, | ||
expected: errors.New("invalid partition count: -1"), | ||
}, | ||
"valid GpuConfig with SpacePartitioning": { | ||
gpuConfig: &GpuConfig{ | ||
Sharing: &GpuSharing{ | ||
Strategy: SpacePartitioningStrategy, | ||
SpacePartitioningConfig: &SpacePartitioningConfig{ | ||
PartitionCount: 1000, | ||
}, | ||
}, | ||
}, | ||
expected: nil, | ||
}, | ||
"default GpuConfig": { | ||
gpuConfig: DefaultGpuConfig(), | ||
expected: nil, | ||
}, | ||
"invalid TimeSlicingConfig ignored with strategy is SpacePartitioning": { | ||
gpuConfig: &GpuConfig{ | ||
Sharing: &GpuSharing{ | ||
Strategy: SpacePartitioningStrategy, | ||
TimeSlicingConfig: &TimeSlicingConfig{}, | ||
SpacePartitioningConfig: &SpacePartitioningConfig{ | ||
PartitionCount: 1, | ||
}, | ||
}, | ||
}, | ||
expected: nil, | ||
}, | ||
"invalid SpacePartitioningConfig ignored with strategy is TimeSlicing": { | ||
gpuConfig: &GpuConfig{ | ||
Sharing: &GpuSharing{ | ||
Strategy: TimeSlicingStrategy, | ||
TimeSlicingConfig: &TimeSlicingConfig{ | ||
Interval: MediumTimeSlice, | ||
}, | ||
SpacePartitioningConfig: &SpacePartitioningConfig{ | ||
PartitionCount: -1, | ||
}, | ||
}, | ||
}, | ||
expected: nil, | ||
}, | ||
} | ||
|
||
for name, test := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
err := test.gpuConfig.Validate() | ||
assert.Equal(t, test.expected, err) | ||
}) | ||
} | ||
} |
Oops, something went wrong.