Skip to content

Commit b57afd8

Browse files
woooosangorlangure
authored andcommitted
added custom image name configuration
1 parent a25809e commit b57afd8

3 files changed

Lines changed: 30 additions & 0 deletions

File tree

gnomock.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ func StartCustom(image string, ports NamedPorts, opts ...Option) (*Container, er
9292
ports = config.CustomNamedPorts
9393
}
9494

95+
if config.CustomImage != "" {
96+
image = config.CustomImage
97+
}
98+
9599
g.log.Infow("starting", "image", image, "ports", ports)
96100
g.log.Infow("using config", "image", image, "ports", ports, "config", config)
97101

gnomock_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,16 @@ func TestGnomock_withExtraHosts(t *testing.T) {
366366
require.NoError(t, gnomock.Stop(container))
367367
}
368368

369+
func TestGnomock_withCustomImage(t *testing.T) {
370+
t.Parallel()
371+
372+
p := &testutil.TestPreset{Img: "docker.io/orlangure/noimage"}
373+
container, err := gnomock.Start(p, gnomock.WithCustomImage(testutil.TestImage))
374+
require.NoError(t, err)
375+
require.NotNil(t, container)
376+
require.NoError(t, gnomock.Stop(container))
377+
}
378+
369379
func initf(context.Context, *gnomock.Container) error {
370380
return nil
371381
}

options.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,14 @@ func WithExtraHosts(hosts []string) Option {
213213
}
214214
}
215215

216+
// WithCustomImage allows to define a custom image name. This option should be used to
217+
// override the image registry and repository defined by presets.
218+
func WithCustomImage(image string) Option {
219+
return func(o *Options) {
220+
o.CustomImage = image
221+
}
222+
}
223+
216224
// HealthcheckFunc defines a function to be used to determine container health.
217225
// It receives a host and a port, and returns an error if the container is not
218226
// ready, or nil when the container can be used. One example of HealthcheckFunc
@@ -307,6 +315,14 @@ type Options struct {
307315
// its re-use in posterior executions.
308316
Reuse bool `json:"reuse"`
309317

318+
// CustomImage allows to override the name of the image set by the presets
319+
// with a custom image name. This option is useful for cases where it is
320+
// required to pull the preset image from a custom registry and repository.
321+
//
322+
// Note that when using this option, you are responsible for verifying the
323+
// validity of the provided image registry and repository.
324+
CustomImage string `json:"customImage"`
325+
310326
ctx context.Context
311327
init InitFunc
312328
healthcheck HealthcheckFunc

0 commit comments

Comments
 (0)