Add String function to oci.Runtime interface

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar 2024-06-24 10:50:59 +02:00
parent f55aac0232
commit 8df5e33ef6
5 changed files with 58 additions and 3 deletions

View File

@ -16,10 +16,11 @@
package oci
//go:generate moq -stub -out runtime_mock.go . Runtime
// Runtime is an interface for a runtime shim. The Exec method accepts a list
// of command line arguments, and returns an error / nil.
//
//go:generate moq -rm -stub -out runtime_mock.go . Runtime
type Runtime interface {
Exec([]string) error
String() string
}

View File

@ -20,6 +20,9 @@ var _ Runtime = &RuntimeMock{}
// ExecFunc: func(strings []string) error {
// panic("mock out the Exec method")
// },
// StringFunc: func() string {
// panic("mock out the String method")
// },
// }
//
// // use mockedRuntime in code that requires Runtime
@ -30,6 +33,9 @@ type RuntimeMock struct {
// ExecFunc mocks the Exec method.
ExecFunc func(strings []string) error
// StringFunc mocks the String method.
StringFunc func() string
// calls tracks calls to the methods.
calls struct {
// Exec holds details about calls to the Exec method.
@ -37,8 +43,12 @@ type RuntimeMock struct {
// Strings is the strings argument value.
Strings []string
}
// String holds details about calls to the String method.
String []struct {
}
}
lockExec sync.RWMutex
lockString sync.RWMutex
}
// Exec calls ExecFunc.
@ -75,3 +85,33 @@ func (mock *RuntimeMock) ExecCalls() []struct {
mock.lockExec.RUnlock()
return calls
}
// String calls StringFunc.
func (mock *RuntimeMock) String() string {
callInfo := struct {
}{}
mock.lockString.Lock()
mock.calls.String = append(mock.calls.String, callInfo)
mock.lockString.Unlock()
if mock.StringFunc == nil {
var (
sOut string
)
return sOut
}
return mock.StringFunc()
}
// StringCalls gets all the calls that were made to String.
// Check the length with:
//
// len(mockedRuntime.StringCalls())
func (mock *RuntimeMock) StringCalls() []struct {
} {
var calls []struct {
}
mock.lockString.RLock()
calls = mock.calls.String
mock.lockString.RUnlock()
return calls
}

View File

@ -83,3 +83,8 @@ func (r *modifyingRuntimeWrapper) modify() error {
}
return nil
}
// String returns a string representation of the runtime.
func (r *modifyingRuntimeWrapper) String() string {
return fmt.Sprintf("modify on-create and forward to %s", r.runtime.String())
}

View File

@ -63,3 +63,8 @@ func (s pathRuntime) Exec(args []string) error {
return s.execRuntime.Exec(runtimeArgs)
}
// String returns the path to the specified runtime as the string representation.
func (s pathRuntime) String() string {
return s.path
}

View File

@ -37,3 +37,7 @@ func (r syscallExec) Exec(args []string) error {
// err is nil or not.
return fmt.Errorf("unexpected return from exec '%v'", args[0])
}
func (r syscallExec) String() string {
return "exec"
}