Skip to content

Commit

Permalink
added custom image name configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
woooosang committed Jul 8, 2024
1 parent a25809e commit 326f3b5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions gnomock.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ func StartCustom(image string, ports NamedPorts, opts ...Option) (*Container, er
ports = config.CustomNamedPorts
}

if config.CustomImage != "" {
image = config.CustomImage
}

g.log.Infow("starting", "image", image, "ports", ports)
g.log.Infow("using config", "image", image, "ports", ports, "config", config)

Expand Down
10 changes: 10 additions & 0 deletions gnomock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,16 @@ func TestGnomock_withExtraHosts(t *testing.T) {
require.NoError(t, gnomock.Stop(container))
}

func TestGnomock_withCustomImage(t *testing.T) {
t.Parallel()

p := &testutil.TestPreset{Img: "docker.io/orlangure/noimage"}
container, err := gnomock.Start(p, gnomock.WithCustomImage(testutil.TestImage))
require.NoError(t, err)
require.NotNil(t, container)
require.NoError(t, gnomock.Stop(container))
}

func initf(context.Context, *gnomock.Container) error {
return nil
}
Expand Down
16 changes: 16 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,14 @@ func WithExtraHosts(hosts []string) Option {
}
}

// WithCustomImage allows to define a custom image name. This option should be used to
// override the image registry and repository defined by presets.
func WithCustomImage(image string) Option {
return func(o *Options) {
o.CustomImage = image
}
}

// HealthcheckFunc defines a function to be used to determine container health.
// It receives a host and a port, and returns an error if the container is not
// ready, or nil when the container can be used. One example of HealthcheckFunc
Expand Down Expand Up @@ -307,6 +315,14 @@ type Options struct {
// its re-use in posterior executions.
Reuse bool `json:"reuse"`

// CustomImage allows to override the name of the image set by the presets
// with a custom image name. This option is useful for cases where it is
// required to pull the preset image from a custom registry and repository.
//
// Note that when using this option, you are responsible for verifying the
// validity of the provided image registry and repository.
CustomImage string `json:"customImage"`

ctx context.Context
init InitFunc
healthcheck HealthcheckFunc
Expand Down

0 comments on commit 326f3b5

Please sign in to comment.