Skip to content

Commit cfe7e11

Browse files
authored
Merge pull request #70 from nojnhuh/unit-tests
add unit tests
2 parents 5e8f4f9 + 3942920 commit cfe7e11

File tree

6 files changed

+402
-3
lines changed

6 files changed

+402
-3
lines changed

.github/workflows/tests.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,5 @@ jobs:
2525
run: make PREFIX=artifacts cmds
2626
- name: List binaries
2727
run: ls -al artifacts/
28-
# no tests yet
29-
# - name: Test
30-
# run: go test -v -race ./...
28+
- name: Test
29+
run: go test -v -race ./...
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright 2025 The Kubernetes Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package v1alpha1
18+
19+
import (
20+
"errors"
21+
"testing"
22+
23+
"github.com/stretchr/testify/assert"
24+
)
25+
26+
func TestGpuConfigNormalize(t *testing.T) {
27+
tests := map[string]struct {
28+
gpuConfig *GpuConfig
29+
expected *GpuConfig
30+
expectedErr error
31+
}{
32+
"nil GpuConfig": {
33+
gpuConfig: nil,
34+
expectedErr: errors.New("config is 'nil'"),
35+
},
36+
"empty GpuConfig": {
37+
gpuConfig: &GpuConfig{},
38+
expected: &GpuConfig{
39+
Sharing: &GpuSharing{
40+
Strategy: TimeSlicingStrategy,
41+
TimeSlicingConfig: &TimeSlicingConfig{
42+
Interval: DefaultTimeSlice,
43+
},
44+
},
45+
},
46+
},
47+
"empty GpuConfig with SpacePartitioning": {
48+
gpuConfig: &GpuConfig{
49+
Sharing: &GpuSharing{
50+
Strategy: SpacePartitioningStrategy,
51+
},
52+
},
53+
expected: &GpuConfig{
54+
Sharing: &GpuSharing{
55+
Strategy: SpacePartitioningStrategy,
56+
SpacePartitioningConfig: &SpacePartitioningConfig{
57+
PartitionCount: 1,
58+
},
59+
},
60+
},
61+
},
62+
"full GpuConfig": {
63+
gpuConfig: &GpuConfig{
64+
Sharing: &GpuSharing{
65+
Strategy: SpacePartitioningStrategy,
66+
TimeSlicingConfig: &TimeSlicingConfig{
67+
Interval: ShortTimeSlice,
68+
},
69+
SpacePartitioningConfig: &SpacePartitioningConfig{
70+
PartitionCount: 5,
71+
},
72+
},
73+
},
74+
expected: &GpuConfig{
75+
Sharing: &GpuSharing{
76+
Strategy: SpacePartitioningStrategy,
77+
TimeSlicingConfig: &TimeSlicingConfig{
78+
Interval: ShortTimeSlice,
79+
},
80+
SpacePartitioningConfig: &SpacePartitioningConfig{
81+
PartitionCount: 5,
82+
},
83+
},
84+
},
85+
},
86+
"default GpuConfig is already normalized": {
87+
gpuConfig: DefaultGpuConfig(),
88+
expected: DefaultGpuConfig(),
89+
},
90+
}
91+
92+
for name, test := range tests {
93+
t.Run(name, func(t *testing.T) {
94+
err := test.gpuConfig.Normalize()
95+
assert.Equal(t, test.expected, test.gpuConfig)
96+
assert.Equal(t, test.expectedErr, err)
97+
})
98+
}
99+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Copyright 2025 The Kubernetes Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package v1alpha1
18+
19+
import (
20+
"errors"
21+
"testing"
22+
23+
"github.com/stretchr/testify/assert"
24+
)
25+
26+
func TestGpuSharingGetTimeSlicingConfig(t *testing.T) {
27+
tests := map[string]struct {
28+
gpuSharing *GpuSharing
29+
expected *TimeSlicingConfig
30+
expectedErr error
31+
}{
32+
"nil GpuSharing": {
33+
gpuSharing: nil,
34+
expectedErr: errors.New("no sharing set to get config from"),
35+
},
36+
"strategy is not TimeSlicing": {
37+
gpuSharing: &GpuSharing{
38+
Strategy: SpacePartitioningStrategy,
39+
},
40+
expectedErr: errors.New("strategy is not set to 'TimeSlicing'"),
41+
},
42+
"non-nil SpacePartitioningConfig": {
43+
gpuSharing: &GpuSharing{
44+
Strategy: TimeSlicingStrategy,
45+
SpacePartitioningConfig: &SpacePartitioningConfig{},
46+
},
47+
expectedErr: errors.New("cannot use SpacePartitioningConfig with the 'TimeSlicing' strategy"),
48+
},
49+
"valid TimeSlicingConfig": {
50+
gpuSharing: &GpuSharing{
51+
Strategy: TimeSlicingStrategy,
52+
TimeSlicingConfig: &TimeSlicingConfig{
53+
Interval: LongTimeSlice,
54+
},
55+
},
56+
expected: &TimeSlicingConfig{
57+
Interval: LongTimeSlice,
58+
},
59+
},
60+
}
61+
62+
for name, test := range tests {
63+
t.Run(name, func(t *testing.T) {
64+
timeSlicing, err := test.gpuSharing.GetTimeSlicingConfig()
65+
assert.Equal(t, test.expected, timeSlicing)
66+
assert.Equal(t, test.expectedErr, err)
67+
})
68+
}
69+
70+
}
71+
func TestGpuSharingGetSpacePartitioningConfig(t *testing.T) {
72+
tests := map[string]struct {
73+
gpuSharing *GpuSharing
74+
expected *SpacePartitioningConfig
75+
expectedErr error
76+
}{
77+
"nil GpuSharing": {
78+
gpuSharing: nil,
79+
expectedErr: errors.New("no sharing set to get config from"),
80+
},
81+
"strategy is not SpacePartitioning": {
82+
gpuSharing: &GpuSharing{
83+
Strategy: TimeSlicingStrategy,
84+
},
85+
expectedErr: errors.New("strategy is not set to 'SpacePartitioning'"),
86+
},
87+
"non-nil TimeSlicingConfig": {
88+
gpuSharing: &GpuSharing{
89+
Strategy: SpacePartitioningStrategy,
90+
TimeSlicingConfig: &TimeSlicingConfig{},
91+
},
92+
expectedErr: errors.New("cannot use TimeSlicingConfig with the 'SpacePartitioning' strategy"),
93+
},
94+
"valid SpacePartitioningConfig": {
95+
gpuSharing: &GpuSharing{
96+
Strategy: SpacePartitioningStrategy,
97+
SpacePartitioningConfig: &SpacePartitioningConfig{
98+
PartitionCount: 5,
99+
},
100+
},
101+
expected: &SpacePartitioningConfig{
102+
PartitionCount: 5,
103+
},
104+
},
105+
}
106+
107+
for name, test := range tests {
108+
t.Run(name, func(t *testing.T) {
109+
spacePartitioning, err := test.gpuSharing.GetSpacePartitioningConfig()
110+
assert.Equal(t, test.expected, spacePartitioning)
111+
assert.Equal(t, test.expectedErr, err)
112+
})
113+
}
114+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* Copyright 2025 The Kubernetes Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package v1alpha1
18+
19+
import (
20+
"errors"
21+
"testing"
22+
23+
"github.com/stretchr/testify/assert"
24+
)
25+
26+
func TestGpuConfigValidate(t *testing.T) {
27+
tests := map[string]struct {
28+
gpuConfig *GpuConfig
29+
expected error
30+
}{
31+
"empty GpuConfig": {
32+
gpuConfig: &GpuConfig{},
33+
expected: errors.New("no sharing strategy set"),
34+
},
35+
"empty GpuConfig.Sharing": {
36+
gpuConfig: &GpuConfig{
37+
Sharing: &GpuSharing{},
38+
},
39+
expected: errors.New("unknown GPU sharing strategy: "),
40+
},
41+
"unknown GPU sharing strategy": {
42+
gpuConfig: &GpuConfig{
43+
Sharing: &GpuSharing{
44+
Strategy: "unknown",
45+
},
46+
},
47+
expected: errors.New("unknown GPU sharing strategy: unknown"),
48+
},
49+
"empty GpuConfig.Sharing.TimeSlicingConfig": {
50+
gpuConfig: &GpuConfig{
51+
Sharing: &GpuSharing{
52+
Strategy: TimeSlicingStrategy,
53+
TimeSlicingConfig: &TimeSlicingConfig{},
54+
},
55+
},
56+
expected: errors.New("unknown time-slice interval: "),
57+
},
58+
"valid GpuConfig with TimeSlicing": {
59+
gpuConfig: &GpuConfig{
60+
Sharing: &GpuSharing{
61+
Strategy: TimeSlicingStrategy,
62+
TimeSlicingConfig: &TimeSlicingConfig{
63+
Interval: MediumTimeSlice,
64+
},
65+
},
66+
},
67+
expected: nil,
68+
},
69+
"negative GpuConfig.Sharing.SpacePartitioningConfig.PartitionCount": {
70+
gpuConfig: &GpuConfig{
71+
Sharing: &GpuSharing{
72+
Strategy: SpacePartitioningStrategy,
73+
SpacePartitioningConfig: &SpacePartitioningConfig{
74+
PartitionCount: -1,
75+
},
76+
},
77+
},
78+
expected: errors.New("invalid partition count: -1"),
79+
},
80+
"valid GpuConfig with SpacePartitioning": {
81+
gpuConfig: &GpuConfig{
82+
Sharing: &GpuSharing{
83+
Strategy: SpacePartitioningStrategy,
84+
SpacePartitioningConfig: &SpacePartitioningConfig{
85+
PartitionCount: 1000,
86+
},
87+
},
88+
},
89+
expected: nil,
90+
},
91+
"default GpuConfig": {
92+
gpuConfig: DefaultGpuConfig(),
93+
expected: nil,
94+
},
95+
"invalid TimeSlicingConfig ignored with strategy is SpacePartitioning": {
96+
gpuConfig: &GpuConfig{
97+
Sharing: &GpuSharing{
98+
Strategy: SpacePartitioningStrategy,
99+
TimeSlicingConfig: &TimeSlicingConfig{},
100+
SpacePartitioningConfig: &SpacePartitioningConfig{
101+
PartitionCount: 1,
102+
},
103+
},
104+
},
105+
expected: nil,
106+
},
107+
"invalid SpacePartitioningConfig ignored with strategy is TimeSlicing": {
108+
gpuConfig: &GpuConfig{
109+
Sharing: &GpuSharing{
110+
Strategy: TimeSlicingStrategy,
111+
TimeSlicingConfig: &TimeSlicingConfig{
112+
Interval: MediumTimeSlice,
113+
},
114+
SpacePartitioningConfig: &SpacePartitioningConfig{
115+
PartitionCount: -1,
116+
},
117+
},
118+
},
119+
expected: nil,
120+
},
121+
}
122+
123+
for name, test := range tests {
124+
t.Run(name, func(t *testing.T) {
125+
err := test.gpuConfig.Validate()
126+
assert.Equal(t, test.expected, err)
127+
})
128+
}
129+
}

0 commit comments

Comments
 (0)