mirror of
https://github.com/containers/image.git
synced 2025-04-18 19:44:05 +03:00
Add transports/StubTransport
This is useful primarily so that we can use build tags to remove transports from transports/alltransports, while giving users more hints about what to do instead of a bland “unknown transport”. Tests can also use this instead of their own mocks. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
This commit is contained in:
parent
97646ea50a
commit
5d6fbfcbc8
36
transports/stub.go
Normal file
36
transports/stub.go
Normal file
@ -0,0 +1,36 @@
|
||||
package transports
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/containers/image/types"
|
||||
)
|
||||
|
||||
// stubTransport is an implementation of types.ImageTransport which has a name, but rejects any references with “the transport $name: is not supported in this build”.
|
||||
type stubTransport string
|
||||
|
||||
// NewStubTransport returns an implementation of types.ImageTransport which has a name, but rejects any references with “the transport $name: is not supported in this build”.
|
||||
func NewStubTransport(name string) types.ImageTransport {
|
||||
return stubTransport(name)
|
||||
}
|
||||
|
||||
// Name returns the name of the transport, which must be unique among other transports.
|
||||
func (s stubTransport) Name() string {
|
||||
return string(s)
|
||||
}
|
||||
|
||||
// ParseReference converts a string, which should not start with the ImageTransport.Name prefix, into an ImageReference.
|
||||
func (s stubTransport) ParseReference(reference string) (types.ImageReference, error) {
|
||||
return nil, fmt.Errorf(`The transport "%s:" is not supported in this build`, string(s))
|
||||
}
|
||||
|
||||
// ValidatePolicyConfigurationScope checks that scope is a valid name for a signature.PolicyTransportScopes keys
|
||||
// (i.e. a valid PolicyConfigurationIdentity() or PolicyConfigurationNamespaces() return value).
|
||||
// It is acceptable to allow an invalid value which will never be matched, it can "only" cause user confusion.
|
||||
// scope passed to this function will not be "", that value is always allowed.
|
||||
func (s stubTransport) ValidatePolicyConfigurationScope(scope string) error {
|
||||
// Allowing any reference in here allows tools with some transports stubbed-out to still
|
||||
// use signature verification policies which refer to these stubbed-out transports.
|
||||
// See also the treatment of unknown transports in policyTransportScopesWithTransport.UnmarshalJSON .
|
||||
return nil
|
||||
}
|
18
transports/stub_test.go
Normal file
18
transports/stub_test.go
Normal file
@ -0,0 +1,18 @@
|
||||
package transports
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestStubTransport(t *testing.T) {
|
||||
const name = "whatever"
|
||||
|
||||
s := NewStubTransport(name)
|
||||
assert.Equal(t, name, s.Name())
|
||||
_, err := s.ParseReference("this is rejected regardless of content")
|
||||
assert.Error(t, err)
|
||||
err = s.ValidatePolicyConfigurationScope("this is accepted regardless of content")
|
||||
assert.NoError(t, err)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user