2022-01-31 14:11:36 +00:00
|
|
|
/*
|
|
|
|
# Copyright (c) 2022, 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.
|
|
|
|
*/
|
|
|
|
|
2023-02-07 20:24:44 +00:00
|
|
|
package oci
|
2022-01-31 14:11:36 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2022-04-08 05:38:19 +00:00
|
|
|
"github.com/opencontainers/runtime-spec/specs-go"
|
2022-01-31 14:11:36 +00:00
|
|
|
testlog "github.com/sirupsen/logrus/hooks/test"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestExec(t *testing.T) {
|
|
|
|
logger, hook := testlog.NewNullLogger()
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
description string
|
2022-04-08 05:38:19 +00:00
|
|
|
shouldLoad bool
|
2022-01-31 14:11:36 +00:00
|
|
|
shouldModify bool
|
|
|
|
shouldFlush bool
|
|
|
|
shouldForward bool
|
|
|
|
args []string
|
|
|
|
modifyError error
|
|
|
|
writeError error
|
2023-02-07 20:24:44 +00:00
|
|
|
modifer SpecModifier
|
2022-01-31 14:11:36 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
description: "no args forwards",
|
2022-04-08 05:38:19 +00:00
|
|
|
shouldLoad: false,
|
2022-01-31 14:11:36 +00:00
|
|
|
shouldModify: false,
|
|
|
|
shouldFlush: false,
|
|
|
|
shouldForward: true,
|
2022-04-08 05:38:19 +00:00
|
|
|
modifer: &modiferMock{},
|
2022-01-31 14:11:36 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "create modifies",
|
|
|
|
args: []string{"create"},
|
2022-04-08 05:38:19 +00:00
|
|
|
shouldLoad: true,
|
2022-01-31 14:11:36 +00:00
|
|
|
shouldModify: true,
|
|
|
|
shouldFlush: true,
|
|
|
|
shouldForward: true,
|
2022-04-08 05:38:19 +00:00
|
|
|
modifer: &modiferMock{},
|
2022-01-31 14:11:36 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "modify error does not write or forward",
|
|
|
|
args: []string{"create"},
|
|
|
|
modifyError: fmt.Errorf("error modifying"),
|
2022-04-08 05:38:19 +00:00
|
|
|
shouldLoad: true,
|
2022-01-31 14:11:36 +00:00
|
|
|
shouldModify: true,
|
|
|
|
shouldFlush: false,
|
|
|
|
shouldForward: false,
|
2022-04-08 05:38:19 +00:00
|
|
|
modifer: &modiferMock{},
|
2022-01-31 14:11:36 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "write error does not forward",
|
|
|
|
args: []string{"create"},
|
|
|
|
writeError: fmt.Errorf("error writing"),
|
2022-04-08 05:38:19 +00:00
|
|
|
shouldLoad: true,
|
2022-01-31 14:11:36 +00:00
|
|
|
shouldModify: true,
|
|
|
|
shouldFlush: true,
|
|
|
|
shouldForward: false,
|
2022-04-08 05:38:19 +00:00
|
|
|
modifer: &modiferMock{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "nil modifier forwards on create",
|
|
|
|
args: []string{"create"},
|
|
|
|
shouldLoad: false,
|
|
|
|
shouldModify: false,
|
|
|
|
shouldFlush: false,
|
|
|
|
shouldForward: true,
|
|
|
|
modifer: nil,
|
2022-01-31 14:11:36 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
hook.Reset()
|
|
|
|
|
|
|
|
t.Run(tc.description, func(t *testing.T) {
|
2023-02-07 20:24:44 +00:00
|
|
|
runtimeMock := &RuntimeMock{}
|
|
|
|
specMock := &SpecMock{
|
|
|
|
ModifyFunc: func(specModifier SpecModifier) error {
|
2022-01-31 14:11:36 +00:00
|
|
|
return tc.modifyError
|
|
|
|
},
|
|
|
|
FlushFunc: func() error {
|
|
|
|
return tc.writeError
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
shim := NewModifyingRuntimeWrapper(
|
|
|
|
logger,
|
|
|
|
runtimeMock,
|
|
|
|
specMock,
|
|
|
|
// TODO: We should test the interactions with the SpecModifier too
|
2022-04-08 05:38:19 +00:00
|
|
|
tc.modifer,
|
|
|
|
)
|
2022-01-31 14:11:36 +00:00
|
|
|
|
|
|
|
err := shim.Exec(tc.args)
|
|
|
|
if tc.modifyError != nil || tc.writeError != nil {
|
|
|
|
require.Error(t, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2022-04-08 05:38:19 +00:00
|
|
|
if tc.shouldLoad {
|
|
|
|
require.Equal(t, 1, len(specMock.LoadCalls()))
|
|
|
|
} else {
|
|
|
|
require.Equal(t, 0, len(specMock.LoadCalls()))
|
|
|
|
}
|
2022-01-31 14:11:36 +00:00
|
|
|
if tc.shouldModify {
|
|
|
|
require.Equal(t, 1, len(specMock.ModifyCalls()))
|
|
|
|
} else {
|
|
|
|
require.Equal(t, 0, len(specMock.ModifyCalls()))
|
|
|
|
}
|
|
|
|
if tc.shouldFlush {
|
|
|
|
require.Equal(t, 1, len(specMock.FlushCalls()))
|
|
|
|
} else {
|
|
|
|
require.Equal(t, 0, len(specMock.FlushCalls()))
|
|
|
|
}
|
|
|
|
if tc.shouldForward {
|
|
|
|
require.Equal(t, 1, len(runtimeMock.ExecCalls()))
|
|
|
|
} else {
|
|
|
|
require.Equal(t, 0, len(runtimeMock.ExecCalls()))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-04-08 05:38:19 +00:00
|
|
|
|
|
|
|
func TestNilModiferReturnsRuntime(t *testing.T) {
|
|
|
|
logger, _ := testlog.NewNullLogger()
|
|
|
|
|
2023-02-07 20:24:44 +00:00
|
|
|
runtimeMock := &RuntimeMock{}
|
|
|
|
specMock := &SpecMock{}
|
2022-04-08 05:38:19 +00:00
|
|
|
|
|
|
|
shim := NewModifyingRuntimeWrapper(
|
|
|
|
logger,
|
|
|
|
runtimeMock,
|
|
|
|
specMock,
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
require.Equal(t, runtimeMock, shim)
|
|
|
|
}
|
|
|
|
|
|
|
|
type modiferMock struct{}
|
|
|
|
|
|
|
|
func (m modiferMock) Modify(*specs.Spec) error {
|
|
|
|
return nil
|
|
|
|
}
|