Skip to content

Commit

Permalink
Merge pull request #70 from nojnhuh/unit-tests
Browse files Browse the repository at this point in the history
add unit tests
  • Loading branch information
k8s-ci-robot authored Jan 18, 2025
2 parents 5e8f4f9 + 3942920 commit cfe7e11
Show file tree
Hide file tree
Showing 6 changed files with 402 additions and 3 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,5 @@ jobs:
run: make PREFIX=artifacts cmds
- name: List binaries
run: ls -al artifacts/
# no tests yet
# - name: Test
# run: go test -v -race ./...
- name: Test
run: go test -v -race ./...
99 changes: 99 additions & 0 deletions api/example.com/resource/gpu/v1alpha1/api_test.go
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)
})
}
}
114 changes: 114 additions & 0 deletions api/example.com/resource/gpu/v1alpha1/sharing_test.go
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)
})
}
}
129 changes: 129 additions & 0 deletions api/example.com/resource/gpu/v1alpha1/validate_test.go
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)
})
}
}
Loading

0 comments on commit cfe7e11

Please sign in to comment.