From 8df5e33ef6e5eeb14aa1a5e5ce0b8bce4ea885b1 Mon Sep 17 00:00:00 2001 From: Evan Lezar Date: Mon, 24 Jun 2024 10:50:59 +0200 Subject: [PATCH] Add String function to oci.Runtime interface Signed-off-by: Evan Lezar --- internal/oci/runtime.go | 5 ++-- internal/oci/runtime_mock.go | 42 +++++++++++++++++++++++++++- internal/oci/runtime_modifier.go | 5 ++++ internal/oci/runtime_path.go | 5 ++++ internal/oci/runtime_syscall_exec.go | 4 +++ 5 files changed, 58 insertions(+), 3 deletions(-) diff --git a/internal/oci/runtime.go b/internal/oci/runtime.go index 438fc5d2..2ea71cb2 100644 --- a/internal/oci/runtime.go +++ b/internal/oci/runtime.go @@ -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 } diff --git a/internal/oci/runtime_mock.go b/internal/oci/runtime_mock.go index b6331f27..147035d4 100644 --- a/internal/oci/runtime_mock.go +++ b/internal/oci/runtime_mock.go @@ -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 + 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 +} diff --git a/internal/oci/runtime_modifier.go b/internal/oci/runtime_modifier.go index 797f5cd9..17c1c2d4 100644 --- a/internal/oci/runtime_modifier.go +++ b/internal/oci/runtime_modifier.go @@ -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()) +} diff --git a/internal/oci/runtime_path.go b/internal/oci/runtime_path.go index 3c43f31d..8c15a107 100644 --- a/internal/oci/runtime_path.go +++ b/internal/oci/runtime_path.go @@ -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 +} diff --git a/internal/oci/runtime_syscall_exec.go b/internal/oci/runtime_syscall_exec.go index 6820e3a1..349edf86 100644 --- a/internal/oci/runtime_syscall_exec.go +++ b/internal/oci/runtime_syscall_exec.go @@ -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" +}