Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nojnhuh committed Dec 10, 2024
1 parent 5ff76f1 commit 9ea6364
Show file tree
Hide file tree
Showing 6 changed files with 436 additions and 4 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 ./...
116 changes: 116 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,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)
}
})
}
}
147 changes: 147 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,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)
}
})
}
}
110 changes: 110 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,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)
}
}
})
}
}
Loading

0 comments on commit 9ea6364

Please sign in to comment.