nvidia-container-toolkit/pkg/nvcdi/transform/simplify_test.go
Evan Lezar 5ff206e1a9 Add transform to deduplicate entities in CDI spec
Signed-off-by: Evan Lezar <elezar@nvidia.com>
2023-03-24 11:01:23 +02:00

126 lines
2.7 KiB
Go

/**
# Copyright (c) NVIDIA CORPORATION. All rights reserved.
#
# 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 transform
import (
"testing"
"github.com/container-orchestrated-devices/container-device-interface/specs-go"
"github.com/stretchr/testify/require"
)
func TestSimplify(t *testing.T) {
testCases := []struct {
description string
spec *specs.Spec
expectedError error
expectedSpec *specs.Spec
}{
{
description: "nil spec is a no-op",
},
{
description: "empty spec is simplified",
spec: &specs.Spec{},
expectedSpec: &specs.Spec{},
},
{
description: "simplify does not allow empty device",
spec: &specs.Spec{
Devices: []specs.Device{
{
Name: "device0",
ContainerEdits: specs.ContainerEdits{
Env: []string{"FOO=BAR"},
},
},
},
ContainerEdits: specs.ContainerEdits{
Env: []string{"FOO=BAR"},
},
},
expectedSpec: &specs.Spec{
Devices: []specs.Device{
{
Name: "device0",
ContainerEdits: specs.ContainerEdits{
Env: []string{"FOO=BAR"},
},
},
},
ContainerEdits: specs.ContainerEdits{
Env: []string{"FOO=BAR"},
},
},
},
{
description: "simplify removes common entities",
spec: &specs.Spec{
Devices: []specs.Device{
{
Name: "device0",
ContainerEdits: specs.ContainerEdits{
Env: []string{"FOO=BAR"},
DeviceNodes: []*specs.DeviceNode{
{
Path: "/dev/gpu0",
},
},
},
},
},
ContainerEdits: specs.ContainerEdits{
Env: []string{"FOO=BAR"},
},
},
expectedSpec: &specs.Spec{
Devices: []specs.Device{
{
Name: "device0",
ContainerEdits: specs.ContainerEdits{
DeviceNodes: []*specs.DeviceNode{
{
Path: "/dev/gpu0",
},
},
},
},
},
ContainerEdits: specs.ContainerEdits{
Env: []string{"FOO=BAR"},
},
},
},
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
s := simplify{}
err := s.Transform(tc.spec)
if tc.expectedError != nil {
require.Error(t, err)
return
}
require.NoError(t, err)
require.EqualValues(t, tc.expectedSpec, tc.spec)
})
}
}