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.
- Loading branch information
Showing
6 changed files
with
436 additions
and
4 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,116 @@ | ||
/* | ||
* Copyright 2024 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 ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func TestGpuConfigNormalize(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
gpuConfig *GpuConfig | ||
expected *GpuConfig | ||
expectedErr string | ||
}{ | ||
{ | ||
name: "nil GpuConfig", | ||
gpuConfig: nil, | ||
expectedErr: "config is 'nil'", | ||
}, | ||
{ | ||
name: "empty GpuConfig", | ||
gpuConfig: &GpuConfig{}, | ||
expected: &GpuConfig{ | ||
Sharing: &GpuSharing{ | ||
Strategy: TimeSlicingStrategy, | ||
TimeSlicingConfig: &TimeSlicingConfig{ | ||
Interval: DefaultTimeSlice, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "empty GpuConfig with SpacePartitioning", | ||
gpuConfig: &GpuConfig{ | ||
Sharing: &GpuSharing{ | ||
Strategy: SpacePartitioningStrategy, | ||
}, | ||
}, | ||
expected: &GpuConfig{ | ||
Sharing: &GpuSharing{ | ||
Strategy: SpacePartitioningStrategy, | ||
SpacePartitioningConfig: &SpacePartitioningConfig{ | ||
PartitionCount: 1, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "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, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "default GpuConfig", | ||
gpuConfig: DefaultGpuConfig(), | ||
expected: DefaultGpuConfig(), // default should be fully normalized already | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
err := test.gpuConfig.Normalize() | ||
if test.expectedErr != "" { | ||
if err == nil { | ||
t.Errorf("expected error %q, but got no error", test.expectedErr) | ||
} else if err.Error() != test.expectedErr { | ||
t.Errorf("expected error %q, but got %v", test.expectedErr, err) | ||
} | ||
} else { | ||
if err != nil { | ||
t.Errorf("expected Normalize to succeed, got error %v", err) | ||
} | ||
} | ||
if diff := cmp.Diff(test.gpuConfig, test.expected); diff != "" { | ||
t.Error("expected configs to be equal:\n", diff) | ||
} | ||
}) | ||
} | ||
} |
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,147 @@ | ||
/* | ||
* Copyright 2024 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 ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func TestGpuSharingGetTimeSlicingConfig(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
gpuSharing *GpuSharing | ||
expected *TimeSlicingConfig | ||
expectedErr string | ||
}{ | ||
{ | ||
name: "nil GpuSharing", | ||
gpuSharing: nil, | ||
expectedErr: "no sharing set to get config from", | ||
}, | ||
{ | ||
name: "strategy is not TimeSlicing", | ||
gpuSharing: &GpuSharing{ | ||
Strategy: "not" + TimeSlicingStrategy, | ||
}, | ||
expectedErr: "strategy is not set to 'TimeSlicing'", | ||
}, | ||
{ | ||
name: "non-nil SpacePartitioningConfig", | ||
gpuSharing: &GpuSharing{ | ||
Strategy: TimeSlicingStrategy, | ||
SpacePartitioningConfig: &SpacePartitioningConfig{}, | ||
}, | ||
expectedErr: "cannot use SpacePartitioningConfig with the 'TimeSlicing' strategy", | ||
}, | ||
{ | ||
name: "valid TimeSlicingConfig", | ||
gpuSharing: &GpuSharing{ | ||
Strategy: TimeSlicingStrategy, | ||
TimeSlicingConfig: &TimeSlicingConfig{ | ||
Interval: LongTimeSlice, | ||
}, | ||
}, | ||
expected: &TimeSlicingConfig{ | ||
Interval: LongTimeSlice, | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
timeSlicing, err := test.gpuSharing.GetTimeSlicingConfig() | ||
if test.expectedErr != "" { | ||
if err == nil { | ||
t.Errorf("expected error %q, but got no error", test.expectedErr) | ||
} else if err.Error() != test.expectedErr { | ||
t.Errorf("expected error %q, but got %v", test.expectedErr, err) | ||
} | ||
} else { | ||
if err != nil { | ||
t.Errorf("expected GetTimeSlicingConfig to succeed, got error %v", err) | ||
} | ||
} | ||
if diff := cmp.Diff(timeSlicing, test.expected); diff != "" { | ||
t.Error("expected time slicing configs to be equal:\n", diff) | ||
} | ||
}) | ||
} | ||
|
||
} | ||
func TestGpuSharingGetSpacePartitioningConfig(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
gpuSharing *GpuSharing | ||
expected *SpacePartitioningConfig | ||
expectedErr string | ||
}{ | ||
{ | ||
name: "nil GpuSharing", | ||
gpuSharing: nil, | ||
expectedErr: "no sharing set to get config from", | ||
}, | ||
{ | ||
name: "strategy is not SpacePartitioning", | ||
gpuSharing: &GpuSharing{ | ||
Strategy: "not" + SpacePartitioningStrategy, | ||
}, | ||
expectedErr: "strategy is not set to 'SpacePartitioning'", | ||
}, | ||
{ | ||
name: "non-nil TimeSlicingConfig", | ||
gpuSharing: &GpuSharing{ | ||
Strategy: SpacePartitioningStrategy, | ||
TimeSlicingConfig: &TimeSlicingConfig{}, | ||
}, | ||
expectedErr: "cannot use TimeSlicingConfig with the 'SpacePartitioning' strategy", | ||
}, | ||
{ | ||
name: "valid SpacePartitioningConfig", | ||
gpuSharing: &GpuSharing{ | ||
Strategy: SpacePartitioningStrategy, | ||
SpacePartitioningConfig: &SpacePartitioningConfig{ | ||
PartitionCount: 5, | ||
}, | ||
}, | ||
expected: &SpacePartitioningConfig{ | ||
PartitionCount: 5, | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
spacePartitioning, err := test.gpuSharing.GetSpacePartitioningConfig() | ||
if test.expectedErr != "" { | ||
if err == nil { | ||
t.Errorf("expected error %q, but got no error", test.expectedErr) | ||
} else if err.Error() != test.expectedErr { | ||
t.Errorf("expected error %q, but got %v", test.expectedErr, err) | ||
} | ||
} else { | ||
if err != nil { | ||
t.Errorf("expected GetSpacePartitioningConfig to succeed, got error %v", err) | ||
} | ||
} | ||
if diff := cmp.Diff(spacePartitioning, test.expected); diff != "" { | ||
t.Error("expected space partitioning configs to be equal:\n", diff) | ||
} | ||
}) | ||
} | ||
} |
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,110 @@ | ||
/* | ||
* Copyright 2024 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 ( | ||
"testing" | ||
) | ||
|
||
func TestGpuConfigValidate(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
gpuConfig *GpuConfig | ||
expected string | ||
}{ | ||
{ | ||
name: "empty GpuConfig", | ||
gpuConfig: &GpuConfig{}, | ||
expected: "no sharing strategy set", | ||
}, | ||
{ | ||
name: "empty GpuConfig.Sharing", | ||
gpuConfig: &GpuConfig{ | ||
Sharing: &GpuSharing{}, | ||
}, | ||
expected: "unknown GPU sharing strategy: ", | ||
}, | ||
{ | ||
name: "empty GpuConfig.Sharing.TimeSlicingConfig", | ||
gpuConfig: &GpuConfig{ | ||
Sharing: &GpuSharing{ | ||
Strategy: TimeSlicingStrategy, | ||
TimeSlicingConfig: &TimeSlicingConfig{}, | ||
}, | ||
}, | ||
expected: "unknown time-slice interval: ", | ||
}, | ||
{ | ||
name: "valid GpuConfig with TimeSlicing", | ||
gpuConfig: &GpuConfig{ | ||
Sharing: &GpuSharing{ | ||
Strategy: TimeSlicingStrategy, | ||
TimeSlicingConfig: &TimeSlicingConfig{ | ||
Interval: MediumTimeSlice, | ||
}, | ||
}, | ||
}, | ||
expected: "", | ||
}, | ||
{ | ||
name: "negative GpuConfig.Sharing.SpacePartitioningConfig.PartitionCount", | ||
gpuConfig: &GpuConfig{ | ||
Sharing: &GpuSharing{ | ||
Strategy: SpacePartitioningStrategy, | ||
SpacePartitioningConfig: &SpacePartitioningConfig{ | ||
PartitionCount: -1, | ||
}, | ||
}, | ||
}, | ||
expected: "invalid partition count: -1", | ||
}, | ||
{ | ||
name: "valid GpuConfig with SpacePartitioning", | ||
gpuConfig: &GpuConfig{ | ||
Sharing: &GpuSharing{ | ||
Strategy: SpacePartitioningStrategy, | ||
SpacePartitioningConfig: &SpacePartitioningConfig{ | ||
PartitionCount: 1000, | ||
}, | ||
}, | ||
}, | ||
expected: "", | ||
}, | ||
{ | ||
name: "default GpuConfig", | ||
gpuConfig: DefaultGpuConfig(), | ||
expected: "", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
err := test.gpuConfig.Validate() | ||
if test.expected != "" { | ||
if err == nil { | ||
t.Errorf("expected error %q, but got no error", test.expected) | ||
} else if err.Error() != test.expected { | ||
t.Errorf("expected error %q, but got %v", test.expected, err) | ||
} | ||
} else { | ||
if err != nil { | ||
t.Errorf("expected Validate to succeed, got error %v", err) | ||
} | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.