2023-02-27 14:53:04 +00:00
|
|
|
/**
|
|
|
|
# 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/stretchr/testify/require"
|
2023-11-01 11:40:51 +00:00
|
|
|
"tags.cncf.io/container-device-interface/specs-go"
|
2023-02-27 14:53:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-02-27 14:53:04 +00:00
|
|
|
{
|
|
|
|
description: "simplify removes hooks",
|
|
|
|
spec: &specs.Spec{
|
|
|
|
Devices: []specs.Device{
|
|
|
|
{
|
|
|
|
Name: "gpu0",
|
|
|
|
ContainerEdits: specs.ContainerEdits{
|
|
|
|
Hooks: []*specs.Hook{
|
|
|
|
{
|
|
|
|
HookName: "createContainer",
|
2024-04-24 08:47:45 +00:00
|
|
|
Path: "/usr/bin/nvidia-cdi-hook",
|
|
|
|
Args: []string{"nvidia-cdi-hook", "chmod", "--mode", "755", "--path", "/dev/dri"},
|
2023-02-27 14:53:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "gpu1",
|
|
|
|
ContainerEdits: specs.ContainerEdits{
|
|
|
|
Hooks: []*specs.Hook{
|
|
|
|
{
|
|
|
|
HookName: "createContainer",
|
2024-04-24 08:47:45 +00:00
|
|
|
Path: "/usr/bin/nvidia-cdi-hook",
|
|
|
|
Args: []string{"nvidia-cdi-hook", "chmod", "--mode", "755", "--path", "/dev/dri"},
|
2023-02-27 14:53:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "all",
|
|
|
|
ContainerEdits: specs.ContainerEdits{
|
|
|
|
Hooks: []*specs.Hook{
|
|
|
|
{
|
|
|
|
HookName: "createContainer",
|
2024-04-24 08:47:45 +00:00
|
|
|
Path: "/usr/bin/nvidia-cdi-hook",
|
|
|
|
Args: []string{"nvidia-cdi-hook", "chmod", "--mode", "755", "--path", "/dev/dri"},
|
2023-02-27 14:53:04 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
HookName: "createContainer",
|
2024-04-24 08:47:45 +00:00
|
|
|
Path: "/usr/bin/nvidia-cdi-hook",
|
|
|
|
Args: []string{"nvidia-cdi-hook", "chmod", "--mode", "755", "--path", "/dev/dri"},
|
2023-02-27 14:53:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedSpec: &specs.Spec{
|
|
|
|
Devices: []specs.Device{
|
|
|
|
{
|
|
|
|
Name: "gpu0",
|
|
|
|
ContainerEdits: specs.ContainerEdits{
|
|
|
|
Hooks: []*specs.Hook{
|
|
|
|
{
|
|
|
|
HookName: "createContainer",
|
2024-04-24 08:47:45 +00:00
|
|
|
Path: "/usr/bin/nvidia-cdi-hook",
|
|
|
|
Args: []string{"nvidia-cdi-hook", "chmod", "--mode", "755", "--path", "/dev/dri"},
|
2023-02-27 14:53:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "gpu1",
|
|
|
|
ContainerEdits: specs.ContainerEdits{
|
|
|
|
Hooks: []*specs.Hook{
|
|
|
|
{
|
|
|
|
HookName: "createContainer",
|
2024-04-24 08:47:45 +00:00
|
|
|
Path: "/usr/bin/nvidia-cdi-hook",
|
|
|
|
Args: []string{"nvidia-cdi-hook", "chmod", "--mode", "755", "--path", "/dev/dri"},
|
2023-02-27 14:53:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "all",
|
|
|
|
ContainerEdits: specs.ContainerEdits{
|
|
|
|
Hooks: []*specs.Hook{
|
|
|
|
{
|
|
|
|
HookName: "createContainer",
|
2024-04-24 08:47:45 +00:00
|
|
|
Path: "/usr/bin/nvidia-cdi-hook",
|
|
|
|
Args: []string{"nvidia-cdi-hook", "chmod", "--mode", "755", "--path", "/dev/dri"},
|
2023-02-27 14:53:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-02-27 14:53:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|