mirror of
https://github.com/NVIDIA/nvidia-container-toolkit
synced 2025-06-26 18:18:24 +00:00
82
internal/runtime/runtime_modifier.go
Normal file
82
internal/runtime/runtime_modifier.go
Normal file
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
# Copyright (c) 2021, 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 runtime
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/NVIDIA/nvidia-container-toolkit/pkg/modify"
|
||||
"github.com/NVIDIA/nvidia-container-toolkit/pkg/oci"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type modifyingRuntimeWrapper struct {
|
||||
logger *log.Logger
|
||||
runtime oci.Runtime
|
||||
ociSpec oci.Spec
|
||||
modifier modify.Modifier
|
||||
}
|
||||
|
||||
var _ oci.Runtime = (*modifyingRuntimeWrapper)(nil)
|
||||
|
||||
// NewModifyingRuntimeWrapperWithLogger creates a runtime wrapper that applies the specified modifier to the OCI specification
|
||||
// before invoking the wrapped runtime.
|
||||
func NewModifyingRuntimeWrapperWithLogger(logger *log.Logger, runtime oci.Runtime, spec oci.Spec, modifier modify.Modifier) oci.Runtime {
|
||||
rt := modifyingRuntimeWrapper{
|
||||
logger: logger,
|
||||
runtime: runtime,
|
||||
ociSpec: spec,
|
||||
modifier: modifier,
|
||||
}
|
||||
return &rt
|
||||
}
|
||||
|
||||
// Exec checks whether a modification of the OCI specification is required and modifies it accordingly before exec-ing
|
||||
// into the wrapped runtime.
|
||||
func (r *modifyingRuntimeWrapper) Exec(args []string) error {
|
||||
if oci.HasCreateSubcommand(args) {
|
||||
err := r.modify()
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not apply required modification to OCI specification: %v", err)
|
||||
}
|
||||
r.logger.Infof("Applied required modification to OCI specification")
|
||||
} else {
|
||||
r.logger.Infof("No modification of OCI specification required")
|
||||
}
|
||||
|
||||
r.logger.Infof("Forwarding command to runtime")
|
||||
return r.runtime.Exec(args)
|
||||
}
|
||||
|
||||
// modify loads, modifies, and flushes the OCI specification using the defined Modifier
|
||||
func (r *modifyingRuntimeWrapper) modify() error {
|
||||
err := r.ociSpec.Load()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error loading OCI specification for modification: %v", err)
|
||||
}
|
||||
|
||||
err = r.modifier.Modify(r.ociSpec)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error modifying OCI spec: %v", err)
|
||||
}
|
||||
|
||||
err = r.ociSpec.Flush()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error writing modified OCI specification: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
160
internal/runtime/runtime_modifier_test.go
Normal file
160
internal/runtime/runtime_modifier_test.go
Normal file
@@ -0,0 +1,160 @@
|
||||
/*
|
||||
# Copyright (c) 2021, 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 runtime
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/NVIDIA/nvidia-container-toolkit/pkg/modify"
|
||||
"github.com/NVIDIA/nvidia-container-toolkit/pkg/oci"
|
||||
testlog "github.com/sirupsen/logrus/hooks/test"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestRuntimeModifier(t *testing.T) {
|
||||
logger, _ := testlog.NewNullLogger()
|
||||
|
||||
testCases := []struct {
|
||||
args []string
|
||||
shouldModify bool
|
||||
}{
|
||||
{},
|
||||
{
|
||||
args: []string{"create"},
|
||||
shouldModify: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
runtimeMock := &oci.RuntimeMock{}
|
||||
specMock := &oci.SpecMock{}
|
||||
modifierMock := &modify.ModifierMock{}
|
||||
|
||||
r := NewModifyingRuntimeWrapperWithLogger(
|
||||
logger,
|
||||
runtimeMock,
|
||||
specMock,
|
||||
modifierMock,
|
||||
)
|
||||
|
||||
err := r.Exec(tc.args)
|
||||
|
||||
require.NoError(t, err)
|
||||
|
||||
expectedCalls := 0
|
||||
if tc.shouldModify {
|
||||
expectedCalls = 1
|
||||
}
|
||||
|
||||
require.Len(t, specMock.LoadCalls(), expectedCalls)
|
||||
require.Len(t, modifierMock.ModifyCalls(), expectedCalls)
|
||||
require.Len(t, specMock.FlushCalls(), expectedCalls)
|
||||
|
||||
require.Len(t, runtimeMock.ExecCalls(), 1)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRuntimeModiferWithLoadError(t *testing.T) {
|
||||
logger, _ := testlog.NewNullLogger()
|
||||
|
||||
runtimeMock := &oci.RuntimeMock{}
|
||||
specMock := &oci.SpecMock{
|
||||
LoadFunc: specErrorFunc,
|
||||
}
|
||||
modifierMock := &modify.ModifierMock{}
|
||||
|
||||
r := NewModifyingRuntimeWrapperWithLogger(
|
||||
logger,
|
||||
runtimeMock,
|
||||
specMock,
|
||||
modifierMock,
|
||||
)
|
||||
|
||||
err := r.Exec([]string{"create"})
|
||||
|
||||
require.Error(t, err)
|
||||
|
||||
require.Len(t, specMock.LoadCalls(), 1)
|
||||
require.Len(t, modifierMock.ModifyCalls(), 0)
|
||||
require.Len(t, specMock.FlushCalls(), 0)
|
||||
|
||||
require.Len(t, runtimeMock.ExecCalls(), 0)
|
||||
}
|
||||
|
||||
func TestRuntimeModiferWithFlushError(t *testing.T) {
|
||||
logger, _ := testlog.NewNullLogger()
|
||||
|
||||
runtimeMock := &oci.RuntimeMock{}
|
||||
specMock := &oci.SpecMock{
|
||||
FlushFunc: specErrorFunc,
|
||||
}
|
||||
modifierMock := &modify.ModifierMock{}
|
||||
|
||||
r := NewModifyingRuntimeWrapperWithLogger(
|
||||
logger,
|
||||
runtimeMock,
|
||||
specMock,
|
||||
modifierMock,
|
||||
)
|
||||
|
||||
err := r.Exec([]string{"create"})
|
||||
|
||||
require.Error(t, err)
|
||||
|
||||
require.Len(t, specMock.LoadCalls(), 1)
|
||||
require.Len(t, modifierMock.ModifyCalls(), 1)
|
||||
require.Len(t, specMock.FlushCalls(), 1)
|
||||
|
||||
require.Len(t, runtimeMock.ExecCalls(), 0)
|
||||
}
|
||||
|
||||
func TestRuntimeModiferWithModifyError(t *testing.T) {
|
||||
logger, _ := testlog.NewNullLogger()
|
||||
|
||||
runtimeMock := &oci.RuntimeMock{}
|
||||
specMock := &oci.SpecMock{}
|
||||
modifierMock := &modify.ModifierMock{
|
||||
ModifyFunc: modifierErrorFunc,
|
||||
}
|
||||
|
||||
r := NewModifyingRuntimeWrapperWithLogger(
|
||||
logger,
|
||||
runtimeMock,
|
||||
specMock,
|
||||
modifierMock,
|
||||
)
|
||||
|
||||
err := r.Exec([]string{"create"})
|
||||
|
||||
require.Error(t, err)
|
||||
|
||||
require.Len(t, specMock.LoadCalls(), 1)
|
||||
require.Len(t, modifierMock.ModifyCalls(), 1)
|
||||
require.Len(t, specMock.FlushCalls(), 0)
|
||||
|
||||
require.Len(t, runtimeMock.ExecCalls(), 0)
|
||||
|
||||
}
|
||||
|
||||
func specErrorFunc() error {
|
||||
return fmt.Errorf("error")
|
||||
}
|
||||
|
||||
func modifierErrorFunc(oci.Spec) error {
|
||||
return fmt.Errorf("error")
|
||||
}
|
||||
Reference in New Issue
Block a user