Skip to content

Commit

Permalink
Merge branch 'master' into 299-network-ipam
Browse files Browse the repository at this point in the history
  • Loading branch information
mdelapenya authored Feb 2, 2022
2 parents 08653cd + a5da993 commit 37f6bf6
Show file tree
Hide file tree
Showing 11 changed files with 369 additions and 56 deletions.
37 changes: 33 additions & 4 deletions compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type waitService struct {
// LocalDockerCompose represents a Docker Compose execution using local binary
// docker-compose or docker-compose.exe, depending on the underlying platform
type LocalDockerCompose struct {
*LocalDockerComposeOptions
Executable string
ComposeFilePaths []string
absComposeFilePaths []string
Expand All @@ -55,15 +56,43 @@ type LocalDockerCompose struct {
WaitStrategyMap map[waitService]wait.Strategy
}

type (
// LocalDockerComposeOptions defines options applicable to LocalDockerCompose
LocalDockerComposeOptions struct {
Logger Logging
}

// LocalDockerComposeOption defines a common interface to modify LocalDockerComposeOptions
// These options can be passed to NewLocalDockerCompose in a variadic way to customize the returned LocalDockerCompose instance
LocalDockerComposeOption interface {
ApplyToLocalCompose(opts *LocalDockerComposeOptions)
}

// LocalDockerComposeOptionsFunc is a shorthand to implement the LocalDockerComposeOption interface
LocalDockerComposeOptionsFunc func(opts *LocalDockerComposeOptions)
)

func (f LocalDockerComposeOptionsFunc) ApplyToLocalCompose(opts *LocalDockerComposeOptions) {
f(opts)
}

// NewLocalDockerCompose returns an instance of the local Docker Compose, using an
// array of Docker Compose file paths and an identifier for the Compose execution.
//
// It will iterate through the array adding '-f compose-file-path' flags to the local
// Docker Compose execution. The identifier represents the name of the execution,
// which will define the name of the underlying Docker network and the name of the
// running Compose services.
func NewLocalDockerCompose(filePaths []string, identifier string) *LocalDockerCompose {
dc := &LocalDockerCompose{}
func NewLocalDockerCompose(filePaths []string, identifier string, opts ...LocalDockerComposeOption) *LocalDockerCompose {
dc := &LocalDockerCompose{
LocalDockerComposeOptions: &LocalDockerComposeOptions{
Logger: Logger,
},
}

for idx := range opts {
opts[idx].ApplyToLocalCompose(dc.LocalDockerComposeOptions)
}

dc.Executable = "docker-compose"
if runtime.GOOS == "windows" {
Expand Down Expand Up @@ -134,11 +163,11 @@ func (dc *LocalDockerCompose) applyStrategyToRunningContainer() error {
}
container := containers[0]
strategy := dc.WaitStrategyMap[k]
dockerProvider, err := NewDockerProvider()
dockerProvider, err := NewDockerProvider(WithLogger(dc.Logger))
if err != nil {
return fmt.Errorf("unable to create new Docker Provider: %w", err)
}
dockercontainer := &DockerContainer{ID: container.ID, WaitingFor: strategy, provider: dockerProvider}
dockercontainer := &DockerContainer{ID: container.ID, WaitingFor: strategy, provider: dockerProvider, logger: dc.Logger}
err = strategy.WaitUntilReady(context.Background(), dockercontainer)
if err != nil {
return fmt.Errorf("Unable to apply wait strategy %v to service %s due to %w", strategy, k.service, err)
Expand Down
24 changes: 12 additions & 12 deletions compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestLocalDockerCompose(t *testing.T) {

identifier := strings.ToLower(uuid.New().String())

compose := NewLocalDockerCompose([]string{path}, identifier)
compose := NewLocalDockerCompose([]string{path}, identifier, WithLogger(TestLogger(t)))
destroyFn := func() {
err := compose.Down()
checkIfError(t, err)
Expand All @@ -112,7 +112,7 @@ func TestDockerComposeStrategyForInvalidService(t *testing.T) {

identifier := strings.ToLower(uuid.New().String())

compose := NewLocalDockerCompose([]string{path}, identifier)
compose := NewLocalDockerCompose([]string{path}, identifier, WithLogger(TestLogger(t)))
destroyFn := func() {
err := compose.Down()
checkIfError(t, err)
Expand All @@ -135,7 +135,7 @@ func TestDockerComposeWithWaitLogStrategy(t *testing.T) {

identifier := strings.ToLower(uuid.New().String())

compose := NewLocalDockerCompose([]string{path}, identifier)
compose := NewLocalDockerCompose([]string{path}, identifier, WithLogger(TestLogger(t)))
destroyFn := func() {
err := compose.Down()
checkIfError(t, err)
Expand All @@ -159,7 +159,7 @@ func TestDockerComposeWithWaitForService(t *testing.T) {

identifier := strings.ToLower(uuid.New().String())

compose := NewLocalDockerCompose([]string{path}, identifier)
compose := NewLocalDockerCompose([]string{path}, identifier, WithLogger(TestLogger(t)))
destroyFn := func() {
err := compose.Down()
checkIfError(t, err)
Expand All @@ -184,7 +184,7 @@ func TestDockerComposeWithWaitHTTPStrategy(t *testing.T) {

identifier := strings.ToLower(uuid.New().String())

compose := NewLocalDockerCompose([]string{path}, identifier)
compose := NewLocalDockerCompose([]string{path}, identifier, WithLogger(TestLogger(t)))
destroyFn := func() {
err := compose.Down()
checkIfError(t, err)
Expand All @@ -209,7 +209,7 @@ func TestDockerComposeWithWaitStrategy_NoExposedPorts(t *testing.T) {

identifier := strings.ToLower(uuid.New().String())

compose := NewLocalDockerCompose([]string{path}, identifier)
compose := NewLocalDockerCompose([]string{path}, identifier, WithLogger(TestLogger(t)))
destroyFn := func() {
err := compose.Down()
checkIfError(t, err)
Expand All @@ -231,7 +231,7 @@ func TestDockerComposeWithMultipleWaitStrategies(t *testing.T) {

identifier := strings.ToLower(uuid.New().String())

compose := NewLocalDockerCompose([]string{path}, identifier)
compose := NewLocalDockerCompose([]string{path}, identifier, WithLogger(TestLogger(t)))
destroyFn := func() {
err := compose.Down()
checkIfError(t, err)
Expand All @@ -255,7 +255,7 @@ func TestDockerComposeWithFailedStrategy(t *testing.T) {

identifier := strings.ToLower(uuid.New().String())

compose := NewLocalDockerCompose([]string{path}, identifier)
compose := NewLocalDockerCompose([]string{path}, identifier, WithLogger(TestLogger(t)))
destroyFn := func() {
err := compose.Down()
checkIfError(t, err)
Expand All @@ -282,7 +282,7 @@ func TestLocalDockerComposeComplex(t *testing.T) {

identifier := strings.ToLower(uuid.New().String())

compose := NewLocalDockerCompose([]string{path}, identifier)
compose := NewLocalDockerCompose([]string{path}, identifier, WithLogger(TestLogger(t)))
destroyFn := func() {
err := compose.Down()
checkIfError(t, err)
Expand All @@ -304,7 +304,7 @@ func TestLocalDockerComposeWithEnvironment(t *testing.T) {

identifier := strings.ToLower(uuid.New().String())

compose := NewLocalDockerCompose([]string{path}, identifier)
compose := NewLocalDockerCompose([]string{path}, identifier, WithLogger(TestLogger(t)))
destroyFn := func() {
err := compose.Down()
checkIfError(t, err)
Expand Down Expand Up @@ -340,7 +340,7 @@ func TestLocalDockerComposeWithMultipleComposeFiles(t *testing.T) {

identifier := strings.ToLower(uuid.New().String())

compose := NewLocalDockerCompose(composeFiles, identifier)
compose := NewLocalDockerCompose(composeFiles, identifier, WithLogger(TestLogger(t)))
destroyFn := func() {
err := compose.Down()
checkIfError(t, err)
Expand Down Expand Up @@ -376,7 +376,7 @@ func TestLocalDockerComposeWithVolume(t *testing.T) {

identifier := strings.ToLower(uuid.New().String())

compose := NewLocalDockerCompose([]string{path}, identifier)
compose := NewLocalDockerCompose([]string{path}, identifier, WithLogger(TestLogger(t)))
destroyFn := func() {
err := compose.Down()
checkIfError(t, err)
Expand Down
40 changes: 34 additions & 6 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,51 @@ type ContainerRequest struct {
ReaperImage string // alternative reaper image
AutoRemove bool // if set to true, the container will be removed from the host when stopped
NetworkMode container.NetworkMode
AlwaysPullImage bool // Always pull image
AlwaysPullImage bool // Always pull image
ImagePlatform string // ImagePlatform describes the platform which the image runs on.
}

// ProviderType is an enum for the possible providers
type ProviderType int
type (
// ProviderType is an enum for the possible providers
ProviderType int

// GenericProviderOptions defines options applicable to all providers
GenericProviderOptions struct {
Logger Logging
}

// GenericProviderOption defines a common interface to modify GenericProviderOptions
// These options can be passed to GetProvider in a variadic way to customize the returned GenericProvider instance
GenericProviderOption interface {
ApplyGenericTo(opts *GenericProviderOptions)
}

// GenericProviderOptionFunc is a shorthand to implement the GenericProviderOption interface
GenericProviderOptionFunc func(opts *GenericProviderOptions)
)

func (f GenericProviderOptionFunc) ApplyGenericTo(opts *GenericProviderOptions) {
f(opts)
}

// possible provider types
const (
ProviderDocker ProviderType = iota // Docker is default = 0
)

// GetProvider provides the provider implementation for a certain type
func (t ProviderType) GetProvider() (GenericProvider, error) {
func (t ProviderType) GetProvider(opts ...GenericProviderOption) (GenericProvider, error) {
opt := &GenericProviderOptions{
Logger: Logger,
}

for _, o := range opts {
o.ApplyGenericTo(opt)
}

switch t {
case ProviderDocker:
provider, err := NewDockerProvider()
provider, err := NewDockerProvider(Generic2DockerOptions(opts...)...)
if err != nil {
return nil, fmt.Errorf("%w, failed to create Docker provider", err)
}
Expand All @@ -124,7 +153,6 @@ func (t ProviderType) GetProvider() (GenericProvider, error) {
// Validate ensures that the ContainerRequest does not have invalid parameters configured to it
// ex. make sure you are not specifying both an image as well as a context
func (c *ContainerRequest) Validate() error {

validationMethods := []func() error{
c.validateContextAndImage,
c.validateContextOrImageIsSpecified,
Expand Down
Loading

0 comments on commit 37f6bf6

Please sign in to comment.